]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
Implement ASN lookups in well-known nat64 prefix 476/head
authorEvan Pratten <ewpratten@gmail.com>
Wed, 31 May 2023 00:22:48 +0000 (20:22 -0400)
committerEvan Pratten <ewpratten@gmail.com>
Wed, 31 May 2023 00:22:48 +0000 (20:22 -0400)
ui/asn.c

index 718f4a030c3a823f595a32b7ca119b3271670834..111c3942dc55d907720e523deda518e355f8b924 100644 (file)
--- a/ui/asn.c
+++ b/ui/asn.c
@@ -21,6 +21,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <sys/types.h>
 #ifdef HAVE_ERROR_H
 #include <error.h>
@@ -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