]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* RIP 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 | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "if.h" | |
24 | #include "prefix.h" | |
25 | #include "command.h" | |
26 | #include "linklist.h" | |
27 | #include "thread.h" | |
28 | #include "memory.h" | |
29 | ||
30 | #include "ripd/ripd.h" | |
31 | ||
32 | /* Linked list of RIP peer. */ | |
33 | struct list *peer_list; | |
6b0655a2 | 34 | |
d62a17ae | 35 | static struct rip_peer *rip_peer_new(void) |
718e3744 | 36 | { |
d62a17ae | 37 | return XCALLOC(MTYPE_RIP_PEER, sizeof(struct rip_peer)); |
718e3744 | 38 | } |
39 | ||
d62a17ae | 40 | static void rip_peer_free(struct rip_peer *peer) |
718e3744 | 41 | { |
d62a17ae | 42 | XFREE(MTYPE_RIP_PEER, peer); |
718e3744 | 43 | } |
44 | ||
d62a17ae | 45 | struct rip_peer *rip_peer_lookup(struct in_addr *addr) |
718e3744 | 46 | { |
d62a17ae | 47 | struct rip_peer *peer; |
48 | struct listnode *node, *nnode; | |
49 | ||
50 | for (ALL_LIST_ELEMENTS(peer_list, node, nnode, peer)) { | |
51 | if (IPV4_ADDR_SAME(&peer->addr, addr)) | |
52 | return peer; | |
53 | } | |
54 | return NULL; | |
718e3744 | 55 | } |
56 | ||
d62a17ae | 57 | struct rip_peer *rip_peer_lookup_next(struct in_addr *addr) |
718e3744 | 58 | { |
d62a17ae | 59 | struct rip_peer *peer; |
60 | struct listnode *node, *nnode; | |
61 | ||
62 | for (ALL_LIST_ELEMENTS(peer_list, node, nnode, peer)) { | |
63 | if (htonl(peer->addr.s_addr) > htonl(addr->s_addr)) | |
64 | return peer; | |
65 | } | |
66 | return NULL; | |
718e3744 | 67 | } |
68 | ||
69 | /* RIP peer is timeout. */ | |
d62a17ae | 70 | static int rip_peer_timeout(struct thread *t) |
718e3744 | 71 | { |
d62a17ae | 72 | struct rip_peer *peer; |
718e3744 | 73 | |
d62a17ae | 74 | peer = THREAD_ARG(t); |
75 | listnode_delete(peer_list, peer); | |
76 | rip_peer_free(peer); | |
718e3744 | 77 | |
d62a17ae | 78 | return 0; |
718e3744 | 79 | } |
80 | ||
81 | /* Get RIP peer. At the same time update timeout thread. */ | |
d62a17ae | 82 | static struct rip_peer *rip_peer_get(struct in_addr *addr) |
718e3744 | 83 | { |
d62a17ae | 84 | struct rip_peer *peer; |
85 | ||
86 | peer = rip_peer_lookup(addr); | |
87 | ||
88 | if (peer) { | |
89 | if (peer->t_timeout) | |
90 | thread_cancel(peer->t_timeout); | |
91 | } else { | |
92 | peer = rip_peer_new(); | |
93 | peer->addr = *addr; | |
94 | listnode_add_sort(peer_list, peer); | |
95 | } | |
96 | ||
97 | /* Update timeout thread. */ | |
98 | peer->t_timeout = NULL; | |
99 | thread_add_timer(master, rip_peer_timeout, peer, RIP_PEER_TIMER_DEFAULT, | |
100 | &peer->t_timeout); | |
101 | ||
102 | /* Last update time set. */ | |
103 | time(&peer->uptime); | |
104 | ||
105 | return peer; | |
718e3744 | 106 | } |
107 | ||
d7c0a89a | 108 | void rip_peer_update(struct sockaddr_in *from, uint8_t version) |
718e3744 | 109 | { |
d62a17ae | 110 | struct rip_peer *peer; |
111 | peer = rip_peer_get(&from->sin_addr); | |
112 | peer->version = version; | |
718e3744 | 113 | } |
114 | ||
d62a17ae | 115 | void rip_peer_bad_route(struct sockaddr_in *from) |
718e3744 | 116 | { |
d62a17ae | 117 | struct rip_peer *peer; |
118 | peer = rip_peer_get(&from->sin_addr); | |
119 | peer->recv_badroutes++; | |
718e3744 | 120 | } |
121 | ||
d62a17ae | 122 | void rip_peer_bad_packet(struct sockaddr_in *from) |
718e3744 | 123 | { |
d62a17ae | 124 | struct rip_peer *peer; |
125 | peer = rip_peer_get(&from->sin_addr); | |
126 | peer->recv_badpackets++; | |
718e3744 | 127 | } |
128 | ||
129 | /* Display peer uptime. */ | |
d62a17ae | 130 | static char *rip_peer_uptime(struct rip_peer *peer, char *buf, size_t len) |
718e3744 | 131 | { |
d62a17ae | 132 | time_t uptime; |
133 | struct tm *tm; | |
134 | ||
135 | /* If there is no connection has been done before print `never'. */ | |
136 | if (peer->uptime == 0) { | |
137 | snprintf(buf, len, "never "); | |
138 | return buf; | |
139 | } | |
140 | ||
141 | /* Get current time. */ | |
142 | uptime = time(NULL); | |
143 | uptime -= peer->uptime; | |
144 | tm = gmtime(&uptime); | |
145 | ||
d62a17ae | 146 | if (uptime < ONE_DAY_SECOND) |
147 | snprintf(buf, len, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, | |
148 | tm->tm_sec); | |
149 | else if (uptime < ONE_WEEK_SECOND) | |
150 | snprintf(buf, len, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour, | |
151 | tm->tm_min); | |
152 | else | |
153 | snprintf(buf, len, "%02dw%dd%02dh", tm->tm_yday / 7, | |
154 | tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour); | |
155 | return buf; | |
718e3744 | 156 | } |
157 | ||
d62a17ae | 158 | void rip_peer_display(struct vty *vty) |
718e3744 | 159 | { |
d62a17ae | 160 | struct rip_peer *peer; |
161 | struct listnode *node, *nnode; | |
718e3744 | 162 | #define RIP_UPTIME_LEN 25 |
d62a17ae | 163 | char timebuf[RIP_UPTIME_LEN]; |
164 | ||
165 | for (ALL_LIST_ELEMENTS(peer_list, node, nnode, peer)) { | |
166 | vty_out(vty, " %-16s %9d %9d %9d %s\n", | |
167 | inet_ntoa(peer->addr), peer->recv_badpackets, | |
168 | peer->recv_badroutes, ZEBRA_RIP_DISTANCE_DEFAULT, | |
169 | rip_peer_uptime(peer, timebuf, RIP_UPTIME_LEN)); | |
170 | } | |
718e3744 | 171 | } |
172 | ||
d62a17ae | 173 | static int rip_peer_list_cmp(struct rip_peer *p1, struct rip_peer *p2) |
718e3744 | 174 | { |
d62a17ae | 175 | return htonl(p1->addr.s_addr) > htonl(p2->addr.s_addr); |
718e3744 | 176 | } |
177 | ||
d62a17ae | 178 | void rip_peer_init(void) |
718e3744 | 179 | { |
d62a17ae | 180 | peer_list = list_new(); |
181 | peer_list->cmp = (int (*)(void *, void *))rip_peer_list_cmp; | |
718e3744 | 182 | } |