From f2680f189a2edf3295b148e322ae9fefcb9927ca Mon Sep 17 00:00:00 2001
From: Jasper Verschueren <jasper.verschueren@apart-audio.com>
Date: Thu, 8 Nov 2018 16:55:54 +0100
Subject: [PATCH 1/2] mDNS: split probe packet definition and split outpkt
 creation

Probe packet creation -> by splitting the definition and the sending
we can use the definition function for probe tiebreaking.
outpkt creation -> by splitting the creation and sending we can use
the creation function for probe tiebreaking.
---
 src/apps/mdns/mdns.c             |  36 +++++------
 src/apps/mdns/mdns_out.c         | 100 ++++++++++++++++++-------------
 src/include/lwip/apps/mdns_out.h |   2 +
 3 files changed, 81 insertions(+), 57 deletions(-)

diff --git a/src/apps/mdns/mdns.c b/src/apps/mdns/mdns.c
index 83e64f988..adaaafae1 100644
--- a/src/apps/mdns/mdns.c
+++ b/src/apps/mdns/mdns.c
@@ -1303,39 +1303,35 @@ mdns_netif_ext_status_callback(struct netif *netif, netif_nsc_reason_t reason, c
 }
 #endif /* LWIP_NETIF_EXT_STATUS_CALLBACK && MDNS_RESP_USENETIF_EXTCALLBACK */
 
-static err_t
-mdns_send_probe(struct netif* netif, const ip_addr_t *destination)
+static void
+mdns_define_probe_rrs_to_send(struct netif *netif, struct mdns_outmsg *outmsg)
 {
-  struct mdns_host* mdns;
-  struct mdns_outmsg outmsg;
-  u8_t i;
-  err_t res;
-
-  mdns = NETIF_TO_HOST(netif);
+  struct mdns_host *mdns = NETIF_TO_HOST(netif);
+  int i;
 
-  memset(&outmsg, 0, sizeof(outmsg));
+  memset(outmsg, 0, sizeof(struct mdns_outmsg));
 
   /* Add unicast questions with rtype ANY for all our desired records */
-  outmsg.host_questions = QUESTION_PROBE_HOST_ANY;
+  outmsg->host_questions = QUESTION_PROBE_HOST_ANY;
 
   for (i = 0; i < MDNS_MAX_SERVICES; i++) {
     struct mdns_service* service = mdns->services[i];
     if (!service) {
       continue;
     }
-    outmsg.serv_questions[i] = QUESTION_PROBE_SERVICE_NAME_ANY;
+    outmsg->serv_questions[i] = QUESTION_PROBE_SERVICE_NAME_ANY;
   }
 
   /* Add answers to the questions above into the authority section for tiebreaking */
 #if LWIP_IPV4
   if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
-    outmsg.host_replies = REPLY_HOST_A;
+    outmsg->host_replies = REPLY_HOST_A;
   }
 #endif
 #if LWIP_IPV6
   for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
     if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
-      outmsg.host_replies |= REPLY_HOST_AAAA;
+      outmsg->host_replies |= REPLY_HOST_AAAA;
     }
   }
 #endif
@@ -1343,16 +1339,22 @@ mdns_send_probe(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) {
-      outmsg.serv_replies[i] = REPLY_SERVICE_SRV | REPLY_SERVICE_TXT;
+      outmsg->serv_replies[i] = REPLY_SERVICE_SRV | REPLY_SERVICE_TXT;
     }
   }
+}
+
+static err_t
+mdns_send_probe(struct netif* netif, const ip_addr_t *destination)
+{
+  struct mdns_outmsg outmsg;
+
+  mdns_define_probe_rrs_to_send(netif, &outmsg);
 
   outmsg.tx_id = 0;
   outmsg.dest_port = LWIP_IANA_PORT_MDNS;
   SMEMCPY(&outmsg.dest_addr, destination, sizeof(outmsg.dest_addr));
-  res = mdns_send_outpacket(&outmsg, netif);
-
-  return res;
+  return mdns_send_outpacket(&outmsg, netif);
 }
 
 /**
diff --git a/src/apps/mdns/mdns_out.c b/src/apps/mdns/mdns_out.c
index d13b3f913..8d12d27b6 100644
--- a/src/apps/mdns/mdns_out.c
+++ b/src/apps/mdns/mdns_out.c
@@ -550,42 +550,40 @@ mdns_add_probe_questions_to_outpacket(struct mdns_outpacket *outpkt, struct mdns
 }
 
 /**
- * Send chosen answers as a reply
+ * Create packet with chosen answers as a reply
  *
- * Add all selected answers (first write will allocate pbuf)
+ * Add all selected answers / questions
  * Add additional answers based on the selected answers
- * Send the packet
  */
 err_t
-mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
+mdns_create_outpacket(struct netif *netif, struct mdns_outmsg *msg,
+                      struct mdns_outpacket *outpkt)
 {
+  struct mdns_host *mdns = netif_mdns_data(netif);
   struct mdns_service *service;
-  struct mdns_outpacket outpkt;
-  err_t res = ERR_ARG;
+  err_t res;
   int i;
-  struct mdns_host *mdns = netif_mdns_data(netif);
   u16_t answers = 0;
 
-  memset(&outpkt, 0, sizeof(outpkt));
 
-  res = mdns_add_probe_questions_to_outpacket(&outpkt, msg, netif);
+  res = mdns_add_probe_questions_to_outpacket(outpkt, msg, netif);
   if (res != ERR_OK) {
-    goto cleanup;
+    return res;
   }
 
   /* Write answers to host questions */
 #if LWIP_IPV4
   if (msg->host_replies & REPLY_HOST_A) {
-    res = mdns_add_a_answer(&outpkt, msg, netif);
+    res = mdns_add_a_answer(outpkt, msg, netif);
     if (res != ERR_OK) {
-      goto cleanup;
+      return res;
     }
     answers++;
   }
   if (msg->host_replies & REPLY_HOST_PTR_V4) {
-    res = mdns_add_hostv4_ptr_answer(&outpkt, msg, netif);
+    res = mdns_add_hostv4_ptr_answer(outpkt, msg, netif);
     if (res != ERR_OK) {
-      goto cleanup;
+      return res;
     }
     answers++;
   }
@@ -595,9 +593,9 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
     int addrindex;
     for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; addrindex++) {
       if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addrindex))) {
-        res = mdns_add_aaaa_answer(&outpkt, msg, netif, addrindex);
+        res = mdns_add_aaaa_answer(outpkt, msg, netif, addrindex);
         if (res != ERR_OK) {
-          goto cleanup;
+          return res;
         }
         answers++;
       }
@@ -608,9 +606,9 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
     int addrindex = 0;
     while (rev_addrs) {
       if (rev_addrs & 1) {
-        res = mdns_add_hostv6_ptr_answer(&outpkt, msg, netif, addrindex);
+        res = mdns_add_hostv6_ptr_answer(outpkt, msg, netif, addrindex);
         if (res != ERR_OK) {
-          goto cleanup;
+          return res;
         }
         answers++;
       }
@@ -628,33 +626,33 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
     }
 
     if (msg->serv_replies[i] & REPLY_SERVICE_TYPE_PTR) {
-      res = mdns_add_servicetype_ptr_answer(&outpkt, msg, service);
+      res = mdns_add_servicetype_ptr_answer(outpkt, msg, service);
       if (res != ERR_OK) {
-        goto cleanup;
+        return res;
       }
       answers++;
     }
 
     if (msg->serv_replies[i] & REPLY_SERVICE_NAME_PTR) {
-      res = mdns_add_servicename_ptr_answer(&outpkt, msg, service);
+      res = mdns_add_servicename_ptr_answer(outpkt, msg, service);
       if (res != ERR_OK) {
-        goto cleanup;
+        return res;
       }
       answers++;
     }
 
     if (msg->serv_replies[i] & REPLY_SERVICE_SRV) {
-      res = mdns_add_srv_answer(&outpkt, msg, mdns, service);
+      res = mdns_add_srv_answer(outpkt, msg, mdns, service);
       if (res != ERR_OK) {
-        goto cleanup;
+        return res;
       }
       answers++;
     }
 
     if (msg->serv_replies[i] & REPLY_SERVICE_TXT) {
-      res = mdns_add_txt_answer(&outpkt, msg, service);
+      res = mdns_add_txt_answer(outpkt, msg, service);
       if (res != ERR_OK) {
-        goto cleanup;
+        return res;
       }
       answers++;
     }
