2 * Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
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
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.
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
30 #include "ripd/ripd.h"
32 DEFINE_MTYPE_STATIC(RIPD
, RIP_PEER
, "RIP peer")
34 static struct rip_peer
*rip_peer_new(void)
36 return XCALLOC(MTYPE_RIP_PEER
, sizeof(struct rip_peer
));
39 static void rip_peer_free(struct rip_peer
*peer
)
41 RIP_TIMER_OFF(peer
->t_timeout
);
42 XFREE(MTYPE_RIP_PEER
, peer
);
45 struct rip_peer
*rip_peer_lookup(struct rip
*rip
, struct in_addr
*addr
)
47 struct rip_peer
*peer
;
48 struct listnode
*node
, *nnode
;
50 for (ALL_LIST_ELEMENTS(rip
->peer_list
, node
, nnode
, peer
)) {
51 if (IPV4_ADDR_SAME(&peer
->addr
, addr
))
57 struct rip_peer
*rip_peer_lookup_next(struct rip
*rip
, struct in_addr
*addr
)
59 struct rip_peer
*peer
;
60 struct listnode
*node
, *nnode
;
62 for (ALL_LIST_ELEMENTS(rip
->peer_list
, node
, nnode
, peer
)) {
63 if (htonl(peer
->addr
.s_addr
) > htonl(addr
->s_addr
))
69 /* RIP peer is timeout. */
70 static int rip_peer_timeout(struct thread
*t
)
72 struct rip_peer
*peer
;
75 listnode_delete(peer
->rip
->peer_list
, peer
);
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
)
84 struct rip_peer
*peer
;
86 peer
= rip_peer_lookup(rip
, addr
);
90 thread_cancel(peer
->t_timeout
);
92 peer
= rip_peer_new();
95 listnode_add_sort(rip
->peer_list
, peer
);
98 /* Update timeout thread. */
99 peer
->t_timeout
= NULL
;
100 thread_add_timer(master
, rip_peer_timeout
, peer
, RIP_PEER_TIMER_DEFAULT
,
103 /* Last update time set. */
109 void rip_peer_update(struct rip
*rip
, struct sockaddr_in
*from
, uint8_t version
)
111 struct rip_peer
*peer
;
112 peer
= rip_peer_get(rip
, &from
->sin_addr
);
113 peer
->version
= version
;
116 void rip_peer_bad_route(struct rip
*rip
, struct sockaddr_in
*from
)
118 struct rip_peer
*peer
;
119 peer
= rip_peer_get(rip
, &from
->sin_addr
);
120 peer
->recv_badroutes
++;
123 void rip_peer_bad_packet(struct rip
*rip
, struct sockaddr_in
*from
)
125 struct rip_peer
*peer
;
126 peer
= rip_peer_get(rip
, &from
->sin_addr
);
127 peer
->recv_badpackets
++;
130 /* Display peer uptime. */
131 static char *rip_peer_uptime(struct rip_peer
*peer
, char *buf
, size_t len
)
135 /* If there is no connection has been done before print `never'. */
136 if (peer
->uptime
== 0) {
137 snprintf(buf
, len
, "never ");
141 /* Get current time. */
143 uptime
-= peer
->uptime
;
145 frrtime_to_interval(uptime
, buf
, len
);
150 void rip_peer_display(struct vty
*vty
, struct rip
*rip
)
152 struct rip_peer
*peer
;
153 struct listnode
*node
, *nnode
;
154 #define RIP_UPTIME_LEN 25
155 char timebuf
[RIP_UPTIME_LEN
];
157 for (ALL_LIST_ELEMENTS(rip
->peer_list
, node
, nnode
, peer
)) {
158 vty_out(vty
, " %-16s %9d %9d %9d %s\n",
159 inet_ntoa(peer
->addr
), peer
->recv_badpackets
,
160 peer
->recv_badroutes
, ZEBRA_RIP_DISTANCE_DEFAULT
,
161 rip_peer_uptime(peer
, timebuf
, RIP_UPTIME_LEN
));
165 int rip_peer_list_cmp(struct rip_peer
*p1
, struct rip_peer
*p2
)
167 if (p2
->addr
.s_addr
== p1
->addr
.s_addr
)
170 return (htonl(p1
->addr
.s_addr
) < htonl(p2
->addr
.s_addr
)) ? -1 : 1;
173 void rip_peer_list_del(void *arg
)