rip6query.c revision 55163
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 55163 1999-12-28 02:37:14Z 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 errx(1, "%s: %s", argv[0], gai_strerror(error)); 123 /*NOTREACHED*/ 124 } 125 if (res->ai_next) { 126 errx(1, "%s: %s", argv[0], "resolved to multiple addrs"); 127 /*NOTREACHED*/ 128 } 129 if (sizeof(sin6) != res->ai_addrlen) { 130 errx(1, "%s: %s", argv[0], "invalid addrlen"); 131 /*NOTREACHED*/ 132 } 133 memcpy(&sin6, res->ai_addr, res->ai_addrlen); 134 if (ifidx >= 0) 135 sin6.sin6_scope_id = ifidx; 136 137 if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) { 138 err(1, "malloc"); 139 /*NOTREACHED*/ 140 } 141 ripbuf->rip6_cmd = RIP6_REQUEST; 142 ripbuf->rip6_vers = RIP6_VERSION; 143 ripbuf->rip6_res1[0] = 0; 144 ripbuf->rip6_res1[1] = 0; 145 np = ripbuf->rip6_nets; 146 bzero(&np->rip6_dest, sizeof(struct in6_addr)); 147 np->rip6_tag = 0; 148 np->rip6_plen = 0; 149 np->rip6_metric = HOPCNT_INFINITY6; 150 if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6, 151 sizeof(struct sockaddr_in6)) < 0) { 152 err(1, "send"); 153 /*NOTREACHED*/ 154 } 155 do { 156 flen = sizeof(fsock); 157 if ((len = recvfrom(s, ripbuf, BUFSIZ, 0, 158 (struct sockaddr *)&fsock, &flen)) < 0) { 159 err(1, "recvfrom"); 160 /*NOTREACHED*/ 161 } 162 printf("Response from %s len %d\n", 163 sa_n2a((struct sockaddr *)&fsock), len); 164 n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) / 165 sizeof(struct netinfo6); 166 np = ripbuf->rip6_nets; 167 for (i = 0; i < n; i++, np++) { 168 printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest), 169 np->rip6_plen, np->rip6_metric); 170 if (np->rip6_tag) 171 printf(" tag=0x%x", ntohs(np->rip6_tag)); 172 printf("\n"); 173 } 174 } while (len == RIPSIZE(24)); 175 176 exit(0); 177} 178 179static void 180usage() 181{ 182 fprintf(stderr, "Usage: rip6query [-I iface] address\n"); 183} 184 185/* getnameinfo() is preferred as we may be able to show ifindex as ifname */ 186static const char * 187sa_n2a(sa) 188 struct sockaddr *sa; 189{ 190 static char buf[BUFSIZ]; 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 199static const char * 200inet6_n2a(addr) 201 struct in6_addr *addr; 202{ 203 static char buf[BUFSIZ]; 204 205 return inet_ntop(AF_INET6, addr, buf, sizeof(buf)); 206} 207