]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_dynhn.c
Merge pull request #5175 from opensourcerouting/debug-nb-yang
[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 extern struct host host;
44
45 struct list *dyn_cache = NULL;
46 static int dyn_cache_cleanup(struct thread *);
47
48 void dyn_cache_init(void)
49 {
50 if (dyn_cache == NULL)
51 dyn_cache = list_new();
52 thread_add_timer(master, dyn_cache_cleanup, NULL, 120,
53 &isis->t_dync_clean);
54 return;
55 }
56
57 static int dyn_cache_cleanup(struct thread *thread)
58 {
59 struct listnode *node, *nnode;
60 struct isis_dynhn *dyn;
61 time_t now = time(NULL);
62
63 isis->t_dync_clean = NULL;
64
65 for (ALL_LIST_ELEMENTS(dyn_cache, node, nnode, dyn)) {
66 if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
67 continue;
68
69 list_delete_node(dyn_cache, node);
70 XFREE(MTYPE_ISIS_DYNHN, dyn);
71 }
72
73 thread_add_timer(master, dyn_cache_cleanup, NULL, 120,
74 &isis->t_dync_clean);
75 return ISIS_OK;
76 }
77
78 struct isis_dynhn *dynhn_find_by_id(const uint8_t *id)
79 {
80 struct listnode *node = NULL;
81 struct isis_dynhn *dyn = NULL;
82
83 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
84 if (memcmp(dyn->id, id, ISIS_SYS_ID_LEN) == 0)
85 return dyn;
86
87 return NULL;
88 }
89
90 struct isis_dynhn *dynhn_find_by_name(const char *hostname)
91 {
92 struct listnode *node = NULL;
93 struct isis_dynhn *dyn = NULL;
94
95 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
96 if (strncmp(dyn->hostname, hostname, 255) == 0)
97 return dyn;
98
99 return NULL;
100 }
101
102 void isis_dynhn_insert(const uint8_t *id, const char *hostname, int level)
103 {
104 struct isis_dynhn *dyn;
105
106 dyn = dynhn_find_by_id(id);
107 if (!dyn) {
108 dyn = XCALLOC(MTYPE_ISIS_DYNHN, sizeof(struct isis_dynhn));
109 memcpy(dyn->id, id, ISIS_SYS_ID_LEN);
110 dyn->level = level;
111 listnode_add(dyn_cache, dyn);
112 }
113
114 snprintf(dyn->hostname, sizeof(dyn->hostname), "%s", hostname);
115 dyn->refresh = time(NULL);
116 }
117
118 void isis_dynhn_remove(const uint8_t *id)
119 {
120 struct isis_dynhn *dyn;
121
122 dyn = dynhn_find_by_id(id);
123 if (!dyn)
124 return;
125 listnode_delete(dyn_cache, dyn);
126 XFREE(MTYPE_ISIS_DYNHN, dyn);
127 }
128
129 /*
130 * Level System ID Dynamic Hostname (notag)
131 * 2 0000.0000.0001 foo-gw
132 * 2 0000.0000.0002 bar-gw
133 * * 0000.0000.0004 this-gw
134 */
135 void dynhn_print_all(struct vty *vty)
136 {
137 struct listnode *node;
138 struct isis_dynhn *dyn;
139
140 vty_out(vty, "Level System ID Dynamic Hostname\n");
141 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn)) {
142 vty_out(vty, "%-7d", dyn->level);
143 vty_out(vty, "%-15s%-15s\n", sysid_print(dyn->id),
144 dyn->hostname);
145 }
146
147 vty_out(vty, " * %s %s\n", sysid_print(isis->sysid),
148 cmd_hostname_get());
149 return;
150 }