Bonjour/mDNS lib, works with the new network stack

Fork of BonjourLib by Dirk-Willem van Gulik (NXP/mbed)

Revision:
3:b8a78d6da192
Parent:
2:18d443d86057
--- a/mDNSResponder.cpp	Fri May 30 09:06:53 2014 +0000
+++ b/mDNSResponder.cpp	Fri May 30 09:43:56 2014 +0000
@@ -47,6 +47,7 @@
 
 #include "mDNSResponder.h"
 #include <stdio.h>
+#include <ctype.h>
 
 #ifndef DNS_RRTYPE_SRV
 #define DNS_RRTYPE_SRV (33)
@@ -56,6 +57,57 @@
 #define DNS_LOCAL "local"
 #endif
 
+void pretty_print_block(char *b, int len)
+{
+    int x, y, indent, count = 0;
+
+    indent = 16; /* whatever */
+
+    printf("\n\r");
+
+    while (count < len)
+    {
+        printf("%04x : ", count);
+        for (x = 0 ; x < indent ; x++)
+        {
+            printf("%02x ", b[x + count]);
+            if ((x + count + 1) >= len)
+            {
+                x++;
+                for (y = 0 ; y < (indent - x) ; y++)
+                    printf("   ");
+                break;
+            }
+        }
+        printf(": ");
+
+        for (x = 0 ; x < indent ; x++)
+        {
+            if (isprint(b[x + count]))
+                putchar(b[x + count]);
+            else if (b[x + count] == 0)
+                putchar(' ');
+            else
+                putchar('.');
+
+            if ((x + count + 1) >= len)
+            {
+                x++;
+                for (y = 0 ; y < (indent - x) ; y++)
+                    putchar(' ');
+                break;
+            }
+        }
+        putchar('\n');
+        putchar('\r');
+        count += indent;
+    }
+    putchar('\n');
+    putchar('\r');
+}
+
+
+
 void announcer(void const *args) {
     printf("in announcer\r\n");
     mDNSResponder *m = (mDNSResponder *)args;
@@ -324,6 +376,7 @@
     return qANY(p,name,DNS_RRTYPE_SRV,DNS_RRCLASS_IN);
 }
 
+/* This si wrong, i think we're answering our own question? or something? */
 void mDNSResponder::sendAnnounce(void) {
     char out[ 1500 ], *p;
     DNSPacket * op = (DNSPacket *)out;
@@ -342,6 +395,8 @@
 
     p = qPTR(p, moi_local_proto);
 
+    printf("sendAnnounce1\r\n");
+
     m_pUDPSocket->sendTo(quack, out, p-out);
 
     initLabelCache(out); // Offsets are relative to the ID header.
@@ -361,7 +416,7 @@
 
     p = mARR(p, moi_local_name, moi_ip);
     
-    printf("sendAnnounce\r\n");
+    printf("sendAnnounce2\r\n");
     
     m_pUDPSocket->sendTo(quack, out, p-out);
     
@@ -370,7 +425,8 @@
 void mDNSResponder::sendReply(uint16_t t, uint16_t tid, Endpoint *dst) {
     // sent a reply - basically a precooked A/PTR/SRV with the TransactionID
     // and my URI squeezed in.
-    char out[ 1500 ], *p;
+    char *out, *p;
+    out = space;
     DNSPacket * op = (DNSPacket *) out;
 
     initLabelCache(out); // Offsets are relative to the ID header.
@@ -395,7 +451,7 @@
 
             break;
         case DNS_RRTYPE_A:    // A record (and nothing else)
-            op->aa_count = ntohs(1);
+            op->aa_count = ntohs(0);
             p = qA(p,moi_local_name);
             break;
 
@@ -408,7 +464,7 @@
                 p = mTXT(p,moi_local_pglue,moi_txt);
             break;        
     }
-    p = mARR(p,moi_local_name,moi_ip);
+    p = mARR(p, moi_local_name, moi_ip);
     
     printf("sendReply\r\n");
 
@@ -417,6 +473,8 @@
     }
     Endpoint quack;
     quack.set_address(dst->get_address(), dst->get_port());
+
+    pretty_print_block(out, p - out);
     
     m_pUDPSocket->sendTo(quack, out, p-out);
 }
@@ -492,6 +550,9 @@
     mDNSResponder::sendAnnounce();
 }
 
+/*
+ Do we support _services._dns-sd._udp.local as well??!?
+*/
 void mDNSResponder::Listen(void const *args) {
     Endpoint from;
     int len;