isis_dynhn.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include <zebra.h>
  23. #include "vty.h"
  24. #include "linklist.h"
  25. #include "memory.h"
  26. #include "log.h"
  27. #include "stream.h"
  28. #include "command.h"
  29. #include "if.h"
  30. #include "thread.h"
  31. #include "isisd/dict.h"
  32. #include "isisd/isis_constants.h"
  33. #include "isisd/isis_common.h"
  34. #include "isisd/isis_flags.h"
  35. #include "isisd/isis_circuit.h"
  36. #include "isisd/isisd.h"
  37. #include "isisd/isis_dynhn.h"
  38. #include "isisd/isis_misc.h"
  39. #include "isisd/isis_constants.h"
  40. extern struct host host;
  41. struct list *dyn_cache = NULL;
  42. static int dyn_cache_cleanup (struct thread *);
  43. void
  44. dyn_cache_init (void)
  45. {
  46. if (dyn_cache == NULL)
  47. dyn_cache = list_new ();
  48. THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
  49. return;
  50. }
  51. static int
  52. dyn_cache_cleanup (struct thread *thread)
  53. {
  54. struct listnode *node, *nnode;
  55. struct isis_dynhn *dyn;
  56. time_t now = time (NULL);
  57. isis->t_dync_clean = NULL;
  58. for (ALL_LIST_ELEMENTS (dyn_cache, node, nnode, dyn))
  59. {
  60. if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
  61. continue;
  62. list_delete_node (dyn_cache, node);
  63. XFREE (MTYPE_ISIS_DYNHN, dyn);
  64. }
  65. THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
  66. return ISIS_OK;
  67. }
  68. struct isis_dynhn *
  69. dynhn_find_by_id (const u_char * id)
  70. {
  71. struct listnode *node = NULL;
  72. struct isis_dynhn *dyn = NULL;
  73. for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
  74. if (memcmp (dyn->id, id, ISIS_SYS_ID_LEN) == 0)
  75. return dyn;
  76. return NULL;
  77. }
  78. struct isis_dynhn *
  79. dynhn_find_by_name (const char *hostname)
  80. {
  81. struct listnode *node = NULL;
  82. struct isis_dynhn *dyn = NULL;
  83. for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
  84. if (strncmp ((char *)dyn->name.name, hostname, 255) == 0)
  85. return dyn;
  86. return NULL;
  87. }
  88. void
  89. isis_dynhn_insert (const u_char * id, struct hostname *hostname, int level)
  90. {
  91. struct isis_dynhn *dyn;
  92. dyn = dynhn_find_by_id (id);
  93. if (dyn)
  94. {
  95. memcpy (&dyn->name, hostname, hostname->namelen + 1);
  96. memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
  97. dyn->refresh = time (NULL);
  98. return;
  99. }
  100. dyn = XCALLOC (MTYPE_ISIS_DYNHN, sizeof (struct isis_dynhn));
  101. if (!dyn)
  102. {
  103. zlog_warn ("isis_dynhn_insert(): out of memory!");
  104. return;
  105. }
  106. /* we also copy the length */
  107. memcpy (&dyn->name, hostname, hostname->namelen + 1);
  108. memcpy (dyn->id, id, ISIS_SYS_ID_LEN);
  109. dyn->refresh = time (NULL);
  110. dyn->level = level;
  111. listnode_add (dyn_cache, dyn);
  112. return;
  113. }
  114. void
  115. isis_dynhn_remove (const u_char * id)
  116. {
  117. struct isis_dynhn *dyn;
  118. dyn = dynhn_find_by_id (id);
  119. if (!dyn)
  120. return;
  121. listnode_delete (dyn_cache, dyn);
  122. XFREE (MTYPE_ISIS_DYNHN, dyn);
  123. return;
  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. */
  131. void
  132. dynhn_print_all (struct vty *vty)
  133. {
  134. struct listnode *node;
  135. struct isis_dynhn *dyn;
  136. vty_out (vty, "Level System ID Dynamic Hostname%s", VTY_NEWLINE);
  137. for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
  138. {
  139. vty_out (vty, "%-7d", dyn->level);
  140. vty_out (vty, "%-15s%-15s%s", sysid_print (dyn->id), dyn->name.name,
  141. VTY_NEWLINE);
  142. }
  143. vty_out (vty, " * %s %s%s", sysid_print (isis->sysid), unix_hostname (),
  144. VTY_NEWLINE);
  145. return;
  146. }