From a1751adda6854f8f586802773cac3c7a7b1fa136 Mon Sep 17 00:00:00 2001
From: Jens Nielsen <deft@telia.com>
Date: Sat, 27 Jan 2018 16:43:06 +0100
Subject: [PATCH 3/3] Populate MDNS probes authoritative section

---
 src/apps/mdns/mdns.c | 65 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 44 insertions(+), 21 deletions(-)

diff --git a/src/apps/mdns/mdns.c b/src/apps/mdns/mdns.c
index 1a25946b..ef5ac697 100644
--- a/src/apps/mdns/mdns.c
+++ b/src/apps/mdns/mdns.c
@@ -1303,6 +1303,7 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
   err_t res = ERR_ARG;
   int i;
   struct mdns_host *mdns = NETIF_TO_HOST(outpkt->netif);
+  u16_t answers = 0;
 
   /* Write answers to host questions */
 #if LWIP_IPV4
@@ -1311,14 +1312,14 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
     if (res != ERR_OK) {
       goto cleanup;
     }
-    outpkt->answers++;
+    answers++;
   }
   if (outpkt->host_replies & REPLY_HOST_PTR_V4) {
     res = mdns_add_hostv4_ptr_answer(outpkt, outpkt->cache_flush, outpkt->netif);
     if (res != ERR_OK) {
       goto cleanup;
     }
-    outpkt->answers++;
+    answers++;
   }
 #endif
 #if LWIP_IPV6
@@ -1330,7 +1331,7 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
         if (res != ERR_OK) {
           goto cleanup;
         }
-        outpkt->answers++;
+        answers++;
       }
     }
   }
@@ -1343,7 +1344,7 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
         if (res != ERR_OK) {
           goto cleanup;
         }
-        outpkt->answers++;
+        answers++;
       }
       addrindex++;
       rev_addrs >>= 1;
@@ -1363,7 +1364,7 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
       if (res != ERR_OK) {
         goto cleanup;
       }
-      outpkt->answers++;
+      answers++;
     }
 
     if (outpkt->serv_replies[i] & REPLY_SERVICE_NAME_PTR) {
@@ -1371,7 +1372,7 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
       if (res != ERR_OK) {
         goto cleanup;
       }
-      outpkt->answers++;
+      answers++;
     }
 
     if (outpkt->serv_replies[i] & REPLY_SERVICE_SRV) {
@@ -1379,7 +1380,7 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
       if (res != ERR_OK) {
         goto cleanup;
       }
-      outpkt->answers++;
+      answers++;
     }
 
     if (outpkt->serv_replies[i] & REPLY_SERVICE_TXT) {
@@ -1387,10 +1388,17 @@ mdns_send_outpacket(struct mdns_outpacket *outpkt, u8_t flags)
       if (res != ERR_OK) {
         goto cleanup;
       }
-      outpkt->answers++;
+      answers++;
     }
   }
 
+  /* if this is a response, the data above is anwers, else this is a probe and the answers above goes into auth section */
+  if ( flags & DNS_FLAG1_RESPONSE ) {
+    outpkt->answers += answers;
+  } else {
+    outpkt->authoritative += answers;
+  }
+
   /* All answers written, add additional RRs */
   for (i = 0; i < MDNS_MAX_SERVICES; ++i) {
     service = mdns->services[i];
@@ -1493,32 +1501,28 @@ cleanup:
   return res;
 }
 
-
 /**
- * Send unsolicited answer containing all our known data
+ * Set flags to indicate that we want to send answers for all our known data.
+ * This is common for unsolicited announce answers and probe queries
  * @param netif The network interface to send on
- * @param destination The target address to send to (usually multicast address)
+ * @param pkt The outgoing packet
  */
 static void
-mdns_announce(struct netif *netif, const ip_addr_t *destination)
+mdns_packet_set_known_rdata(struct netif *netif, struct mdns_outpacket *pkt)
 {
-  struct mdns_outpacket announce;
   int i;
   struct mdns_host *mdns = NETIF_TO_HOST(netif);
 
-  memset(&announce, 0, sizeof(announce));
-  announce.netif = netif;
-  announce.cache_flush = 1;
 #if LWIP_IPV4
   if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
-    announce.host_replies = REPLY_HOST_A | REPLY_HOST_PTR_V4;
+    pkt->host_replies = REPLY_HOST_A | REPLY_HOST_PTR_V4;
   }
 #endif
 #if LWIP_IPV6
   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; ++i) {
     if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
-      announce.host_replies |= REPLY_HOST_AAAA | REPLY_HOST_PTR_V6;
-      announce.host_reverse_v6_replies |= (1 << i);
+      pkt->host_replies |= REPLY_HOST_AAAA | REPLY_HOST_PTR_V6;
+      pkt->host_reverse_v6_replies |= (1 << i);
     }
   }
 #endif
@@ -1526,10 +1530,26 @@ mdns_announce(struct netif *netif, const ip_addr_t *destination)
   for (i = 0; i < MDNS_MAX_SERVICES; i++) {
     struct mdns_service *serv = mdns->services[i];
     if (serv) {
-      announce.serv_replies[i] = REPLY_SERVICE_TYPE_PTR | REPLY_SERVICE_NAME_PTR |
-                                 REPLY_SERVICE_SRV | REPLY_SERVICE_TXT;
+      pkt->serv_replies[i] = REPLY_SERVICE_TYPE_PTR | REPLY_SERVICE_NAME_PTR |
+                             REPLY_SERVICE_SRV | REPLY_SERVICE_TXT;
     }
   }
+}
+/**
+ * Send unsolicited answer containing all our known data
+ * @param netif The network interface to send on
+ * @param destination The target address to send to (usually multicast address)
+ */
+static void
+mdns_announce(struct netif *netif, const ip_addr_t *destination)
+{
+  struct mdns_outpacket announce;
+
+  memset(&announce, 0, sizeof(announce));
+  announce.netif = netif;
+  announce.cache_flush = 1;
+
+  mdns_packet_set_known_rdata(netif, &announce);
 
   announce.dest_port = LWIP_IANA_PORT_MDNS;
   SMEMCPY(&announce.dest_addr, destination, sizeof(announce.dest_addr));
@@ -1969,7 +1989,10 @@ mdns_send_probe(struct netif* netif, const ip_addr_t *destination)
     pkt.questions++;
   }
 
+  mdns_packet_set_known_rdata(netif, &pkt);
+
   pkt.tx_id = 0;
+  pkt.dest_port = LWIP_IANA_PORT_MDNS;
   SMEMCPY(&pkt.dest_addr, destination, sizeof(pkt.dest_addr));
   res = mdns_send_outpacket(&pkt, 0);
 
-- 
2.12.2.windows.2

