178064Sume/*	$KAME: rip6query.c,v 1.11 2001/05/08 04:36:37 itojun Exp $	*/
278064Sume
355163Sshin/*
455163Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
555163Sshin * All rights reserved.
662752Skris *
755163Sshin * Redistribution and use in source and binary forms, with or without
855163Sshin * modification, are permitted provided that the following conditions
955163Sshin * are met:
1055163Sshin * 1. Redistributions of source code must retain the above copyright
1155163Sshin *    notice, this list of conditions and the following disclaimer.
1255163Sshin * 2. Redistributions in binary form must reproduce the above copyright
1355163Sshin *    notice, this list of conditions and the following disclaimer in the
1455163Sshin *    documentation and/or other materials provided with the distribution.
1555163Sshin * 3. Neither the name of the project nor the names of its contributors
1655163Sshin *    may be used to endorse or promote products derived from this software
1755163Sshin *    without specific prior written permission.
1862752Skris *
1955163Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2055163Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2155163Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2255163Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2355163Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2455163Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2555163Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2655163Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2755163Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2855163Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2955163Sshin * SUCH DAMAGE.
3055163Sshin *
3155163Sshin * $FreeBSD$
3255163Sshin */
3355163Sshin
3455163Sshin#include <stdio.h>
3555163Sshin
3655163Sshin#include <unistd.h>
3755163Sshin#include <stdlib.h>
3855163Sshin#include <string.h>
3955163Sshin#include <ctype.h>
4055163Sshin#include <signal.h>
4162752Skris#include <errno.h>
4255163Sshin#include <err.h>
4355163Sshin
4455163Sshin#include <sys/types.h>
4555163Sshin#include <sys/socket.h>
4678064Sume#include <sys/queue.h>
4778064Sume
4855163Sshin#include <net/if.h>
4962752Skris#if defined(__FreeBSD__) && __FreeBSD__ >= 3
5055163Sshin#include <net/if_var.h>
5162752Skris#endif /* __FreeBSD__ >= 3 */
5255163Sshin#include <netinet/in.h>
5355163Sshin#include <netinet/in_var.h>
5455163Sshin#include <arpa/inet.h>
5555163Sshin#include <netdb.h>
5655163Sshin
5755163Sshin#include "route6d.h"
5855163Sshin
59227258Sedstatic int	s;
60227258Sedstatic struct sockaddr_in6 sin6;
61227258Sedstatic struct rip6	*ripbuf;
6255163Sshin
6355163Sshin#define	RIPSIZE(n)	(sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
6455163Sshin
65173412Skevloint main(int, char **);
66173412Skevlostatic void usage(void);
67173412Skevlostatic const char *sa_n2a(struct sockaddr *);
68173412Skevlostatic const char *inet6_n2a(struct in6_addr *);
6955163Sshin
7055163Sshinint
71201387Sedmain(int argc, char *argv[])
7255163Sshin{
7355163Sshin	struct netinfo6 *np;
7455163Sshin	struct sockaddr_in6 fsock;
75162489Skan	int i, n, len;
7655163Sshin	int c;
7755163Sshin	int ifidx = -1;
7855163Sshin	int error;
79162489Skan	socklen_t flen;
8055163Sshin	char pbuf[10];
8155163Sshin	struct addrinfo hints, *res;
8255163Sshin
8378064Sume	while ((c = getopt(argc, argv, "I:")) != -1) {
8455163Sshin		switch (c) {
8555163Sshin		case 'I':
8655163Sshin			ifidx = if_nametoindex(optarg);
8755163Sshin			if (ifidx == 0) {
8855163Sshin				errx(1, "invalid interface %s", optarg);
8955163Sshin				/*NOTREACHED*/
9055163Sshin			}
9155163Sshin			break;
9255163Sshin		default:
9355163Sshin			usage();
9455163Sshin			exit(1);
9555163Sshin			/*NOTREACHED*/
9655163Sshin		}
9755163Sshin	}
9855163Sshin	argv += optind;
9955163Sshin	argc -= optind;
10055163Sshin
10155163Sshin	if (argc != 1) {
10255163Sshin		usage();
10378064Sume		exit(1);
10455163Sshin	}
10555163Sshin
10655163Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
10755163Sshin		err(1, "socket");
10855163Sshin		/*NOTREACHED*/
10955163Sshin	}
11055163Sshin
11155163Sshin	/* getaddrinfo is preferred for addr@ifname syntax */
11255163Sshin	snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
11355163Sshin	memset(&hints, 0, sizeof(hints));
11455163Sshin	hints.ai_family = AF_INET6;
11562752Skris	hints.ai_socktype = SOCK_DGRAM;
11655163Sshin	error = getaddrinfo(argv[0], pbuf, &hints, &res);
11755163Sshin	if (error) {
11862752Skris		errx(1, "%s: %s", argv[0], gai_strerror(error));
11955163Sshin		/*NOTREACHED*/
12055163Sshin	}
12155163Sshin	if (res->ai_next) {
12255163Sshin		errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
12355163Sshin		/*NOTREACHED*/
12455163Sshin	}
12555163Sshin	if (sizeof(sin6) != res->ai_addrlen) {
12655163Sshin		errx(1, "%s: %s", argv[0], "invalid addrlen");
12755163Sshin		/*NOTREACHED*/
12855163Sshin	}
12955163Sshin	memcpy(&sin6, res->ai_addr, res->ai_addrlen);
13055163Sshin	if (ifidx >= 0)
13155163Sshin		sin6.sin6_scope_id = ifidx;
13255163Sshin
13355163Sshin	if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
13455163Sshin		err(1, "malloc");
13555163Sshin		/*NOTREACHED*/
13655163Sshin	}
13755163Sshin	ripbuf->rip6_cmd = RIP6_REQUEST;
13855163Sshin	ripbuf->rip6_vers = RIP6_VERSION;
13955163Sshin	ripbuf->rip6_res1[0] = 0;
14055163Sshin	ripbuf->rip6_res1[1] = 0;
14155163Sshin	np = ripbuf->rip6_nets;
14255163Sshin	bzero(&np->rip6_dest, sizeof(struct in6_addr));
14355163Sshin	np->rip6_tag = 0;
14455163Sshin	np->rip6_plen = 0;
14555163Sshin	np->rip6_metric = HOPCNT_INFINITY6;
14655163Sshin	if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
14755163Sshin			sizeof(struct sockaddr_in6)) < 0) {
14855163Sshin		err(1, "send");
14955163Sshin		/*NOTREACHED*/
15055163Sshin	}
15155163Sshin	do {
15255163Sshin		flen = sizeof(fsock);
15355163Sshin		if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
15455163Sshin				(struct sockaddr *)&fsock, &flen)) < 0) {
15555163Sshin			err(1, "recvfrom");
15655163Sshin			/*NOTREACHED*/
15755163Sshin		}
15855163Sshin		printf("Response from %s len %d\n",
15955163Sshin			sa_n2a((struct sockaddr *)&fsock), len);
16055163Sshin		n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
16155163Sshin			sizeof(struct netinfo6);
16255163Sshin		np = ripbuf->rip6_nets;
16355163Sshin		for (i = 0; i < n; i++, np++) {
16455163Sshin			printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
16555163Sshin				np->rip6_plen, np->rip6_metric);
16655163Sshin			if (np->rip6_tag)
16755163Sshin				printf(" tag=0x%x", ntohs(np->rip6_tag));
16855163Sshin			printf("\n");
16955163Sshin		}
17055163Sshin	} while (len == RIPSIZE(24));
17155163Sshin
17255163Sshin	exit(0);
17355163Sshin}
17455163Sshin
17555163Sshinstatic void
176201387Sedusage(void)
17755163Sshin{
17895258Sdes	fprintf(stderr, "usage: rip6query [-I iface] address\n");
17955163Sshin}
18055163Sshin
18155163Sshin/* getnameinfo() is preferred as we may be able to show ifindex as ifname */
18255163Sshinstatic const char *
183201387Sedsa_n2a(struct sockaddr *sa)
18455163Sshin{
18562752Skris	static char buf[NI_MAXHOST];
18655163Sshin
18755163Sshin	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
188146187Sume			NULL, 0, NI_NUMERICHOST) != 0) {
18955163Sshin		snprintf(buf, sizeof(buf), "%s", "(invalid)");
19055163Sshin	}
19155163Sshin	return buf;
19255163Sshin}
19355163Sshin
19455163Sshinstatic const char *
195201387Sedinet6_n2a(struct in6_addr *addr)
19655163Sshin{
19762752Skris	static char buf[NI_MAXHOST];
19855163Sshin
19955163Sshin	return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
20055163Sshin}
201