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