1 /*
2  * IS-IS Rout(e)ing protocol - isis_dynhn.c
3  *                             Dynamic hostname cache
4  * Copyright (C) 2001,2002   Sampo Saaristo
5  *                           Tampere University of Technology
6  *                           Institute of Communications Engineering
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public Licenseas published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; see the file COPYING; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <zebra.h>
24 
25 #include "vty.h"
26 #include "linklist.h"
27 #include "memory.h"
28 #include "log.h"
29 #include "stream.h"
30 #include "command.h"
31 #include "if.h"
32 #include "thread.h"
33 
34 #include "isisd/isis_constants.h"
35 #include "isisd/isis_common.h"
36 #include "isisd/isis_flags.h"
37 #include "isisd/isis_circuit.h"
38 #include "isisd/isisd.h"
39 #include "isisd/isis_dynhn.h"
40 #include "isisd/isis_misc.h"
41 #include "isisd/isis_constants.h"
42 
43 extern struct host host;
44 
45 struct list *dyn_cache = NULL;
46 static int dyn_cache_cleanup(struct thread *);
47 
dyn_cache_init(struct isis * isis)48 void dyn_cache_init(struct isis *isis)
49 {
50 	if (dyn_cache == NULL)
51 		dyn_cache = list_new();
52 	if (!CHECK_FLAG(im->options, F_ISIS_UNIT_TEST))
53 		thread_add_timer(master, dyn_cache_cleanup, isis, 120,
54 				 &isis->t_dync_clean);
55 	return;
56 }
57 
dyn_cache_cleanup_all(void)58 void dyn_cache_cleanup_all(void)
59 {
60 	struct listnode *node, *nnode;
61 	struct isis_dynhn *dyn;
62 
63 	for (ALL_LIST_ELEMENTS(dyn_cache, node, nnode, dyn)) {
64 		list_delete_node(dyn_cache, node);
65 		XFREE(MTYPE_ISIS_DYNHN, dyn);
66 	}
67 }
68 
dyn_cache_cleanup(struct thread * thread)69 static int dyn_cache_cleanup(struct thread *thread)
70 {
71 	struct listnode *node, *nnode;
72 	struct isis_dynhn *dyn;
73 	time_t now = time(NULL);
74 	struct isis *isis = NULL;
75 
76 	isis = THREAD_ARG(thread);
77 
78 	isis->t_dync_clean = NULL;
79 
80 	for (ALL_LIST_ELEMENTS(dyn_cache, node, nnode, dyn)) {
81 		if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
82 			continue;
83 		list_delete_node(dyn_cache, node);
84 		XFREE(MTYPE_ISIS_DYNHN, dyn);
85 	}
86 
87 	thread_add_timer(master, dyn_cache_cleanup, isis, 120,
88 			&isis->t_dync_clean);
89 
90 	return ISIS_OK;
91 }
92 
dynhn_find_by_id(const uint8_t * id)93 struct isis_dynhn *dynhn_find_by_id(const uint8_t *id)
94 {
95 	struct listnode *node = NULL;
96 	struct isis_dynhn *dyn = NULL;
97 
98 	for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
99 		if (memcmp(dyn->id, id, ISIS_SYS_ID_LEN) == 0)
100 			return dyn;
101 
102 	return NULL;
103 }
104 
dynhn_find_by_name(const char * hostname)105 struct isis_dynhn *dynhn_find_by_name(const char *hostname)
106 {
107 	struct listnode *node = NULL;
108 	struct isis_dynhn *dyn = NULL;
109 
110 	for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
111 		if (strncmp(dyn->hostname, hostname, 255) == 0)
112 			return dyn;
113 
114 	return NULL;
115 }
116 
isis_dynhn_insert(const uint8_t * id,const char * hostname,int level)117 void isis_dynhn_insert(const uint8_t *id, const char *hostname, int level)
118 {
119 	struct isis_dynhn *dyn;
120 
121 	dyn = dynhn_find_by_id(id);
122 	if (!dyn) {
123 		dyn = XCALLOC(MTYPE_ISIS_DYNHN, sizeof(struct isis_dynhn));
124 		memcpy(dyn->id, id, ISIS_SYS_ID_LEN);
125 		dyn->level = level;
126 		listnode_add(dyn_cache, dyn);
127 	}
128 
129 	snprintf(dyn->hostname, sizeof(dyn->hostname), "%s", hostname);
130 	dyn->refresh = time(NULL);
131 }
132 
isis_dynhn_remove(const uint8_t * id)133 void isis_dynhn_remove(const uint8_t *id)
134 {
135 	struct isis_dynhn *dyn;
136 
137 	dyn = dynhn_find_by_id(id);
138 	if (!dyn)
139 		return;
140 	listnode_delete(dyn_cache, dyn);
141 	XFREE(MTYPE_ISIS_DYNHN, dyn);
142 }
143 
144 /*
145  * Level  System ID      Dynamic Hostname  (notag)
146  *  2     0000.0000.0001 foo-gw
147  *  2     0000.0000.0002 bar-gw
148  *      * 0000.0000.0004 this-gw
149  */
dynhn_print_all(struct vty * vty,struct isis * isis)150 void dynhn_print_all(struct vty *vty, struct isis *isis)
151 {
152 	struct listnode *node;
153 	struct isis_dynhn *dyn;
154 
155 	vty_out(vty, "vrf     : %s\n", isis->name);
156 	if (!isis->sysid_set)
157 		return;
158 	vty_out(vty, "Level  System ID      Dynamic Hostname\n");
159 	for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn)) {
160 		vty_out(vty, "%-7d", dyn->level);
161 		vty_out(vty, "%-15s%-15s\n", sysid_print(dyn->id),
162 			dyn->hostname);
163 	}
164 
165 	vty_out(vty, "     * %s %s\n", sysid_print(isis->sysid),
166 		cmd_hostname_get());
167 	return;
168 }
169