blob: ffb0d503fb648bf31f4f0bf9789619b257a3db3b [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
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
jardineb5d44e2003-12-23 08:09:43 +000023#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000024
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"
hassod3d74742005-09-28 18:30:51 +000032#include "thread.h"
jardineb5d44e2003-12-23 08:09:43 +000033
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
jardineb5d44e2003-12-23 08:09:43 +000044extern struct host host;
45
46struct list *dyn_cache = NULL;
hassod3d74742005-09-28 18:30:51 +000047static int dyn_cache_cleanup (struct thread *);
jardineb5d44e2003-12-23 08:09:43 +000048
49void
50dyn_cache_init (void)
51{
Josh Bailey3f045a02012-03-24 08:35:20 -070052 if (dyn_cache == NULL)
53 dyn_cache = list_new ();
hassod3d74742005-09-28 18:30:51 +000054 THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
jardineb5d44e2003-12-23 08:09:43 +000055 return;
56}
57
hassod3d74742005-09-28 18:30:51 +000058static int
59dyn_cache_cleanup (struct thread *thread)
60{
61 struct listnode *node, *nnode;
62 struct isis_dynhn *dyn;
63 time_t now = time (NULL);
64
65 isis->t_dync_clean = NULL;
66
67 for (ALL_LIST_ELEMENTS (dyn_cache, node, nnode, dyn))
68 {
Josh Bailey3f045a02012-03-24 08:35:20 -070069 if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
70 continue;
hassod3d74742005-09-28 18:30:51 +000071
72 list_delete_node (dyn_cache, node);
73 XFREE (MTYPE_ISIS_DYNHN, dyn);
74 }
75
76 THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
77 return ISIS_OK;
78}
79
hassof390d2c2004-09-10 20:48:21 +000080struct isis_dynhn *
81dynhn_find_by_id (u_char * id)
jardineb5d44e2003-12-23 08:09:43 +000082{
83 struct listnode *node = NULL;
84 struct isis_dynhn *dyn = NULL;
85
paul1eb8ef22005-04-07 07:30:20 +000086 for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
87 if (memcmp (dyn->id, id, ISIS_SYS_ID_LEN) == 0)
88 return dyn;
hassof390d2c2004-09-10 20:48:21 +000089
jardineb5d44e2003-12-23 08:09:43 +000090 return NULL;
91}
92
Josh Bailey3f045a02012-03-24 08:35:20 -070093struct isis_dynhn *
94dynhn_find_by_name (const char *hostname)
95{
96 struct listnode *node = NULL;
97 struct isis_dynhn *dyn = NULL;
98
99 for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
100 if (strncmp ((char *)dyn->name.name, hostname, 255) == 0)
101 return dyn;
102
103 return NULL;
104}
105
jardineb5d44e2003-12-23 08:09:43 +0000106void
hassof390d2c2004-09-10 20:48:21 +0000107isis_dynhn_insert (u_char * id, struct hostname *hostname, int level)
jardineb5d44e2003-12-23 08:09:43 +0000108{
109 struct isis_dynhn *dyn;
110
111 dyn = dynhn_find_by_id (id);
hassof390d2c2004-09-10 20:48:21 +0000112 if (dyn)
113 {
114 memcpy (&dyn->name, hostname, hostname->namelen + 1);
115 memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
116 dyn->refresh = time (NULL);
117 return;
118 }
hassod3d74742005-09-28 18:30:51 +0000119 dyn = XCALLOC (MTYPE_ISIS_DYNHN, sizeof (struct isis_dynhn));
hassof390d2c2004-09-10 20:48:21 +0000120 if (!dyn)
121 {
122 zlog_warn ("isis_dynhn_insert(): out of memory!");
123 return;
124 }
hassod3d74742005-09-28 18:30:51 +0000125
jardineb5d44e2003-12-23 08:09:43 +0000126 /* we also copy the length */
hassof390d2c2004-09-10 20:48:21 +0000127 memcpy (&dyn->name, hostname, hostname->namelen + 1);
jardineb5d44e2003-12-23 08:09:43 +0000128 memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
129 dyn->refresh = time (NULL);
130 dyn->level = level;
hassof390d2c2004-09-10 20:48:21 +0000131
jardineb5d44e2003-12-23 08:09:43 +0000132 listnode_add (dyn_cache, dyn);
133
134 return;
135}
136
Josh Bailey3f045a02012-03-24 08:35:20 -0700137void
138isis_dynhn_remove (u_char * id)
139{
140 struct isis_dynhn *dyn;
141
142 dyn = dynhn_find_by_id (id);
143 if (!dyn)
144 return;
145 listnode_delete (dyn_cache, dyn);
146 XFREE (MTYPE_ISIS_DYNHN, dyn);
147 return;
148}
149
jardineb5d44e2003-12-23 08:09:43 +0000150/*
151 * Level System ID Dynamic Hostname (notag)
152 * 2 0000.0000.0001 foo-gw
153 * 2 0000.0000.0002 bar-gw
154 * * 0000.0000.0004 this-gw
155 */
hassof390d2c2004-09-10 20:48:21 +0000156void
157dynhn_print_all (struct vty *vty)
jardineb5d44e2003-12-23 08:09:43 +0000158{
159 struct listnode *node;
160 struct isis_dynhn *dyn;
161
162 vty_out (vty, "Level System ID Dynamic Hostname%s", VTY_NEWLINE);
paul1eb8ef22005-04-07 07:30:20 +0000163 for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
hassof390d2c2004-09-10 20:48:21 +0000164 {
hassof390d2c2004-09-10 20:48:21 +0000165 vty_out (vty, "%-7d", dyn->level);
166 vty_out (vty, "%-15s%-15s%s", sysid_print (dyn->id), dyn->name.name,
167 VTY_NEWLINE);
168 }
169
170 vty_out (vty, " * %s %s%s", sysid_print (isis->sysid), unix_hostname (),
jardineb5d44e2003-12-23 08:09:43 +0000171 VTY_NEWLINE);
172 return;
173}