From c2f82f7164c9473e274f2c5391dff5c673d43fb5 Mon Sep 17 00:00:00 2001
From: Jens Nielsen <deft@telia.com>
Date: Mon, 29 Jan 2018 08:49:47 +0100
Subject: [PATCH 5/5] MDNS minor fixes and documentation

---
 doc/mdns.txt                 | 13 ++++++-------
 src/apps/mdns/mdns.c         | 21 ++++++++++++---------
 src/include/lwip/apps/mdns.h |  6 +++++-
 3 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/doc/mdns.txt b/doc/mdns.txt
index c5929b76..0c28970d 100644
--- a/doc/mdns.txt
+++ b/doc/mdns.txt
@@ -57,11 +57,11 @@ Answers will use the supplied TTL (in seconds)
 MDNS allows UTF-8 names, but it is recommended to stay within ASCII,
 since the default case-insensitive comparison assumes this.
 
-It is recommended to call this function after an IPv4 address has been set,
-since there is currently no check if the v4 address is valid.
+Call mdns_resp_announce() every time the IP address on the netif has changed.
 
-Call mdns_resp_netif_settings_changed() every time the IP address
-on the netif has changed.
+Call mdns_resp_restart() every time the network interface comes up after being
+down, for example cable connected after being disconnected, administrative 
+interface comes up after being down, or the device wakes up from sleep.
 
 To stop responding on a netif, run
   mdns_resp_remove_netif(struct netif *netif)
@@ -108,6 +108,5 @@ and point them to <hostname>.local:80
 Relevant information will be sent as additional records to reduce number of
 requests required from a client.
 
-Removing services is currently not supported. Services are removed when the
-netif is removed.
-
+To remove a service from a netif, run
+  mdns_resp_del_service(struct netif *netif, s8_t slot)
\ No newline at end of file
diff --git a/src/apps/mdns/mdns.c b/src/apps/mdns/mdns.c
index 3c59ea7b..26d9c410 100644
--- a/src/apps/mdns/mdns.c
+++ b/src/apps/mdns/mdns.c
@@ -1999,6 +1999,7 @@ mdns_send_probe(struct netif* netif, const ip_addr_t *destination)
   }
 
   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);
 
@@ -2046,8 +2047,7 @@ mdns_probe(void* arg)
 
 /**
  * @ingroup mdns
- * Activate MDNS responder for a network interface and send announce packets.
- * Don't forget to call mdns_resp_announce() after you added all netifs and services.
+ * Activate MDNS responder for a network interface.
  * @param netif The network interface to activate.
  * @param hostname Name to use. Queries for &lt;hostname&gt;.local will be answered
  *                 with the IP addresses of the netif. The hostname will be copied, the
@@ -2145,7 +2145,6 @@ mdns_resp_remove_netif(struct netif *netif)
 /**
  * @ingroup mdns
  * Add a service to the selected network interface.
- * Don't forget to call mdns_resp_announce() after you added all services.
  * @param netif The network interface to publish this service on
  * @param name The name of the service
  * @param service The service type, like "_http"
@@ -2252,10 +2251,11 @@ mdns_resp_add_service_txtitem(struct mdns_service *service, const char *txt, u8_
 void
 mdns_resp_announce(struct netif *netif)
 {
-  struct mdns_host* mdns = NETIF_TO_HOST(netif);
+  struct mdns_host* mdns;
   LWIP_ASSERT_CORE_LOCKED();
   LWIP_ERROR("mdns_resp_announce: netif != NULL", (netif != NULL), return);
 
+  mdns = NETIF_TO_HOST(netif);
   if (mdns == NULL) {
     return;
   }
@@ -2289,19 +2289,22 @@ mdns_resp_register_name_result_cb(mdns_name_result_cb_t cb)
 void
 mdns_resp_restart(struct netif *netif)
 {
-  struct mdns_host* mdns = NETIF_TO_HOST(netif);
+  struct mdns_host* mdns;
   LWIP_ASSERT_CORE_LOCKED();
-  LWIP_ERROR("mdns_resp_start: netif != NULL", (netif != NULL), return);
+  LWIP_ERROR("mdns_resp_restart: netif != NULL", (netif != NULL), return);
 
+  mdns = NETIF_TO_HOST(netif);
   if (mdns == NULL) {
     return;
   }
 
-  /*todo first probe timeout should be random 0-250 ms*/
-  /*todo if we've failed 15 times within a 10 second period we should wait 5 seconds (or wait 5 seconds every time except first)*/
+  if (mdns->probing_state == MDNS_PROBING_ONGOING) {
+    sys_untimeout(mdns_probe, netif);
+  }
+  /*todo first probe timeout SHOULD be random 0-250 ms*/
+  /*todo if we've failed 15 times within a 10 second period we MUST wait 5 seconds (or wait 5 seconds every time except first)*/
   mdns->probes_sent = 0;
   mdns->probing_state = MDNS_PROBING_ONGOING;
-  sys_untimeout(mdns_probe, netif);
   sys_timeout(250, mdns_probe, netif);
 }
 
diff --git a/src/include/lwip/apps/mdns.h b/src/include/lwip/apps/mdns.h
index c072ba58..cbc1e193 100644
--- a/src/include/lwip/apps/mdns.h
+++ b/src/include/lwip/apps/mdns.h
@@ -56,7 +56,11 @@ struct mdns_service;
 /** Callback function to add text to a reply, called when generating the reply */
 typedef void (*service_get_txt_fn_t)(struct mdns_service *service, void *txt_userdata);
 
-typedef void (* mdns_name_result_cb_t)(struct netif* netif, u8_t result);
+/** Callback function to let application know the result of probing network for name
+ *  uniqueness, called with result 1 if no other node claimed use for the name for the
+ *  netif or a service and is safe to use, or 0 if another node is already using it and
+ *  mdns is disabled on this interface */
+typedef void (*mdns_name_result_cb_t)(struct netif* netif, u8_t result);
 
 void mdns_resp_init(void);
 
-- 
2.12.2.windows.2

