blob: 13acae71f301599e8b842d0bcb852e446ed428e4 [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
23#include <time.h>
24#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000025
26#include "vty.h"
27#include "linklist.h"
28#include "memory.h"
29#include "log.h"
30#include "stream.h"
31#include "command.h"
32#include "if.h"
hassod3d74742005-09-28 18:30:51 +000033#include "thread.h"
jardineb5d44e2003-12-23 08:09:43 +000034
35#include "isisd/dict.h"
36#include "isisd/isis_constants.h"
37#include "isisd/isis_common.h"
38#include "isisd/isis_flags.h"
39#include "isisd/isis_circuit.h"
40#include "isisd/isisd.h"
41#include "isisd/isis_dynhn.h"
42#include "isisd/isis_misc.h"
43#include "isisd/isis_constants.h"
44
45extern struct isis *isis;
hassod3d74742005-09-28 18:30:51 +000046extern struct thread_master *master;
jardineb5d44e2003-12-23 08:09:43 +000047extern struct host host;
48
49struct list *dyn_cache = NULL;
hassod3d74742005-09-28 18:30:51 +000050static int dyn_cache_cleanup (struct thread *);
jardineb5d44e2003-12-23 08:09:43 +000051
52void
53dyn_cache_init (void)
54{
55 dyn_cache = list_new ();
hassod3d74742005-09-28 18:30:51 +000056 THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
jardineb5d44e2003-12-23 08:09:43 +000057 return;
58}
59
hassod3d74742005-09-28 18:30:51 +000060static int
61dyn_cache_cleanup (struct thread *thread)
62{
63 struct listnode *node, *nnode;
64 struct isis_dynhn *dyn;
65 time_t now = time (NULL);
66
67 isis->t_dync_clean = NULL;
68
69 for (ALL_LIST_ELEMENTS (dyn_cache, node, nnode, dyn))
70 {
71 if ((now - dyn->refresh) < (MAX_AGE + 120))
72 continue;
73
74 list_delete_node (dyn_cache, node);
75 XFREE (MTYPE_ISIS_DYNHN, dyn);
76 }
77
78 THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
79 return ISIS_OK;
80}
81
hassof390d2c2004-09-10 20:48:21 +000082struct isis_dynhn *
83dynhn_find_by_id (u_char * id)
jardineb5d44e2003-12-23 08:09:43 +000084{
85 struct listnode *node = NULL;
86 struct isis_dynhn *dyn = NULL;
87
paul1eb8ef22005-04-07 07:30:20 +000088 for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
89 if (memcmp (dyn->id, id, ISIS_SYS_ID_LEN) == 0)
90 return dyn;
hassof390d2c2004-09-10 20:48:21 +000091
jardineb5d44e2003-12-23 08:09:43 +000092 return NULL;
93}
94
95void
hassof390d2c2004-09-10 20:48:21 +000096isis_dynhn_insert (u_char * id, struct hostname *hostname, int level)
jardineb5d44e2003-12-23 08:09:43 +000097{
98 struct isis_dynhn *dyn;
99
100 dyn = dynhn_find_by_id (id);
hassof390d2c2004-09-10 20:48:21 +0000101 if (dyn)
102 {
103 memcpy (&dyn->name, hostname, hostname->namelen + 1);
104 memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
105 dyn->refresh = time (NULL);
106 return;
107 }
hassod3d74742005-09-28 18:30:51 +0000108 dyn = XCALLOC (MTYPE_ISIS_DYNHN, sizeof (struct isis_dynhn));
hassof390d2c2004-09-10 20:48:21 +0000109 if (!dyn)
110 {
111 zlog_warn ("isis_dynhn_insert(): out of memory!");
112 return;
113 }
hassod3d74742005-09-28 18:30:51 +0000114
jardineb5d44e2003-12-23 08:09:43 +0000115 /* we also copy the length */
hassof390d2c2004-09-10 20:48:21 +0000116 memcpy (&dyn->name, hostname, hostname->namelen + 1);
jardineb5d44e2003-12-23 08:09:43 +0000117 memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
118 dyn->refresh = time (NULL);
119 dyn->level = level;
hassof390d2c2004-09-10 20:48:21 +0000120
jardineb5d44e2003-12-23 08:09:43 +0000121 listnode_add (dyn_cache, dyn);
122
123 return;
124}
125
126/*
127 * Level System ID Dynamic Hostname (notag)
128 * 2 0000.0000.0001 foo-gw
129 * 2 0000.0000.0002 bar-gw
130 * * 0000.0000.0004 this-gw
131 */
hassof390d2c2004-09-10 20:48:21 +0000132void
133dynhn_print_all (struct vty *vty)
jardineb5d44e2003-12-23 08:09:43 +0000134{
135 struct listnode *node;
136 struct isis_dynhn *dyn;
137
138 vty_out (vty, "Level System ID Dynamic Hostname%s", VTY_NEWLINE);
paul1eb8ef22005-04-07 07:30:20 +0000139 for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
hassof390d2c2004-09-10 20:48:21 +0000140 {
hassof390d2c2004-09-10 20:48:21 +0000141 vty_out (vty, "%-7d", dyn->level);
142 vty_out (vty, "%-15s%-15s%s", sysid_print (dyn->id), dyn->name.name,
143 VTY_NEWLINE);
144 }
145
146 vty_out (vty, " * %s %s%s", sysid_print (isis->sysid), unix_hostname (),
jardineb5d44e2003-12-23 08:09:43 +0000147 VTY_NEWLINE);
148 return;
149}