From c1bdf8c4cfe424836b320fd17d5ff9fc8918d778 Mon Sep 17 00:00:00 2001 From: gubaojian Date: Thu, 26 Jun 2025 16:57:30 +0800 Subject: [PATCH] check the prefix convert ip6 back to ip4 if ip is ip4 --- src/AsyncSocket.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index 5b6c64747..9531538d0 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -26,6 +26,7 @@ #include #include +#include #include "libusockets.h" @@ -212,6 +213,12 @@ struct AsyncSocket { ipLength = snprintf(buf, 64, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]); + // ip4 127.0.0.1 be converted to ip6 0000:0000:0000:0000:0000:ffff:7f00:0001 + // following code will check the prefix convert ip6 back to ip4 if ip is ip4 + const char* IPV6_IPV4_MAPPED_PREFIX="0000:0000:0000:0000:0000:ffff:"; + if (0 == memcmp(buf, IPV6_IPV4_MAPPED_PREFIX, strlen(IPV6_IPV4_MAPPED_PREFIX))) { + ipLength = snprintf(buf, 64, "%u.%u.%u.%u", b[12], b[13], b[14], b[15]); + } } return {buf, (unsigned int) ipLength};