jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 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 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 23 | #include <zebra.h> |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 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" |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 32 | #include "thread.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 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 isis *isis; |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 45 | extern struct thread_master *master; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 46 | extern struct host host; |
| 47 | |
| 48 | struct list *dyn_cache = NULL; |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 49 | static int dyn_cache_cleanup (struct thread *); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 50 | |
| 51 | void |
| 52 | dyn_cache_init (void) |
| 53 | { |
| 54 | dyn_cache = list_new (); |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 55 | THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 59 | static int |
| 60 | dyn_cache_cleanup (struct thread *thread) |
| 61 | { |
| 62 | struct listnode *node, *nnode; |
| 63 | struct isis_dynhn *dyn; |
| 64 | time_t now = time (NULL); |
| 65 | |
| 66 | isis->t_dync_clean = NULL; |
| 67 | |
| 68 | for (ALL_LIST_ELEMENTS (dyn_cache, node, nnode, dyn)) |
| 69 | { |
| 70 | if ((now - dyn->refresh) < (MAX_AGE + 120)) |
| 71 | continue; |
| 72 | |
| 73 | list_delete_node (dyn_cache, node); |
| 74 | XFREE (MTYPE_ISIS_DYNHN, dyn); |
| 75 | } |
| 76 | |
| 77 | THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120); |
| 78 | return ISIS_OK; |
| 79 | } |
| 80 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 81 | struct isis_dynhn * |
| 82 | dynhn_find_by_id (u_char * id) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 83 | { |
| 84 | struct listnode *node = NULL; |
| 85 | struct isis_dynhn *dyn = NULL; |
| 86 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 87 | for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn)) |
| 88 | if (memcmp (dyn->id, id, ISIS_SYS_ID_LEN) == 0) |
| 89 | return dyn; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 90 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | void |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 95 | isis_dynhn_insert (u_char * id, struct hostname *hostname, int level) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 96 | { |
| 97 | struct isis_dynhn *dyn; |
| 98 | |
| 99 | dyn = dynhn_find_by_id (id); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 100 | if (dyn) |
| 101 | { |
| 102 | memcpy (&dyn->name, hostname, hostname->namelen + 1); |
| 103 | memcpy (dyn->id, id, ISIS_SYS_ID_LEN); |
| 104 | dyn->refresh = time (NULL); |
| 105 | return; |
| 106 | } |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 107 | dyn = XCALLOC (MTYPE_ISIS_DYNHN, sizeof (struct isis_dynhn)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 108 | if (!dyn) |
| 109 | { |
| 110 | zlog_warn ("isis_dynhn_insert(): out of memory!"); |
| 111 | return; |
| 112 | } |
hasso | d3d7474 | 2005-09-28 18:30:51 +0000 | [diff] [blame] | 113 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 114 | /* we also copy the length */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 115 | memcpy (&dyn->name, hostname, hostname->namelen + 1); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 116 | memcpy (dyn->id, id, ISIS_SYS_ID_LEN); |
| 117 | dyn->refresh = time (NULL); |
| 118 | dyn->level = level; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 119 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 120 | listnode_add (dyn_cache, dyn); |
| 121 | |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Level System ID Dynamic Hostname (notag) |
| 127 | * 2 0000.0000.0001 foo-gw |
| 128 | * 2 0000.0000.0002 bar-gw |
| 129 | * * 0000.0000.0004 this-gw |
| 130 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 131 | void |
| 132 | dynhn_print_all (struct vty *vty) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 133 | { |
| 134 | struct listnode *node; |
| 135 | struct isis_dynhn *dyn; |
| 136 | |
| 137 | vty_out (vty, "Level System ID Dynamic Hostname%s", VTY_NEWLINE); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 138 | for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn)) |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 139 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 140 | vty_out (vty, "%-7d", dyn->level); |
| 141 | vty_out (vty, "%-15s%-15s%s", sysid_print (dyn->id), dyn->name.name, |
| 142 | VTY_NEWLINE); |
| 143 | } |
| 144 | |
| 145 | vty_out (vty, " * %s %s%s", sysid_print (isis->sysid), unix_hostname (), |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 146 | VTY_NEWLINE); |
| 147 | return; |
| 148 | } |