Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
59:e0e556c8bd46
Parent:
57:e0fb648acf48
--- a/udp/dns/dnsserver.cpp	Thu Dec 07 20:44:32 2017 +0000
+++ b/udp/dns/dnsserver.cpp	Thu Dec 14 20:55:40 2017 +0000
@@ -52,7 +52,7 @@
         }
         
         //Bomb out if we are tending to overrun the buffer
-        if (p - DnsHdrPacket > 512)
+        if (p - DnsHdrPacket > DnsHdrDataLength)
         {
             if (DnsServerTrace) LogTimeF("DnsServer-readQuestions - overrunning the buffer\r\n");
             return -1;
@@ -90,16 +90,13 @@
     return 0;
 }
 static int addAnswers(int dnsProtocol)
-{
-    //Strip the questions if this is MDNS
-    //if (dnsProtocol == DNS_PROTOCOL_MDNS) p = DnsHdrData; 
-    
+{    
     //Go through each answer
     DnsHdrAncount = 0;
     for (int i = 0; i < answerCount; i++)
     {                
         //Bomb out if we are tending to overrun the buffer
-        if (p - DnsHdrPacket > 512)
+        if (p - DnsHdrPacket > DnsHdrDataLength)
         {
             if (DnsServerTrace) LogTimeF("DnsServer-addAnswers - reply is getting too big\r\n");
             return -1;
@@ -164,19 +161,8 @@
     return 0;
 }
 
-int DnsServerHandleQuery(void (*traceback)(void), int dnsProtocol, int *pSize) //Received an mdns or llmnr query on port 5353 or 5355
-{    
-    if (*pSize > 512)
-    {
-        if (DnsServerTrace || NetTraceHostGetMatched())
-        {
-            if (NetTraceNewLine) Log("\r\n");
-            LogTimeF("DnsServer length %d too long\r\n",  *pSize );
-            if (NetTraceStack) traceback();
-        }
-        return DO_NOTHING;
-    }
-    
+int DnsServerHandleQuery(void (*traceback)(void), int dnsProtocol, void* pPacketTx, int *pSizeTx) //Received an mdns or llmnr query on port 5353 or 5355
+{            
     myFullName4Length = DnsMakeFullNameFromName(dnsProtocol, NetName4, sizeof(myFullName4), myFullName4);
     myFullName6Length = DnsMakeFullNameFromName(dnsProtocol, NetName6, sizeof(myFullName6), myFullName6);
     
@@ -191,13 +177,40 @@
         DnsHdrLog(dnsProtocol);
     }
     
+    char* pRx = DnsHdrData;
+    char* pEndRx = p;
+    int qdcount = DnsHdrQdcount;
+    int nscount = DnsHdrNscount;
+    int arcount = DnsHdrArcount;
+    
+    DnsHdrSetup(pPacketTx, *pSizeTx);
+    p = DnsHdrData; 
+    
+    //Add the questions if this is not MDNS
+    if (dnsProtocol == DNS_PROTOCOL_MDNS)
+    {
+        DnsHdrQdcount = 0;
+        DnsHdrNscount = 0;
+        DnsHdrArcount = 0;
+    }
+    else
+    {
+        DnsHdrQdcount = qdcount;
+        DnsHdrNscount = nscount;
+        DnsHdrArcount = arcount;
+        while (pRx < pEndRx) *p++ = *pRx++;
+    }
+    
+
     if (addAnswers(dnsProtocol)) return DO_NOTHING;
     
-    DnsHdrIsReply         = true;
-    DnsHdrIsAuthoritative = true;
+    DnsHdrIsReply          = true;
+    DnsHdrIsAuthoritative  = false;
+    DnsHdrIsRecursiveQuery = false;
+    
     DnsHdrWrite();
     
-    *pSize = p - DnsHdrPacket;
+    *pSizeTx = p - DnsHdrPacket;
     
     if (DnsServerTrace || NetTraceHostGetMatched()) DnsHdrLog(dnsProtocol);