Skip to content

Commit

Permalink
ping: support IPv4-Mapped-in-IPv6 target addresses
Browse files Browse the repository at this point in the history
Addresses of the form ::ffff:127.0.0.1 would be treated as IPv6. Check
the target early and switch to IPv4 path if found.

    $ ./builddir/ping/ping -c1 -v ::ffff:127.0.0.1
    ./builddir/ping/ping: IPv4-Mapped-in-IPv6 address, using IPv4 127.0.0.1
    ./builddir/ping/ping: sock4.fd: 3 (socktype: SOCK_DGRAM), sock6.fd: -1 (socktype: 0), hints.ai_family: AF_INET

    ai->ai_family: AF_INET, ai->ai_canonname: '127.0.0.1'
    PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
    64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.058 ms

Closes: iputils#558
Reviewed-by: Petr Vorel <[email protected]>
Signed-off-by: Tj <[email protected]>
[ pvorel: use _() to translate,  error() to redirect to stderr ]
Signed-off-by: Petr Vorel <[email protected]>
  • Loading branch information
Tj authored and pevik committed Oct 18, 2024
1 parent dca2358 commit 8ed7ffc
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions ping/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ main(int argc, char **argv)
.ni.subject_type = -1,
};
unsigned char buf[sizeof(struct in6_addr)];
struct in6_addr a6;

/* FIXME: global_rts will be removed in future */
global_rts = &rts;
Expand Down Expand Up @@ -620,6 +621,14 @@ main(int argc, char **argv)
hints.ai_socktype = SOCK_RAW;
}

if (inet_pton(AF_INET6, target, &a6) && IN6_IS_ADDR_V4MAPPED(&a6)) {
target = strrchr(target, ':') + 1;
hints.ai_family = AF_INET;

if (rts.opt_verbose)
error(0, 0, _("IPv4-Mapped-in-IPv6 address, using IPv4 %s"), target);
}

if (hints.ai_family != AF_INET6) {
create_socket(&rts, &sock4, AF_INET, hints.ai_socktype, IPPROTO_ICMP,
hints.ai_family == AF_INET);
Expand Down

0 comments on commit 8ed7ffc

Please sign in to comment.