rip_peer.c 4.6 KB

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