]>
Commit | Line | Data |
---|---|---|
eb5d44eb | 1 | /* |
2 | * IS-IS Rout(e)ing protocol - isis_dynhn.c | |
3 | * Dynamic hostname cache | |
4 | * Copyright (C) 2001,2002 Sampo Saaristo | |
d62a17ae | 5 | * Tampere University of Technology |
eb5d44eb | 6 | * Institute of Communications Engineering |
7 | * | |
d62a17ae | 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) | |
eb5d44eb | 11 | * any later version. |
12 | * | |
d62a17ae | 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 | |
eb5d44eb | 16 | * more details. |
17 | * | |
896014f4 DL |
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 | |
eb5d44eb | 21 | */ |
22 | ||
eb5d44eb | 23 | #include <zebra.h> |
eb5d44eb | 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" | |
d3d7474b | 32 | #include "thread.h" |
eb5d44eb | 33 | |
eb5d44eb | 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 | ||
66b9a381 DL |
43 | DEFINE_MTYPE_STATIC(ISISD, ISIS_DYNHN, "ISIS dyn hostname"); |
44 | ||
cc9f21da | 45 | static void dyn_cache_cleanup(struct thread *); |
eb5d44eb | 46 | |
eab88f36 | 47 | void dyn_cache_init(struct isis *isis) |
eb5d44eb | 48 | { |
240f48b3 | 49 | isis->dyn_cache = list_new(); |
52a7c25e RW |
50 | if (!CHECK_FLAG(im->options, F_ISIS_UNIT_TEST)) |
51 | thread_add_timer(master, dyn_cache_cleanup, isis, 120, | |
52 | &isis->t_dync_clean); | |
eb5d44eb | 53 | } |
54 | ||
240f48b3 | 55 | void dyn_cache_finish(struct isis *isis) |
52a7c25e RW |
56 | { |
57 | struct listnode *node, *nnode; | |
58 | struct isis_dynhn *dyn; | |
59 | ||
240f48b3 IR |
60 | thread_cancel(&isis->t_dync_clean); |
61 | ||
62 | for (ALL_LIST_ELEMENTS(isis->dyn_cache, node, nnode, dyn)) { | |
63 | list_delete_node(isis->dyn_cache, node); | |
52a7c25e RW |
64 | XFREE(MTYPE_ISIS_DYNHN, dyn); |
65 | } | |
240f48b3 IR |
66 | |
67 | list_delete(&isis->dyn_cache); | |
52a7c25e RW |
68 | } |
69 | ||
cc9f21da | 70 | static void dyn_cache_cleanup(struct thread *thread) |
d3d7474b | 71 | { |
d62a17ae | 72 | struct listnode *node, *nnode; |
73 | struct isis_dynhn *dyn; | |
74 | time_t now = time(NULL); | |
eab88f36 K |
75 | struct isis *isis = NULL; |
76 | ||
77 | isis = THREAD_ARG(thread); | |
d3d7474b | 78 | |
d62a17ae | 79 | isis->t_dync_clean = NULL; |
d3d7474b | 80 | |
240f48b3 | 81 | for (ALL_LIST_ELEMENTS(isis->dyn_cache, node, nnode, dyn)) { |
d62a17ae | 82 | if ((now - dyn->refresh) < MAX_LSP_LIFETIME) |
83 | continue; | |
240f48b3 | 84 | list_delete_node(isis->dyn_cache, node); |
d62a17ae | 85 | XFREE(MTYPE_ISIS_DYNHN, dyn); |
86 | } | |
d3d7474b | 87 | |
eab88f36 K |
88 | thread_add_timer(master, dyn_cache_cleanup, isis, 120, |
89 | &isis->t_dync_clean); | |
d3d7474b | 90 | } |
91 | ||
240f48b3 | 92 | struct isis_dynhn *dynhn_find_by_id(struct isis *isis, const uint8_t *id) |
eb5d44eb | 93 | { |
d62a17ae | 94 | struct listnode *node = NULL; |
95 | struct isis_dynhn *dyn = NULL; | |
eb5d44eb | 96 | |
240f48b3 | 97 | for (ALL_LIST_ELEMENTS_RO(isis->dyn_cache, node, dyn)) |
d62a17ae | 98 | if (memcmp(dyn->id, id, ISIS_SYS_ID_LEN) == 0) |
99 | return dyn; | |
f390d2c7 | 100 | |
d62a17ae | 101 | return NULL; |
eb5d44eb | 102 | } |
103 | ||
240f48b3 | 104 | struct isis_dynhn *dynhn_find_by_name(struct isis *isis, const char *hostname) |
3f045a08 | 105 | { |
d62a17ae | 106 | struct listnode *node = NULL; |
107 | struct isis_dynhn *dyn = NULL; | |
3f045a08 | 108 | |
240f48b3 | 109 | for (ALL_LIST_ELEMENTS_RO(isis->dyn_cache, node, dyn)) |
af8ac8f9 | 110 | if (strncmp(dyn->hostname, hostname, 255) == 0) |
d62a17ae | 111 | return dyn; |
3f045a08 | 112 | |
d62a17ae | 113 | return NULL; |
3f045a08 JB |
114 | } |
115 | ||
240f48b3 IR |
116 | void isis_dynhn_insert(struct isis *isis, const uint8_t *id, |
117 | const char *hostname, int level) | |
eb5d44eb | 118 | { |
d62a17ae | 119 | struct isis_dynhn *dyn; |
120 | ||
240f48b3 | 121 | dyn = dynhn_find_by_id(isis, id); |
d62a17ae | 122 | if (!dyn) { |
af8ac8f9 CF |
123 | dyn = XCALLOC(MTYPE_ISIS_DYNHN, sizeof(struct isis_dynhn)); |
124 | memcpy(dyn->id, id, ISIS_SYS_ID_LEN); | |
125 | dyn->level = level; | |
240f48b3 | 126 | listnode_add(isis->dyn_cache, dyn); |
d62a17ae | 127 | } |
128 | ||
af8ac8f9 | 129 | snprintf(dyn->hostname, sizeof(dyn->hostname), "%s", hostname); |
d62a17ae | 130 | dyn->refresh = time(NULL); |
eb5d44eb | 131 | } |
132 | ||
240f48b3 | 133 | void isis_dynhn_remove(struct isis *isis, const uint8_t *id) |
3f045a08 | 134 | { |
d62a17ae | 135 | struct isis_dynhn *dyn; |
136 | ||
240f48b3 | 137 | dyn = dynhn_find_by_id(isis, id); |
d62a17ae | 138 | if (!dyn) |
139 | return; | |
240f48b3 | 140 | listnode_delete(isis->dyn_cache, dyn); |
d62a17ae | 141 | XFREE(MTYPE_ISIS_DYNHN, dyn); |
3f045a08 JB |
142 | } |
143 | ||
eb5d44eb | 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 | */ | |
eab88f36 | 150 | void dynhn_print_all(struct vty *vty, struct isis *isis) |
eb5d44eb | 151 | { |
d62a17ae | 152 | struct listnode *node; |
153 | struct isis_dynhn *dyn; | |
154 | ||
eab88f36 K |
155 | vty_out(vty, "vrf : %s\n", isis->name); |
156 | if (!isis->sysid_set) | |
157 | return; | |
d62a17ae | 158 | vty_out(vty, "Level System ID Dynamic Hostname\n"); |
240f48b3 | 159 | for (ALL_LIST_ELEMENTS_RO(isis->dyn_cache, node, dyn)) { |
d62a17ae | 160 | vty_out(vty, "%-7d", dyn->level); |
161 | vty_out(vty, "%-15s%-15s\n", sysid_print(dyn->id), | |
af8ac8f9 | 162 | dyn->hostname); |
d62a17ae | 163 | } |
164 | ||
165 | vty_out(vty, " * %s %s\n", sysid_print(isis->sysid), | |
6b3ee3a0 | 166 | cmd_hostname_get()); |
d62a17ae | 167 | return; |
eb5d44eb | 168 | } |
1ee746d9 | 169 | |
240f48b3 IR |
170 | struct isis_dynhn *dynhn_snmp_next(struct isis *isis, const uint8_t *id, |
171 | int level) | |
1ee746d9 | 172 | { |
173 | struct listnode *node = NULL; | |
174 | struct isis_dynhn *dyn = NULL; | |
175 | struct isis_dynhn *found_dyn = NULL; | |
176 | int res; | |
177 | ||
240f48b3 | 178 | for (ALL_LIST_ELEMENTS_RO(isis->dyn_cache, node, dyn)) { |
1ee746d9 | 179 | res = memcmp(dyn->id, id, ISIS_SYS_ID_LEN); |
180 | ||
181 | if (res < 0) | |
182 | continue; | |
183 | ||
184 | if (res == 0 && dyn->level <= level) | |
185 | continue; | |
186 | ||
187 | if (res == 0) { | |
188 | /* | |
189 | * This is the best match, we can stop | |
190 | * searching | |
191 | */ | |
192 | ||
193 | found_dyn = dyn; | |
194 | break; | |
195 | } | |
196 | ||
197 | if (found_dyn == NULL | |
198 | || memcmp(dyn->id, found_dyn->id, ISIS_SYS_ID_LEN) < 0) { | |
199 | found_dyn = dyn; | |
200 | } | |
201 | } | |
202 | ||
203 | return found_dyn; | |
204 | } |