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