rip6query.c revision 55167
1/* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/usr.sbin/rip6query/rip6query.c 55167 1999-12-28 05:37:39Z shin $ 30 */ 31 32#include <stdio.h> 33 34#include <unistd.h> 35#include <stdlib.h> 36#include <string.h> 37#include <ctype.h> 38#include <signal.h> 39#include <err.h> 40 41#include <sys/types.h> 42#include <sys/socket.h> 43#include <net/if.h> 44#if defined(__FreeBSD__) && __FreeBSD__ >= 3 45#include <net/if_var.h> 46#endif /* __FreeBSD__ >= 3 */ 47#include <netinet/in.h> 48#include <netinet/in_var.h> 49#include <arpa/inet.h> 50#include <netdb.h> 51 52#include "route6d.h" 53 54/* wrapper for KAME-special getnameinfo() */ 55#ifndef NI_WITHSCOPEID 56#define NI_WITHSCOPEID 0 57#endif 58 59int s; 60extern int errno; 61struct sockaddr_in6 sin6; 62struct rip6 *ripbuf; 63 64#define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6)) 65 66int main __P((int, char **)); 67static void usage __P((void)); 68static const char *sa_n2a __P((struct sockaddr *)); 69static const char *inet6_n2a __P((struct in6_addr *)); 70 71int 72main(argc, argv) 73 int argc; 74 char **argv; 75{ 76 struct netinfo6 *np; 77 struct sockaddr_in6 fsock; 78 int i, n, len, flen; 79 int c; 80 extern char *optarg; 81 extern int optind; 82 int ifidx = -1; 83 int error; 84 char pbuf[10]; 85 struct addrinfo hints, *res; 86 87 while ((c = getopt(argc, argv, "I:")) != EOF) { 88 switch (c) { 89 case 'I': 90 ifidx = if_nametoindex(optarg); 91 if (ifidx == 0) { 92 errx(1, "invalid interface %s", optarg); 93 /*NOTREACHED*/ 94 } 95 break; 96 default: 97 usage(); 98 exit(1); 99 /*NOTREACHED*/ 100 } 101 } 102 argv += optind; 103 argc -= optind; 104 105 if (argc != 1) { 106 usage(); 107 exit(-1); 108 } 109 110 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 111 err(1, "socket"); 112 /*NOTREACHED*/ 113 } 114 115 /* getaddrinfo is preferred for addr@ifname syntax */ 116 snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT); 117 memset(&hints, 0, sizeof(hints)); 118 hints.ai_family = AF_INET6; 119 hints.ai_socktype = SOCK_STREAM; 120 error = getaddrinfo(argv[0], pbuf, &hints, &res); 121 if (error) { 122 fprintf(stderr, "rip6query: %s: %s\n", argv[0], 123 gai_strerror(error)); 124 if (error == EAI_SYSTEM) 125 errx(1, "%s", strerror(errno)); 126 exit(1); 127 /*NOTREACHED*/ 128 } 129 if (res->ai_next) { 130 errx(1, "%s: %s", argv[0], "resolved to multiple addrs"); 131 /*NOTREACHED*/ 132 } 133 if (sizeof(sin6) != res->ai_addrlen) { 134 errx(1, "%s: %s", argv[0], "invalid addrlen"); 135 /*NOTREACHED*/ 136 } 137 memcpy(&sin6, res->ai_addr, res->ai_addrlen); 138 if (ifidx >= 0) 139 sin6.sin6_scope_id = ifidx; 140 141 if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) { 142 err(1, "malloc"); 143 /*NOTREACHED*/ 144 } 145 ripbuf->rip6_cmd = RIP6_REQUEST; 146 ripbuf->rip6_vers = RIP6_VERSION; 147 ripbuf->rip6_res1[0] = 0; 148 ripbuf->rip6_res1[1] = 0; 149 np = ripbuf->rip6_nets; 150 bzero(&np->rip6_dest, sizeof(struct in6_addr)); 151 np->rip6_tag = 0; 152 np->rip6_plen = 0; 153 np->rip6_metric = HOPCNT_INFINITY6; 154 if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6, 155 sizeof(struct sockaddr_in6)) < 0) { 156 err(1, "send"); 157 /*NOTREACHED*/ 158 } 159 do { 160 flen = sizeof(fsock); 161 if ((len = recvfrom(s, ripbuf, BUFSIZ, 0, 162 (struct sockaddr *)&fsock, &flen)) < 0) { 163 err(1, "recvfrom"); 164 /*NOTREACHED*/ 165 } 166 printf("Response from %s len %d\n", 167 sa_n2a((struct sockaddr *)&fsock), len); 168 n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) / 169 sizeof(struct netinfo6); 170 np = ripbuf->rip6_nets; 171 for (i = 0; i < n; i++, np++) { 172 printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest), 173 np->rip6_plen, np->rip6_metric); 174 if (np->rip6_tag) 175 printf(" tag=0x%x", ntohs(np->rip6_tag)); 176 printf("\n"); 177 } 178 } while (len == RIPSIZE(24)); 179 180 exit(0); 181} 182 183static void 184usage() 185{ 186 fprintf(stderr, "Usage: rip6query [-I iface] address\n"); 187} 188 189/* getnameinfo() is preferred as we may be able to show ifindex as ifname */ 190static const char * 191sa_n2a(sa) 192 struct sockaddr *sa; 193{ 194 static char buf[BUFSIZ]; 195 196 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), 197 NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) { 198 snprintf(buf, sizeof(buf), "%s", "(invalid)"); 199 } 200 return buf; 201} 202 203static const char * 204inet6_n2a(addr) 205 struct in6_addr *addr; 206{ 207 static char buf[BUFSIZ]; 208 209 return inet_ntop(AF_INET6, addr, buf, sizeof(buf)); 210} 211