1 /* $KAME: rip6query.c,v 1.11 2001/05/08 04:36:37 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/queue.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <arpa/inet.h>
57 static struct sockaddr_in6 sin6;
58 static struct rip6 *ripbuf;
60 #define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
62 int main(int, char **);
63 static void usage(void);
64 static const char *sa_n2a(struct sockaddr *);
65 static const char *inet6_n2a(struct in6_addr *);
68 main(int argc, char *argv[])
71 struct sockaddr_in6 fsock;
78 struct addrinfo hints, *res;
80 while ((c = getopt(argc, argv, "I:")) != -1) {
83 ifidx = if_nametoindex(optarg);
85 errx(1, "invalid interface %s", optarg);
103 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
108 /* getaddrinfo is preferred for addr@ifname syntax */
109 snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
110 memset(&hints, 0, sizeof(hints));
111 hints.ai_family = AF_INET6;
112 hints.ai_socktype = SOCK_DGRAM;
113 error = getaddrinfo(argv[0], pbuf, &hints, &res);
115 errx(1, "%s: %s", argv[0], gai_strerror(error));
119 errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
122 if (sizeof(sin6) != res->ai_addrlen) {
123 errx(1, "%s: %s", argv[0], "invalid addrlen");
126 memcpy(&sin6, res->ai_addr, res->ai_addrlen);
128 sin6.sin6_scope_id = ifidx;
130 if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
134 ripbuf->rip6_cmd = RIP6_REQUEST;
135 ripbuf->rip6_vers = RIP6_VERSION;
136 ripbuf->rip6_res1[0] = 0;
137 ripbuf->rip6_res1[1] = 0;
138 np = ripbuf->rip6_nets;
139 bzero(&np->rip6_dest, sizeof(struct in6_addr));
142 np->rip6_metric = HOPCNT_INFINITY6;
143 if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
144 sizeof(struct sockaddr_in6)) < 0) {
149 flen = sizeof(fsock);
150 if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
151 (struct sockaddr *)&fsock, &flen)) < 0) {
155 printf("Response from %s len %d\n",
156 sa_n2a((struct sockaddr *)&fsock), len);
157 n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
158 sizeof(struct netinfo6);
159 np = ripbuf->rip6_nets;
160 for (i = 0; i < n; i++, np++) {
161 printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
162 np->rip6_plen, np->rip6_metric);
164 printf(" tag=0x%x", ntohs(np->rip6_tag));
167 } while (len == RIPSIZE(24));
175 fprintf(stderr, "usage: rip6query [-I iface] address\n");
178 /* getnameinfo() is preferred as we may be able to show ifindex as ifname */
180 sa_n2a(struct sockaddr *sa)
182 static char buf[NI_MAXHOST];
184 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
185 NULL, 0, NI_NUMERICHOST) != 0) {
186 snprintf(buf, sizeof(buf), "%s", "(invalid)");
192 inet6_n2a(struct in6_addr *addr)
194 static char buf[NI_MAXHOST];
196 return inet_ntop(AF_INET6, addr, buf, sizeof(buf));