ripng_peer.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* RIPng peer support
  2. * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. /* RIPng support added by Vincent Jardin <vincent.jardin@6wind.com>
  22. * Copyright (C) 2002 6WIND
  23. */
  24. #include <zebra.h>
  25. #include "if.h"
  26. #include "prefix.h"
  27. #include "command.h"
  28. #include "linklist.h"
  29. #include "thread.h"
  30. #include "memory.h"
  31. #include "ripngd/ripngd.h"
  32. #include "ripngd/ripng_nexthop.h"
  33. /* Linked list of RIPng peer. */
  34. struct list *peer_list;
  35. static struct ripng_peer *
  36. ripng_peer_new (void)
  37. {
  38. struct ripng_peer *new;
  39. new = XMALLOC (MTYPE_RIPNG_PEER, sizeof (struct ripng_peer));
  40. memset (new, 0, sizeof (struct ripng_peer));
  41. return new;
  42. }
  43. static void
  44. ripng_peer_free (struct ripng_peer *peer)
  45. {
  46. XFREE (MTYPE_RIPNG_PEER, peer);
  47. }
  48. struct ripng_peer *
  49. ripng_peer_lookup (struct in6_addr *addr)
  50. {
  51. struct ripng_peer *peer;
  52. struct listnode *node, *nnode;
  53. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  54. {
  55. if (IPV6_ADDR_SAME (&peer->addr, addr))
  56. return peer;
  57. }
  58. return NULL;
  59. }
  60. struct ripng_peer *
  61. ripng_peer_lookup_next (struct in6_addr *addr)
  62. {
  63. struct ripng_peer *peer;
  64. struct listnode *node, *nnode;
  65. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  66. {
  67. if (addr6_cmp(&peer->addr, addr) > 0)
  68. return peer;
  69. }
  70. return NULL;
  71. }
  72. /* RIPng peer is timeout.
  73. * Garbage collector.
  74. **/
  75. static int
  76. ripng_peer_timeout (struct thread *t)
  77. {
  78. struct ripng_peer *peer;
  79. peer = THREAD_ARG (t);
  80. listnode_delete (peer_list, peer);
  81. ripng_peer_free (peer);
  82. return 0;
  83. }
  84. /* Get RIPng peer. At the same time update timeout thread. */
  85. static struct ripng_peer *
  86. ripng_peer_get (struct in6_addr *addr)
  87. {
  88. struct ripng_peer *peer;
  89. peer = ripng_peer_lookup (addr);
  90. if (peer)
  91. {
  92. if (peer->t_timeout)
  93. thread_cancel (peer->t_timeout);
  94. }
  95. else
  96. {
  97. peer = ripng_peer_new ();
  98. peer->addr = *addr; /* XXX */
  99. listnode_add_sort (peer_list, peer);
  100. }
  101. /* Update timeout thread. */
  102. peer->t_timeout = thread_add_timer (master, ripng_peer_timeout, peer,
  103. RIPNG_PEER_TIMER_DEFAULT);
  104. /* Last update time set. */
  105. time (&peer->uptime);
  106. return peer;
  107. }
  108. void
  109. ripng_peer_update (struct sockaddr_in6 *from, u_char version)
  110. {
  111. struct ripng_peer *peer;
  112. peer = ripng_peer_get (&from->sin6_addr);
  113. peer->version = version;
  114. }
  115. void
  116. ripng_peer_bad_route (struct sockaddr_in6 *from)
  117. {
  118. struct ripng_peer *peer;
  119. peer = ripng_peer_get (&from->sin6_addr);
  120. peer->recv_badroutes++;
  121. }
  122. void
  123. ripng_peer_bad_packet (struct sockaddr_in6 *from)
  124. {
  125. struct ripng_peer *peer;
  126. peer = ripng_peer_get (&from->sin6_addr);
  127. peer->recv_badpackets++;
  128. }
  129. /* Display peer uptime. */
  130. static char *
  131. ripng_peer_uptime (struct ripng_peer *peer, char *buf, size_t len)
  132. {
  133. time_t uptime;
  134. struct tm *tm;
  135. /* If there is no connection has been done before print `never'. */
  136. if (peer->uptime == 0)
  137. {
  138. snprintf (buf, len, "never ");
  139. return buf;
  140. }
  141. /* Get current time. */
  142. uptime = time (NULL);
  143. uptime -= peer->uptime;
  144. tm = gmtime (&uptime);
  145. /* Making formatted timer strings. */
  146. #define ONE_DAY_SECOND 60*60*24
  147. #define ONE_WEEK_SECOND 60*60*24*7
  148. if (uptime < ONE_DAY_SECOND)
  149. snprintf (buf, len, "%02d:%02d:%02d",
  150. tm->tm_hour, tm->tm_min, tm->tm_sec);
  151. else if (uptime < ONE_WEEK_SECOND)
  152. snprintf (buf, len, "%dd%02dh%02dm",
  153. tm->tm_yday, tm->tm_hour, tm->tm_min);
  154. else
  155. snprintf (buf, len, "%02dw%dd%02dh",
  156. tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
  157. return buf;
  158. }
  159. void
  160. ripng_peer_display (struct vty *vty)
  161. {
  162. struct ripng_peer *peer;
  163. struct listnode *node, *nnode;
  164. #define RIPNG_UPTIME_LEN 25
  165. char timebuf[RIPNG_UPTIME_LEN];
  166. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  167. {
  168. vty_out (vty, " %s %s%14s %10d %10d %10d %s%s", inet6_ntoa (peer->addr),
  169. VTY_NEWLINE, " ",
  170. peer->recv_badpackets, peer->recv_badroutes,
  171. ZEBRA_RIPNG_DISTANCE_DEFAULT,
  172. ripng_peer_uptime (peer, timebuf, RIPNG_UPTIME_LEN),
  173. VTY_NEWLINE);
  174. }
  175. }
  176. static int
  177. ripng_peer_list_cmp (struct ripng_peer *p1, struct ripng_peer *p2)
  178. {
  179. return addr6_cmp(&p1->addr, &p2->addr) > 0;
  180. }
  181. void
  182. ripng_peer_init ()
  183. {
  184. peer_list = list_new ();
  185. peer_list->cmp = (int (*)(void *, void *)) ripng_peer_list_cmp;
  186. }