]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_dynhn.c
*: reindent
[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; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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/dict.h"
35 #include "isisd/isis_constants.h"
36 #include "isisd/isis_common.h"
37 #include "isisd/isis_flags.h"
38 #include "isisd/isis_circuit.h"
39 #include "isisd/isisd.h"
40 #include "isisd/isis_dynhn.h"
41 #include "isisd/isis_misc.h"
42 #include "isisd/isis_constants.h"
43
44 extern struct host host;
45
46 struct list *dyn_cache = NULL;
47 static int dyn_cache_cleanup(struct thread *);
48
49 void dyn_cache_init(void)
50 {
51 if (dyn_cache == NULL)
52 dyn_cache = list_new();
53 THREAD_TIMER_ON(master, isis->t_dync_clean, dyn_cache_cleanup, NULL,
54 120);
55 return;
56 }
57
58 static int dyn_cache_cleanup(struct thread *thread)
59 {
60 struct listnode *node, *nnode;
61 struct isis_dynhn *dyn;
62 time_t now = time(NULL);
63
64 isis->t_dync_clean = NULL;
65
66 for (ALL_LIST_ELEMENTS(dyn_cache, node, nnode, dyn)) {
67 if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
68 continue;
69
70 list_delete_node(dyn_cache, node);
71 XFREE(MTYPE_ISIS_DYNHN, dyn);
72 }
73
74 THREAD_TIMER_ON(master, isis->t_dync_clean, dyn_cache_cleanup, NULL,
75 120);
76 return ISIS_OK;
77 }
78
79 struct isis_dynhn *dynhn_find_by_id(const u_char *id)
80 {
81 struct listnode *node = NULL;
82 struct isis_dynhn *dyn = NULL;
83
84 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
85 if (memcmp(dyn->id, id, ISIS_SYS_ID_LEN) == 0)
86 return dyn;
87
88 return NULL;
89 }
90
91 struct isis_dynhn *dynhn_find_by_name(const char *hostname)
92 {
93 struct listnode *node = NULL;
94 struct isis_dynhn *dyn = NULL;
95
96 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn))
97 if (strncmp((char *)dyn->name.name, hostname, 255) == 0)
98 return dyn;
99
100 return NULL;
101 }
102
103 void isis_dynhn_insert(const u_char *id, struct hostname *hostname, int level)
104 {
105 struct isis_dynhn *dyn;
106
107 dyn = dynhn_find_by_id(id);
108 if (dyn) {
109 memcpy(&dyn->name, hostname, hostname->namelen + 1);
110 memcpy(dyn->id, id, ISIS_SYS_ID_LEN);
111 dyn->refresh = time(NULL);
112 return;
113 }
114 dyn = XCALLOC(MTYPE_ISIS_DYNHN, sizeof(struct isis_dynhn));
115 if (!dyn) {
116 zlog_warn("isis_dynhn_insert(): out of memory!");
117 return;
118 }
119
120 /* we also copy the length */
121 memcpy(&dyn->name, hostname, hostname->namelen + 1);
122 memcpy(dyn->id, id, ISIS_SYS_ID_LEN);
123 dyn->refresh = time(NULL);
124 dyn->level = level;
125
126 listnode_add(dyn_cache, dyn);
127
128 return;
129 }
130
131 void isis_dynhn_remove(const u_char *id)
132 {
133 struct isis_dynhn *dyn;
134
135 dyn = dynhn_find_by_id(id);
136 if (!dyn)
137 return;
138 listnode_delete(dyn_cache, dyn);
139 XFREE(MTYPE_ISIS_DYNHN, dyn);
140 return;
141 }
142
143 /*
144 * Level System ID Dynamic Hostname (notag)
145 * 2 0000.0000.0001 foo-gw
146 * 2 0000.0000.0002 bar-gw
147 * * 0000.0000.0004 this-gw
148 */
149 void dynhn_print_all(struct vty *vty)
150 {
151 struct listnode *node;
152 struct isis_dynhn *dyn;
153
154 vty_out(vty, "Level System ID Dynamic Hostname%s", VTY_NEWLINE);
155 for (ALL_LIST_ELEMENTS_RO(dyn_cache, node, dyn)) {
156 vty_out(vty, "%-7d", dyn->level);
157 vty_out(vty, "%-15s%-15s%s", sysid_print(dyn->id),
158 dyn->name.name, VTY_NEWLINE);
159 }
160
161 vty_out(vty, " * %s %s%s", sysid_print(isis->sysid),
162 unix_hostname(), VTY_NEWLINE);
163 return;
164 }