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
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
31 #include "ripd/ripd.h"
33 /* Linked list of RIP peer. */
34 struct list
*peer_list
;
36 static struct rip_peer
*rip_peer_new(void)
38 return XCALLOC(MTYPE_RIP_PEER
, sizeof(struct rip_peer
));
41 static void rip_peer_free(struct rip_peer
*peer
)
43 XFREE(MTYPE_RIP_PEER
, peer
);
46 struct rip_peer
*rip_peer_lookup(struct in_addr
*addr
)
48 struct rip_peer
*peer
;
49 struct listnode
*node
, *nnode
;
51 for (ALL_LIST_ELEMENTS(peer_list
, node
, nnode
, peer
)) {
52 if (IPV4_ADDR_SAME(&peer
->addr
, addr
))
58 struct rip_peer
*rip_peer_lookup_next(struct in_addr
*addr
)
60 struct rip_peer
*peer
;
61 struct listnode
*node
, *nnode
;
63 for (ALL_LIST_ELEMENTS(peer_list
, node
, nnode
, peer
)) {
64 if (htonl(peer
->addr
.s_addr
) > htonl(addr
->s_addr
))
70 /* RIP peer is timeout. */
71 static int rip_peer_timeout(struct thread
*t
)
73 struct rip_peer
*peer
;
76 listnode_delete(peer_list
, peer
);
82 /* Get RIP peer. At the same time update timeout thread. */
83 static struct rip_peer
*rip_peer_get(struct in_addr
*addr
)
85 struct rip_peer
*peer
;
87 peer
= rip_peer_lookup(addr
);
91 thread_cancel(peer
->t_timeout
);
93 peer
= rip_peer_new();
95 listnode_add_sort(peer_list
, peer
);
98 /* Update timeout thread. */
99 peer
->t_timeout
= thread_add_timer(master
, rip_peer_timeout
, peer
,
100 RIP_PEER_TIMER_DEFAULT
);
102 /* Last update time set. */
108 void rip_peer_update(struct sockaddr_in
*from
, u_char version
)
110 struct rip_peer
*peer
;
111 peer
= rip_peer_get(&from
->sin_addr
);
112 peer
->version
= version
;
115 void rip_peer_bad_route(struct sockaddr_in
*from
)
117 struct rip_peer
*peer
;
118 peer
= rip_peer_get(&from
->sin_addr
);
119 peer
->recv_badroutes
++;
122 void rip_peer_bad_packet(struct sockaddr_in
*from
)
124 struct rip_peer
*peer
;
125 peer
= rip_peer_get(&from
->sin_addr
);
126 peer
->recv_badpackets
++;
129 /* Display peer uptime. */
130 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
;
144 tm
= gmtime(&uptime
);
146 /* Making formatted timer strings. */
147 #define ONE_DAY_SECOND 60*60*24
148 #define ONE_WEEK_SECOND 60*60*24*7
150 if (uptime
< ONE_DAY_SECOND
)
151 snprintf(buf
, len
, "%02d:%02d:%02d", tm
->tm_hour
, tm
->tm_min
,
153 else if (uptime
< ONE_WEEK_SECOND
)
154 snprintf(buf
, len
, "%dd%02dh%02dm", tm
->tm_yday
, tm
->tm_hour
,
157 snprintf(buf
, len
, "%02dw%dd%02dh", tm
->tm_yday
/ 7,
158 tm
->tm_yday
- ((tm
->tm_yday
/ 7) * 7), tm
->tm_hour
);
162 void rip_peer_display(struct vty
*vty
)
164 struct rip_peer
*peer
;
165 struct listnode
*node
, *nnode
;
166 #define RIP_UPTIME_LEN 25
167 char timebuf
[RIP_UPTIME_LEN
];
169 for (ALL_LIST_ELEMENTS(peer_list
, node
, nnode
, peer
)) {
170 vty_out(vty
, " %-16s %9d %9d %9d %s%s",
171 inet_ntoa(peer
->addr
), peer
->recv_badpackets
,
172 peer
->recv_badroutes
, ZEBRA_RIP_DISTANCE_DEFAULT
,
173 rip_peer_uptime(peer
, timebuf
, RIP_UPTIME_LEN
),
178 static int rip_peer_list_cmp(struct rip_peer
*p1
, struct rip_peer
*p2
)
180 return htonl(p1
->addr
.s_addr
) > htonl(p2
->addr
.s_addr
);
183 void rip_peer_init(void)
185 peer_list
= list_new();
186 peer_list
->cmp
= (int (*)(void *, void *))rip_peer_list_cmp
;