1 /* $NetBSD: rip6query.c,v 1.9 2003/04/15 07:23:19 itojun Exp $ */
2 /* $KAME: rip6query.c,v 1.17 2002/09/08 01:35:17 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/queue.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>
58 #define DEFAULT_WAIT 5
61 int query_wait
= DEFAULT_WAIT
;
62 struct sockaddr_in6 sin6
;
65 #define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
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
*));
79 struct sockaddr_in6 fsock
;
85 char pbuf
[NI_MAXSERV
];
86 struct addrinfo hints
, *res
;
88 while ((c
= getopt(argc
, argv
, "I:w:")) != -1) {
91 ifidx
= if_nametoindex(optarg
);
93 errx(1, "invalid interface %s", optarg
);
98 query_wait
= atoi(optarg
);
114 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
119 /* getaddrinfo is preferred for addr%scope syntax */
120 snprintf(pbuf
, sizeof(pbuf
), "%d", RIP6_PORT
);
121 memset(&hints
, 0, sizeof(hints
));
122 hints
.ai_family
= AF_INET6
;
123 hints
.ai_socktype
= SOCK_DGRAM
;
124 error
= getaddrinfo(argv
[0], pbuf
, &hints
, &res
);
126 errx(1, "%s: %s", argv
[0], gai_strerror(error
));
130 errx(1, "%s: %s", argv
[0], "resolved to multiple addrs");
133 if (sizeof(sin6
) != res
->ai_addrlen
) {
134 errx(1, "%s: %s", argv
[0], "invalid addrlen");
137 memcpy(&sin6
, res
->ai_addr
, res
->ai_addrlen
);
139 sin6
.sin6_scope_id
= ifidx
;
141 if ((ripbuf
= (struct rip6
*)malloc(BUFSIZ
)) == NULL
) {
145 ripbuf
->rip6_cmd
= RIP6_REQUEST
;
146 ripbuf
->rip6_vers
= RIP6_VERSION
;
147 ripbuf
->rip6_res1
[0] = 0;
148 ripbuf
->rip6_res1
[1] = 0;
149 np
= ripbuf
->rip6_nets
;
150 bzero(&np
->rip6_dest
, sizeof(struct in6_addr
));
153 np
->rip6_metric
= HOPCNT_INFINITY6
;
154 if (sendto(s
, ripbuf
, RIPSIZE(1), 0, (struct sockaddr
*)&sin6
,
155 sizeof(struct sockaddr_in6
)) < 0) {
159 signal(SIGALRM
, sigalrm_handler
);
161 flen
= sizeof(fsock
);
163 if ((len
= recvfrom(s
, ripbuf
, BUFSIZ
, 0,
164 (struct sockaddr
*)&fsock
, &flen
)) < 0) {
169 printf("Response from %s len %d\n",
170 sa_n2a((struct sockaddr
*)&fsock
), len
);
171 n
= (len
- sizeof(struct rip6
) + sizeof(struct netinfo6
)) /
172 sizeof(struct netinfo6
);
173 np
= ripbuf
->rip6_nets
;
174 for (i
= 0; i
< n
; i
++, np
++) {
175 printf("\t%s/%d [%d]", inet6_n2a(&np
->rip6_dest
),
176 np
->rip6_plen
, np
->rip6_metric
);
178 printf(" tag=0x%x", ntohs(np
->rip6_tag
));
189 fprintf(stderr
, "usage: rip6query [-I iface] [-w wait] address\n");
192 /* getnameinfo() is preferred as we may be able to show ifindex as ifname */
197 static char buf
[NI_MAXHOST
];
199 if (getnameinfo(sa
, sa
->sa_len
, buf
, sizeof(buf
),
200 NULL
, 0, NI_NUMERICHOST
) != 0) {
201 snprintf(buf
, sizeof(buf
), "%s", "(invalid)");
208 struct in6_addr
*addr
;
210 static char buf
[NI_MAXHOST
];
212 return inet_ntop(AF_INET6
, addr
, buf
, sizeof(buf
));