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 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 #include <netinet/in.h> 50 #include <netinet/in_var.h> 51 #include <arpa/inet.h> 52 #include <netdb.h> 53 54 #include "route6d.h" 55 56 static int s; 57 static struct sockaddr_in6 sin6; 58 static struct rip6 *ripbuf; 59 60 #define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6)) 61 62 int main(int, char **); 63 static void usage(void) __dead2; 64 static const char *sa_n2a(struct sockaddr *); 65 static const char *inet6_n2a(struct in6_addr *); 66 67 int 68 main(int argc, char *argv[]) 69 { 70 struct netinfo6 *np; 71 struct sockaddr_in6 fsock; 72 int i, n, len; 73 int c; 74 int ifidx = -1; 75 int error; 76 socklen_t flen; 77 char pbuf[10]; 78 struct addrinfo hints, *res; 79 80 while ((c = getopt(argc, argv, "I:")) != -1) { 81 switch (c) { 82 case 'I': 83 ifidx = if_nametoindex(optarg); 84 if (ifidx == 0) { 85 errx(1, "invalid interface %s", optarg); 86 /*NOTREACHED*/ 87 } 88 break; 89 default: 90 usage(); 91 /*NOTREACHED*/ 92 } 93 } 94 argv += optind; 95 argc -= optind; 96 97 if (argc != 1) { 98 usage(); 99 } 100 101 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 102 err(1, "socket"); 103 /*NOTREACHED*/ 104 } 105 106 /* getaddrinfo is preferred for addr@ifname syntax */ 107 snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT); 108 memset(&hints, 0, sizeof(hints)); 109 hints.ai_family = AF_INET6; 110 hints.ai_socktype = SOCK_DGRAM; 111 error = getaddrinfo(argv[0], pbuf, &hints, &res); 112 if (error) { 113 errx(1, "%s: %s", argv[0], gai_strerror(error)); 114 /*NOTREACHED*/ 115 } 116 if (res->ai_next) { 117 errx(1, "%s: %s", argv[0], "resolved to multiple addrs"); 118 /*NOTREACHED*/ 119 } 120 if (sizeof(sin6) != res->ai_addrlen) { 121 errx(1, "%s: %s", argv[0], "invalid addrlen"); 122 /*NOTREACHED*/ 123 } 124 memcpy(&sin6, res->ai_addr, res->ai_addrlen); 125 if (ifidx >= 0) 126 sin6.sin6_scope_id = ifidx; 127 128 if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) { 129 err(1, "malloc"); 130 /*NOTREACHED*/ 131 } 132 ripbuf->rip6_cmd = RIP6_REQUEST; 133 ripbuf->rip6_vers = RIP6_VERSION; 134 ripbuf->rip6_res1[0] = 0; 135 ripbuf->rip6_res1[1] = 0; 136 np = ripbuf->rip6_nets; 137 bzero(&np->rip6_dest, sizeof(struct in6_addr)); 138 np->rip6_tag = 0; 139 np->rip6_plen = 0; 140 np->rip6_metric = HOPCNT_INFINITY6; 141 if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6, 142 sizeof(struct sockaddr_in6)) < 0) { 143 err(1, "send"); 144 /*NOTREACHED*/ 145 } 146 do { 147 flen = sizeof(fsock); 148 if ((len = recvfrom(s, ripbuf, BUFSIZ, 0, 149 (struct sockaddr *)&fsock, &flen)) < 0) { 150 err(1, "recvfrom"); 151 /*NOTREACHED*/ 152 } 153 printf("Response from %s len %d\n", 154 sa_n2a((struct sockaddr *)&fsock), len); 155 n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) / 156 sizeof(struct netinfo6); 157 np = ripbuf->rip6_nets; 158 for (i = 0; i < n; i++, np++) { 159 printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest), 160 np->rip6_plen, np->rip6_metric); 161 if (np->rip6_tag) 162 printf(" tag=0x%x", ntohs(np->rip6_tag)); 163 printf("\n"); 164 } 165 } while (len == RIPSIZE(24)); 166 167 return 0; 168 } 169 170 static void 171 usage(void) 172 { 173 fprintf(stderr, "usage: rip6query [-I iface] address\n"); 174 exit(1); 175 } 176 177 /* getnameinfo() is preferred as we may be able to show ifindex as ifname */ 178 static const char * 179 sa_n2a(struct sockaddr *sa) 180 { 181 static char buf[NI_MAXHOST]; 182 183 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), 184 NULL, 0, NI_NUMERICHOST) != 0) { 185 snprintf(buf, sizeof(buf), "%s", "(invalid)"); 186 } 187 return buf; 188 } 189 190 static const char * 191 inet6_n2a(struct in6_addr *addr) 192 { 193 static char buf[NI_MAXHOST]; 194 195 return inet_ntop(AF_INET6, addr, buf, sizeof(buf)); 196 } 197