]> git.proxmox.com Git - mirror_frr.git/blame - ripngd/ripng_peer.c
Merge pull request #531 from qlyoung/fix-stack-ref
[mirror_frr.git] / ripngd / ripng_peer.c
CommitLineData
a94434b6 1/* RIPng 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
22/* RIPng support added by Vincent Jardin <vincent.jardin@6wind.com>
23 * Copyright (C) 2002 6WIND
24 */
25
26#include <zebra.h>
27
28#include "if.h"
29#include "prefix.h"
30#include "command.h"
31#include "linklist.h"
32#include "thread.h"
33#include "memory.h"
34
35#include "ripngd/ripngd.h"
36#include "ripngd/ripng_nexthop.h"
37
38
39/* Linked list of RIPng peer. */
40struct list *peer_list;
6b0655a2 41
6ac29a51
PJ
42static struct ripng_peer *
43ripng_peer_new (void)
a94434b6 44{
393deb9b 45 return XCALLOC (MTYPE_RIPNG_PEER, sizeof (struct ripng_peer));
a94434b6 46}
47
6ac29a51 48static void
a94434b6 49ripng_peer_free (struct ripng_peer *peer)
50{
51 XFREE (MTYPE_RIPNG_PEER, peer);
52}
53
54struct ripng_peer *
55ripng_peer_lookup (struct in6_addr *addr)
56{
57 struct ripng_peer *peer;
1eb8ef25 58 struct listnode *node, *nnode;
a94434b6 59
1eb8ef25 60 for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
a94434b6 61 {
62 if (IPV6_ADDR_SAME (&peer->addr, addr))
63 return peer;
64 }
65 return NULL;
66}
67
68struct ripng_peer *
69ripng_peer_lookup_next (struct in6_addr *addr)
70{
71 struct ripng_peer *peer;
1eb8ef25 72 struct listnode *node, *nnode;
a94434b6 73
1eb8ef25 74 for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
a94434b6 75 {
76 if (addr6_cmp(&peer->addr, addr) > 0)
77 return peer;
78 }
79 return NULL;
80}
81
82/* RIPng peer is timeout.
83 * Garbage collector.
84 **/
6ac29a51 85static int
a94434b6 86ripng_peer_timeout (struct thread *t)
87{
88 struct ripng_peer *peer;
89
90 peer = THREAD_ARG (t);
91 listnode_delete (peer_list, peer);
92 ripng_peer_free (peer);
93
94 return 0;
95}
96
97/* Get RIPng peer. At the same time update timeout thread. */
6ac29a51 98static struct ripng_peer *
a94434b6 99ripng_peer_get (struct in6_addr *addr)
100{
101 struct ripng_peer *peer;
102
103 peer = ripng_peer_lookup (addr);
104
105 if (peer)
106 {
107 if (peer->t_timeout)
108 thread_cancel (peer->t_timeout);
109 }
110 else
111 {
112 peer = ripng_peer_new ();
113 peer->addr = *addr; /* XXX */
114 listnode_add_sort (peer_list, peer);
115 }
116
117 /* Update timeout thread. */
66e78ae6
QY
118 peer->t_timeout = NULL;
119 thread_add_timer(master, ripng_peer_timeout, peer, RIPNG_PEER_TIMER_DEFAULT,
120 &peer->t_timeout);
a94434b6 121
122 /* Last update time set. */
123 time (&peer->uptime);
124
125 return peer;
126}
127
128void
129ripng_peer_update (struct sockaddr_in6 *from, u_char version)
130{
131 struct ripng_peer *peer;
132 peer = ripng_peer_get (&from->sin6_addr);
133 peer->version = version;
134}
135
136void
137ripng_peer_bad_route (struct sockaddr_in6 *from)
138{
139 struct ripng_peer *peer;
140 peer = ripng_peer_get (&from->sin6_addr);
141 peer->recv_badroutes++;
142}
143
144void
145ripng_peer_bad_packet (struct sockaddr_in6 *from)
146{
147 struct ripng_peer *peer;
148 peer = ripng_peer_get (&from->sin6_addr);
149 peer->recv_badpackets++;
150}
151
152/* Display peer uptime. */
6ac29a51 153static char *
a94434b6 154ripng_peer_uptime (struct ripng_peer *peer, char *buf, size_t len)
155{
156 time_t uptime;
157 struct tm *tm;
158
159 /* If there is no connection has been done before print `never'. */
160 if (peer->uptime == 0)
161 {
162 snprintf (buf, len, "never ");
163 return buf;
164 }
165
166 /* Get current time. */
167 uptime = time (NULL);
168 uptime -= peer->uptime;
169 tm = gmtime (&uptime);
170
171 /* Making formatted timer strings. */
172#define ONE_DAY_SECOND 60*60*24
173#define ONE_WEEK_SECOND 60*60*24*7
174
175 if (uptime < ONE_DAY_SECOND)
176 snprintf (buf, len, "%02d:%02d:%02d",
177 tm->tm_hour, tm->tm_min, tm->tm_sec);
178 else if (uptime < ONE_WEEK_SECOND)
179 snprintf (buf, len, "%dd%02dh%02dm",
180 tm->tm_yday, tm->tm_hour, tm->tm_min);
181 else
182 snprintf (buf, len, "%02dw%dd%02dh",
183 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
184 return buf;
185}
186
187void
188ripng_peer_display (struct vty *vty)
189{
190 struct ripng_peer *peer;
1eb8ef25 191 struct listnode *node, *nnode;
a94434b6 192#define RIPNG_UPTIME_LEN 25
193 char timebuf[RIPNG_UPTIME_LEN];
194
1eb8ef25 195 for (ALL_LIST_ELEMENTS (peer_list, node, nnode, peer))
a94434b6 196 {
3a2ce6a1 197 vty_out (vty, " %s %s%14s %10d %10d %10d %s%s", inet6_ntoa (peer->addr),
a94434b6 198 VTY_NEWLINE, " ",
199 peer->recv_badpackets, peer->recv_badroutes,
200 ZEBRA_RIPNG_DISTANCE_DEFAULT,
201 ripng_peer_uptime (peer, timebuf, RIPNG_UPTIME_LEN),
202 VTY_NEWLINE);
203 }
204}
205
6ac29a51 206static int
a94434b6 207ripng_peer_list_cmp (struct ripng_peer *p1, struct ripng_peer *p2)
208{
209 return addr6_cmp(&p1->addr, &p2->addr) > 0;
210}
211
212void
213ripng_peer_init ()
214{
215 peer_list = list_new ();
216 peer_list->cmp = (int (*)(void *, void *)) ripng_peer_list_cmp;
217}