[PATCH v2 1/1] ping: Add support for IPv4-mapped IPv6 address format

Petr Vorel petr.vorel at gmail.com
Fri Oct 18 18:51:49 UTC 2024


IP addresses with ::ffff: prefix are IPv6 addresses which are mapped to
IPv4. Therefore ping resulting IPv4 address.

    # ./busybox ping -c1 -v ::ffff:127.0.0.1
    IPv4-Mapped-in-IPv6 address, using IPv4 127.0.0.1
    PING 127.0.0.1 (127.0.0.1): 56 data bytes
    64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.085 ms

Cc: Tj <linux at iam.tj>
Signed-off-by: Petr Vorel <petr.vorel at gmail.com>
---
Changes v1->v2:
* Add example in the commit message.

 networking/ping.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/networking/ping.c b/networking/ping.c
index b7e6955a9..7ed58f9d6 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -986,6 +986,14 @@ static int common_ping_main(int opt, char **argv)
 			af = AF_INET;
 		if (opt & OPT_IPV6)
 			af = AF_INET6;
+
+		struct in6_addr a6;
+		if (inet_pton(AF_INET6, hostname, &a6) && IN6_IS_ADDR_V4MAPPED(&a6)) {
+			hostname = strrchr(hostname, ':') + 1;
+			af = AF_INET;
+			printf("IPv4-Mapped-in-IPv6 address, using IPv4 %s\n", hostname);
+		}
+
 		lsa = xhost_and_af2sockaddr(hostname, 0, af);
 	}
 #else
-- 
2.45.2



More information about the busybox mailing list