rip6query.c revision 1.9 1 1.9 itojun /* $NetBSD: rip6query.c,v 1.9 2003/04/15 07:23:19 itojun Exp $ */
2 1.8 itojun /* $KAME: rip6query.c,v 1.17 2002/09/08 01:35:17 itojun Exp $ */
3 1.2 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.1 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun #include <stdio.h>
34 1.1 itojun
35 1.1 itojun #include <unistd.h>
36 1.1 itojun #include <stdlib.h>
37 1.1 itojun #include <string.h>
38 1.1 itojun #include <ctype.h>
39 1.1 itojun #include <signal.h>
40 1.4 itojun #include <errno.h>
41 1.3 itojun #include <err.h>
42 1.1 itojun
43 1.1 itojun #include <sys/types.h>
44 1.1 itojun #include <sys/socket.h>
45 1.4 itojun #include <sys/queue.h>
46 1.4 itojun
47 1.1 itojun #include <net/if.h>
48 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
49 1.1 itojun #include <net/if_var.h>
50 1.1 itojun #endif /* __FreeBSD__ >= 3 */
51 1.1 itojun #include <netinet/in.h>
52 1.1 itojun #include <netinet/in_var.h>
53 1.1 itojun #include <arpa/inet.h>
54 1.1 itojun #include <netdb.h>
55 1.1 itojun
56 1.1 itojun #include "route6d.h"
57 1.1 itojun
58 1.7 itojun #define DEFAULT_WAIT 5
59 1.3 itojun
60 1.1 itojun int s;
61 1.7 itojun int query_wait = DEFAULT_WAIT;
62 1.1 itojun struct sockaddr_in6 sin6;
63 1.1 itojun struct rip6 *ripbuf;
64 1.1 itojun
65 1.1 itojun #define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
66 1.1 itojun
67 1.1 itojun int main __P((int, char **));
68 1.3 itojun static void usage __P((void));
69 1.7 itojun static void sigalrm_handler __P((int));
70 1.3 itojun static const char *sa_n2a __P((struct sockaddr *));
71 1.3 itojun static const char *inet6_n2a __P((struct in6_addr *));
72 1.1 itojun
73 1.3 itojun int
74 1.3 itojun main(argc, argv)
75 1.1 itojun int argc;
76 1.1 itojun char **argv;
77 1.1 itojun {
78 1.1 itojun struct netinfo6 *np;
79 1.1 itojun struct sockaddr_in6 fsock;
80 1.8 itojun int i, n, len;
81 1.8 itojun socklen_t flen;
82 1.3 itojun int c;
83 1.3 itojun int ifidx = -1;
84 1.3 itojun int error;
85 1.9 itojun char pbuf[NI_MAXSERV];
86 1.3 itojun struct addrinfo hints, *res;
87 1.3 itojun
88 1.7 itojun while ((c = getopt(argc, argv, "I:w:")) != -1) {
89 1.3 itojun switch (c) {
90 1.3 itojun case 'I':
91 1.3 itojun ifidx = if_nametoindex(optarg);
92 1.3 itojun if (ifidx == 0) {
93 1.3 itojun errx(1, "invalid interface %s", optarg);
94 1.3 itojun /*NOTREACHED*/
95 1.3 itojun }
96 1.3 itojun break;
97 1.7 itojun case 'w':
98 1.7 itojun query_wait = atoi(optarg);
99 1.7 itojun break;
100 1.3 itojun default:
101 1.3 itojun usage();
102 1.3 itojun exit(1);
103 1.3 itojun /*NOTREACHED*/
104 1.3 itojun }
105 1.3 itojun }
106 1.3 itojun argv += optind;
107 1.3 itojun argc -= optind;
108 1.1 itojun
109 1.3 itojun if (argc != 1) {
110 1.3 itojun usage();
111 1.5 wiz exit(1);
112 1.1 itojun }
113 1.1 itojun
114 1.3 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
115 1.3 itojun err(1, "socket");
116 1.3 itojun /*NOTREACHED*/
117 1.3 itojun }
118 1.1 itojun
119 1.7 itojun /* getaddrinfo is preferred for addr%scope syntax */
120 1.3 itojun snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
121 1.3 itojun memset(&hints, 0, sizeof(hints));
122 1.3 itojun hints.ai_family = AF_INET6;
123 1.3 itojun hints.ai_socktype = SOCK_DGRAM;
124 1.3 itojun error = getaddrinfo(argv[0], pbuf, &hints, &res);
125 1.3 itojun if (error) {
126 1.3 itojun errx(1, "%s: %s", argv[0], gai_strerror(error));
127 1.3 itojun /*NOTREACHED*/
128 1.3 itojun }
129 1.3 itojun if (res->ai_next) {
130 1.3 itojun errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
131 1.3 itojun /*NOTREACHED*/
132 1.3 itojun }
133 1.3 itojun if (sizeof(sin6) != res->ai_addrlen) {
134 1.3 itojun errx(1, "%s: %s", argv[0], "invalid addrlen");
135 1.3 itojun /*NOTREACHED*/
136 1.3 itojun }
137 1.3 itojun memcpy(&sin6, res->ai_addr, res->ai_addrlen);
138 1.3 itojun if (ifidx >= 0)
139 1.3 itojun sin6.sin6_scope_id = ifidx;
140 1.3 itojun
141 1.3 itojun if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
142 1.3 itojun err(1, "malloc");
143 1.3 itojun /*NOTREACHED*/
144 1.1 itojun }
145 1.1 itojun ripbuf->rip6_cmd = RIP6_REQUEST;
146 1.1 itojun ripbuf->rip6_vers = RIP6_VERSION;
147 1.1 itojun ripbuf->rip6_res1[0] = 0;
148 1.1 itojun ripbuf->rip6_res1[1] = 0;
149 1.1 itojun np = ripbuf->rip6_nets;
150 1.1 itojun bzero(&np->rip6_dest, sizeof(struct in6_addr));
151 1.1 itojun np->rip6_tag = 0;
152 1.1 itojun np->rip6_plen = 0;
153 1.1 itojun np->rip6_metric = HOPCNT_INFINITY6;
154 1.3 itojun if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
155 1.8 itojun sizeof(struct sockaddr_in6)) < 0) {
156 1.3 itojun err(1, "send");
157 1.3 itojun /*NOTREACHED*/
158 1.3 itojun }
159 1.7 itojun signal(SIGALRM, sigalrm_handler);
160 1.7 itojun for (;;) {
161 1.3 itojun flen = sizeof(fsock);
162 1.7 itojun alarm(query_wait);
163 1.1 itojun if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
164 1.8 itojun (struct sockaddr *)&fsock, &flen)) < 0) {
165 1.3 itojun err(1, "recvfrom");
166 1.3 itojun /*NOTREACHED*/
167 1.3 itojun }
168 1.7 itojun alarm(0);
169 1.1 itojun printf("Response from %s len %d\n",
170 1.8 itojun sa_n2a((struct sockaddr *)&fsock), len);
171 1.1 itojun n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
172 1.8 itojun sizeof(struct netinfo6);
173 1.1 itojun np = ripbuf->rip6_nets;
174 1.1 itojun for (i = 0; i < n; i++, np++) {
175 1.1 itojun printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
176 1.8 itojun np->rip6_plen, np->rip6_metric);
177 1.1 itojun if (np->rip6_tag)
178 1.1 itojun printf(" tag=0x%x", ntohs(np->rip6_tag));
179 1.1 itojun printf("\n");
180 1.1 itojun }
181 1.7 itojun }
182 1.1 itojun
183 1.1 itojun exit(0);
184 1.1 itojun }
185 1.1 itojun
186 1.3 itojun static void
187 1.3 itojun usage()
188 1.3 itojun {
189 1.7 itojun fprintf(stderr, "Usage: rip6query [-I iface] [-w wait] address\n");
190 1.3 itojun }
191 1.3 itojun
192 1.3 itojun /* getnameinfo() is preferred as we may be able to show ifindex as ifname */
193 1.3 itojun static const char *
194 1.3 itojun sa_n2a(sa)
195 1.3 itojun struct sockaddr *sa;
196 1.1 itojun {
197 1.3 itojun static char buf[NI_MAXHOST];
198 1.3 itojun
199 1.3 itojun if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
200 1.8 itojun NULL, 0, NI_NUMERICHOST) != 0) {
201 1.3 itojun snprintf(buf, sizeof(buf), "%s", "(invalid)");
202 1.3 itojun }
203 1.3 itojun return buf;
204 1.1 itojun }
205 1.1 itojun
206 1.3 itojun static const char *
207 1.3 itojun inet6_n2a(addr)
208 1.3 itojun struct in6_addr *addr;
209 1.1 itojun {
210 1.3 itojun static char buf[NI_MAXHOST];
211 1.1 itojun
212 1.3 itojun return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
213 1.7 itojun }
214 1.7 itojun
215 1.7 itojun static void
216 1.7 itojun sigalrm_handler(sig)
217 1.7 itojun int sig;
218 1.7 itojun {
219 1.7 itojun
220 1.8 itojun _exit(0);
221 1.1 itojun }
222