#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <net/if.h>
#if defined(__FreeBSD__) && __FreeBSD__ >= 3
#include <net/if_var.h>
#endif
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "route6d.h"
#define DEFAULT_WAIT 5
static int s;
static int query_wait = DEFAULT_WAIT;
static struct sockaddr_in6 sin6;
static struct rip6 *ripbuf;
#define RIPSIZE(n) (sizeof(struct rip6) + (n-1) * sizeof(struct netinfo6))
__dead static void usage(void);
__dead static void sigalrm_handler(int);
static const char *sa_n2a(struct sockaddr *);
static const char *inet6_n2a(struct in6_addr *);
int
main(int argc, char **argv)
{
struct netinfo6 *np;
struct sockaddr_in6 fsock;
int i, n, len;
socklen_t flen;
int c;
int ifidx = -1;
int error;
char pbuf[NI_MAXSERV];
struct addrinfo hints, *res;
while ((c = getopt(argc, argv, "I:w:")) != -1) {
switch (c) {
case 'I':
ifidx = if_nametoindex(optarg);
if (ifidx == 0) {
errx(1, "invalid interface %s", optarg);
}
break;
case 'w':
query_wait = atoi(optarg);
break;
default:
usage();
}
}
argv += optind;
argc -= optind;
if (argc != 1)
usage();
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
err(1, "socket");
}
snprintf(pbuf, sizeof(pbuf), "%d", RIP6_PORT);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(argv[0], pbuf, &hints, &res);
if (error) {
errx(1, "%s: %s", argv[0], gai_strerror(error));
}
if (res->ai_next) {
errx(1, "%s: %s", argv[0], "resolved to multiple addrs");
}
if (sizeof(sin6) != res->ai_addrlen) {
errx(1, "%s: %s", argv[0], "invalid addrlen");
}
memcpy(&sin6, res->ai_addr, res->ai_addrlen);
if (ifidx >= 0)
sin6.sin6_scope_id = ifidx;
if ((ripbuf = (struct rip6 *)malloc(BUFSIZ)) == NULL) {
err(1, "malloc");
}
ripbuf->rip6_cmd = RIP6_REQUEST;
ripbuf->rip6_vers = RIP6_VERSION;
ripbuf->rip6_res1[0] = 0;
ripbuf->rip6_res1[1] = 0;
np = ripbuf->rip6_nets;
bzero(&np->rip6_dest, sizeof(struct in6_addr));
np->rip6_tag = 0;
np->rip6_plen = 0;
np->rip6_metric = HOPCNT_INFINITY6;
if (sendto(s, ripbuf, RIPSIZE(1), 0, (struct sockaddr *)&sin6,
sizeof(struct sockaddr_in6)) < 0) {
err(1, "send");
}
signal(SIGALRM, sigalrm_handler);
for (;;) {
flen = sizeof(fsock);
alarm(query_wait);
if ((len = recvfrom(s, ripbuf, BUFSIZ, 0,
(struct sockaddr *)&fsock, &flen)) < 0) {
err(1, "recvfrom");
}
alarm(0);
printf("Response from %s len %d\n",
sa_n2a((struct sockaddr *)&fsock), len);
n = (len - sizeof(struct rip6) + sizeof(struct netinfo6)) /
sizeof(struct netinfo6);
np = ripbuf->rip6_nets;
for (i = 0; i < n; i++, np++) {
printf("\t%s/%d [%d]", inet6_n2a(&np->rip6_dest),
np->rip6_plen, np->rip6_metric);
if (np->rip6_tag)
printf(" tag=0x%x", ntohs(np->rip6_tag));
printf("\n");
}
}
exit(0);
}
static void
usage(void)
{
fprintf(stderr, "usage: rip6query [-I iface] [-w wait] address\n");
exit(1);
}
static const char *
sa_n2a(struct sockaddr *sa)
{
static char buf[NI_MAXHOST];
if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf),
NULL, 0, NI_NUMERICHOST) != 0) {
snprintf(buf, sizeof(buf), "%s", "(invalid)");
}
return buf;
}
static const char *
inet6_n2a(struct in6_addr *addr)
{
static char buf[NI_MAXHOST];
return inet_ntop(AF_INET6, addr, buf, sizeof(buf));
}
static void
sigalrm_handler(int sig)
{
_exit(0);
}