Module Name: src
Committed By: christos
Date: Tue May 10 01:58:58 UTC 2011
Modified Files:
src/usr.sbin/traceroute6: traceroute6.8 traceroute6.c
Log Message:
AS additions from Zafer Aydogan to print the AS path. Minor cleanups to compile
without WARNS=1 hackery.
To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.sbin/traceroute6/traceroute6.8
cvs rdiff -u -r1.39 -r1.40 src/usr.sbin/traceroute6/traceroute6.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.sbin/traceroute6/traceroute6.8
diff -u src/usr.sbin/traceroute6/traceroute6.8:1.11 src/usr.sbin/traceroute6/traceroute6.8:1.12
--- src/usr.sbin/traceroute6/traceroute6.8:1.11 Sat Sep 17 11:16:11 2005
+++ src/usr.sbin/traceroute6/traceroute6.8 Mon May 9 21:58:58 2011
@@ -1,4 +1,4 @@
-.\" $NetBSD: traceroute6.8,v 1.11 2005/09/17 15:16:11 ginsbach Exp $
+.\" $NetBSD: traceroute6.8,v 1.12 2011/05/10 01:58:58 christos Exp $
.\" $KAME: traceroute6.8,v 1.8 2000/06/12 16:29:18 itojun Exp $
.\"
.\" Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -39,7 +39,7 @@
.Sh SYNOPSIS
.Nm traceroute6
.Bk -words
-.Op Fl dIlnrv
+.Op Fl adIlnrv
.Ek
.Bk -words
.Op Fl f Ar firsthop
@@ -63,12 +63,19 @@
.Op Fl w Ar waittime
.Ek
.Bk -words
+.Op Fl A Ar as_server
+.Ek
+.Bk -words
.Ar target
.Op Ar datalen
.Ek
.\"
.Sh DESCRIPTION
.Bl -tag -width Ds
+.It Fl a
+Turn on AS# lookups for each hop encountered.
+.It Fl A
+Turn on AS# lookups and use the given server instead of the default.
.It Fl d
Debug mode.
.It Fl f Ar firsthop
Index: src/usr.sbin/traceroute6/traceroute6.c
diff -u src/usr.sbin/traceroute6/traceroute6.c:1.39 src/usr.sbin/traceroute6/traceroute6.c:1.40
--- src/usr.sbin/traceroute6/traceroute6.c:1.39 Thu May 13 14:19:18 2010
+++ src/usr.sbin/traceroute6/traceroute6.c Mon May 9 21:58:58 2011
@@ -1,4 +1,4 @@
-/* $NetBSD: traceroute6.c,v 1.39 2010/05/13 18:19:18 christos Exp $ */
+/* $NetBSD: traceroute6.c,v 1.40 2011/05/10 01:58:58 christos Exp $ */
/* $KAME: traceroute6.c,v 1.67 2004/01/25 03:24:39 itojun Exp $ */
/*
@@ -75,7 +75,7 @@
#else
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: traceroute6.c,v 1.39 2010/05/13 18:19:18 christos Exp $");
+__RCSID("$NetBSD: traceroute6.c,v 1.40 2011/05/10 01:58:58 christos Exp $");
#endif
#endif
@@ -282,6 +282,8 @@
#include <netinet6/ipsec.h>
#endif
+#include "as.h"
+
#define DUMMY_PORT 10010
#define MAXPACKET 65535 /* max ip packet size */
@@ -306,68 +308,71 @@
struct tv32 tv; /* time packet left */
} __packed;
-u_char packet[512]; /* last inbound (icmp) packet */
-struct opacket *outpacket; /* last output (udp) packet */
+static u_char packet[512]; /* last inbound (icmp) packet */
+static struct opacket *outpacket; /* last output (udp) packet */
-int main __P((int, char *[]));
-int wait_for_reply __P((int, struct msghdr *));
+static ssize_t wait_for_reply(int, struct msghdr *);
#ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC
-int setpolicy __P((int so, char *policy));
+static int setpolicy(int so, const char *policy);
+#endif
#endif
+static void send_probe(int, u_long);
+static struct udphdr *get_udphdr(struct ip6_hdr *, u_char *);
+#if 0
+static int get_hoplim(struct msghdr *);
#endif
-void send_probe __P((int, u_long));
-struct udphdr *get_udphdr __P((struct ip6_hdr *, u_char *));
-int get_hoplim __P((struct msghdr *));
-double deltaT __P((struct timeval *, struct timeval *));
-char *pr_type __P((int));
-int packet_ok __P((struct msghdr *, int, int));
-void print __P((struct msghdr *, int));
-const char *inetname __P((struct sockaddr *));
-void usage __P((void));
-
-int rcvsock; /* receive (icmp) socket file descriptor */
-int sndsock; /* send (udp) socket file descriptor */
-
-struct msghdr rcvmhdr;
-struct iovec rcviov[2];
-int rcvhlim;
-struct in6_pktinfo *rcvpktinfo;
+static double deltaT(struct timeval *, struct timeval *);
+static const char *pr_type(int);
+static int packet_ok(struct msghdr *, ssize_t, int);
+static void print(struct msghdr *, int);
+static const char *inetname(struct sockaddr *);
+static void usage(void) __attribute__((__noreturn__));
+
+static int rcvsock; /* receive (icmp) socket file descriptor */
+static int sndsock; /* send (udp) socket file descriptor */
+
+static struct msghdr rcvmhdr;
+static struct iovec rcviov[2];
+static int rcvhlim;
+static struct in6_pktinfo *rcvpktinfo;
-struct sockaddr_in6 Src, Dst, Rcv;
-u_long datalen; /* How much data */
+static struct sockaddr_in6 Src, Dst, Rcv;
+static u_long datalen; /* How much data */
#define ICMP6ECHOLEN 8
/* XXX: 2064 = 127(max hops in type 0 rthdr) * sizeof(ip6_hdr) + 16(margin) */
-char rtbuf[2064];
+static char rtbuf[2064];
#ifdef USE_RFC3542
-struct ip6_rthdr *rth;
+static struct ip6_rthdr *rth;
#endif
-struct cmsghdr *cmsg;
+static struct cmsghdr *cmsg;
-char *source = 0;
-char *hostname;
+static char *source;
+static char *hostname;
-u_long nprobes = 3;
-u_long first_hop = 1;
-u_long max_hops = 30;
-u_int16_t srcport;
-u_int16_t port = 32768+666; /* start udp dest port # for probe packets */
-u_int16_t ident;
-int options; /* socket options */
-int verbose;
-int waittime = 5; /* time to wait for response (in seconds) */
-int nflag; /* print addresses numerically */
-int useicmp;
-int lflag; /* print both numerical address & hostname */
+static u_long nprobes = 3;
+static u_long first_hop = 1;
+static u_long max_hops = 30;
+static u_int16_t srcport;
+static u_int16_t port = 32768+666;/* start udp dest port # for probe packets */
+static u_int16_t ident;
+static int options; /* socket options */
+static int verbose;
+static int waittime = 5; /* time to wait for response (in seconds) */
+static int nflag; /* print addresses numerically */
+static int useicmp;
+static int lflag; /* print both numerical address & hostname */
+static int as_path; /* print as numbers for each hop */
+static char *as_server = NULL;
+static void *asn;
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM };
char hbuf[NI_MAXHOST], src0[NI_MAXHOST], *ep;
- int ch, i, on = 1, seq, rcvcmsglen, error, minlen;
+ int ch, i, on = 1, seq, rcvcmsglen, error;
+ u_long minlen;
struct addrinfo hints, *res;
static u_char *rcvcmsgbuf;
u_long probe, hops, lport;
@@ -377,10 +382,8 @@
/*
* Receive ICMP
*/
- if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
- perror("socket(ICMPv6)");
- exit(5);
- }
+ if ((rcvsock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0)
+ err(5, "socket(ICMPv6)");
/* revoke privs */
seteuid(getuid());
@@ -414,8 +417,15 @@
seq = 0;
- while ((ch = getopt(argc, argv, "df:g:Ilm:np:q:rs:w:v")) != -1)
+ while ((ch = getopt(argc, argv, "aA:df:g:Ilm:np:q:rs:w:v")) != -1)
switch (ch) {
+ case 'a':
+ as_path = 1;
+ break;
+ case 'A':
+ as_path = 1;
+ as_server = optarg;
+ break;
case 'd':
options |= SO_DEBUG;
break;
@@ -423,19 +433,13 @@
ep = NULL;
errno = 0;
first_hop = strtoul(optarg, &ep, 0);
- if (errno || !*optarg || *ep|| first_hop > 255) {
- fprintf(stderr,
- "traceroute6: invalid min hoplimit.\n");
- exit(1);
- }
+ if (errno || !*optarg || *ep|| first_hop > 255)
+ errx(1, "Invalid min hoplimit `%s'", optarg);
break;
case 'g':
hp = getipnodebyname(optarg, AF_INET6, 0, &h_errno);
- if (hp == NULL) {
- fprintf(stderr,
- "traceroute6: unknown host %s\n", optarg);
- exit(1);
- }
+ if (hp == NULL)
+ errx(1, "Unknown host `%s'", optarg);
#ifdef USE_RFC3542
if (rth == NULL) {
/*
@@ -445,21 +449,17 @@
if ((rth = inet6_rth_init((void *)rtbuf,
sizeof(rtbuf), IPV6_RTHDR_TYPE_0,
0)) == NULL) {
- fprintf(stderr,
- "inet6_rth_init failed.\n");
- exit(1);
+ errx(1, "inet6_rth_init failed");
}
}
if (inet6_rth_add((void *)rth,
- (struct in6_addr *)hp->h_addr)) {
- fprintf(stderr,
- "inet6_rth_add failed for %s\n",
+ (struct in6_addr *)hp->h_addr))
+ errx(1, "inet6_rth_add failed for `%s'",
optarg);
- exit(1);
- }
#else /* old advanced API */
if (cmsg == NULL)
- cmsg = inet6_rthdr_init(rtbuf, IPV6_RTHDR_TYPE_0);
+ cmsg = inet6_rthdr_init(rtbuf,
+ IPV6_RTHDR_TYPE_0);
inet6_rthdr_add(cmsg, (struct in6_addr *)hp->h_addr,
IPV6_RTHDR_LOOSE);
#endif
@@ -477,11 +477,8 @@
ep = NULL;
errno = 0;
max_hops = strtoul(optarg, &ep, 0);
- if (errno || !*optarg || *ep || max_hops > 255) {
- fprintf(stderr,
- "traceroute6: invalid max hoplimit.\n");
- exit(1);
- }
+ if (errno || !*optarg || *ep || max_hops > 255)
+ errx(1, "Invalid max hoplimit `%s'", optarg);
break;
case 'n':
nflag++;
@@ -490,31 +487,20 @@
ep = NULL;
errno = 0;
lport = strtoul(optarg, &ep, 0);
- if (errno || !*optarg || *ep) {
- fprintf(stderr, "traceroute6: invalid port.\n");
- exit(1);
- }
- if (lport == 0 || lport != (lport & 0xffff)) {
- fprintf(stderr,
- "traceroute6: port out of range.\n");
- exit(1);
- }
+ if (errno || !*optarg || *ep)
+ errx(1, "Invalid port `%s'", optarg);
+ if (lport == 0 || lport != (lport & 0xffff))
+ errx(1, "Port `%s' out of range", optarg);
port = lport & 0xffff;
break;
case 'q':
ep = NULL;
errno = 0;
nprobes = strtoul(optarg, &ep, 0);
- if (errno || !*optarg || *ep) {
- fprintf(stderr,
- "traceroute6: invalid nprobes.\n");
- exit(1);
- }
- if (nprobes < 1) {
- fprintf(stderr,
- "traceroute6: nprobes must be >0.\n");
- exit(1);
- }
+ if (errno || !*optarg || *ep)
+ errx(1, "Invalid nprobes `%s'", optarg);
+ if (nprobes < 1)
+ errx(1, "nprobes `%s' must be > 0", optarg);
break;
case 'r':
options |= SO_DONTROUTE;
@@ -533,16 +519,10 @@
ep = NULL;
errno = 0;
waittime = strtoul(optarg, &ep, 0);
- if (errno || !*optarg || *ep) {
- fprintf(stderr,
- "traceroute6: invalid wait time.\n");
- exit(1);
- }
- if (waittime <= 1) {
- fprintf(stderr,
- "traceroute6: wait must be >1 sec.\n");
- exit(1);
- }
+ if (errno || !*optarg || *ep)
+ errx(1, "Invalid wait time `%s'", optarg);
+ if (waittime <= 1)
+ errx(1, "Wait `%s' must be > 1 sec", optarg);
break;
default:
usage();
@@ -550,11 +530,9 @@
argc -= optind;
argv += optind;
- if (max_hops < first_hop) {
- fprintf(stderr,
- "traceroute6: max hoplimit must be larger than first hoplimit.\n");
- exit(1);
- }
+ if (max_hops < first_hop)
+ errx(1, "max hoplimit `%lu' must be larger than "
+ "first hoplimit `%lu'", max_hops, first_hop);
if (argc < 1 || argc > 2)
usage();
@@ -571,39 +549,28 @@
hints.ai_protocol = IPPROTO_ICMPV6;
hints.ai_flags = AI_CANONNAME;
error = getaddrinfo(*argv, NULL, &hints, &res);
- if (error) {
- fprintf(stderr,
- "traceroute6: %s\n", gai_strerror(error));
- exit(1);
- }
- if (res->ai_addrlen != sizeof(Dst)) {
- fprintf(stderr,
- "traceroute6: size of sockaddr mismatch\n");
- exit(1);
- }
+ if (error)
+ errx(1, "%s", gai_strerror(error));
+ if (res->ai_addrlen != sizeof(Dst))
+ errx(1, "size of sockaddr mismatch");
memcpy(&Dst, res->ai_addr, res->ai_addrlen);
hostname = res->ai_canonname ? strdup(res->ai_canonname) : *argv;
- if (!hostname) {
- fprintf(stderr, "traceroute6: not enough core\n");
- exit(1);
- }
+ if (!hostname)
+ err(1, NULL);
if (res->ai_next) {
if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
strlcpy(hbuf, "?", sizeof(hbuf));
- fprintf(stderr, "traceroute6: Warning: %s has multiple "
- "addresses; using %s\n", hostname, hbuf);
+ warnx("`%s' has multiple addresses; using `%s'",
+ hostname, hbuf);
}
if (*++argv) {
ep = NULL;
errno = 0;
datalen = strtoul(*argv, &ep, 0);
- if (errno || !*argv || *ep) {
- fprintf(stderr,
- "traceroute6: invalid packet length.\n");
- exit(1);
- }
+ if (errno || !*argv || *ep)
+ errx(1, "Invalid packet length `%s'", *argv);
}
if (useicmp)
minlen = ICMP6ECHOLEN + sizeof(struct tv32);
@@ -611,18 +578,13 @@
minlen = sizeof(struct opacket);
if (datalen < minlen)
datalen = minlen;
- else if (datalen >= MAXPACKET) {
- fprintf(stderr,
- "traceroute6: packet size must be %d <= s < %ld.\n",
- minlen, (long)MAXPACKET);
- exit(1);
- }
+ else if (datalen >= MAXPACKET)
+ errx(1, "Packet size must be %lu <= s < %lu",
+ minlen, (u_long)MAXPACKET);
outpacket = (struct opacket *)malloc((unsigned)datalen);
- if (!outpacket) {
- perror("malloc");
- exit(1);
- }
- (void) bzero((char *)outpacket, datalen);
+ if (!outpacket)
+ err(1, NULL);
+ memset(outpacket, 0, datalen);
/* initialize msghdr for receiving packets */
rcviov[0].iov_base = (caddr_t)packet;
@@ -633,10 +595,8 @@
rcvmhdr.msg_iovlen = 1;
rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
CMSG_SPACE(sizeof(int));
- if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
- fprintf(stderr, "traceroute6: malloc failed\n");
- exit(1);
- }
+ if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL)
+ err(1, NULL);
rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
rcvmhdr.msg_controllen = rcvcmsglen;
@@ -685,18 +645,14 @@
if (useicmp) {
sndsock = rcvsock;
} else {
- if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
- perror("socket(SOCK_DGRAM)");
- exit(5);
- }
+ if ((sndsock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
+ err(5, "socket(SOCK_DGRAM)");
}
#ifdef SO_SNDBUF
i = datalen;
if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&i,
- sizeof(i)) < 0) {
- perror("setsockopt(SO_SNDBUF)");
- exit(6);
- }
+ sizeof(i)) < 0)
+ err(6, "setsockopt(SO_SNDBUF)");
#endif /* SO_SNDBUF */
if (options & SO_DEBUG)
(void) setsockopt(sndsock, SOL_SOCKET, SO_DEBUG,
@@ -708,21 +664,15 @@
if (rth) {/* XXX: there is no library to finalize the header... */
rth->ip6r_len = rth->ip6r_segleft * 2;
if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_RTHDR,
- (void *)rth, (rth->ip6r_len + 1) << 3)) {
- fprintf(stderr, "setsockopt(IPV6_RTHDR): %s\n",
- strerror(errno));
- exit(1);
- }
+ (void *)rth, (rth->ip6r_len + 1) << 3))
+ err(1, "setsockopt(IPV6_RTHDR)");
}
#else /* old advanced API */
if (cmsg != NULL) {
inet6_rthdr_lasthop(cmsg, IPV6_RTHDR_LOOSE);
if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_PKTOPTIONS,
- rtbuf, cmsg->cmsg_len) < 0) {
- fprintf(stderr, "setsockopt(IPV6_PKTOPTIONS): %s\n",
- strerror(errno));
- exit(1);
- }
+ rtbuf, cmsg->cmsg_len) < 0)
+ err(1, "setsockopt(IPV6_PKTOPTIONS)");
}
#endif /* USE_RFC3542 */
#ifdef IPSEC
@@ -761,26 +711,21 @@
/*
* Source selection
*/
- bzero(&Src, sizeof(Src));
+ memset(&Src, 0, sizeof(Src));
if (source) {
- struct addrinfo hints, *res;
- int error;
+ struct addrinfo hints0, *res0;
+ int error0;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET6;
- hints.ai_socktype = SOCK_DGRAM; /*dummy*/
- hints.ai_flags = AI_NUMERICHOST;
- error = getaddrinfo(source, "0", &hints, &res);
- if (error) {
- printf("traceroute6: %s: %s\n", source,
- gai_strerror(error));
- exit(1);
- }
- if (res->ai_addrlen > sizeof(Src)) {
- printf("traceroute6: %s: %s\n", source,
- gai_strerror(error));
- exit(1);
- }
+ memset(&hints0, 0, sizeof(hints0));
+ hints0.ai_family = AF_INET6;
+ hints0.ai_socktype = SOCK_DGRAM; /*dummy*/
+ hints0.ai_flags = AI_NUMERICHOST;
+ error0 = getaddrinfo(source, "0", &hints0, &res0);
+ if (error0)
+ errx(1, "Cannot get address for `%s' (%s)", source,
+ gai_strerror(error0));
+ if (res->ai_addrlen > sizeof(Src))
+ errx(1, "Bad incompatible address length");
memcpy(&Src, res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
} else {
@@ -791,47 +736,45 @@
Nxt = Dst;
Nxt.sin6_port = htons(DUMMY_PORT);
if (cmsg != NULL)
- bcopy(inet6_rthdr_getaddr(cmsg, 1), &Nxt.sin6_addr,
+ memcpy( &Nxt.sin6_addr, inet6_rthdr_getaddr(cmsg, 1),
sizeof(Nxt.sin6_addr));
- if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
- perror("socket");
- exit(1);
- }
- if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0) {
- perror("connect");
- exit(1);
- }
+ if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
+ err(1, "socket");
+ if (connect(dummy, (struct sockaddr *)&Nxt, Nxt.sin6_len) < 0)
+ err(1, "connect");
len = sizeof(Src);
- if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0) {
- perror("getsockname");
- exit(1);
- }
- if (getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
- src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)) {
- fprintf(stderr, "getnameinfo failed for source\n");
- exit(1);
- }
+ if (getsockname(dummy, (struct sockaddr *)&Src, &len) < 0)
+ err(1, "getsockname");
+ if ((error = getnameinfo((struct sockaddr *)&Src, Src.sin6_len,
+ src0, sizeof(src0), NULL, 0, NI_NUMERICHOST)))
+ errx(1, "getnameinfo failed for source (%s)",
+ gai_strerror(error));
source = src0;
close(dummy);
}
Src.sin6_port = htons(0);
- if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0) {
- perror("bind");
- exit(1);
- }
+ if (bind(sndsock, (struct sockaddr *)&Src, Src.sin6_len) < 0)
+ err(1, "bind");
{
socklen_t len;
len = sizeof(Src);
- if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0) {
- perror("getsockname");
- exit(1);
- }
+ if (getsockname(sndsock, (struct sockaddr *)&Src, &len) < 0)
+ err(1, "bind");
srcport = ntohs(Src.sin6_port);
}
+ if (as_path) {
+ asn = as_setup(as_server);
+ if (asn == NULL) {
+ warnx("as_setup failed, AS# lookups disabled");
+ (void)fflush(stderr);
+ as_path = 0;
+ }
+ }
+
/*
* Message to users
*/
@@ -855,10 +798,10 @@
for (hops = first_hop; hops <= max_hops; ++hops) {
struct in6_addr lastaddr;
int got_there = 0;
- int unreachable = 0;
+ u_long unreachable = 0;
printf("%2lu ", hops);
- bzero(&lastaddr, sizeof(lastaddr));
+ memset(&lastaddr, 0, sizeof(lastaddr));
for (probe = 0; probe < nprobes; ++probe) {
int cc;
struct timeval t1, t2;
@@ -914,17 +857,18 @@
}
}
+ if (as_path)
+ as_shutdown(asn);
+
exit(0);
}
-int
-wait_for_reply(sock, mhdr)
- int sock;
- struct msghdr *mhdr;
+static ssize_t
+wait_for_reply(int sock, struct msghdr *mhdr)
{
#ifdef HAVE_POLL
struct pollfd pfd[1];
- int cc = 0;
+ ssize_t cc = 0;
pfd[0].fd = sock;
pfd[0].events = POLLIN;
@@ -933,11 +877,12 @@
if (poll(pfd, 1, waittime * 1000) > 0)
cc = recvmsg(rcvsock, mhdr, 0);
- return(cc);
+ return cc;
#else
fd_set *fdsp;
struct timeval wait;
- int cc = 0, fdsn;
+ ssize_t cc = 0;
+ int fdsn;
fdsn = howmany(sock + 1, NFDBITS) * sizeof(fd_mask);
if ((fdsp = (fd_set *)malloc(fdsn)) == NULL)
@@ -950,16 +895,14 @@
cc = recvmsg(rcvsock, mhdr, 0);
free(fdsp);
- return(cc);
+ return cc;
#endif
}
#ifdef IPSEC
#ifdef IPSEC_POLICY_IPSEC
-int
-setpolicy(so, policy)
- int so;
- char *policy;
+static int
+setpolicy(int so, const char *policy)
{
char *buf;
@@ -978,10 +921,8 @@
#endif
#endif
-void
-send_probe(seq, hops)
- int seq;
- u_long hops;
+static void
+send_probe(int seq, u_long hops)
{
struct timeval tv;
struct tv32 tv32;
@@ -989,9 +930,8 @@
i = hops;
if (setsockopt(sndsock, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
- (char *)&i, sizeof(i)) < 0) {
- perror("setsockopt IPV6_UNICAST_HOPS");
- }
+ (char *)&i, sizeof(i)) < 0)
+ warn("setsockopt IPV6_UNICAST_HOPS");
Dst.sin6_port = htons(port + seq);
(void) gettimeofday(&tv, NULL);
@@ -1006,30 +946,30 @@
icp->icmp6_cksum = 0;
icp->icmp6_id = ident;
icp->icmp6_seq = htons(seq);
- bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN),
+ memcpy(((u_int8_t *)outpacket + ICMP6ECHOLEN), &tv32,
sizeof(tv32));
} else {
struct opacket *op = outpacket;
op->seq = seq;
op->hops = hops;
- bcopy(&tv32, &op->tv, sizeof tv32);
+ memcpy(&op->tv, &tv32, sizeof tv32);
}
i = sendto(sndsock, (char *)outpacket, datalen , 0,
(struct sockaddr *)&Dst, Dst.sin6_len);
- if (i < 0 || i != datalen) {
+ if (i < 0 || i != (int)datalen) {
if (i < 0)
- perror("sendto");
+ warnx("sendto");
printf("traceroute6: wrote %s %lu chars, ret=%d\n",
hostname, datalen, i);
(void) fflush(stdout);
}
}
-int
-get_hoplim(mhdr)
- struct msghdr *mhdr;
+#if 0
+static int
+get_hoplim(struct msghdr *mhdr)
{
struct cmsghdr *cm;
@@ -1038,32 +978,31 @@
if (cm->cmsg_level == IPPROTO_IPV6 &&
cm->cmsg_type == IPV6_HOPLIMIT &&
cm->cmsg_len == CMSG_LEN(sizeof(int)))
- return(*(int *)CMSG_DATA(cm));
+ return *(int *)CMSG_DATA(cm);
}
- return(-1);
+ return -1;
}
+#endif
-double
-deltaT(t1p, t2p)
- struct timeval *t1p, *t2p;
+static double
+deltaT(struct timeval *t1p, struct timeval *t2p)
{
double dt;
dt = (double)(t2p->tv_sec - t1p->tv_sec) * 1000.0 +
(double)(t2p->tv_usec - t1p->tv_usec) / 1000.0;
- return (dt);
+ return dt;
}
/*
* Convert an ICMP "type" field to a printable string.
*/
-char *
-pr_type(t0)
- int t0;
+static const char *
+pr_type(int t0)
{
u_char t = t0 & 0xff;
- char *cp;
+ const char *cp;
switch (t) {
case ICMP6_DST_UNREACH:
@@ -1115,11 +1054,8 @@
return cp;
}
-int
-packet_ok(mhdr, cc, seq)
- struct msghdr *mhdr;
- int cc;
- int seq;
+static int
+packet_ok(struct msghdr *mhdr, ssize_t cc, int seq)
{
struct icmp6_hdr *icp;
struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
@@ -1145,19 +1081,20 @@
printf("packet too short (%d bytes) from %s\n", cc,
hbuf);
}
- return (0);
+ return 0;
}
cc -= hlen;
icp = (struct icmp6_hdr *)(buf + hlen);
#else
- if (cc < sizeof(struct icmp6_hdr)) {
+ if (cc < (ssize_t)sizeof(struct icmp6_hdr)) {
if (verbose) {
if (getnameinfo((struct sockaddr *)from, from->sin6_len,
hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
strlcpy(hbuf, "invalid", sizeof(hbuf));
- printf("data too short (%d bytes) from %s\n", cc, hbuf);
+ printf("data too short (%zd bytes) from %s\n", cc,
+ hbuf);
}
- return(0);
+ return 0;
}
icp = (struct icmp6_hdr *)buf;
#endif
@@ -1180,7 +1117,7 @@
if (rcvpktinfo == NULL || hlimp == NULL) {
warnx("failed to get received hop limit or packet info");
#if 0
- return(0);
+ return 0;
#else
rcvhlim = 0; /*XXX*/
#endif
@@ -1199,30 +1136,30 @@
if ((up = get_udphdr(hip, (u_char *)(buf + cc))) == NULL) {
if (verbose)
warnx("failed to get upper layer header");
- return(0);
+ return 0;
}
if (useicmp &&
((struct icmp6_hdr *)up)->icmp6_id == ident &&
((struct icmp6_hdr *)up)->icmp6_seq == htons(seq))
- return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
+ return type == ICMP6_TIME_EXCEEDED ? -1 : code + 1;
else if (!useicmp &&
up->uh_sport == htons(srcport) &&
up->uh_dport == htons(port + seq))
- return (type == ICMP6_TIME_EXCEEDED ? -1 : code + 1);
+ return type == ICMP6_TIME_EXCEEDED ? -1 : code + 1;
} else if (useicmp && type == ICMP6_ECHO_REPLY) {
if (icp->icmp6_id == ident &&
icp->icmp6_seq == htons(seq))
- return (ICMP6_DST_UNREACH_NOPORT + 1);
+ return ICMP6_DST_UNREACH_NOPORT + 1;
}
if (verbose) {
char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN];
u_int8_t *p;
- int i;
+ ssize_t i;
if (getnameinfo((struct sockaddr *)from, from->sin6_len,
sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST) != 0)
strlcpy(sbuf, "invalid", sizeof(sbuf));
- printf("\n%d bytes from %s to %s", cc, sbuf,
+ printf("\n%zd bytes from %s to %s", cc, sbuf,
rcvpktinfo ? inet_ntop(AF_INET6, &rcvpktinfo->ipi6_addr,
dbuf, sizeof(dbuf)) : "?");
printf(": icmp type %d (%s) code %d\n", type, pr_type(type),
@@ -1231,7 +1168,7 @@
#define WIDTH 16
for (i = 0; i < cc; i++) {
if (i % WIDTH == 0)
- printf("%04x:", i);
+ printf("%04zx:", i);
if (i % 4 == 0)
printf(" ");
printf("%02x", p[i]);
@@ -1241,22 +1178,20 @@
if (cc % WIDTH != 0)
printf("\n");
}
- return(0);
+ return 0;
}
/*
* Increment pointer until find the UDP or ICMP header.
*/
-struct udphdr *
-get_udphdr(ip6, lim)
- struct ip6_hdr *ip6;
- u_char *lim;
+static struct udphdr *
+get_udphdr(struct ip6_hdr *ip6, u_char *lim)
{
u_char *cp = (u_char *)ip6, nh;
int hlen;
if (cp + sizeof(*ip6) >= lim)
- return(NULL);
+ return NULL;
nh = ip6->ip6_nxt;
cp += sizeof(struct ip6_hdr);
@@ -1265,11 +1200,11 @@
switch (nh) {
case IPPROTO_ESP:
case IPPROTO_TCP:
- return(NULL);
+ return NULL;
case IPPROTO_ICMPV6:
- return(useicmp ? (struct udphdr *)cp : NULL);
+ return useicmp ? (struct udphdr *)cp : NULL;
case IPPROTO_UDP:
- return(useicmp ? NULL : (struct udphdr *)cp);
+ return useicmp ? NULL : (struct udphdr *)cp;
case IPPROTO_FRAGMENT:
hlen = sizeof(struct ip6_frag);
nh = ((struct ip6_frag *)cp)->ip6f_nxt;
@@ -1287,13 +1222,11 @@
cp += hlen;
}
- return(NULL);
+ return NULL;
}
-void
-print(mhdr, cc)
- struct msghdr *mhdr;
- int cc;
+static void
+print(struct msghdr *mhdr, int cc)
{
struct sockaddr_in6 *from = (struct sockaddr_in6 *)mhdr->msg_name;
char hbuf[NI_MAXHOST];
@@ -1301,6 +1234,8 @@
if (getnameinfo((struct sockaddr *)from, from->sin6_len,
hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
strlcpy(hbuf, "invalid", sizeof(hbuf));
+ if (as_path)
+ printf(" [AS%u]", as_lookup(asn, hbuf, AF_INET6));
if (nflag)
printf(" %s", hbuf);
else if (lflag)
@@ -1326,9 +1261,8 @@
* If the nflag has been supplied, give
* numeric value, otherwise try for symbolic name.
*/
-const char *
-inetname(sa)
- struct sockaddr *sa;
+static const char *
+inetname(struct sockaddr *sa)
{
static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1];
static int first = 1;
@@ -1361,12 +1295,13 @@
return line;
}
-void
-usage()
+static void
+usage(void)
{
fprintf(stderr,
-"usage: traceroute6 [-dIlnrv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
-" [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n");
+"Usage: %s [-dIlnrv] [-f firsthop] [-g gateway] [-m hoplimit]\n"
+"\t%s [-p port] [-q probes] [-s src] [-w waittime] target [datalen]\n",
+ getprogname(), getprogname());
exit(1);
}