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