Deleted Added
full compact
rip6query.c (95258) rip6query.c (146187)
1/* $KAME: rip6query.c,v 1.11 2001/05/08 04:36:37 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 *
1/* $KAME: rip6query.c,v 1.11 2001/05/08 04:36:37 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD: head/usr.sbin/rip6query/rip6query.c 95258 2002-04-22 13:44:47Z des $
31 * $FreeBSD: head/usr.sbin/rip6query/rip6query.c 146187 2005-05-13 16:31:11Z ume $
32 */
33
34#include <stdio.h>
35
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40#include <signal.h>
41#include <errno.h>
42#include <err.h>
43
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <sys/queue.h>
47
48#include <net/if.h>
49#if defined(__FreeBSD__) && __FreeBSD__ >= 3
50#include <net/if_var.h>
51#endif /* __FreeBSD__ >= 3 */
52#include <netinet/in.h>
53#include <netinet/in_var.h>
54#include <arpa/inet.h>
55#include <netdb.h>
56
57#include "route6d.h"
58
32 */
33
34#include <stdio.h>
35
36#include <unistd.h>
37#include <stdlib.h>
38#include <string.h>
39#include <ctype.h>
40#include <signal.h>
41#include <errno.h>
42#include <err.h>
43
44#include <sys/types.h>
45#include <sys/socket.h>
46#include <sys/queue.h>
47
48#include <net/if.h>
49#if defined(__FreeBSD__) && __FreeBSD__ >= 3
50#include <net/if_var.h>
51#endif /* __FreeBSD__ >= 3 */
52#include <netinet/in.h>
53#include <netinet/in_var.h>
54#include <arpa/inet.h>
55#include <netdb.h>
56
57#include "route6d.h"
58
59/* wrapper for KAME-special getnameinfo() */
60#ifndef NI_WITHSCOPEID
61#define NI_WITHSCOPEID 0
62#endif
63
64int s;
65struct sockaddr_in6 sin6;
66struct rip6 *ripbuf;
67
68#define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
69
70int main __P((int, char **));
71static void usage __P((void));
72static const char *sa_n2a __P((struct sockaddr *));
73static const char *inet6_n2a __P((struct in6_addr *));
74
75int
76main(argc, argv)
77 int argc;
78 char **argv;
79{
80 struct netinfo6 *np;
81 struct sockaddr_in6 fsock;
82 int i, n, len, flen;
83 int c;
84 int ifidx = -1;
85 int error;
86 char pbuf[10];
87 struct addrinfo hints, *res;
88
89 while ((c = getopt(argc, argv, "I:")) != -1) {
90 switch (c) {
91 case 'I':
92 ifidx = if_nametoindex(optarg);
93 if (ifidx == 0) {
94 errx(1, "invalid interface %s", optarg);
95 /*NOTREACHED*/
96 }
97 break;
98 default:
99 usage();
100 exit(1);
101 /*NOTREACHED*/
102 }
103 }
104 argv += optind;
105 argc -= optind;
106
107 if (argc != 1) {
108 usage();
109 exit(1);
110 }
111
112 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
113 err(1, "socket");
114 /*NOTREACHED*/
115 }
116
117 /* getaddrinfo is preferred for addr@ifname syntax */
118 snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
119 memset(&hints, 0, sizeof(hints));
120 hints.ai_family = AF_INET6;
121 hints.ai_socktype = SOCK_DGRAM;
122 error = getaddrinfo(argv[0], pbuf, &hints, &res);
123 if (error) {
124 errx(1, "%s: %s", argv[0], gai_strerror(error));
125 /*NOTREACHED*/
126 }
127 if (res->ai_next) {
128 errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
129 /*NOTREACHED*/
130 }
131 if (sizeof(sin6) != res->ai_addrlen) {
132 errx(1, "%s: %s", argv[0], "invalid addrlen");
133 /*NOTREACHED*/
134 }
135 memcpy(&sin6, res->ai_addr, res->ai_addrlen);
136 if (ifidx >= 0)
137 sin6.sin6_scope_id = ifidx;
138
139 if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
140 err(1, "malloc");
141 /*NOTREACHED*/
142 }
143 ripbuf->rip6_cmd = RIP6_REQUEST;
144 ripbuf->rip6_vers = RIP6_VERSION;
145 ripbuf->rip6_res1[0] = 0;
146 ripbuf->rip6_res1[1] = 0;
147 np = ripbuf->rip6_nets;
148 bzero(&np->rip6_dest, sizeof(struct in6_addr));
149 np->rip6_tag = 0;
150 np->rip6_plen = 0;
151 np->rip6_metric = HOPCNT_INFINITY6;
152 if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
153 sizeof(struct sockaddr_in6)) < 0) {
154 err(1, "send");
155 /*NOTREACHED*/
156 }
157 do {
158 flen = sizeof(fsock);
159 if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
160 (struct sockaddr *)&fsock, &flen)) < 0) {
161 err(1, "recvfrom");
162 /*NOTREACHED*/
163 }
164 printf("Response from %s len %d\n",
165 sa_n2a((struct sockaddr *)&fsock), len);
166 n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
167 sizeof(struct netinfo6);
168 np = ripbuf->rip6_nets;
169 for (i = 0; i < n; i++, np++) {
170 printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
171 np->rip6_plen, np->rip6_metric);
172 if (np->rip6_tag)
173 printf(" tag=0x%x", ntohs(np->rip6_tag));
174 printf("\n");
175 }
176 } while (len == RIPSIZE(24));
177
178 exit(0);
179}
180
181static void
182usage()
183{
184 fprintf(stderr, "usage: rip6query [-I iface] address\n");
185}
186
187/* getnameinfo() is preferred as we may be able to show ifindex as ifname */
188static const char *
189sa_n2a(sa)
190 struct sockaddr *sa;
191{
192 static char buf[NI_MAXHOST];
193
194 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
59int s;
60struct sockaddr_in6 sin6;
61struct rip6 *ripbuf;
62
63#define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
64
65int main __P((int, char **));
66static void usage __P((void));
67static const char *sa_n2a __P((struct sockaddr *));
68static const char *inet6_n2a __P((struct in6_addr *));
69
70int
71main(argc, argv)
72 int argc;
73 char **argv;
74{
75 struct netinfo6 *np;
76 struct sockaddr_in6 fsock;
77 int i, n, len, flen;
78 int c;
79 int ifidx = -1;
80 int error;
81 char pbuf[10];
82 struct addrinfo hints, *res;
83
84 while ((c = getopt(argc, argv, "I:")) != -1) {
85 switch (c) {
86 case 'I':
87 ifidx = if_nametoindex(optarg);
88 if (ifidx == 0) {
89 errx(1, "invalid interface %s", optarg);
90 /*NOTREACHED*/
91 }
92 break;
93 default:
94 usage();
95 exit(1);
96 /*NOTREACHED*/
97 }
98 }
99 argv += optind;
100 argc -= optind;
101
102 if (argc != 1) {
103 usage();
104 exit(1);
105 }
106
107 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
108 err(1, "socket");
109 /*NOTREACHED*/
110 }
111
112 /* getaddrinfo is preferred for addr@ifname syntax */
113 snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
114 memset(&hints, 0, sizeof(hints));
115 hints.ai_family = AF_INET6;
116 hints.ai_socktype = SOCK_DGRAM;
117 error = getaddrinfo(argv[0], pbuf, &hints, &res);
118 if (error) {
119 errx(1, "%s: %s", argv[0], gai_strerror(error));
120 /*NOTREACHED*/
121 }
122 if (res->ai_next) {
123 errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
124 /*NOTREACHED*/
125 }
126 if (sizeof(sin6) != res->ai_addrlen) {
127 errx(1, "%s: %s", argv[0], "invalid addrlen");
128 /*NOTREACHED*/
129 }
130 memcpy(&sin6, res->ai_addr, res->ai_addrlen);
131 if (ifidx >= 0)
132 sin6.sin6_scope_id = ifidx;
133
134 if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
135 err(1, "malloc");
136 /*NOTREACHED*/
137 }
138 ripbuf->rip6_cmd = RIP6_REQUEST;
139 ripbuf->rip6_vers = RIP6_VERSION;
140 ripbuf->rip6_res1[0] = 0;
141 ripbuf->rip6_res1[1] = 0;
142 np = ripbuf->rip6_nets;
143 bzero(&np->rip6_dest, sizeof(struct in6_addr));
144 np->rip6_tag = 0;
145 np->rip6_plen = 0;
146 np->rip6_metric = HOPCNT_INFINITY6;
147 if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
148 sizeof(struct sockaddr_in6)) < 0) {
149 err(1, "send");
150 /*NOTREACHED*/
151 }
152 do {
153 flen = sizeof(fsock);
154 if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
155 (struct sockaddr *)&fsock, &flen)) < 0) {
156 err(1, "recvfrom");
157 /*NOTREACHED*/
158 }
159 printf("Response from %s len %d\n",
160 sa_n2a((struct sockaddr *)&fsock), len);
161 n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
162 sizeof(struct netinfo6);
163 np = ripbuf->rip6_nets;
164 for (i = 0; i < n; i++, np++) {
165 printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
166 np->rip6_plen, np->rip6_metric);
167 if (np->rip6_tag)
168 printf(" tag=0x%x", ntohs(np->rip6_tag));
169 printf("\n");
170 }
171 } while (len == RIPSIZE(24));
172
173 exit(0);
174}
175
176static void
177usage()
178{
179 fprintf(stderr, "usage: rip6query [-I iface] address\n");
180}
181
182/* getnameinfo() is preferred as we may be able to show ifindex as ifname */
183static const char *
184sa_n2a(sa)
185 struct sockaddr *sa;
186{
187 static char buf[NI_MAXHOST];
188
189 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
195 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) {
190 NULL, 0, NI_NUMERICHOST) != 0) {
196 snprintf(buf, sizeof(buf), "%s", "(invalid)");
197 }
198 return buf;
199}
200
201static const char *
202inet6_n2a(addr)
203 struct in6_addr *addr;
204{
205 static char buf[NI_MAXHOST];
206
207 return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
208}
191 snprintf(buf, sizeof(buf), "%s", "(invalid)");
192 }
193 return buf;
194}
195
196static const char *
197inet6_n2a(addr)
198 struct in6_addr *addr;
199{
200 static char buf[NI_MAXHOST];
201
202 return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
203}