ripng_peer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. return XCALLOC (MTYPE_RIPNG_PEER, sizeof (struct ripng_peer));
  39. }
  40. static void
  41. ripng_peer_free (struct ripng_peer *peer)
  42. {
  43. XFREE (MTYPE_RIPNG_PEER, peer);
  44. }
  45. struct ripng_peer *
  46. ripng_peer_lookup (struct in6_addr *addr)
  47. {
  48. struct ripng_peer *peer;
  49. struct listnode *node, *nnode;
  50. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  51. {
  52. if (IPV6_ADDR_SAME (&peer->addr, addr))
  53. return peer;
  54. }
  55. return NULL;
  56. }
  57. struct ripng_peer *
  58. ripng_peer_lookup_next (struct in6_addr *addr)
  59. {
  60. struct ripng_peer *peer;
  61. struct listnode *node, *nnode;
  62. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  63. {
  64. if (addr6_cmp(&peer->addr, addr) > 0)
  65. return peer;
  66. }
  67. return NULL;
  68. }
  69. /* RIPng peer is timeout.
  70. * Garbage collector.
  71. **/
  72. static int
  73. ripng_peer_timeout (struct thread *t)
  74. {
  75. struct ripng_peer *peer;
  76. peer = THREAD_ARG (t);
  77. listnode_delete (peer_list, peer);
  78. ripng_peer_free (peer);
  79. return 0;
  80. }
  81. /* Get RIPng peer. At the same time update timeout thread. */
  82. static struct ripng_peer *
  83. ripng_peer_get (struct in6_addr *addr)
  84. {
  85. struct ripng_peer *peer;
  86. peer = ripng_peer_lookup (addr);
  87. if (peer)
  88. {
  89. if (peer->t_timeout)
  90. thread_cancel (peer->t_timeout);
  91. }
  92. else
  93. {
  94. peer = ripng_peer_new ();
  95. peer->addr = *addr; /* XXX */
  96. listnode_add_sort (peer_list, peer);
  97. }
  98. /* Update timeout thread. */
  99. peer->t_timeout = thread_add_timer (master, ripng_peer_timeout, peer,
  100. RIPNG_PEER_TIMER_DEFAULT);
  101. /* Last update time set. */
  102. time (&peer->uptime);
  103. return peer;
  104. }
  105. void
  106. ripng_peer_update (struct sockaddr_in6 *from, u_char version)
  107. {
  108. struct ripng_peer *peer;
  109. peer = ripng_peer_get (&from->sin6_addr);
  110. peer->version = version;
  111. }
  112. void
  113. ripng_peer_bad_route (struct sockaddr_in6 *from)
  114. {
  115. struct ripng_peer *peer;
  116. peer = ripng_peer_get (&from->sin6_addr);
  117. peer->recv_badroutes++;
  118. }
  119. void
  120. ripng_peer_bad_packet (struct sockaddr_in6 *from)
  121. {
  122. struct ripng_peer *peer;
  123. peer = ripng_peer_get (&from->sin6_addr);
  124. peer->recv_badpackets++;
  125. }
  126. /* Display peer uptime. */
  127. static char *
  128. ripng_peer_uptime (struct ripng_peer *peer, char *buf, size_t len)
  129. {
  130. time_t uptime;
  131. struct tm *tm;
  132. /* If there is no connection has been done before print `never'. */
  133. if (peer->uptime == 0)
  134. {
  135. snprintf (buf, len, "never ");
  136. return buf;
  137. }
  138. /* Get current time. */
  139. uptime = time (NULL);
  140. uptime -= peer->uptime;
  141. tm = gmtime (&uptime);
  142. /* Making formatted timer strings. */
  143. #define ONE_DAY_SECOND 60*60*24
  144. #define ONE_WEEK_SECOND 60*60*24*7
  145. if (uptime < ONE_DAY_SECOND)
  146. snprintf (buf, len, "%02d:%02d:%02d",
  147. tm->tm_hour, tm->tm_min, tm->tm_sec);
  148. else if (uptime < ONE_WEEK_SECOND)
  149. snprintf (buf, len, "%dd%02dh%02dm",
  150. tm->tm_yday, tm->tm_hour, tm->tm_min);
  151. else
  152. snprintf (buf, len, "%02dw%dd%02dh",
  153. tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
  154. return buf;
  155. }
  156. void
  157. ripng_peer_display (struct vty *vty)
  158. {
  159. struct ripng_peer *peer;
  160. struct listnode *node, *nnode;
  161. #define RIPNG_UPTIME_LEN 25
  162. char timebuf[RIPNG_UPTIME_LEN];
  163. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  164. {
  165. vty_out (vty, " %s %s%14s %10d %10d %10d %s%s", inet6_ntoa (peer->addr),
  166. VTY_NEWLINE, " ",
  167. peer->recv_badpackets, peer->recv_badroutes,
  168. ZEBRA_RIPNG_DISTANCE_DEFAULT,
  169. ripng_peer_uptime (peer, timebuf, RIPNG_UPTIME_LEN),
  170. VTY_NEWLINE);
  171. }
  172. }
  173. static int
  174. ripng_peer_list_cmp (struct ripng_peer *p1, struct ripng_peer *p2)
  175. {
  176. return addr6_cmp(&p1->addr, &p2->addr) > 0;
  177. }
  178. void
  179. ripng_peer_init ()
  180. {
  181. peer_list = list_new ();
  182. peer_list->cmp = (int (*)(void *, void *)) ripng_peer_list_cmp;
  183. }