From: Evan Pratten Date: Wed, 31 May 2023 00:22:48 +0000 (-0400) Subject: Implement ASN lookups in well-known nat64 prefix X-Git-Tag: v0.96~24^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91035185dd36fa721db5fee813e020f966403ceb;p=thirdparty%2Fmtr.git Implement ASN lookups in well-known nat64 prefix --- diff --git a/ui/asn.c b/ui/asn.c index 718f4a0..111c394 100644 --- a/ui/asn.c +++ b/ui/asn.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #ifdef HAVE_ERROR_H #include @@ -215,6 +216,13 @@ static void reverse_host6( } #endif +#ifdef ENABLE_IPV6 +static bool is_well_known_nat64(struct in6_addr *addr){ + // 64:ff9b:: + return addr->s6_addr[0] == 0x00 && addr->s6_addr[1] == 0x64 && addr->s6_addr[2] == 0xff && addr->s6_addr[3] == 0x9b; +} +#endif + static char *get_ipinfo( struct mtr_ctl *ctl, ip_t * addr) @@ -229,10 +237,23 @@ static char *get_ipinfo( if (ctl->af == AF_INET6) { #ifdef ENABLE_IPV6 - reverse_host6(addr, key, NAMELEN); - if (snprintf(lookup_key, NAMELEN, "%s.%s", key, ctl->ipinfo_provider6) - >= NAMELEN) - return NULL; + if (is_well_known_nat64(addr)) { + // Treats the final 4 bytes as IPv4 address + unsigned char buff[4]; + memcpy(buff, addr->s6_addr + 12, 4); + if (snprintf + (key, NAMELEN, "%d.%d.%d.%d", buff[3], buff[2], buff[1], + buff[0]) >= NAMELEN) + return NULL; + if (snprintf(lookup_key, NAMELEN, "%s.%s", key, ctl->ipinfo_provider4) + >= NAMELEN) + return NULL; + } else { + reverse_host6(addr, key, NAMELEN); + if (snprintf(lookup_key, NAMELEN, "%s.%s", key, ctl->ipinfo_provider6) + >= NAMELEN) + return NULL; + } #else return NULL; #endif