xref: /freebsd/usr.sbin/rip6query/rip6query.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
133841545SHajimu UMEMOTO /*	$KAME: rip6query.c,v 1.11 2001/05/08 04:36:37 itojun Exp $	*/
233841545SHajimu UMEMOTO 
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
67d56d374SYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
77d56d374SYoshinobu Inoue  * All rights reserved.
87d56d374SYoshinobu Inoue  *
97d56d374SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
107d56d374SYoshinobu Inoue  * modification, are permitted provided that the following conditions
117d56d374SYoshinobu Inoue  * are met:
127d56d374SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
137d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
147d56d374SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
157d56d374SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
167d56d374SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
177d56d374SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
187d56d374SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
197d56d374SYoshinobu Inoue  *    without specific prior written permission.
207d56d374SYoshinobu Inoue  *
217d56d374SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
227d56d374SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
237d56d374SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
247d56d374SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
257d56d374SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
267d56d374SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
277d56d374SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
287d56d374SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297d56d374SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
307d56d374SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
317d56d374SYoshinobu Inoue  * SUCH DAMAGE.
327d56d374SYoshinobu Inoue  */
337d56d374SYoshinobu Inoue 
347d56d374SYoshinobu Inoue #include <stdio.h>
357d56d374SYoshinobu Inoue 
367d56d374SYoshinobu Inoue #include <unistd.h>
377d56d374SYoshinobu Inoue #include <stdlib.h>
387d56d374SYoshinobu Inoue #include <string.h>
397d56d374SYoshinobu Inoue #include <ctype.h>
407d56d374SYoshinobu Inoue #include <signal.h>
412b39a7c8SKris Kennaway #include <errno.h>
427d56d374SYoshinobu Inoue #include <err.h>
437d56d374SYoshinobu Inoue 
447d56d374SYoshinobu Inoue #include <sys/types.h>
457d56d374SYoshinobu Inoue #include <sys/socket.h>
4633841545SHajimu UMEMOTO #include <sys/queue.h>
4733841545SHajimu UMEMOTO 
487d56d374SYoshinobu Inoue #include <net/if.h>
497d56d374SYoshinobu Inoue #include <netinet/in.h>
507d56d374SYoshinobu Inoue #include <netinet/in_var.h>
517d56d374SYoshinobu Inoue #include <arpa/inet.h>
527d56d374SYoshinobu Inoue #include <netdb.h>
537d56d374SYoshinobu Inoue 
547d56d374SYoshinobu Inoue #include "route6d.h"
557d56d374SYoshinobu Inoue 
56d1931b80SEd Schouten static int	s;
57d1931b80SEd Schouten static struct sockaddr_in6 sin6;
58d1931b80SEd Schouten static struct rip6	*ripbuf;
597d56d374SYoshinobu Inoue 
607d56d374SYoshinobu Inoue #define	RIPSIZE(n)	(sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
617d56d374SYoshinobu Inoue 
62784bddbcSKevin Lo int main(int, char **);
63*a2cc93ecSAlfonso Gregory static void usage(void) __dead2;
64784bddbcSKevin Lo static const char *sa_n2a(struct sockaddr *);
65784bddbcSKevin Lo static const char *inet6_n2a(struct in6_addr *);
667d56d374SYoshinobu Inoue 
677d56d374SYoshinobu Inoue int
main(int argc,char * argv[])689a958de5SEd Schouten main(int argc, char *argv[])
697d56d374SYoshinobu Inoue {
707d56d374SYoshinobu Inoue 	struct netinfo6 *np;
717d56d374SYoshinobu Inoue 	struct sockaddr_in6 fsock;
7253d588beSAlexander Kabaev 	int i, n, len;
737d56d374SYoshinobu Inoue 	int c;
747d56d374SYoshinobu Inoue 	int ifidx = -1;
757d56d374SYoshinobu Inoue 	int error;
7653d588beSAlexander Kabaev 	socklen_t flen;
777d56d374SYoshinobu Inoue 	char pbuf[10];
787d56d374SYoshinobu Inoue 	struct addrinfo hints, *res;
797d56d374SYoshinobu Inoue 
8033841545SHajimu UMEMOTO 	while ((c = getopt(argc, argv, "I:")) != -1) {
817d56d374SYoshinobu Inoue 		switch (c) {
827d56d374SYoshinobu Inoue 		case 'I':
837d56d374SYoshinobu Inoue 			ifidx = if_nametoindex(optarg);
847d56d374SYoshinobu Inoue 			if (ifidx == 0) {
857d56d374SYoshinobu Inoue 				errx(1, "invalid interface %s", optarg);
867d56d374SYoshinobu Inoue 				/*NOTREACHED*/
877d56d374SYoshinobu Inoue 			}
887d56d374SYoshinobu Inoue 			break;
897d56d374SYoshinobu Inoue 		default:
907d56d374SYoshinobu Inoue 			usage();
917d56d374SYoshinobu Inoue 			/*NOTREACHED*/
927d56d374SYoshinobu Inoue 		}
937d56d374SYoshinobu Inoue 	}
947d56d374SYoshinobu Inoue 	argv += optind;
957d56d374SYoshinobu Inoue 	argc -= optind;
967d56d374SYoshinobu Inoue 
977d56d374SYoshinobu Inoue 	if (argc != 1) {
987d56d374SYoshinobu Inoue 		usage();
997d56d374SYoshinobu Inoue 	}
1007d56d374SYoshinobu Inoue 
1017d56d374SYoshinobu Inoue 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1027d56d374SYoshinobu Inoue 		err(1, "socket");
1037d56d374SYoshinobu Inoue 		/*NOTREACHED*/
1047d56d374SYoshinobu Inoue 	}
1057d56d374SYoshinobu Inoue 
1067d56d374SYoshinobu Inoue 	/* getaddrinfo is preferred for addr@ifname syntax */
1077d56d374SYoshinobu Inoue 	snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
1087d56d374SYoshinobu Inoue 	memset(&hints, 0, sizeof(hints));
1097d56d374SYoshinobu Inoue 	hints.ai_family = AF_INET6;
1102b39a7c8SKris Kennaway 	hints.ai_socktype = SOCK_DGRAM;
1117d56d374SYoshinobu Inoue 	error = getaddrinfo(argv[0], pbuf, &hints, &res);
1127d56d374SYoshinobu Inoue 	if (error) {
1132b39a7c8SKris Kennaway 		errx(1, "%s: %s", argv[0], gai_strerror(error));
1147d56d374SYoshinobu Inoue 		/*NOTREACHED*/
1157d56d374SYoshinobu Inoue 	}
1167d56d374SYoshinobu Inoue 	if (res->ai_next) {
1177d56d374SYoshinobu Inoue 		errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
1187d56d374SYoshinobu Inoue 		/*NOTREACHED*/
1197d56d374SYoshinobu Inoue 	}
1207d56d374SYoshinobu Inoue 	if (sizeof(sin6) != res->ai_addrlen) {
1217d56d374SYoshinobu Inoue 		errx(1, "%s: %s", argv[0], "invalid addrlen");
1227d56d374SYoshinobu Inoue 		/*NOTREACHED*/
1237d56d374SYoshinobu Inoue 	}
1247d56d374SYoshinobu Inoue 	memcpy(&sin6, res->ai_addr, res->ai_addrlen);
1257d56d374SYoshinobu Inoue 	if (ifidx >= 0)
1267d56d374SYoshinobu Inoue 		sin6.sin6_scope_id = ifidx;
1277d56d374SYoshinobu Inoue 
1287d56d374SYoshinobu Inoue 	if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
1297d56d374SYoshinobu Inoue 		err(1, "malloc");
1307d56d374SYoshinobu Inoue 		/*NOTREACHED*/
1317d56d374SYoshinobu Inoue 	}
1327d56d374SYoshinobu Inoue 	ripbuf->rip6_cmd = RIP6_REQUEST;
1337d56d374SYoshinobu Inoue 	ripbuf->rip6_vers = RIP6_VERSION;
1347d56d374SYoshinobu Inoue 	ripbuf->rip6_res1[0] = 0;
1357d56d374SYoshinobu Inoue 	ripbuf->rip6_res1[1] = 0;
1367d56d374SYoshinobu Inoue 	np = ripbuf->rip6_nets;
1377d56d374SYoshinobu Inoue 	bzero(&np->rip6_dest, sizeof(struct in6_addr));
1387d56d374SYoshinobu Inoue 	np->rip6_tag = 0;
1397d56d374SYoshinobu Inoue 	np->rip6_plen = 0;
1407d56d374SYoshinobu Inoue 	np->rip6_metric = HOPCNT_INFINITY6;
1417d56d374SYoshinobu Inoue 	if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
1427d56d374SYoshinobu Inoue 			sizeof(struct sockaddr_in6)) < 0) {
1437d56d374SYoshinobu Inoue 		err(1, "send");
1447d56d374SYoshinobu Inoue 		/*NOTREACHED*/
1457d56d374SYoshinobu Inoue 	}
1467d56d374SYoshinobu Inoue 	do {
1477d56d374SYoshinobu Inoue 		flen = sizeof(fsock);
1487d56d374SYoshinobu Inoue 		if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
1497d56d374SYoshinobu Inoue 				(struct sockaddr *)&fsock, &flen)) < 0) {
1507d56d374SYoshinobu Inoue 			err(1, "recvfrom");
1517d56d374SYoshinobu Inoue 			/*NOTREACHED*/
1527d56d374SYoshinobu Inoue 		}
1537d56d374SYoshinobu Inoue 		printf("Response from %s len %d\n",
1547d56d374SYoshinobu Inoue 			sa_n2a((struct sockaddr *)&fsock), len);
1557d56d374SYoshinobu Inoue 		n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
1567d56d374SYoshinobu Inoue 			sizeof(struct netinfo6);
1577d56d374SYoshinobu Inoue 		np = ripbuf->rip6_nets;
1587d56d374SYoshinobu Inoue 		for (i = 0; i < n; i++, np++) {
1597d56d374SYoshinobu Inoue 			printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
1607d56d374SYoshinobu Inoue 				np->rip6_plen, np->rip6_metric);
1617d56d374SYoshinobu Inoue 			if (np->rip6_tag)
1627d56d374SYoshinobu Inoue 				printf(" tag=0x%x", ntohs(np->rip6_tag));
1637d56d374SYoshinobu Inoue 			printf("\n");
1647d56d374SYoshinobu Inoue 		}
1657d56d374SYoshinobu Inoue 	} while (len == RIPSIZE(24));
1667d56d374SYoshinobu Inoue 
167*a2cc93ecSAlfonso Gregory 	return 0;
1687d56d374SYoshinobu Inoue }
1697d56d374SYoshinobu Inoue 
1707d56d374SYoshinobu Inoue static void
usage(void)1719a958de5SEd Schouten usage(void)
1727d56d374SYoshinobu Inoue {
173d3974088SDag-Erling Smørgrav 	fprintf(stderr, "usage: rip6query [-I iface] address\n");
174*a2cc93ecSAlfonso Gregory 	exit(1);
1757d56d374SYoshinobu Inoue }
1767d56d374SYoshinobu Inoue 
1777d56d374SYoshinobu Inoue /* getnameinfo() is preferred as we may be able to show ifindex as ifname */
1787d56d374SYoshinobu Inoue static const char *
sa_n2a(struct sockaddr * sa)1799a958de5SEd Schouten sa_n2a(struct sockaddr *sa)
1807d56d374SYoshinobu Inoue {
1812b39a7c8SKris Kennaway 	static char buf[NI_MAXHOST];
1827d56d374SYoshinobu Inoue 
1837d56d374SYoshinobu Inoue 	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
1844f101318SHajimu UMEMOTO 			NULL, 0, NI_NUMERICHOST) != 0) {
1857d56d374SYoshinobu Inoue 		snprintf(buf, sizeof(buf), "%s", "(invalid)");
1867d56d374SYoshinobu Inoue 	}
1877d56d374SYoshinobu Inoue 	return buf;
1887d56d374SYoshinobu Inoue }
1897d56d374SYoshinobu Inoue 
1907d56d374SYoshinobu Inoue static const char *
inet6_n2a(struct in6_addr * addr)1919a958de5SEd Schouten inet6_n2a(struct in6_addr *addr)
1927d56d374SYoshinobu Inoue {
1932b39a7c8SKris Kennaway 	static char buf[NI_MAXHOST];
1947d56d374SYoshinobu Inoue 
1957d56d374SYoshinobu Inoue 	return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
1967d56d374SYoshinobu Inoue }
197