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