diff options
author | Florian Obser <florian@cvs.openbsd.org> | 2016-09-17 09:31:05 +0000 |
---|---|---|
committer | Florian Obser <florian@cvs.openbsd.org> | 2016-09-17 09:31:05 +0000 |
commit | 3ffdfc25af055da68bccd2e436727639dc640f29 (patch) | |
tree | 6f93656986f20158eb99dabff147218e81d7cdfc | |
parent | e1b11cec4b2f7fe3ccdd65f8ac6356f246164c89 (diff) |
Make source address selection more AF independent.
-rw-r--r-- | sbin/ping/ping.c | 31 | ||||
-rw-r--r-- | sbin/ping6/ping6.c | 19 |
2 files changed, 21 insertions, 29 deletions
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c index fcff9d60551..f0e91efb6bc 100644 --- a/sbin/ping/ping.c +++ b/sbin/ping/ping.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ping.c,v 1.196 2016/09/17 09:30:26 florian Exp $ */ +/* $OpenBSD: ping.c,v 1.197 2016/09/17 09:31:04 florian Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -392,7 +392,7 @@ main(int argc, char *argv[]) if (inet_aton(*argv, &dst4.sin_addr) != 0) { hostname = *argv; if ((target = strdup(inet_ntoa(dst4.sin_addr))) == NULL) - errx(1, "malloc"); + err(1, "malloc"); } else target = *argv; @@ -436,27 +436,26 @@ main(int argc, char *argv[]) freeaddrinfo(res); if (source) { - memset(&from4, 0, sizeof(from4)); - from4.sin_family = AF_INET; - if (inet_aton(source, &from4.sin_addr) == 0) { - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_DGRAM; /*dummy*/ - if ((error = getaddrinfo(source, NULL, &hints, &res))) - errx(1, "%s: %s", source, gai_strerror(error)); - if (res->ai_addrlen != sizeof(from4)) - errx(1, "size of sockaddr mismatch"); - memcpy(&from4, res->ai_addr, res->ai_addrlen); - freeaddrinfo(res); + if (inet_aton(source, &from4.sin_addr) != 0) { + if ((source = strdup(inet_ntoa(from4.sin_addr))) == + NULL) + err(1, "malloc"); } + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; + if ((error = getaddrinfo(source, NULL, &hints, &res))) + errx(1, "%s: %s", source, gai_strerror(error)); + if (res->ai_addrlen != sizeof(from4)) + errx(1, "size of sockaddr mismatch"); + memcpy(from, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); if (IN_MULTICAST(ntohl(dst4.sin_addr.s_addr))) { if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &from4.sin_addr, sizeof(from4.sin_addr)) < 0) err(1, "setsockopt IP_MULTICAST_IF"); } else { - if (bind(s, (struct sockaddr *)&from4, sizeof(from4)) - < 0) + if (bind(s, from, from->sa_len) < 0) err(1, "bind"); } } diff --git a/sbin/ping6/ping6.c b/sbin/ping6/ping6.c index 833271924fc..a42d8872611 100644 --- a/sbin/ping6/ping6.c +++ b/sbin/ping6/ping6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ping6.c,v 1.212 2016/09/17 09:30:26 florian Exp $ */ +/* $OpenBSD: ping6.c,v 1.213 2016/09/17 09:31:04 florian Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -417,23 +417,16 @@ main(int argc, char *argv[]) freeaddrinfo(res); if (source) { - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */ + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; - hints.ai_socktype = SOCK_RAW; - hints.ai_protocol = IPPROTO_ICMPV6; - - error = getaddrinfo(source, NULL, &hints, &res); - if (error) - errx(1, "invalid source address: %s", - gai_strerror(error)); - + if ((error = getaddrinfo(source, NULL, &hints, &res))) + errx(1, "%s: %s", source, gai_strerror(error)); if (res->ai_family != AF_INET6 || res->ai_addrlen != sizeof(from6)) errx(1, "invalid source address"); - memcpy(&from6, res->ai_addr, sizeof(from6)); + memcpy(from, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); - if (bind(s, (struct sockaddr *)&from6, sizeof(from6)) != 0) + if (bind(s, from, from->sa_len) < 0) err(1, "bind"); } |