rip_peer.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* RIP 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. #include <zebra.h>
  22. #include "if.h"
  23. #include "prefix.h"
  24. #include "command.h"
  25. #include "linklist.h"
  26. #include "thread.h"
  27. #include "memory.h"
  28. #include "ripd/ripd.h"
  29. /* Linked list of RIP peer. */
  30. struct list *peer_list;
  31. static struct rip_peer *
  32. rip_peer_new (void)
  33. {
  34. struct rip_peer *new;
  35. new = XMALLOC (MTYPE_RIP_PEER, sizeof (struct rip_peer));
  36. memset (new, 0, sizeof (struct rip_peer));
  37. return new;
  38. }
  39. static void
  40. rip_peer_free (struct rip_peer *peer)
  41. {
  42. XFREE (MTYPE_RIP_PEER, peer);
  43. }
  44. struct rip_peer *
  45. rip_peer_lookup (struct in_addr *addr)
  46. {
  47. struct rip_peer *peer;
  48. struct listnode *node, *nnode;
  49. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  50. {
  51. if (IPV4_ADDR_SAME (&peer->addr, addr))
  52. return peer;
  53. }
  54. return NULL;
  55. }
  56. struct rip_peer *
  57. rip_peer_lookup_next (struct in_addr *addr)
  58. {
  59. struct rip_peer *peer;
  60. struct listnode *node, *nnode;
  61. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  62. {
  63. if (htonl (peer->addr.s_addr) > htonl (addr->s_addr))
  64. return peer;
  65. }
  66. return NULL;
  67. }
  68. /* RIP peer is timeout. */
  69. static int
  70. rip_peer_timeout (struct thread *t)
  71. {
  72. struct rip_peer *peer;
  73. peer = THREAD_ARG (t);
  74. listnode_delete (peer_list, peer);
  75. rip_peer_free (peer);
  76. return 0;
  77. }
  78. /* Get RIP peer. At the same time update timeout thread. */
  79. static struct rip_peer *
  80. rip_peer_get (struct in_addr *addr)
  81. {
  82. struct rip_peer *peer;
  83. peer = rip_peer_lookup (addr);
  84. if (peer)
  85. {
  86. if (peer->t_timeout)
  87. thread_cancel (peer->t_timeout);
  88. }
  89. else
  90. {
  91. peer = rip_peer_new ();
  92. peer->addr = *addr;
  93. listnode_add_sort (peer_list, peer);
  94. }
  95. /* Update timeout thread. */
  96. peer->t_timeout = thread_add_timer (master, rip_peer_timeout, peer,
  97. RIP_PEER_TIMER_DEFAULT);
  98. /* Last update time set. */
  99. time (&peer->uptime);
  100. return peer;
  101. }
  102. void
  103. rip_peer_update (struct sockaddr_in *from, u_char version)
  104. {
  105. struct rip_peer *peer;
  106. peer = rip_peer_get (&from->sin_addr);
  107. peer->version = version;
  108. }
  109. void
  110. rip_peer_bad_route (struct sockaddr_in *from)
  111. {
  112. struct rip_peer *peer;
  113. peer = rip_peer_get (&from->sin_addr);
  114. peer->recv_badroutes++;
  115. }
  116. void
  117. rip_peer_bad_packet (struct sockaddr_in *from)
  118. {
  119. struct rip_peer *peer;
  120. peer = rip_peer_get (&from->sin_addr);
  121. peer->recv_badpackets++;
  122. }
  123. /* Display peer uptime. */
  124. static char *
  125. rip_peer_uptime (struct rip_peer *peer, char *buf, size_t len)
  126. {
  127. time_t uptime;
  128. struct tm *tm;
  129. /* If there is no connection has been done before print `never'. */
  130. if (peer->uptime == 0)
  131. {
  132. snprintf (buf, len, "never ");
  133. return buf;
  134. }
  135. /* Get current time. */
  136. uptime = time (NULL);
  137. uptime -= peer->uptime;
  138. tm = gmtime (&uptime);
  139. /* Making formatted timer strings. */
  140. #define ONE_DAY_SECOND 60*60*24
  141. #define ONE_WEEK_SECOND 60*60*24*7
  142. if (uptime < ONE_DAY_SECOND)
  143. snprintf (buf, len, "%02d:%02d:%02d",
  144. tm->tm_hour, tm->tm_min, tm->tm_sec);
  145. else if (uptime < ONE_WEEK_SECOND)
  146. snprintf (buf, len, "%dd%02dh%02dm",
  147. tm->tm_yday, tm->tm_hour, tm->tm_min);
  148. else
  149. snprintf (buf, len, "%02dw%dd%02dh",
  150. tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
  151. return buf;
  152. }
  153. void
  154. rip_peer_display (struct vty *vty)
  155. {
  156. struct rip_peer *peer;
  157. struct listnode *node, *nnode;
  158. #define RIP_UPTIME_LEN 25
  159. char timebuf[RIP_UPTIME_LEN];
  160. for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
  161. {
  162. vty_out (vty, " %-16s %9d %9d %9d %s%s", inet_ntoa (peer->addr),
  163. peer->recv_badpackets, peer->recv_badroutes,
  164. ZEBRA_RIP_DISTANCE_DEFAULT,
  165. rip_peer_uptime (peer, timebuf, RIP_UPTIME_LEN),
  166. VTY_NEWLINE);
  167. }
  168. }
  169. static int
  170. rip_peer_list_cmp (struct rip_peer *p1, struct rip_peer *p2)
  171. {
  172. return htonl (p1->addr.s_addr) > htonl (p2->addr.s_addr);
  173. }
  174. void
  175. rip_peer_init (void)
  176. {
  177. peer_list = list_new ();
  178. peer_list->cmp = (int (*)(void *, void *)) rip_peer_list_cmp;
  179. }