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