]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
Included apple ASN fix master
authorR.E. Wolff <R.E.Wolff@BitWizard.nl>
Tue, 2 Dec 2025 07:35:16 +0000 (08:35 +0100)
committerR.E. Wolff <R.E.Wolff@BitWizard.nl>
Tue, 2 Dec 2025 07:35:16 +0000 (08:35 +0100)
ui/asn.c
ui/split.c

index 28f5cd30149fb7ae5984156fef14dfdb3360ae43..f05183277f5f2ca4760678b3a40ee709e72716f5 100644 (file)
--- a/ui/asn.c
+++ b/ui/asn.c
 #endif
 #include <errno.h>
 
 #endif
 #include <errno.h>
 
+#ifdef __APPLE__
+#define BIND_8_COMPAT
+#include <dns_sd.h>
+#include <sys/select.h>
+#endif
 #include <arpa/nameser.h>
 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
 #include <arpa/nameser_compat.h>
 #include <arpa/nameser.h>
 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
 #include <arpa/nameser_compat.h>
@@ -70,10 +75,110 @@ static items_t items_a;         /* without hash: items */
 static char txtrec[NAMELEN + 1];        /* without hash: txtrec */
 static items_t *items = &items_a;
 
 static char txtrec[NAMELEN + 1];        /* without hash: txtrec */
 static items_t *items = &items_a;
 
+#ifdef __APPLE__
+typedef struct query_context {
+    char txt[NAMELEN + 1];
+    bool more;
+} query_context_t;
+
+static void query_callback(
+    DNSServiceRef sdRef,
+    DNSServiceFlags flags,
+    uint32_t interfaceIndex,
+    DNSServiceErrorType errorCode,
+    const char *fullname,
+    uint16_t rrtype,
+    uint16_t rrclass,
+    uint16_t rdlen,
+    const void *rdata,
+    uint32_t ttl,
+    void *context
+) {
+    struct query_context *q_context = (struct query_context *)context;
+
+    if (errorCode == kDNSServiceErr_NoError && rdlen > 1) {
+        memset(q_context->txt, 0, sizeof(q_context->txt));
+        int remain = sizeof(q_context->txt) - 1;
+        char *p = q_context->txt;
+        const unsigned char *ptr = rdata;
+        const unsigned char *end = ptr + rdlen;
+
+        while (ptr < end) {
+            int len = *ptr++;
+            if (ptr + len > end) {
+                break;
+            }
+            memcpy(p, ptr, (len < remain) ? len : remain);
+            p += len;
+            ptr += len;
+            remain -= len;
+            if (remain <= 0) break;
+        }
+    }
+    q_context->more = (flags & kDNSServiceFlagsMoreComing) && (errorCode == kDNSServiceErr_NoError);
+}
+#endif
 
 static char *ipinfo_lookup(
     const char *domain)
 {
 
 static char *ipinfo_lookup(
     const char *domain)
 {
+#ifdef __APPLE__
+    DNSServiceRef serviceRef;
+    struct query_context context = {};
+    DNSServiceErrorType error;
+
+    error = DNSServiceQueryRecord(&serviceRef, 0, 0, domain,
+                                  kDNSServiceType_TXT, kDNSServiceClass_IN,
+                                  query_callback, &context);
+
+    if (error != kDNSServiceErr_NoError) {
+        fprintf(stderr, "mtr: DNSServiceQueryRecord failed: %d\n", error);
+        return xstrdup(UNKN);
+    }
+
+    int dns_sd_fd = DNSServiceRefSockFD(serviceRef);
+    if (dns_sd_fd < 0) {
+        fprintf(stderr, "mtr: DNSServiceRefSockFD failed\n");
+        DNSServiceRefDeallocate(serviceRef);
+        return xstrdup(UNKN);
+    }
+
+    do {
+        fd_set rdfds;
+        struct timeval tv;
+        int result;
+
+        FD_ZERO(&rdfds);
+        FD_SET(dns_sd_fd, &rdfds);
+
+        tv.tv_sec = 1;
+        tv.tv_usec = 0;
+
+        result = select(dns_sd_fd + 1, &rdfds, NULL, NULL, &tv);
+
+        if (result > 0) {
+            if (FD_ISSET(dns_sd_fd, &rdfds)) {
+                DNSServiceProcessResult(serviceRef);
+            }
+        } else {
+            break;
+        }
+    } while (context.more);
+
+    DNSServiceRefDeallocate(serviceRef);
+
+    if (context.txt[0]) {
+        if (iihash) {
+            DEB_syslog(LOG_INFO, "Malloc-txt(%p): %s", context.txt, context.txt);
+            return strdup(context.txt);
+        } else {
+            xstrncpy(txtrec, context.txt, sizeof(txtrec));
+            return txtrec;
+        }
+    }
+
+    return xstrdup(UNKN);
+#else
     unsigned char answer[PACKETSZ], *pt;
     char host[128];
     char *txt;
     unsigned char answer[PACKETSZ], *pt;
     char host[128];
     char *txt;
@@ -150,6 +255,7 @@ static char *ipinfo_lookup(
         DEB_syslog(LOG_INFO, "Malloc-txt(%p): %s", txt, txt);
 
     return txt;
         DEB_syslog(LOG_INFO, "Malloc-txt(%p): %s", txt, txt);
 
     return txt;
+#endif
 }
 
 /* originX.asn.cymru.com txtrec:    ASN | Route | Country | Registry | Allocated */
 }
 
 /* originX.asn.cymru.com txtrec:    ASN | Route | Country | Registry | Allocated */
index f8c250c37ca0e7643be93d4ada123a1c11744664..a22131813125e51738f66e36a351fde3029ed72e 100644 (file)
@@ -110,18 +110,19 @@ void split_redraw(
             snprintf(newLine, sizeof(newLine), "???");
         }
 
             snprintf(newLine, sizeof(newLine), "???");
         }
 
-        if (strcmp(newLine, Lines[at]) == 0) {
-            /* The same, so do nothing */
-#if DEBUG
-            printf("SAME LINE\n");
-#endif
-        } else {
+        if (strcmp(newLine, Lines[at]) != 0) {
+           // something changed, so we print it.
             printf("%d %s\n", at + 1, newLine);
             fflush(stdout);
             xstrncpy(Lines[at], newLine, MAX_LINE_SIZE);
             if (LineCount < (at + 1)) {
                 LineCount = at + 1;
             }
             printf("%d %s\n", at + 1, newLine);
             fflush(stdout);
             xstrncpy(Lines[at], newLine, MAX_LINE_SIZE);
             if (LineCount < (at + 1)) {
                 LineCount = at + 1;
             }
+        } else {
+            /* The same, so do nothing */
+#if DEBUG
+            printf("SAME LINE\n");
+#endif
         }
     }
 }
         }
     }
 }