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