From 35e148e26d96ea5ce3523016116ebbddbf69ad60 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Tue, 14 Feb 2017 21:03:49 +0100 Subject: [PATCH] MDNS: Use new netif callback, now does not need mdns_resp_netif_settings_changed() calls from user any more when LWIP_NETIF_EXT_STATUS_CALLBACK is enabled --- src/apps/mdns/mdns.c | 102 ++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 30 deletions(-) diff --git a/src/apps/mdns/mdns.c b/src/apps/mdns/mdns.c index 14334fc8..fd266711 100644 --- a/src/apps/mdns/mdns.c +++ b/src/apps/mdns/mdns.c @@ -18,7 +18,6 @@ * - Checking that source address of unicast requests are on the same network * - Limiting multicast responses to 1 per second per resource record * - Fragmenting replies if required - * - Subscribe to netif address/link change events and act on them (currently needs to be done manually) * - Handling multi-packet known answers * - Individual known answer detection for all local IPv6 addresses * - Dynamic size of outgoing packet @@ -102,6 +101,7 @@ static const ip_addr_t v6group = DNS_MQUERY_IPV6_GROUP_INIT; static u8_t mdns_netif_client_id; static struct udp_pcb *mdns_pcb; +NETIF_DECLARE_EXT_CALLBACK(netif_callback) #define NETIF_TO_HOST(netif) (struct mdns_host*)(netif_get_client_data(netif, mdns_netif_client_id)) @@ -1803,36 +1803,12 @@ dealloc: pbuf_free(p); } -/** - * @ingroup mdns - * Initiate MDNS responder. Will open UDP sockets on port 5353 - */ -void -mdns_resp_init(void) -{ - err_t res; - - mdns_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); - LWIP_ASSERT("Failed to allocate pcb", mdns_pcb != NULL); -#if LWIP_MULTICAST_TX_OPTIONS - udp_set_multicast_ttl(mdns_pcb, MDNS_TTL); -#else - mdns_pcb->ttl = MDNS_TTL; -#endif - res = udp_bind(mdns_pcb, IP_ANY_TYPE, MDNS_PORT); - LWIP_UNUSED_ARG(res); /* in case of LWIP_NOASSERT */ - LWIP_ASSERT("Failed to bind pcb", res == ERR_OK); - udp_recv(mdns_pcb, mdns_recv, NULL); - - mdns_netif_client_id = netif_alloc_client_data_id(); -} - /** * @ingroup mdns * Announce IP settings have changed on netif. * Call this in your callback registered by netif_set_status_callback(). - * This function may go away in the future when netif supports registering - * multiple callback functions. + * No need to call this function when LWIP_NETIF_EXT_STATUS_CALLBACK==1, + * this handled automatically for you. * @param netif The network interface where settings have changed. */ void @@ -1846,13 +1822,52 @@ mdns_resp_netif_settings_changed(struct netif *netif) /* Announce on IPv6 and IPv4 */ #if LWIP_IPV6 - mdns_announce(netif, IP6_ADDR_ANY); + mdns_announce(netif, IP6_ADDR_ANY); #endif #if LWIP_IPV4 - mdns_announce(netif, IP4_ADDR_ANY); + mdns_announce(netif, IP4_ADDR_ANY); #endif } +#if LWIP_NETIF_EXT_STATUS_CALLBACK +static void +mdns_netif_ext_status_callback(struct netif* netif, netif_nsc_reason_t reason, u16_t num, const void* arg) +{ + LWIP_UNUSED_ARG(num); + LWIP_UNUSED_ARG(arg); + + /* MDNS enabled on netif? */ + if (NETIF_TO_HOST(netif) == NULL) { + return; + } + + switch (reason) + { + case LWIP_NSC_STATUS_CHANGED: + if (num != 0) { + mdns_resp_netif_settings_changed(netif); + } + /* TODO: send goodbye message */ + break; + case LWIP_NSC_LINK_CHANGED: + if (num != 0) { + mdns_resp_netif_settings_changed(netif); + } + break; + case LWIP_NSC_IPV4_ADDRESS_CHANGED: /* fall through */ + case LWIP_NSC_IPV4_GATEWAY_CHANGED: /* fall through */ + case LWIP_NSC_IPV4_NETMASK_CHANGED: /* fall through */ + case LWIP_NSC_IPV4_SETTINGS_CHANGED: /* fall through */ + case LWIP_NSC_IPV6_SET: /* fall through */ + case LWIP_NSC_IPV6_ADDR_STATE_CHANGED: + mdns_resp_netif_settings_changed(netif); + break; + default: + break; + } +} +#endif + /** * @ingroup mdns * Activate MDNS responder for a network interface and send announce packets. @@ -1875,7 +1890,7 @@ mdns_resp_add_netif(struct netif *netif, const char *hostname, u32_t dns_ttl) LWIP_ASSERT("mdns_resp_add_netif: Double add", NETIF_TO_HOST(netif) == NULL); mdns = (struct mdns_host *) mem_malloc(sizeof(struct mdns_host)); LWIP_ERROR("mdns_resp_add_netif: Alloc failed", (mdns != NULL), return ERR_MEM); - + netif_set_client_data(netif, mdns_netif_client_id, mdns); memset(mdns, 0, sizeof(struct mdns_host)); @@ -2025,4 +2040,31 @@ mdns_resp_add_service_txtitem(struct mdns_service *service, const char *txt, u8_ return mdns_domain_add_label(&service->txtdata, txt, txt_len); } +/** + * @ingroup mdns + * Initiate MDNS responder. Will open UDP sockets on port 5353 + */ +void +mdns_resp_init(void) +{ + err_t res; + + mdns_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); + LWIP_ASSERT("Failed to allocate pcb", mdns_pcb != NULL); +#if LWIP_MULTICAST_TX_OPTIONS + udp_set_multicast_ttl(mdns_pcb, MDNS_TTL); +#else + mdns_pcb->ttl = MDNS_TTL; +#endif + res = udp_bind(mdns_pcb, IP_ANY_TYPE, MDNS_PORT); + LWIP_UNUSED_ARG(res); /* in case of LWIP_NOASSERT */ + LWIP_ASSERT("Failed to bind pcb", res == ERR_OK); + udp_recv(mdns_pcb, mdns_recv, NULL); + + mdns_netif_client_id = netif_alloc_client_data_id(); + + /* register for netif events when started on first netif */ + netif_add_ext_callback(&netif_callback, mdns_netif_ext_status_callback); +} + #endif /* LWIP_MDNS_RESPONDER */