rip6query.c revision 1.1 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun
30 1.1 itojun #include <stdio.h>
31 1.1 itojun
32 1.1 itojun #include <unistd.h>
33 1.1 itojun #include <stdlib.h>
34 1.1 itojun #include <string.h>
35 1.1 itojun #include <ctype.h>
36 1.1 itojun #include <signal.h>
37 1.1 itojun
38 1.1 itojun #include <sys/types.h>
39 1.1 itojun #include <sys/socket.h>
40 1.1 itojun #include <net/if.h>
41 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
42 1.1 itojun #include <net/if_var.h>
43 1.1 itojun #endif /* __FreeBSD__ >= 3 */
44 1.1 itojun #include <netinet/in.h>
45 1.1 itojun #include <netinet/in_var.h>
46 1.1 itojun #include <arpa/inet.h>
47 1.1 itojun #include <netdb.h>
48 1.1 itojun
49 1.1 itojun #include "route6d.h"
50 1.1 itojun
51 1.1 itojun int s;
52 1.1 itojun extern int errno;
53 1.1 itojun struct sockaddr_in6 sin6;
54 1.1 itojun struct rip6 *ripbuf;
55 1.1 itojun
56 1.1 itojun #define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
57 1.1 itojun
58 1.1 itojun int main __P((int, char **));
59 1.1 itojun void fatal __P((char *));
60 1.1 itojun const char *inet6_n2a __P((struct in6_addr *));
61 1.1 itojun
62 1.1 itojun int main(argc, argv)
63 1.1 itojun int argc;
64 1.1 itojun char **argv;
65 1.1 itojun {
66 1.1 itojun struct netinfo6 *np;
67 1.1 itojun struct sockaddr_in6 fsock;
68 1.1 itojun struct hostent *hp;
69 1.1 itojun char *hostname;
70 1.1 itojun int i, n, len, flen;
71 1.1 itojun
72 1.1 itojun if (argc != 2) {
73 1.1 itojun fprintf(stderr, "Usage: %s address\n", *argv);
74 1.1 itojun exit(-1);
75 1.1 itojun }
76 1.1 itojun
77 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
78 1.1 itojun fatal("socket");
79 1.1 itojun
80 1.1 itojun hp = (struct hostent *)gethostbyname2(argv[1], AF_INET6);
81 1.1 itojun if (hp == NULL) {
82 1.1 itojun if (inet_pton(AF_INET6, argv[1], (u_int32_t *)&sin6.sin6_addr)
83 1.1 itojun != 1) {
84 1.1 itojun fprintf(stderr, "%s: unknown host %s\n",
85 1.1 itojun argv[0], argv[1]);
86 1.1 itojun exit(-1);
87 1.1 itojun }
88 1.1 itojun } else {
89 1.1 itojun bcopy(hp->h_addr, (caddr_t)&sin6.sin6_addr, hp->h_length);
90 1.1 itojun hostname = strdup(hp->h_name);
91 1.1 itojun }
92 1.1 itojun
93 1.1 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
94 1.1 itojun sin6.sin6_family = AF_INET6;
95 1.1 itojun sin6.sin6_port = htons(RIP6_PORT);
96 1.1 itojun
97 1.1 itojun if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL)
98 1.1 itojun fatal("malloc");
99 1.1 itojun ripbuf->rip6_cmd = RIP6_REQUEST;
100 1.1 itojun ripbuf->rip6_vers = RIP6_VERSION;
101 1.1 itojun ripbuf->rip6_res1[0] = 0;
102 1.1 itojun ripbuf->rip6_res1[1] = 0;
103 1.1 itojun np = ripbuf->rip6_nets;
104 1.1 itojun bzero(&np->rip6_dest, sizeof(struct in6_addr));
105 1.1 itojun np->rip6_tag = 0;
106 1.1 itojun np->rip6_plen = 0;
107 1.1 itojun np->rip6_metric = HOPCNT_INFINITY6;
108 1.1 itojun if (sendto(s, ripbuf, RIPSIZE(1), 0,
109 1.1 itojun (struct sockaddr *)&sin6, sizeof(struct sockaddr_in6)) < 0)
110 1.1 itojun fatal("send");
111 1.1 itojun do {
112 1.1 itojun flen = sizeof(struct sockaddr_in6);
113 1.1 itojun if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
114 1.1 itojun (struct sockaddr *)&fsock, &flen)) < 0)
115 1.1 itojun fatal("recvfrom");
116 1.1 itojun printf("Response from %s len %d\n",
117 1.1 itojun inet6_n2a(&fsock.sin6_addr), len);
118 1.1 itojun n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
119 1.1 itojun sizeof(struct netinfo6);
120 1.1 itojun np = ripbuf->rip6_nets;
121 1.1 itojun for (i = 0; i < n; i++, np++) {
122 1.1 itojun printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
123 1.1 itojun np->rip6_plen, np->rip6_metric);
124 1.1 itojun if (np->rip6_tag)
125 1.1 itojun printf(" tag=0x%x", ntohs(np->rip6_tag));
126 1.1 itojun printf("\n");
127 1.1 itojun }
128 1.1 itojun } while (len == RIPSIZE(24));
129 1.1 itojun
130 1.1 itojun exit(0);
131 1.1 itojun }
132 1.1 itojun
133 1.1 itojun void fatal(p)
134 1.1 itojun char *p;
135 1.1 itojun {
136 1.1 itojun fprintf(stderr, "%s: %s", p, strerror(errno));
137 1.1 itojun exit(-1);
138 1.1 itojun }
139 1.1 itojun
140 1.1 itojun const char *inet6_n2a(p)
141 1.1 itojun struct in6_addr *p;
142 1.1 itojun {
143 1.1 itojun static char buf[BUFSIZ];
144 1.1 itojun
145 1.1 itojun return inet_ntop(AF_INET6, (u_int32_t *)p, buf, sizeof(buf));
146 1.1 itojun }
147