@@ -663,9 +661,9 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
   /* if this is a response, the data above is anwers, else this is a probe and
    * the answers above goes into auth section */
   if (msg->flags & DNS_FLAG1_RESPONSE) {
-    outpkt.answers += answers;
+    outpkt->answers += answers;
   } else {
-    outpkt.authoritative += answers;
+    outpkt->authoritative += answers;
   }
 
   /* All answers written, add additional RRs */
@@ -679,19 +677,19 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
       /* Our service instance requested, include SRV & TXT
        * if they are already not requested. */
       if (!(msg->serv_replies[i] & REPLY_SERVICE_SRV)) {
-        res = mdns_add_srv_answer(&outpkt, msg, mdns, service);
+        res = mdns_add_srv_answer(outpkt, msg, mdns, service);
         if (res != ERR_OK) {
-          goto cleanup;
+          return res;
         }
-        outpkt.additional++;
+        outpkt->additional++;
       }
 
       if (!(msg->serv_replies[i] & REPLY_SERVICE_TXT)) {
-        res = mdns_add_txt_answer(&outpkt, msg, service);
+        res = mdns_add_txt_answer(outpkt, msg, service);
         if (res != ERR_OK) {
-          goto cleanup;
+          return res;
         }
-        outpkt.additional++;
+        outpkt->additional++;
       }
     }
 
@@ -705,11 +703,11 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
         int addrindex;
         for (addrindex = 0; addrindex < LWIP_IPV6_NUM_ADDRESSES; addrindex++) {
           if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addrindex))) {
-            res = mdns_add_aaaa_answer(&outpkt, msg, netif, addrindex);
+            res = mdns_add_aaaa_answer(outpkt, msg, netif, addrindex);
             if (res != ERR_OK) {
-              goto cleanup;
+              return res;
             }
-            outpkt.additional++;
+            outpkt->additional++;
           }
         }
       }
@@ -717,16 +715,38 @@ mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
 #if LWIP_IPV4
       if (!(msg->host_replies & REPLY_HOST_A) &&
           !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
-        res = mdns_add_a_answer(&outpkt, msg, netif);
+        res = mdns_add_a_answer(outpkt, msg, netif);
         if (res != ERR_OK) {
-          goto cleanup;
+          return res;
         }
-        outpkt.additional++;
+        outpkt->additional++;
       }
 #endif
     }
   }
 
+  return res;
+}
+
+/**
+ * Send chosen answers as a reply
+ *
+ * Create the packet
+ * Send the packet
+ */
+err_t
+mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif)
+{
+  struct mdns_outpacket outpkt;
+  err_t res;
+
+  memset(&outpkt, 0, sizeof(outpkt));
+
+  res = mdns_create_outpacket(netif, msg, &outpkt);
+  if (res != ERR_OK) {
+    goto cleanup;
+  }
+
   if (outpkt.pbuf) {
     struct dns_hdr hdr;
 
diff --git a/src/include/lwip/apps/mdns_out.h b/src/include/lwip/apps/mdns_out.h
index 83afab24f..a6e33234d 100644
--- a/src/include/lwip/apps/mdns_out.h
+++ b/src/include/lwip/apps/mdns_out.h
@@ -103,6 +103,8 @@ extern "C" {
  */
 #define MDNS_MULTICAST_TIMEOUT_25TTL  30000
 
+err_t mdns_create_outpacket(struct netif *netif, struct mdns_outmsg *msg,
+                            struct mdns_outpacket *outpkt);
 err_t mdns_send_outpacket(struct mdns_outmsg *msg, struct netif *netif);
 void mdns_set_timeout(struct netif *netif, u32_t msecs,
                         sys_timeout_handler handler, u8_t *busy_flag);
-- 
2.19.1

