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