فهرست منبع

mdns: update probe conflict function to provide service in conflict

- Send service slot index to the mdns result function. In case of conflict, the user
  will have to remove the service or rename it.
- Break after hostname conflict in order to managed it first, and managed service name
  conflict after.
- Provide a function to get the TXT userdata for a service (allowing app to match with
  its own data).

Signed-off-by: Simon Goldschmidt <goldsimon@gmx.de>
David Girault 6 سال پیش
والد
کامیت
aa79b90d3c
3فایلهای تغییر یافته به همراه28 افزوده شده و 14 حذف شده
  1. 2 2
      contrib/examples/mdns/mdns_example.c
  2. 23 11
      src/apps/mdns/mdns.c
  3. 3 1
      src/include/lwip/apps/mdns.h

+ 2 - 2
contrib/examples/mdns/mdns_example.c

@@ -44,9 +44,9 @@ srv_txt(struct mdns_service *service, void *txt_userdata)
 
 #if LWIP_MDNS_RESPONDER
 static void
-mdns_example_report(struct netif* netif, u8_t result)
+mdns_example_report(struct netif* netif, u8_t result, s8_t service)
 {
-  LWIP_PLATFORM_DIAG(("mdns status[netif %d]: %d\n", netif->num, result));
+  LWIP_PLATFORM_DIAG(("mdns status[netif %d][service %d]: %d\n", netif->num, service, result));
 }
 #endif
 

+ 23 - 11
src/apps/mdns/mdns.c

@@ -1670,9 +1670,10 @@ mdns_handle_question(struct mdns_packet *pkt, struct netif *netif)
  *  - Let the user know there is a conflict.
  *
  * @param netif network interface on which the conflict occured.
+ * @param slot service index +1 on which the conflict occured (0 indicate hostname conflict).
  */
 static void
-mdns_probe_conflict(struct netif *netif)
+mdns_probe_conflict(struct netif *netif, s8_t slot)
 {
   struct mdns_host* mdns = NETIF_TO_HOST(netif);
   int i;
@@ -1707,7 +1708,7 @@ mdns_probe_conflict(struct netif *netif)
 
   /* Inform the host on the conflict, if a callback is set */
   if (mdns_name_result_cb != NULL) {
-    mdns_name_result_cb(netif, MDNS_PROBING_CONFLICT);
+    mdns_name_result_cb(netif, MDNS_PROBING_CONFLICT, slot);
   }
 }
 
@@ -1851,12 +1852,12 @@ mdns_handle_response(struct mdns_packet *pkt, struct netif *netif)
         (mdns->state == MDNS_STATE_ANNOUNCE_WAIT)) {
       struct mdns_domain domain;
       u8_t i;
-      u8_t conflict = 0;
 
       res = mdns_build_host_domain(&domain, mdns);
       if (res == ERR_OK && mdns_domain_eq(&ans.info.domain, &domain)) {
         LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Probe response matches host domain!"));
-        conflict = 1;
+        mdns_probe_conflict(netif, 0);
+        break;
       }
 
       for (i = 0; i < MDNS_MAX_SERVICES; i++) {
@@ -1867,14 +1868,10 @@ mdns_handle_response(struct mdns_packet *pkt, struct netif *netif)
         res = mdns_build_service_domain(&domain, service, 1);
         if ((res == ERR_OK) && mdns_domain_eq(&ans.info.domain, &domain)) {
           LWIP_DEBUGF(MDNS_DEBUG, ("MDNS: Probe response matches service domain!"));
-          conflict = 1;
+          mdns_probe_conflict(netif, i + 1);
+          break;
         }
       }
-
-      if (conflict != 0) {
-        mdns_probe_conflict(netif);
-        break;
-      }
     }
     /* Perform conflict resolution (RFC6762 section 9):
      * We assume a conflict if the hostname or service name matches the answers
@@ -2210,7 +2207,7 @@ mdns_probe_and_announce(void* arg)
         mdns->rate_limit_activated = 0;
         /* Let the client know probing was successful */
         if (mdns_name_result_cb != NULL) {
-          mdns_name_result_cb(netif, MDNS_PROBING_SUCCESSFUL);
+          mdns_name_result_cb(netif, MDNS_PROBING_SUCCESSFUL, 0);
         }
       }
 
@@ -2693,4 +2690,19 @@ mdns_resp_init(void)
 #endif
 }
 
+/**
+ * @ingroup mdns
+ * Return TXT userdata of a specific service on a network interface.
+ * @param netif Network interface.
+ * @param slot Service index.
+ */
+void *mdns_get_service_txt_userdata(struct netif *netif, s8_t slot)
+{
+  struct mdns_host *mdns = NETIF_TO_HOST(netif);
+  struct mdns_service *s;
+  LWIP_ASSERT("mdns_get_service_txt_userdata: index out of range", slot < MDNS_MAX_SERVICES);
+  s = mdns->services[slot];
+  return s ? s->txt_userdata : NULL;
+}
+
 #endif /* LWIP_MDNS_RESPONDER */

+ 3 - 1
src/include/lwip/apps/mdns.h

@@ -99,7 +99,9 @@ typedef void (*service_get_txt_fn_t)(struct mdns_service *service, void *txt_use
  * uniqueness, called with result MDNS_PROBING_SUCCESSFUL if no other node claimed
  * use for the name for the netif or a service and is safe to use, or MDNS_PROBING_CONFLICT
  * 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);
+typedef void (*mdns_name_result_cb_t)(struct netif* netif, u8_t result, s8_t slot);
+
+void *mdns_get_service_txt_userdata(struct netif *netif, s8_t slot);
 
 void mdns_resp_init(void);