From 58d00a141bd61dc22935f0bb660830332dc46683 Mon Sep 17 00:00:00 2001 From: florian Date: Fri, 18 Apr 2014 15:58:43 +0000 Subject: [PATCH] Factor out build_probe{4,6} from send_probe; now send_probe is AF independent. While there define outpacket as u_char and cast as needed in traceroute6. OK benno@ --- usr.sbin/traceroute/traceroute.c | 31 +++++++++++++++------ usr.sbin/traceroute6/traceroute6.c | 44 +++++++++++++++++++++--------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/usr.sbin/traceroute/traceroute.c b/usr.sbin/traceroute/traceroute.c index ea6f5cd5fb9..9c2f33ae92d 100644 --- a/usr.sbin/traceroute/traceroute.c +++ b/usr.sbin/traceroute/traceroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: traceroute.c,v 1.98 2014/04/18 08:44:25 florian Exp $ */ +/* $OpenBSD: traceroute.c,v 1.99 2014/04/18 15:58:43 florian Exp $ */ /* $NetBSD: traceroute.c,v 1.10 1995/05/21 15:50:45 mycroft Exp $ */ /*- @@ -259,7 +259,8 @@ u_char packet[512], *outpacket; /* last inbound (icmp) packet */ int wait_for_reply(int, struct msghdr *); void dump_packet(void); -void send_probe(int, u_int8_t, int, struct sockaddr_in *); +void build_probe4(int, u_int8_t, int); +void send_probe(int, u_int8_t, int, struct sockaddr *); int packet_ok(u_char *, int, struct sockaddr_in *, int, int); void dump_packet(void); void print_exthdr(u_char *, int); @@ -301,6 +302,8 @@ int xflag; /* show ICMP extension header */ int tflag; /* tos flag was set */ int Aflag; /* lookup ASN */ +extern char *__progname; + int main(int argc, char *argv[]) { @@ -632,7 +635,7 @@ main(int argc, char *argv[]) int code; (void) gettimeofday(&t1, NULL); - send_probe(++seq, ttl, incflag, &to); + send_probe(++seq, ttl, incflag, (struct sockaddr*)&to); while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) { (void) gettimeofday(&t2, NULL); i = packet_ok(packet, cc, &from, seq, incflag); @@ -898,7 +901,7 @@ dump_packet(void) } void -send_probe(int seq, u_int8_t ttl, int iflag, struct sockaddr_in *to) +build_probe4(int seq, u_int8_t ttl, int iflag) { struct ip *ip = (struct ip *)outpacket; u_char *p = (u_char *)(ip + 1); @@ -906,7 +909,6 @@ send_probe(int seq, u_int8_t ttl, int iflag, struct sockaddr_in *to) struct icmp *icmpp = (struct icmp *)(p + lsrrlen); struct packetdata *op; struct timeval tv; - int i; ip->ip_len = htons(datalen); ip->ip_ttl = ttl; @@ -962,16 +964,29 @@ send_probe(int seq, u_int8_t ttl, int iflag, struct sockaddr_in *to) if (icmpp->icmp_cksum == 0) icmpp->icmp_cksum = 0xffff; } +} + +void +send_probe(int seq, u_int8_t ttl, int iflag, struct sockaddr *to) +{ + int i; + + switch (to->sa_family) { + case AF_INET: + build_probe4(seq, ttl, iflag); + break; + default: + errx(1, "unsupported AF: %d", to->sa_family); + } if (dump) dump_packet(); - i = sendto(sndsock, outpacket, datalen, 0, (struct sockaddr *)to, - sizeof(struct sockaddr_in)); + i = sendto(sndsock, outpacket, datalen, 0, to, to->sa_len); if (i < 0 || i != datalen) { if (i < 0) perror("sendto"); - printf("traceroute: wrote %s %d chars, ret=%d\n", hostname, + printf("%s: wrote %s %d chars, ret=%d\n", __progname, hostname, datalen, i); (void) fflush(stdout); } diff --git a/usr.sbin/traceroute6/traceroute6.c b/usr.sbin/traceroute6/traceroute6.c index c4d97e4943f..d7339b5a1bb 100644 --- a/usr.sbin/traceroute6/traceroute6.c +++ b/usr.sbin/traceroute6/traceroute6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: traceroute6.c,v 1.69 2014/03/27 09:32:18 florian Exp $ */ +/* $OpenBSD: traceroute6.c,v 1.70 2014/04/18 15:58:43 florian Exp $ */ /* $KAME: traceroute6.c,v 1.63 2002/10/24 12:53:25 itojun Exp $ */ /* @@ -284,12 +284,13 @@ struct opacket { } __packed; u_char packet[512]; /* last inbound (icmp) packet */ -struct opacket *outpacket; /* last output (udp) packet */ +u_char *outpacket; /* last output (udp) packet */ int main(int, char *[]); int wait_for_reply(int, struct msghdr *); void dump_packet(void); -void send_probe(int, u_int8_t, int); +void build_probe6(int, u_int8_t, int, struct sockaddr *); +void send_probe(int, u_int8_t, int, struct sockaddr *); struct udphdr *get_udphdr(struct ip6_hdr *, u_char *); int get_hoplim(struct msghdr *); double deltaT(struct timeval *, struct timeval *); @@ -332,6 +333,8 @@ int dump; int useicmp; int Aflag; /* lookup ASN */ +extern char *__progname; + int main(int argc, char *argv[]) { @@ -716,7 +719,8 @@ main(int argc, char *argv[]) struct timeval t1, t2; (void) gettimeofday(&t1, NULL); - send_probe(++seq, hops, incflag); + send_probe(++seq, hops, incflag, (struct sockaddr*) + &Dst); while ((cc = wait_for_reply(rcvsock, &rcvmhdr))) { (void) gettimeofday(&t2, NULL); if ((i = packet_ok(&rcvmhdr, cc, seq, @@ -810,7 +814,7 @@ dump_packet(void) } void -send_probe(int seq, u_int8_t hops, int iflag) +build_probe6(int seq, u_int8_t hops, int iflag, struct sockaddr *to) { struct timeval tv; struct tv32 tv32; @@ -823,9 +827,9 @@ send_probe(int seq, u_int8_t hops, int iflag) } if (iflag) - Dst.sin6_port = htons(port + seq); + ((struct sockaddr_in6*)to)->sin6_port = htons(port + seq); else - Dst.sin6_port = htons(port); + ((struct sockaddr_in6*)to)->sin6_port = htons(port); (void) gettimeofday(&tv, NULL); tv32.tv32_sec = htonl(tv.tv_sec); tv32.tv32_usec = htonl(tv.tv_usec); @@ -838,25 +842,39 @@ send_probe(int seq, u_int8_t hops, int iflag) icp->icmp6_cksum = 0; icp->icmp6_id = ident; icp->icmp6_seq = htons(seq); - bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN), sizeof tv32); + bcopy(&tv32, ((u_int8_t *)outpacket + ICMP6ECHOLEN), + sizeof tv32); } else { - struct opacket *op = outpacket; + struct opacket *op = (struct opacket *)outpacket; op->seq = seq; op->hops = hops; bcopy(&tv32, &op->tv, sizeof tv32); } +} + +void +send_probe(int seq, u_int8_t hops, int iflag, struct sockaddr *to) +{ + int i; + + switch (to->sa_family) { + case AF_INET6: + build_probe6(seq, hops, iflag, to); + break; + default: + errx(1, "unsupported AF: %d", to->sa_family); + } if (dump) dump_packet(); - i = sendto(sndsock, (char *)outpacket, datalen, 0, - (struct sockaddr *)&Dst, Dst.sin6_len); + i = sendto(sndsock, outpacket, datalen, 0, to, to->sa_len); if (i < 0 || i != datalen) { if (i < 0) perror("sendto"); - printf("traceroute6: wrote %s %d chars, ret=%d\n", - hostname, datalen, i); + printf("%s: wrote %s %d chars, ret=%d\n", __progname, hostname, + datalen, i); (void) fflush(stdout); } } -- 2.20.1