rip6query.c revision 62752
155163Sshin/*
255163Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
355163Sshin * All rights reserved.
462752Skris *
555163Sshin * Redistribution and use in source and binary forms, with or without
655163Sshin * modification, are permitted provided that the following conditions
755163Sshin * are met:
855163Sshin * 1. Redistributions of source code must retain the above copyright
955163Sshin *    notice, this list of conditions and the following disclaimer.
1055163Sshin * 2. Redistributions in binary form must reproduce the above copyright
1155163Sshin *    notice, this list of conditions and the following disclaimer in the
1255163Sshin *    documentation and/or other materials provided with the distribution.
1355163Sshin * 3. Neither the name of the project nor the names of its contributors
1455163Sshin *    may be used to endorse or promote products derived from this software
1555163Sshin *    without specific prior written permission.
1662752Skris *
1755163Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
1855163Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1955163Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2055163Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2155163Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2255163Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2355163Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2455163Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2555163Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2655163Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2755163Sshin * SUCH DAMAGE.
2855163Sshin *
2955163Sshin * $FreeBSD: head/usr.sbin/rip6query/rip6query.c 62752 2000-07-07 07:35:51Z kris $
3055163Sshin */
3155163Sshin
3255163Sshin#include <stdio.h>
3355163Sshin
3455163Sshin#include <unistd.h>
3555163Sshin#include <stdlib.h>
3655163Sshin#include <string.h>
3755163Sshin#include <ctype.h>
3855163Sshin#include <signal.h>
3962752Skris#include <errno.h>
4055163Sshin#include <err.h>
4155163Sshin
4255163Sshin#include <sys/types.h>
4355163Sshin#include <sys/socket.h>
4455163Sshin#include <net/if.h>
4562752Skris#if defined(__FreeBSD__) && __FreeBSD__ >= 3
4655163Sshin#include <net/if_var.h>
4762752Skris#endif /* __FreeBSD__ >= 3 */
4855163Sshin#include <netinet/in.h>
4955163Sshin#include <netinet/in_var.h>
5055163Sshin#include <arpa/inet.h>
5155163Sshin#include <netdb.h>
5255163Sshin
5355163Sshin#include "route6d.h"
5455163Sshin
5555163Sshin/* wrapper for KAME-special getnameinfo() */
5655163Sshin#ifndef NI_WITHSCOPEID
5762752Skris#define NI_WITHSCOPEID	0
5855163Sshin#endif
5955163Sshin
6055163Sshinint	s;
6155163Sshinstruct sockaddr_in6 sin6;
6255163Sshinstruct rip6	*ripbuf;
6355163Sshin
6455163Sshin#define	RIPSIZE(n)	(sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
6555163Sshin
6655163Sshinint main __P((int, char **));
6755163Sshinstatic void usage __P((void));
6855163Sshinstatic const char *sa_n2a __P((struct sockaddr *));
6955163Sshinstatic const char *inet6_n2a __P((struct in6_addr *));
7055163Sshin
7155163Sshinint
7255163Sshinmain(argc, argv)
7355163Sshin	int argc;
7455163Sshin	char **argv;
7555163Sshin{
7655163Sshin	struct netinfo6 *np;
7755163Sshin	struct sockaddr_in6 fsock;
7855163Sshin	int i, n, len, flen;
7955163Sshin	int c;
8055163Sshin	int ifidx = -1;
8155163Sshin	int error;
8255163Sshin	char pbuf[10];
8355163Sshin	struct addrinfo hints, *res;
8455163Sshin
8555163Sshin	while ((c = getopt(argc, argv, "I:")) != EOF) {
8655163Sshin		switch (c) {
8755163Sshin		case 'I':
8855163Sshin			ifidx = if_nametoindex(optarg);
8955163Sshin			if (ifidx == 0) {
9055163Sshin				errx(1, "invalid interface %s", optarg);
9155163Sshin				/*NOTREACHED*/
9255163Sshin			}
9355163Sshin			break;
9455163Sshin		default:
9555163Sshin			usage();
9655163Sshin			exit(1);
9755163Sshin			/*NOTREACHED*/
9855163Sshin		}
9955163Sshin	}
10055163Sshin	argv += optind;
10155163Sshin	argc -= optind;
10255163Sshin
10355163Sshin	if (argc != 1) {
10455163Sshin		usage();
10555163Sshin		exit(-1);
10655163Sshin	}
10755163Sshin
10855163Sshin	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
10955163Sshin		err(1, "socket");
11055163Sshin		/*NOTREACHED*/
11155163Sshin	}
11255163Sshin
11355163Sshin	/* getaddrinfo is preferred for addr@ifname syntax */
11455163Sshin	snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
11555163Sshin	memset(&hints, 0, sizeof(hints));
11655163Sshin	hints.ai_family = AF_INET6;
11762752Skris	hints.ai_socktype = SOCK_DGRAM;
11855163Sshin	error = getaddrinfo(argv[0], pbuf, &hints, &res);
11955163Sshin	if (error) {
12062752Skris		errx(1, "%s: %s", argv[0], gai_strerror(error));
12155163Sshin		/*NOTREACHED*/
12255163Sshin	}
12355163Sshin	if (res->ai_next) {
12455163Sshin		errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
12555163Sshin		/*NOTREACHED*/
12655163Sshin	}
12755163Sshin	if (sizeof(sin6) != res->ai_addrlen) {
12855163Sshin		errx(1, "%s: %s", argv[0], "invalid addrlen");
12955163Sshin		/*NOTREACHED*/
13055163Sshin	}
13155163Sshin	memcpy(&sin6, res->ai_addr, res->ai_addrlen);
13255163Sshin	if (ifidx >= 0)
13355163Sshin		sin6.sin6_scope_id = ifidx;
13455163Sshin
13555163Sshin	if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
13655163Sshin		err(1, "malloc");
13755163Sshin		/*NOTREACHED*/
13855163Sshin	}
13955163Sshin	ripbuf->rip6_cmd = RIP6_REQUEST;
14055163Sshin	ripbuf->rip6_vers = RIP6_VERSION;
14155163Sshin	ripbuf->rip6_res1[0] = 0;
14255163Sshin	ripbuf->rip6_res1[1] = 0;
14355163Sshin	np = ripbuf->rip6_nets;
14455163Sshin	bzero(&np->rip6_dest, sizeof(struct in6_addr));
14555163Sshin	np->rip6_tag = 0;
14655163Sshin	np->rip6_plen = 0;
14755163Sshin	np->rip6_metric = HOPCNT_INFINITY6;
14855163Sshin	if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
14955163Sshin			sizeof(struct sockaddr_in6)) < 0) {
15055163Sshin		err(1, "send");
15155163Sshin		/*NOTREACHED*/
15255163Sshin	}
15355163Sshin	do {
15455163Sshin		flen = sizeof(fsock);
15555163Sshin		if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
15655163Sshin				(struct sockaddr *)&fsock, &flen)) < 0) {
15755163Sshin			err(1, "recvfrom");
15855163Sshin			/*NOTREACHED*/
15955163Sshin		}
16055163Sshin		printf("Response from %s len %d\n",
16155163Sshin			sa_n2a((struct sockaddr *)&fsock), len);
16255163Sshin		n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
16355163Sshin			sizeof(struct netinfo6);
16455163Sshin		np = ripbuf->rip6_nets;
16555163Sshin		for (i = 0; i < n; i++, np++) {
16655163Sshin			printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
16755163Sshin				np->rip6_plen, np->rip6_metric);
16855163Sshin			if (np->rip6_tag)
16955163Sshin				printf(" tag=0x%x", ntohs(np->rip6_tag));
17055163Sshin			printf("\n");
17155163Sshin		}
17255163Sshin	} while (len == RIPSIZE(24));
17355163Sshin
17455163Sshin	exit(0);
17555163Sshin}
17655163Sshin
17755163Sshinstatic void
17855163Sshinusage()
17955163Sshin{
18055163Sshin	fprintf(stderr, "Usage: rip6query [-I iface] address\n");
18155163Sshin}
18255163Sshin
18355163Sshin/* getnameinfo() is preferred as we may be able to show ifindex as ifname */
18455163Sshinstatic const char *
18555163Sshinsa_n2a(sa)
18655163Sshin	struct sockaddr *sa;
18755163Sshin{
18862752Skris	static char buf[NI_MAXHOST];
18955163Sshin
19055163Sshin	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
19155163Sshin			NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) {
19255163Sshin		snprintf(buf, sizeof(buf), "%s", "(invalid)");
19355163Sshin	}
19455163Sshin	return buf;
19555163Sshin}
19655163Sshin
19755163Sshinstatic const char *
19855163Sshininet6_n2a(addr)
19955163Sshin	struct in6_addr *addr;
20055163Sshin{
20162752Skris	static char buf[NI_MAXHOST];
20255163Sshin
20355163Sshin	return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
20455163Sshin}
205