xref: /netbsd/usr.sbin/rip6query/rip6query.c (revision bf9ec67e)
1 /*	$NetBSD: rip6query.c,v 1.7 2001/11/16 07:09:37 itojun Exp $	*/
2 /*	$KAME: rip6query.c,v 1.15 2001/11/16 07:01:21 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 int	s;
61 int	query_wait = DEFAULT_WAIT;
62 struct sockaddr_in6 sin6;
63 struct rip6	*ripbuf;
64 
65 #define	RIPSIZE(n)	(sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
66 
67 int main __P((int, char **));
68 static void usage __P((void));
69 static void sigalrm_handler __P((int));
70 static const char *sa_n2a __P((struct sockaddr *));
71 static const char *inet6_n2a __P((struct in6_addr *));
72 
73 int
74 main(argc, argv)
75 	int argc;
76 	char **argv;
77 {
78 	struct netinfo6 *np;
79 	struct sockaddr_in6 fsock;
80 	int i, n, len, flen;
81 	int c;
82 	int ifidx = -1;
83 	int error;
84 	char pbuf[10];
85 	struct addrinfo hints, *res;
86 
87 	while ((c = getopt(argc, argv, "I:w:")) != -1) {
88 		switch (c) {
89 		case 'I':
90 			ifidx = if_nametoindex(optarg);
91 			if (ifidx == 0) {
92 				errx(1, "invalid interface %s", optarg);
93 				/*NOTREACHED*/
94 			}
95 			break;
96 		case 'w':
97 			query_wait = atoi(optarg);
98 			break;
99 		default:
100 			usage();
101 			exit(1);
102 			/*NOTREACHED*/
103 		}
104 	}
105 	argv += optind;
106 	argc -= optind;
107 
108 	if (argc != 1) {
109 		usage();
110 		exit(1);
111 	}
112 
113 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
114 		err(1, "socket");
115 		/*NOTREACHED*/
116 	}
117 
118 	/* getaddrinfo is preferred for addr%scope syntax */
119 	snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
120 	memset(&hints, 0, sizeof(hints));
121 	hints.ai_family = AF_INET6;
122 	hints.ai_socktype = SOCK_DGRAM;
123 	error = getaddrinfo(argv[0], pbuf, &hints, &res);
124 	if (error) {
125 		errx(1, "%s: %s", argv[0], gai_strerror(error));
126 		/*NOTREACHED*/
127 	}
128 	if (res->ai_next) {
129 		errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
130 		/*NOTREACHED*/
131 	}
132 	if (sizeof(sin6) != res->ai_addrlen) {
133 		errx(1, "%s: %s", argv[0], "invalid addrlen");
134 		/*NOTREACHED*/
135 	}
136 	memcpy(&sin6, res->ai_addr, res->ai_addrlen);
137 	if (ifidx >= 0)
138 		sin6.sin6_scope_id = ifidx;
139 
140 	if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
141 		err(1, "malloc");
142 		/*NOTREACHED*/
143 	}
144 	ripbuf->rip6_cmd = RIP6_REQUEST;
145 	ripbuf->rip6_vers = RIP6_VERSION;
146 	ripbuf->rip6_res1[0] = 0;
147 	ripbuf->rip6_res1[1] = 0;
148 	np = ripbuf->rip6_nets;
149 	bzero(&np->rip6_dest, sizeof(struct in6_addr));
150 	np->rip6_tag = 0;
151 	np->rip6_plen = 0;
152 	np->rip6_metric = HOPCNT_INFINITY6;
153 	if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
154 			sizeof(struct sockaddr_in6)) < 0) {
155 		err(1, "send");
156 		/*NOTREACHED*/
157 	}
158 	signal(SIGALRM, sigalrm_handler);
159 	for (;;) {
160 		flen = sizeof(fsock);
161 		alarm(query_wait);
162 		if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
163 				(struct sockaddr *)&fsock, &flen)) < 0) {
164 			err(1, "recvfrom");
165 			/*NOTREACHED*/
166 		}
167 		alarm(0);
168 		printf("Response from %s len %d\n",
169 			sa_n2a((struct sockaddr *)&fsock), len);
170 		n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
171 			sizeof(struct netinfo6);
172 		np = ripbuf->rip6_nets;
173 		for (i = 0; i < n; i++, np++) {
174 			printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
175 				np->rip6_plen, np->rip6_metric);
176 			if (np->rip6_tag)
177 				printf(" tag=0x%x", ntohs(np->rip6_tag));
178 			printf("\n");
179 		}
180 	}
181 
182 	exit(0);
183 }
184 
185 static void
186 usage()
187 {
188 	fprintf(stderr, "Usage: rip6query [-I iface] [-w wait] address\n");
189 }
190 
191 /* getnameinfo() is preferred as we may be able to show ifindex as ifname */
192 static const char *
193 sa_n2a(sa)
194 	struct sockaddr *sa;
195 {
196 	static char buf[NI_MAXHOST];
197 
198 	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
199 			NULL, 0, NI_NUMERICHOST) != 0) {
200 		snprintf(buf, sizeof(buf), "%s", "(invalid)");
201 	}
202 	return buf;
203 }
204 
205 static const char *
206 inet6_n2a(addr)
207 	struct in6_addr *addr;
208 {
209 	static char buf[NI_MAXHOST];
210 
211 	return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
212 }
213 
214 static void
215 sigalrm_handler(sig)
216 	int sig;
217 {
218 
219 	exit(0);
220 }
221