diff options
author | 2004-01-25 03:25:49 +0000 | |
---|---|---|
committer | 2004-01-25 03:25:49 +0000 | |
commit | c74f7ad031354b628c7a0b2e5f2572bbe93d0c2e (patch) | |
tree | dc04ecaa66e65429bae1d80461d6b9382cc3c2b6 | |
parent | sync (diff) | |
download | wireguard-openbsd-c74f7ad031354b628c7a0b2e5f2572bbe93d0c2e.tar.xz wireguard-openbsd-c74f7ad031354b628c7a0b2e5f2572bbe93d0c2e.zip |
knf and cleanup. as well avoid sending out packet information which
contains byte-order, alignment, or padding information about the src
host. while at it, fix unaligned access operations. ok itojun
-rw-r--r-- | usr.sbin/traceroute6/traceroute6.c | 88 |
1 files changed, 34 insertions, 54 deletions
diff --git a/usr.sbin/traceroute6/traceroute6.c b/usr.sbin/traceroute6/traceroute6.c index f5037743428..bf3edf09a14 100644 --- a/usr.sbin/traceroute6/traceroute6.c +++ b/usr.sbin/traceroute6/traceroute6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: traceroute6.c,v 1.36 2003/06/11 23:33:30 deraadt Exp $ */ +/* $OpenBSD: traceroute6.c,v 1.37 2004/01/25 03:25:49 deraadt Exp $ */ /* $KAME: traceroute6.c,v 1.63 2002/10/24 12:53:25 itojun Exp $ */ /* @@ -287,16 +287,18 @@ static char sccsid[] = "@(#)traceroute.c 8.1 (Berkeley) 6/6/93"; /* * format of a (udp) probe packet. */ -struct opacket { - u_char seq; /* sequence number of this packet */ - u_char hops; /* hop limit of the packet */ - struct timeval tv; /* time packet left */ -}; struct tv32 { u_int32_t tv32_sec; u_int32_t tv32_usec; }; +struct opacket { + u_char seq; /* sequence number of this packet */ + u_char hops; /* hop limit of the packet */ + u_char pad[2]; + struct tv32 tv; /* time packet left */ +} __packed; + u_char packet[512]; /* last inbound (icmp) packet */ struct opacket *outpacket; /* last output (udp) packet */ @@ -314,7 +316,6 @@ double deltaT(struct timeval *, struct timeval *); char *pr_type(int); int packet_ok(struct msghdr *, int, int); void print(struct msghdr *, int); -void tvsub(struct timeval *, struct timeval *); const char *inetname(struct sockaddr *); void usage(void); @@ -355,18 +356,14 @@ int lflag; /* print both numerical address & hostname */ int main(int argc, char *argv[]) { - struct hostent *hp; - int error; + 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; struct addrinfo hints, *res; - int ch, i, on, seq, rcvcmsglen; - u_long probe, hops; static u_char *rcvcmsgbuf; - char hbuf[NI_MAXHOST], src0[NI_MAXHOST]; - char *ep; - int mib[4] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_DEFHLIM }; + u_long probe, hops, lport; + struct hostent *hp; size_t size; - u_long lport; - int minlen; /* * Receive ICMP @@ -384,8 +381,6 @@ main(int argc, char *argv[]) (void) sysctl(mib, sizeof(mib)/sizeof(mib[0]), &i, &size, NULL, 0); max_hops = i; - /* set a minimum set of socket options */ - on = 1; /* specify to tell receiving interface */ #ifdef IPV6_RECVPKTINFO if (setsockopt(rcvsock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, @@ -409,7 +404,7 @@ main(int argc, char *argv[]) #endif seq = 0; - + while ((ch = getopt(argc, argv, "df:g:Ilm:np:q:rs:w:v")) != -1) switch (ch) { case 'd': @@ -557,7 +552,7 @@ main(int argc, char *argv[]) #if 1 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); #else - setlinebuf (stdout); + setlinebuf(stdout); #endif memset(&hints, 0, sizeof(hints)); @@ -613,7 +608,7 @@ main(int argc, char *argv[]) exit(1); } outpacket = (struct opacket *)malloc((unsigned)datalen); - if (! outpacket) { + if (!outpacket) { perror("malloc"); exit(1); } @@ -626,8 +621,8 @@ main(int argc, char *argv[]) rcvmhdr.msg_namelen = sizeof(Rcv); rcvmhdr.msg_iov = rcviov; rcvmhdr.msg_iovlen = 1; - rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) - + CMSG_SPACE(sizeof(int)); + rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + + CMSG_SPACE(sizeof(int)); if ((rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) { fprintf(stderr, "traceroute6: malloc failed\n"); exit(1); @@ -863,7 +858,7 @@ main(int argc, char *argv[]) while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) { (void) gettimeofday(&t2, NULL); if ((i = packet_ok(&rcvmhdr, cc, seq))) { - if (! IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr, + if (!IN6_ARE_ADDR_EQUAL(&Rcv.sin6_addr, &lastaddr)) { print(&rcvmhdr, cc); lastaddr = Rcv.sin6_addr; @@ -972,6 +967,8 @@ setpolicy(int so, char *policy) void send_probe(int seq, u_long hops) { + struct timeval tv; + struct tv32 tv32; int i; i = hops; @@ -981,32 +978,31 @@ send_probe(int seq, u_long hops) } Dst.sin6_port = htons(port + seq); + (void) gettimeofday(&tv, NULL); + tv32.tv32_sec = htonl(tv.tv_sec); + tv32.tv32_usec = htonl(tv.tv_usec); if (useicmp) { struct icmp6_hdr *icp = (struct icmp6_hdr *)outpacket; - struct timeval tv; - struct tv32 *tv32; icp->icmp6_type = ICMP6_ECHO_REQUEST; icp->icmp6_code = 0; icp->icmp6_cksum = 0; icp->icmp6_id = ident; icp->icmp6_seq = htons(seq); - (void) gettimeofday(&tv, NULL); - tv32 = (struct tv32 *)((u_int8_t *)outpacket + ICMP6ECHOLEN); - tv32->tv32_sec = htonl(tv.tv_sec); - tv32->tv32_usec = htonl(tv.tv_usec); + bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN), sizeof tv32); } else { struct opacket *op = outpacket; + op->seq = seq; op->hops = hops; - (void) gettimeofday(&op->tv, NULL); + bcopy(&tv32, &op->tv, sizeof tv32); } - i = sendto(sndsock, (char *)outpacket, datalen , 0, + i = sendto(sndsock, (char *)outpacket, datalen, 0, (struct sockaddr *)&Dst, Dst.sin6_len); if (i < 0 || i != datalen) { - if (i<0) + if (i < 0) perror("sendto"); printf("traceroute6: wrote %s %lu chars, ret=%d\n", hostname, datalen, i); @@ -1172,7 +1168,7 @@ packet_ok(struct msghdr *mhdr, int cc, int seq) type = icp->icmp6_type; code = icp->icmp6_code; if ((type == ICMP6_TIME_EXCEEDED && code == ICMP6_TIME_EXCEED_TRANSIT) - || type == ICMP6_DST_UNREACH) { + || type == ICMP6_DST_UNREACH) { struct ip6_hdr *hip; struct udphdr *up; @@ -1196,9 +1192,9 @@ packet_ok(struct msghdr *mhdr, int cc, int seq) return (ICMP6_DST_UNREACH_NOPORT + 1); } if (verbose) { - int i; - u_int8_t *p; char sbuf[NI_MAXHOST+1], dbuf[INET6_ADDRSTRLEN]; + u_int8_t *p; + int i; if (getnameinfo((struct sockaddr *)from, from->sin6_len, sbuf, sizeof(sbuf), NULL, 0, NI_NUMERICHOST) != 0) @@ -1299,21 +1295,6 @@ print(struct msghdr *mhdr, int cc) } /* - * Subtract 2 timeval structs: out = out - in. - * Out is assumed to be >= in. - */ -void -tvsub(struct timeval *out, struct timeval *in) -{ - - if ((out->tv_usec -= in->tv_usec) < 0) { - out->tv_sec--; - out->tv_usec += 1000000; - } - out->tv_sec -= in->tv_sec; -} - -/* * Construct an Internet address representation. * If the nflag has been supplied, give * numeric value, otherwise try for symbolic name. @@ -1321,10 +1302,9 @@ tvsub(struct timeval *out, struct timeval *in) const char * inetname(struct sockaddr *sa) { - char *cp; - static char line[NI_MAXHOST]; - static char domain[MAXHOSTNAMELEN + 1]; + static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1]; static int first = 1; + char *cp; if (first && !nflag) { first = 0; |