rip6query.c revision 95258
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: head/usr.sbin/rip6query/rip6query.c 95258 2002-04-22 13:44:47Z des $ 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 5955163Sshin/* wrapper for KAME-special getnameinfo() */ 6055163Sshin#ifndef NI_WITHSCOPEID 6162752Skris#define NI_WITHSCOPEID 0 6255163Sshin#endif 6355163Sshin 6455163Sshinint s; 6555163Sshinstruct sockaddr_in6 sin6; 6655163Sshinstruct rip6 *ripbuf; 6755163Sshin 6855163Sshin#define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6)) 6955163Sshin 7055163Sshinint main __P((int, char **)); 7155163Sshinstatic void usage __P((void)); 7255163Sshinstatic const char *sa_n2a __P((struct sockaddr *)); 7355163Sshinstatic const char *inet6_n2a __P((struct in6_addr *)); 7455163Sshin 7555163Sshinint 7655163Sshinmain(argc, argv) 7755163Sshin int argc; 7855163Sshin char **argv; 7955163Sshin{ 8055163Sshin struct netinfo6 *np; 8155163Sshin struct sockaddr_in6 fsock; 8255163Sshin int i, n, len, flen; 8355163Sshin int c; 8455163Sshin int ifidx = -1; 8555163Sshin int error; 8655163Sshin char pbuf[10]; 8755163Sshin struct addrinfo hints, *res; 8855163Sshin 8978064Sume while ((c = getopt(argc, argv, "I:")) != -1) { 9055163Sshin switch (c) { 9155163Sshin case 'I': 9255163Sshin ifidx = if_nametoindex(optarg); 9355163Sshin if (ifidx == 0) { 9455163Sshin errx(1, "invalid interface %s", optarg); 9555163Sshin /*NOTREACHED*/ 9655163Sshin } 9755163Sshin break; 9855163Sshin default: 9955163Sshin usage(); 10055163Sshin exit(1); 10155163Sshin /*NOTREACHED*/ 10255163Sshin } 10355163Sshin } 10455163Sshin argv += optind; 10555163Sshin argc -= optind; 10655163Sshin 10755163Sshin if (argc != 1) { 10855163Sshin usage(); 10978064Sume exit(1); 11055163Sshin } 11155163Sshin 11255163Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 11355163Sshin err(1, "socket"); 11455163Sshin /*NOTREACHED*/ 11555163Sshin } 11655163Sshin 11755163Sshin /* getaddrinfo is preferred for addr@ifname syntax */ 11855163Sshin snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT); 11955163Sshin memset(&hints, 0, sizeof(hints)); 12055163Sshin hints.ai_family = AF_INET6; 12162752Skris hints.ai_socktype = SOCK_DGRAM; 12255163Sshin error = getaddrinfo(argv[0], pbuf, &hints, &res); 12355163Sshin if (error) { 12462752Skris errx(1, "%s: %s", argv[0], gai_strerror(error)); 12555163Sshin /*NOTREACHED*/ 12655163Sshin } 12755163Sshin if (res->ai_next) { 12855163Sshin errx(1, "%s: %s", argv[0], "resolved to multiple addrs"); 12955163Sshin /*NOTREACHED*/ 13055163Sshin } 13155163Sshin if (sizeof(sin6) != res->ai_addrlen) { 13255163Sshin errx(1, "%s: %s", argv[0], "invalid addrlen"); 13355163Sshin /*NOTREACHED*/ 13455163Sshin } 13555163Sshin memcpy(&sin6, res->ai_addr, res->ai_addrlen); 13655163Sshin if (ifidx >= 0) 13755163Sshin sin6.sin6_scope_id = ifidx; 13855163Sshin 13955163Sshin if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) { 14055163Sshin err(1, "malloc"); 14155163Sshin /*NOTREACHED*/ 14255163Sshin } 14355163Sshin ripbuf->rip6_cmd = RIP6_REQUEST; 14455163Sshin ripbuf->rip6_vers = RIP6_VERSION; 14555163Sshin ripbuf->rip6_res1[0] = 0; 14655163Sshin ripbuf->rip6_res1[1] = 0; 14755163Sshin np = ripbuf->rip6_nets; 14855163Sshin bzero(&np->rip6_dest, sizeof(struct in6_addr)); 14955163Sshin np->rip6_tag = 0; 15055163Sshin np->rip6_plen = 0; 15155163Sshin np->rip6_metric = HOPCNT_INFINITY6; 15255163Sshin if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6, 15355163Sshin sizeof(struct sockaddr_in6)) < 0) { 15455163Sshin err(1, "send"); 15555163Sshin /*NOTREACHED*/ 15655163Sshin } 15755163Sshin do { 15855163Sshin flen = sizeof(fsock); 15955163Sshin if ((len = recvfrom(s, ripbuf, BUFSIZ, 0, 16055163Sshin (struct sockaddr *)&fsock, &flen)) < 0) { 16155163Sshin err(1, "recvfrom"); 16255163Sshin /*NOTREACHED*/ 16355163Sshin } 16455163Sshin printf("Response from %s len %d\n", 16555163Sshin sa_n2a((struct sockaddr *)&fsock), len); 16655163Sshin n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) / 16755163Sshin sizeof(struct netinfo6); 16855163Sshin np = ripbuf->rip6_nets; 16955163Sshin for (i = 0; i < n; i++, np++) { 17055163Sshin printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest), 17155163Sshin np->rip6_plen, np->rip6_metric); 17255163Sshin if (np->rip6_tag) 17355163Sshin printf(" tag=0x%x", ntohs(np->rip6_tag)); 17455163Sshin printf("\n"); 17555163Sshin } 17655163Sshin } while (len == RIPSIZE(24)); 17755163Sshin 17855163Sshin exit(0); 17955163Sshin} 18055163Sshin 18155163Sshinstatic void 18255163Sshinusage() 18355163Sshin{ 18495258Sdes fprintf(stderr, "usage: rip6query [-I iface] address\n"); 18555163Sshin} 18655163Sshin 18755163Sshin/* getnameinfo() is preferred as we may be able to show ifindex as ifname */ 18855163Sshinstatic const char * 18955163Sshinsa_n2a(sa) 19055163Sshin struct sockaddr *sa; 19155163Sshin{ 19262752Skris static char buf[NI_MAXHOST]; 19355163Sshin 19455163Sshin if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), 19555163Sshin NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) { 19655163Sshin snprintf(buf, sizeof(buf), "%s", "(invalid)"); 19755163Sshin } 19855163Sshin return buf; 19955163Sshin} 20055163Sshin 20155163Sshinstatic const char * 20255163Sshininet6_n2a(addr) 20355163Sshin struct in6_addr *addr; 20455163Sshin{ 20562752Skris static char buf[NI_MAXHOST]; 20655163Sshin 20755163Sshin return inet_ntop(AF_INET6, addr, buf, sizeof(buf)); 20855163Sshin} 209