From e6e9fbeaff16ecd29d1d701b276551bd868179c4 Mon Sep 17 00:00:00 2001 From: Petr Vorel Date: Fri, 18 Oct 2024 20:48:08 +0200 Subject: [PATCH] ping: Add support for IPv4-mapped IPv6 address format IP addresses with ::ffff: prefix are IPv6 addresses which are mapped to IPv4. Therefore ping resulting IPv4 address. $ ./src/fping -c1 ::ffff:127.0.0.1 IPv4-Mapped-in-IPv6 address, using IPv4 127.0.0.1 [<- 127.0.0.1]127.0.0.1 : [0], 64 bytes, 0.066 ms (0.066 avg, 0% loss) Signed-off-by: Petr Vorel --- ci/test-01-basics.pl | 14 +++++++++++++- src/fping.c | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ci/test-01-basics.pl b/ci/test-01-basics.pl index e54bc225..acfc6aea 100755 --- a/ci/test-01-basics.pl +++ b/ci/test-01-basics.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -w -use Test::Command tests => 15; +use Test::Command tests => 18; use Test::More; # ping 127.0.0.1 @@ -35,6 +35,18 @@ $cmd->stderr_like(qr{ \[<- .*\]}); } +# ping ::ffff:127.0.0.1 +SKIP: { + if($ENV{SKIP_IPV6}) { + skip 'Skip IPv6 tests', 3; + } + my $cmd = Test::Command->new(cmd => "fping ::ffff:127.0.0.1"); + $cmd->exit_is_num(0); + $cmd->stdout_is_eq("IPv4-Mapped-in-IPv6 address, using IPv4 127.0.0.1 +127.0.0.1 is alive\n"); + $cmd->stderr_like(qr{ \[<- 127.0.0.1\]}); +} + # ping 3 times 127.0.0.1 { my $cmd = Test::Command->new(cmd => "fping -p 100 -C3 127.0.0.1"); diff --git a/src/fping.c b/src/fping.c index 87b5fb9e..d8c6a56d 100644 --- a/src/fping.c +++ b/src/fping.c @@ -1208,6 +1208,14 @@ int main(int argc, char **argv) * for each of them */ for (cursor = event_queue_ping.first; cursor; cursor = cursor->ev_next) { table[i] = cursor->host; + + struct in6_addr a6; + if (inet_pton(AF_INET6, cursor->host->host, &a6) && IN6_IS_ADDR_V4MAPPED(&a6)) { + cursor->host->host = strrchr(cursor->host->host, ':') + 1; + cursor->host->saddr.ss_family = AF_INET; + printf("IPv4-Mapped-in-IPv6 address, using IPv4 %s\n", cursor->host->host); + } + cursor->host->i = i; i++; }