]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_dynhn.c
Merge pull request #8253 from opensourcerouting/topotest-python3-backports
[mirror_frr.git] / isisd / isis_dynhn.c
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 DEFINE_MTYPE_STATIC(ISISD, ISIS_DYNHN, "ISIS dyn hostname");
44
45 extern struct host host;
46
47 struct list *dyn_cache = NULL;
48 static int dyn_cache_cleanup(struct thread *);
49
50 void dyn_cache_init(struct isis *isis)
51 {
52 if (dyn_cache == NULL)
53 dyn_cache = list_new();
54 if (!CHECK_FLAG(im->options, F_ISIS_UNIT_TEST))
55 thread_add_timer(master, dyn_cache_cleanup, isis, 120,
56 &isis->t_dync_clean);
57 return;
58 }
59
60 void dyn_cache_cleanup_all(void)
61 {
62 struct listnode *node, *nnode;
63 struct isis_dynhn *dyn;
64
65 for (ALL_LIST_ELEMENTS(dyn_cache, node, nnode, dyn)) {
66 list_delete_node(dyn_cache, node);
67 XFREE(MTYPE_ISIS_DYNHN, dyn);
68 }
69 }
70
71 static int dyn_cache_cleanup(struct thread *thread)
72 {
73 struct listnode *node, *nnode;
74 struct isis_dynhn *dyn;
75 time_t now = time(NULL);
76 struct isis *isis = NULL;
77
78 isis = THREAD_ARG(thread);
79
80 isis->t_dync_clean = NULL;
81
82 for (ALL_LIST_ELEMENTS(dyn_cache, node, nnode, dyn)) {
83 if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
84 continue;
85 list_delete_node(dyn_cache, node);
86 XFREE(MTYPE_ISIS_DYNHN, dyn);
87 }
88
89 thread_add_timer(master, dyn_cache_cleanup, isis, 120,
90 &isis->t_dync_clean);
91
92 return ISIS_OK;
93 }
94
95 struct isis_dynhn *dynhn_find_by_id(const uint8_t *id)
96 {
97 struct listnode *node = NULL;
98 struct isis_dynhn *dyn = NULL;
99
100 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
101 if (memcmp(dyn->id, id, ISIS_SYS_ID_LEN) == 0)
102 return dyn;
103
104 return NULL;
105 }
106
107 struct isis_dynhn *dynhn_find_by_name(const char *hostname)
108 {
109 struct listnode *node = NULL;
110 struct isis_dynhn *dyn = NULL;
111
112 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
113 if (strncmp(dyn->hostname, hostname, 255) == 0)
114 return dyn;
115
116 return NULL;
117 }
118
119 void isis_dynhn_insert(const uint8_t *id, const char *hostname, int level)
120 {
121 struct isis_dynhn *dyn;
122
123 dyn = dynhn_find_by_id(id);
124 if (!dyn) {
125 dyn = XCALLOC(MTYPE_ISIS_DYNHN, sizeof(struct isis_dynhn));
126 memcpy(dyn->id, id, ISIS_SYS_ID_LEN);
127 dyn->level = level;
128 listnode_add(dyn_cache, dyn);
129 }
130
131 snprintf(dyn->hostname, sizeof(dyn->hostname), "%s", hostname);
132 dyn->refresh = time(NULL);
133 }
134
135 void isis_dynhn_remove(const uint8_t *id)
136 {
137 struct isis_dynhn *dyn;
138
139 dyn = dynhn_find_by_id(id);
140 if (!dyn)
141 return;
142 listnode_delete(dyn_cache, dyn);
143 XFREE(MTYPE_ISIS_DYNHN, dyn);
144 }
145
146 /*
147 * Level System ID Dynamic Hostname (notag)
148 * 2 0000.0000.0001 foo-gw
149 * 2 0000.0000.0002 bar-gw
150 * * 0000.0000.0004 this-gw
151 */
152 void dynhn_print_all(struct vty *vty, struct isis *isis)
153 {
154 struct listnode *node;
155 struct isis_dynhn *dyn;
156
157 vty_out(vty, "vrf : %s\n", isis->name);
158 if (!isis->sysid_set)
159 return;
160 vty_out(vty, "Level System ID Dynamic Hostname\n");
161 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn)) {
162 vty_out(vty, "%-7d", dyn->level);
163 vty_out(vty, "%-15s%-15s\n", sysid_print(dyn->id),
164 dyn->hostname);
165 }
166
167 vty_out(vty, " * %s %s\n", sysid_print(isis->sysid),
168 cmd_hostname_get());
169 return;
170 }
171
172 struct isis_dynhn *dynhn_snmp_next(const uint8_t *id, int level)
173 {
174 struct listnode *node = NULL;
175 struct isis_dynhn *dyn = NULL;
176 struct isis_dynhn *found_dyn = NULL;
177 int res;
178
179 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn)) {
180 res = memcmp(dyn->id, id, ISIS_SYS_ID_LEN);
181
182 if (res < 0)
183 continue;
184
185 if (res == 0 && dyn->level <= level)
186 continue;
187
188 if (res == 0) {
189 /*
190 * This is the best match, we can stop
191 * searching
192 */
193
194 found_dyn = dyn;
195 break;
196 }
197
198 if (found_dyn == NULL
199 || memcmp(dyn->id, found_dyn->id, ISIS_SYS_ID_LEN) < 0) {
200 found_dyn = dyn;
201 }
202 }
203
204 return found_dyn;
205 }