summaryrefslogtreecommitdiff
path: root/usr.sbin/traceroute
diff options
context:
space:
mode:
authorFlorian Obser <florian@cvs.openbsd.org>2014-04-19 14:06:11 +0000
committerFlorian Obser <florian@cvs.openbsd.org>2014-04-19 14:06:11 +0000
commit1df171f5d90861f4fa6bb288988459b655b96e4d (patch)
treee21790342212d0a5ef4d16fc8329d6cc35b10bbc /usr.sbin/traceroute
parent6997fbf178cc64f6a456e862e4b006393053f42a (diff)
Fix display of destination IP when host is an IP address.
Pointed out by and OK benno@
Diffstat (limited to 'usr.sbin/traceroute')
-rw-r--r--usr.sbin/traceroute/traceroute.c61
1 files changed, 34 insertions, 27 deletions
diff --git a/usr.sbin/traceroute/traceroute.c b/usr.sbin/traceroute/traceroute.c
index b6382b29a2f..cb647521afa 100644
--- a/usr.sbin/traceroute/traceroute.c
+++ b/usr.sbin/traceroute/traceroute.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: traceroute.c,v 1.110 2014/04/18 17:01:47 florian Exp $ */
+/* $OpenBSD: traceroute.c,v 1.111 2014/04/19 14:06:10 florian Exp $ */
/* $NetBSD: traceroute.c,v 1.10 1995/05/21 15:50:45 mycroft Exp $ */
/*-
@@ -323,7 +323,7 @@ main(int argc, char *argv[])
struct ip *ip, *inner_ip;
struct icmp *icp;
u_int8_t ttl;
- char *ep, hbuf[NI_MAXHOST];
+ char *ep, hbuf[NI_MAXHOST], *dest;
const char *errstr;
long l;
uid_t uid;
@@ -509,36 +509,43 @@ main(int argc, char *argv[])
(tmprnd & 0x7ff);
usec_perturb = arc4random();
- (void) memset(&to, 0, sizeof(struct sockaddr));
- to.sin_family = AF_INET;
- if (inet_aton(*argv, &to.sin_addr) != 0)
+ (void) memset(&to, 0, sizeof(to));
+
+ if (inet_aton(*argv, &to.sin_addr) != 0) {
hostname = *argv;
- else {
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = PF_INET;
- hints.ai_socktype = SOCK_RAW;
- hints.ai_protocol = IPPROTO_ICMP;
- hints.ai_flags = AI_CANONNAME;
- if ((error = getaddrinfo(*argv, NULL, &hints, &res)))
- errx(1, "%s", gai_strerror(error));
-
- if (res->ai_addrlen != sizeof(to))
- errx(1, "size of sockaddr mismatch");
-
- memcpy(&to, res->ai_addr, res->ai_addrlen);
- hostname = res->ai_canonname ? strdup(res->ai_canonname) :
- *argv;
+ if ((dest = strdup(inet_ntoa(to.sin_addr))) == NULL)
+ errx(1, "malloc");
+ } else
+ dest = *argv;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = PF_INET;
+ hints.ai_socktype = SOCK_RAW;
+ hints.ai_protocol = IPPROTO_ICMP;
+ hints.ai_flags = AI_CANONNAME;
+ if ((error = getaddrinfo(dest, NULL, &hints, &res)))
+ errx(1, "%s", gai_strerror(error));
+
+ if (res->ai_addrlen != sizeof(to))
+ errx(1, "size of sockaddr mismatch");
+
+ memcpy(&to, res->ai_addr, res->ai_addrlen);
+
+ if (!hostname) {
+ hostname = res->ai_canonname ? strdup(res->ai_canonname) : dest;
if (!hostname)
errx(1, "malloc");
+ }
- if (res->ai_next) {
- if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
- sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
- strlcpy(hbuf, "?", sizeof(hbuf));
- warnx("Warning: %s has multiple "
- "addresses; using %s\n", hostname, hbuf);
- }
+ if (res->ai_next) {
+ if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
+ sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
+ strlcpy(hbuf, "?", sizeof(hbuf));
+ warnx("Warning: %s has multiple "
+ "addresses; using %s\n", hostname, hbuf);
}
+ freeaddrinfo(res);
+
if (*++argv) {
errno = 0;
ep = NULL;