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