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