diff --git a/components/mdns/include/mdns.h b/components/mdns/include/mdns.h index ddb31feac5..d0c86e068c 100644 --- a/components/mdns/include/mdns.h +++ b/components/mdns/include/mdns.h @@ -12,14 +12,14 @@ extern "C" { #include -#define MDNS_TYPE_A 0x0001 -#define MDNS_TYPE_PTR 0x000C -#define MDNS_TYPE_TXT 0x0010 -#define MDNS_TYPE_AAAA 0x001C -#define MDNS_TYPE_SRV 0x0021 -#define MDNS_TYPE_OPT 0x0029 -#define MDNS_TYPE_NSEC 0x002F -#define MDNS_TYPE_ANY 0x00FF +#define MDNS_TYPE_A 0x0001 +#define MDNS_TYPE_PTR 0x000C +#define MDNS_TYPE_TXT 0x0010 +#define MDNS_TYPE_AAAA 0x001C +#define MDNS_TYPE_SRV 0x0021 +#define MDNS_TYPE_OPT 0x0029 +#define MDNS_TYPE_NSEC 0x002F +#define MDNS_TYPE_ANY 0x00FF /** * @brief Asynchronous query handle @@ -32,14 +32,14 @@ typedef struct mdns_search_once_s mdns_search_once_t; typedef struct mdns_browse_s mdns_browse_t; typedef enum { - MDNS_EVENT_ENABLE_IP4 = 1 << 1, - MDNS_EVENT_ENABLE_IP6 = 1 << 2, - MDNS_EVENT_ANNOUNCE_IP4 = 1 << 3, - MDNS_EVENT_ANNOUNCE_IP6 = 1 << 4, - MDNS_EVENT_DISABLE_IP4 = 1 << 5, - MDNS_EVENT_DISABLE_IP6 = 1 << 6, - MDNS_EVENT_IP4_REVERSE_LOOKUP = 1 << 7, - MDNS_EVENT_IP6_REVERSE_LOOKUP = 1 << 8, + MDNS_EVENT_ENABLE_IP4 = 1 << 1, + MDNS_EVENT_ENABLE_IP6 = 1 << 2, + MDNS_EVENT_ANNOUNCE_IP4 = 1 << 3, + MDNS_EVENT_ANNOUNCE_IP6 = 1 << 4, + MDNS_EVENT_DISABLE_IP4 = 1 << 5, + MDNS_EVENT_DISABLE_IP6 = 1 << 6, + MDNS_EVENT_IP4_REVERSE_LOOKUP = 1 << 7, + MDNS_EVENT_IP6_REVERSE_LOOKUP = 1 << 8, } mdns_event_actions_t; /** @@ -56,16 +56,24 @@ typedef enum { * Used in mdns_service_add() */ typedef struct { - const char *key; /*!< item key name */ - const char *value; /*!< item value string */ + const char* key; /*!< item key name */ + const char* value; /*!< item value string */ } mdns_txt_item_t; +/** + * @brief mDNS basic subtype item structure + * Used in mdns_service_add() + */ +typedef struct { + const char* subtype; /*!< subtype name */ +} mdns_subtype_item_t; + /** * @brief mDNS query linked list IP item */ typedef struct mdns_ip_addr_s { - esp_ip_addr_t addr; /*!< IP address */ - struct mdns_ip_addr_s *next; /*!< next IP, or NULL for the last IP in the list */ + esp_ip_addr_t addr; /*!< IP address */ + struct mdns_ip_addr_s* next; /*!< next IP, or NULL for the last IP in the list */ } mdns_ip_addr_t; /** @@ -80,29 +88,29 @@ typedef enum { * @brief mDNS query result structure */ typedef struct mdns_result_s { - struct mdns_result_s *next; /*!< next result, or NULL for the last result in the list */ + struct mdns_result_s* next; /*!< next result, or NULL for the last result in the list */ - esp_netif_t *esp_netif; /*!< ptr to corresponding esp-netif */ - uint32_t ttl; /*!< time to live */ + esp_netif_t* esp_netif; /*!< ptr to corresponding esp-netif */ + uint32_t ttl; /*!< time to live */ - mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */ + mdns_ip_protocol_t ip_protocol; /*!< ip_protocol type of the interface (v4/v6) */ // PTR - char *instance_name; /*!< instance name */ - char *service_type; /*!< service type */ - char *proto; /*!< srevice protocol */ + char* instance_name; /*!< instance name */ + char* service_type; /*!< service type */ + char* proto; /*!< srevice protocol */ // SRV - char *hostname; /*!< hostname */ - uint16_t port; /*!< service port */ + char* hostname; /*!< hostname */ + uint16_t port; /*!< service port */ // TXT - mdns_txt_item_t *txt; /*!< txt record */ - uint8_t *txt_value_len; /*!< array of txt value len of each record */ - size_t txt_count; /*!< number of txt items */ + mdns_txt_item_t* txt; /*!< txt record */ + uint8_t* txt_value_len; /*!< array of txt value len of each record */ + size_t txt_count; /*!< number of txt items */ // A and AAAA - mdns_ip_addr_t *addr; /*!< linked list of IP addresses found */ + mdns_ip_addr_t* addr; /*!< linked list of IP addresses found */ } mdns_result_t; -typedef void (*mdns_query_notify_t)(mdns_search_once_t *search); -typedef void (*mdns_browse_notify_t)(mdns_result_t *result); +typedef void (*mdns_query_notify_t)(mdns_search_once_t* search); +typedef void (*mdns_browse_notify_t)(mdns_result_t* result); /** * @brief Initialize mDNS on given interface @@ -132,7 +140,7 @@ void mdns_free(void); * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_hostname_set(const char *hostname); +esp_err_t mdns_hostname_set(const char* hostname); /** * @brief Get the hostname for mDNS server @@ -145,7 +153,7 @@ esp_err_t mdns_hostname_set(const char *hostname); * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_INVALID_STATE when mdns is not initialized */ -esp_err_t mdns_hostname_get(char *hostname); +esp_err_t mdns_hostname_get(char* hostname); /** * @brief Adds a hostname and address to be delegated @@ -162,7 +170,7 @@ esp_err_t mdns_hostname_get(char *hostname); * - ESP_ERR_NO_MEM memory error * */ -esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t *address_list); +esp_err_t mdns_delegate_hostname_add(const char* hostname, const mdns_ip_addr_t* address_list); /** * @brief Set the address to a delegated hostname @@ -177,7 +185,7 @@ esp_err_t mdns_delegate_hostname_add(const char *hostname, const mdns_ip_addr_t * - ESP_ERR_NO_MEM memory error * */ -esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip_addr_t *address_list); +esp_err_t mdns_delegate_hostname_set_address(const char* hostname, const mdns_ip_addr_t* address_list); /** * @brief Remove a delegated hostname @@ -192,7 +200,7 @@ esp_err_t mdns_delegate_hostname_set_address(const char *hostname, const mdns_ip * - ESP_ERR_NO_MEM memory error * */ -esp_err_t mdns_delegate_hostname_remove(const char *hostname); +esp_err_t mdns_delegate_hostname_remove(const char* hostname); /** * @brief Query whether a hostname has been added @@ -204,7 +212,7 @@ esp_err_t mdns_delegate_hostname_remove(const char *hostname); * - false The hostname has not been added. * */ -bool mdns_hostname_exists(const char *hostname); +bool mdns_hostname_exists(const char* hostname); /** * @brief Set the default instance name for mDNS server @@ -216,7 +224,7 @@ bool mdns_hostname_exists(const char *hostname); * - ESP_ERR_INVALID_ARG Parameter error * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_instance_name_set(const char *instance_name); +esp_err_t mdns_instance_name_set(const char* instance_name); /** * @brief Add service to mDNS server @@ -240,7 +248,7 @@ esp_err_t mdns_instance_name_set(const char *instance_name); * - ESP_ERR_NO_MEM memory error * - ESP_FAIL failed to add service */ -esp_err_t mdns_service_add(const char *instance_name, const char *service_type, const char *proto, uint16_t port, mdns_txt_item_t txt[], size_t num_items); +esp_err_t mdns_service_add(const char* instance_name, const char* service_type, const char* proto, uint16_t port, mdns_txt_item_t txt[], size_t num_items); /** * @brief Add service to mDNS server with a delegated hostname @@ -265,8 +273,8 @@ esp_err_t mdns_service_add(const char *instance_name, const char *service_type, * - ESP_ERR_NO_MEM memory error * - ESP_FAIL failed to add service */ -esp_err_t mdns_service_add_for_host(const char *instance_name, const char *service_type, const char *proto, - const char *hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items); +esp_err_t mdns_service_add_for_host(const char* instance_name, const char* service_type, const char* proto, + const char* hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items); /** * @brief Check whether a service has been added. @@ -279,7 +287,7 @@ esp_err_t mdns_service_add_for_host(const char *instance_name, const char *servi * - true Correspondding service has been added. * - false Service not found. */ -bool mdns_service_exists(const char *service_type, const char *proto, const char *hostname); +bool mdns_service_exists(const char* service_type, const char* proto, const char* hostname); /** * @brief Check whether a service has been added. @@ -293,8 +301,8 @@ bool mdns_service_exists(const char *service_type, const char *proto, const char * - true Correspondding service has been added. * - false Service not found. */ -bool mdns_service_exists_with_instance(const char *instance, const char *service_type, const char *proto, - const char *hostname); +bool mdns_service_exists_with_instance(const char* instance, const char* service_type, const char* proto, + const char* hostname); /** * @brief Remove service from mDNS server @@ -308,7 +316,7 @@ bool mdns_service_exists_with_instance(const char *instance, const char *service * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_remove(const char *service_type, const char *proto); +esp_err_t mdns_service_remove(const char* service_type, const char* proto); /** * @brief Remove service from mDNS server with hostname @@ -324,7 +332,7 @@ esp_err_t mdns_service_remove(const char *service_type, const char *proto); * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_remove_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname); +esp_err_t mdns_service_remove_for_host(const char* instance, const char* service_type, const char* proto, const char* hostname); /** * @brief Set instance name for service @@ -339,7 +347,7 @@ esp_err_t mdns_service_remove_for_host(const char *instance, const char *service * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_instance_name_set(const char *service_type, const char *proto, const char *instance_name); +esp_err_t mdns_service_instance_name_set(const char* service_type, const char* proto, const char* instance_name); /** * @brief Set instance name for service with hostname @@ -356,8 +364,8 @@ esp_err_t mdns_service_instance_name_set(const char *service_type, const char *p * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service_type, const char *proto, const char *hostname, - const char *instance_name); +esp_err_t mdns_service_instance_name_set_for_host(const char* instance_old, const char* service_type, const char* proto, const char* hostname, + const char* instance_name); /** * @brief Set service port @@ -372,8 +380,7 @@ esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, cons * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_port_set(const char *service_type, const char *proto, uint16_t port); - +esp_err_t mdns_service_port_set(const char* service_type, const char* proto, uint16_t port); /** * @brief Set service port with hostname @@ -390,8 +397,8 @@ esp_err_t mdns_service_port_set(const char *service_type, const char *proto, uin * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_port_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname, - uint16_t port); +esp_err_t mdns_service_port_set_for_host(const char* instance, const char* service_type, const char* proto, const char* hostname, + uint16_t port); /** * @brief Replace all TXT items for service @@ -409,7 +416,7 @@ esp_err_t mdns_service_port_set_for_host(const char *instance, const char *servi * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_set(const char *service_type, const char *proto, mdns_txt_item_t txt[], uint8_t num_items); +esp_err_t mdns_service_txt_set(const char* service_type, const char* proto, mdns_txt_item_t txt[], uint8_t num_items); /** * @brief Replace all TXT items for service with hostname @@ -429,8 +436,8 @@ esp_err_t mdns_service_txt_set(const char *service_type, const char *proto, mdns * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname, - mdns_txt_item_t txt[], uint8_t num_items); +esp_err_t mdns_service_txt_set_for_host(const char* instance, const char* service_type, const char* proto, const char* hostname, + mdns_txt_item_t txt[], uint8_t num_items); /** * @brief Set/Add TXT item for service TXT record @@ -448,7 +455,7 @@ esp_err_t mdns_service_txt_set_for_host(const char *instance, const char *servic * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_item_set(const char *service_type, const char *proto, const char *key, const char *value); +esp_err_t mdns_service_txt_item_set(const char* service_type, const char* proto, const char* key, const char* value); /** * @brief Set/Add TXT item for service TXT record @@ -465,8 +472,8 @@ esp_err_t mdns_service_txt_item_set(const char *service_type, const char *proto, * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service_type, const char *proto, - const char *key, const char *value, uint8_t value_len); +esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char* service_type, const char* proto, + const char* key, const char* value, uint8_t value_len); /** * @brief Set/Add TXT item for service TXT record with hostname @@ -486,8 +493,8 @@ esp_err_t mdns_service_txt_item_set_with_explicit_value_len(const char *service_ * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname, - const char *key, const char *value); +esp_err_t mdns_service_txt_item_set_for_host(const char* instance, const char* service_type, const char* proto, const char* hostname, + const char* key, const char* value); /** * @brief Set/Add TXT item for service TXT record with hostname and txt value length @@ -506,9 +513,9 @@ esp_err_t mdns_service_txt_item_set_for_host(const char *instance, const char *s * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char *instance, const char *service_type, const char *proto, - const char *hostname, const char *key, - const char *value, uint8_t value_len); +esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char* instance, const char* service_type, const char* proto, + const char* hostname, const char* key, + const char* value, uint8_t value_len); /** * @brief Remove TXT item for service TXT record @@ -523,7 +530,7 @@ esp_err_t mdns_service_txt_item_set_for_host_with_explicit_value_len(const char * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_item_remove(const char *service_type, const char *proto, const char *key); +esp_err_t mdns_service_txt_item_remove(const char* service_type, const char* proto, const char* key); /** * @brief Remove TXT item for service TXT record with hostname @@ -540,11 +547,11 @@ esp_err_t mdns_service_txt_item_remove(const char *service_type, const char *pro * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char *service_type, const char *proto, const char *hostname, - const char *key); +esp_err_t mdns_service_txt_item_remove_for_host(const char* instance, const char* service_type, const char* proto, const char* hostname, + const char* key); /** - * @brief Add subtype for service. + * @brief Add a subtype for service. * * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol. * @param service_type service type (_http, _ftp, etc) @@ -558,9 +565,63 @@ esp_err_t mdns_service_txt_item_remove_for_host(const char *instance, const char * - ESP_ERR_NOT_FOUND Service not found * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service_type, const char *proto, - const char *hostname, const char *subtype); +esp_err_t mdns_service_subtype_add_for_host(const char* instance_name, const char* service_type, const char* proto, + const char* hostname, const char* subtype); + +/** + * @brief Remove a subtype for service. + * + * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol. + * @param service_type service type (_http, _ftp, etc) + * @param proto service protocol (_tcp, _udp) + * @param hostname service hostname. If NULL, local hostname will be used. + * @param subtype The subtype to remove. + * + * @return + * - ESP_OK success + * - ESP_ERR_INVALID_ARG Parameter error + * - ESP_ERR_NOT_FOUND Service not found + */ +esp_err_t mdns_service_subtype_remove_for_host(const char* instance_name, const char* service, const char* proto, + const char* hostname, const char* subtype) + + /** + * @brief Add multiple subtypes for service at once. + * + * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol. + * @param service_type service type (_http, _ftp, etc) + * @param proto service protocol (_tcp, _udp) + * @param hostname service hostname. If NULL, local hostname will be used. + * @param subtype the pointer of subtype array to add. + * @param num_items number of items in subtype array + * + * @return + * - ESP_OK success + * - ESP_ERR_INVALID_ARG Parameter error + * - ESP_ERR_NOT_FOUND Service not found + * - ESP_ERR_NO_MEM memory error + */ + esp_err_t mdns_service_subtype_add_multiple_items_for_host(const char* instance_name, const char* service, const char* proto, + const char* hostname, mdns_subtype_item_t subtype[], uint8_t num_items); +/** + * @brief Update subtype for service. + * + * @param instance_name instance name. If NULL, will find the first service with the same service type and protocol. + * @param service_type service type (_http, _ftp, etc) + * @param proto service protocol (_tcp, _udp) + * @param hostname service hostname. If NULL, local hostname will be used. + * @param subtype the pointer of subtype array to add. + * @param num_items number of items in subtype array + * + * @return + * - ESP_OK success + * - ESP_ERR_INVALID_ARG Parameter error + * - ESP_ERR_NOT_FOUND Service not found + * - ESP_ERR_NO_MEM memory error + */ +esp_err_t mdns_service_subtype_update_multiple_items_for_host(const char* instance_name, const char* service, const char* proto, + const char* hostname, mdns_subtype_item_t subtype[], uint8_t num_items); /** * @brief Remove and free all services from mDNS server * @@ -580,7 +641,7 @@ esp_err_t mdns_service_remove_all(void); * - ESP_ERR_INVALID_STATE search has not finished * - ESP_ERR_INVALID_ARG pointer to search object is NULL */ -esp_err_t mdns_query_async_delete(mdns_search_once_t *search); +esp_err_t mdns_query_async_delete(mdns_search_once_t* search); /** * @brief Get results from search pointer. Results available as a pointer to the output parameter. @@ -596,7 +657,7 @@ esp_err_t mdns_query_async_delete(mdns_search_once_t *search); * True if search has finished before or at timeout * False if search timeout is over */ -bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, mdns_result_t **results, uint8_t *num_results); +bool mdns_query_async_get_results(mdns_search_once_t* search, uint32_t timeout, mdns_result_t** results, uint8_t* num_results); /** * @brief Query mDNS for host or service asynchronousely. @@ -613,8 +674,8 @@ bool mdns_query_async_get_results(mdns_search_once_t *search, uint32_t timeout, * @return mdns_search_once_s pointer to new search object if query initiated successfully. * NULL otherwise. */ -mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_type, const char *proto, uint16_t type, - uint32_t timeout, size_t max_results, mdns_query_notify_t notifier); +mdns_search_once_t* mdns_query_async_new(const char* name, const char* service_type, const char* proto, uint16_t type, + uint32_t timeout, size_t max_results, mdns_query_notify_t notifier); /** * @brief Generic mDNS query @@ -636,8 +697,8 @@ mdns_search_once_t *mdns_query_async_new(const char *name, const char *service_t * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG timeout was not given */ -esp_err_t mdns_query_generic(const char *name, const char *service_type, const char *proto, uint16_t type, - mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t **results); +esp_err_t mdns_query_generic(const char* name, const char* service_type, const char* proto, uint16_t type, + mdns_query_transmission_type_t transmission_type, uint32_t timeout, size_t max_results, mdns_result_t** results); /** * @brief Query mDNS for host or service @@ -659,14 +720,14 @@ esp_err_t mdns_query_generic(const char *name, const char *service_type, const c * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG timeout was not given */ -esp_err_t mdns_query(const char *name, const char *service_type, const char *proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t **results); +esp_err_t mdns_query(const char* name, const char* service_type, const char* proto, uint16_t type, uint32_t timeout, size_t max_results, mdns_result_t** results); /** * @brief Free query results * * @param results linked list of results to be freed */ -void mdns_query_results_free(mdns_result_t *results); +void mdns_query_results_free(mdns_result_t* results); /** * @brief Query mDNS for service @@ -683,7 +744,7 @@ void mdns_query_results_free(mdns_result_t *results); * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_ptr(const char *service_type, const char *proto, uint32_t timeout, size_t max_results, mdns_result_t **results); +esp_err_t mdns_query_ptr(const char* service_type, const char* proto, uint32_t timeout, size_t max_results, mdns_result_t** results); /** * @brief Query mDNS for SRV record @@ -700,7 +761,7 @@ esp_err_t mdns_query_ptr(const char *service_type, const char *proto, uint32_t t * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_srv(const char *instance_name, const char *service_type, const char *proto, uint32_t timeout, mdns_result_t **result); +esp_err_t mdns_query_srv(const char* instance_name, const char* service_type, const char* proto, uint32_t timeout, mdns_result_t** result); /** * @brief Query mDNS for TXT record @@ -717,7 +778,7 @@ esp_err_t mdns_query_srv(const char *instance_name, const char *service_type, co * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_txt(const char *instance_name, const char *service_type, const char *proto, uint32_t timeout, mdns_result_t **result); +esp_err_t mdns_query_txt(const char* instance_name, const char* service_type, const char* proto, uint32_t timeout, mdns_result_t** result); /** * @brief Look up delegated services. @@ -734,8 +795,8 @@ esp_err_t mdns_query_txt(const char *instance_name, const char *service_type, co * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_lookup_delegated_service(const char *instance, const char *service_type, const char *proto, size_t max_results, - mdns_result_t **result); +esp_err_t mdns_lookup_delegated_service(const char* instance, const char* service_type, const char* proto, size_t max_results, + mdns_result_t** result); /** * @brief Look up self hosted services. @@ -752,8 +813,8 @@ esp_err_t mdns_lookup_delegated_service(const char *instance, const char *servic * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *service_type, const char *proto, size_t max_results, - mdns_result_t **result); +esp_err_t mdns_lookup_selfhosted_service(const char* instance, const char* service_type, const char* proto, size_t max_results, + mdns_result_t** result); /** * @brief Query mDNS for A record @@ -768,7 +829,7 @@ esp_err_t mdns_lookup_selfhosted_service(const char *instance, const char *servi * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_a(const char *host_name, uint32_t timeout, esp_ip4_addr_t *addr); +esp_err_t mdns_query_a(const char* host_name, uint32_t timeout, esp_ip4_addr_t* addr); #if CONFIG_LWIP_IPV6 /** @@ -786,10 +847,9 @@ esp_err_t mdns_query_a(const char *host_name, uint32_t timeout, esp_ip4_addr_t * * - ESP_ERR_NO_MEM memory error * - ESP_ERR_INVALID_ARG parameter error */ -esp_err_t mdns_query_aaaa(const char *host_name, uint32_t timeout, esp_ip6_addr_t *addr); +esp_err_t mdns_query_aaaa(const char* host_name, uint32_t timeout, esp_ip6_addr_t* addr); #endif - /** * @brief Register custom esp_netif with mDNS functionality * mDNS service runs by default on preconfigured interfaces (STA, AP, ETH). @@ -802,7 +862,7 @@ esp_err_t mdns_query_aaaa(const char *host_name, uint32_t timeout, esp_ip6_addr_ * - ESP_ERR_INVALID_STATE mDNS is not running or this netif is already registered * - ESP_ERR_NO_MEM not enough memory for this in interface in the netif list (see CONFIG_MDNS_MAX_INTERFACES) */ -esp_err_t mdns_register_netif(esp_netif_t *esp_netif); +esp_err_t mdns_register_netif(esp_netif_t* esp_netif); /** * @brief Unregister esp-netif already registered in mDNS service @@ -813,7 +873,7 @@ esp_err_t mdns_register_netif(esp_netif_t *esp_netif); * - ESP_ERR_INVALID_STATE mDNS is not running * - ESP_ERR_NOT_FOUND this esp-netif was not registered in mDNS service */ -esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif); +esp_err_t mdns_unregister_netif(esp_netif_t* esp_netif); /** * @brief Set esp_netif to a desired state, or perform a desired action, such as enable/disable this interface @@ -834,7 +894,7 @@ esp_err_t mdns_unregister_netif(esp_netif_t *esp_netif); * - ESP_ERR_INVALID_STATE mDNS is not running or this netif is not registered * - ESP_ERR_NO_MEM memory error */ -esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_action); +esp_err_t mdns_netif_action(esp_netif_t* esp_netif, mdns_event_actions_t event_action); /** * @brief Browse mDNS for a service `_service._proto`. @@ -845,7 +905,7 @@ esp_err_t mdns_netif_action(esp_netif_t *esp_netif, mdns_event_actions_t event_a * @return mdns_browse_t pointer to new browse object if initiated successfully. * NULL otherwise. */ -mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_browse_notify_t notifier); +mdns_browse_t* mdns_browse_new(const char* service, const char* proto, mdns_browse_notify_t notifier); /** * @brief Stop the `_service._proto` browse. @@ -856,7 +916,7 @@ mdns_browse_t *mdns_browse_new(const char *service, const char *proto, mdns_brow * - ESP_ERR_FAIL mDNS is not running or the browsing of `_service._proto` is never started. * - ESP_ERR_NO_MEM memory error. */ -esp_err_t mdns_browse_delete(const char *service, const char *proto); +esp_err_t mdns_browse_delete(const char* service, const char* proto); #ifdef __cplusplus } diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 6efff05b1d..c55d788b1b 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -2794,6 +2794,16 @@ static void _mdns_remove_scheduled_service_packets(mdns_service_t *service) } } +static void _mdns_free_service_subtype(mdns_service_t *service) +{ + while (service->subtype) { + mdns_subtype_t *next = service->subtype->next; + free((char *)service->subtype->subtype); + free(service->subtype); + service->subtype = next; + } +} + /** * @brief free service memory * @@ -2815,12 +2825,7 @@ static void _mdns_free_service(mdns_service_t *service) free((char *)s->value); free(s); } - while (service->subtype) { - mdns_subtype_t *next = service->subtype->next; - free((char *)service->subtype->subtype); - free(service->subtype); - service->subtype = next; - } + _mdns_free_service_subtype(service); free(service); } @@ -6299,7 +6304,32 @@ esp_err_t mdns_service_txt_item_remove(const char *service, const char *proto, c return mdns_service_txt_item_remove_for_host(NULL, service, proto, NULL, key); } -esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service, const char *proto, +static esp_err_t _mdns_service_subtype_remove_for_host(mdns_srv_item_t *service, const char *subtype) +{ + esp_err_t ret = ESP_ERR_NOT_FOUND; + mdns_subtype_t *srv_subtype = service->service->subtype; + mdns_subtype_t *pre = service->service->subtype; + while (srv_subtype) { + if (strcmp(srv_subtype->subtype, subtype) == 0) { + // Target subtype is found. + if (srv_subtype == service->service->subtype) { + // The first node needs to be removed + service->service->subtype = service->service->subtype->next; + } else { + pre->next = srv_subtype->next; + } + free((char *)srv_subtype->subtype); + free(srv_subtype); + ret = ESP_OK; + } + pre = srv_subtype; + srv_subtype = srv_subtype->next; + } + + return ret; +} + +esp_err_t mdns_service_subtype_remove_for_host(const char *instance_name, const char *service, const char *proto, const char *hostname, const char *subtype) { MDNS_SERVICE_LOCK(); @@ -6310,30 +6340,107 @@ esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const cha mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname); ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); - mdns_subtype_t *srv_subtype = s->service->subtype; + ret = _mdns_service_subtype_remove_for_host(s, subtype); + ESP_GOTO_ON_FALSE(s == ESP_OK, ESP_ERR_NOT_FOUND, err, TAG, "Subtype doesn't exist"); + + // TODO: Need to transmit a sendbye message for the removed subtype. + // TODO: Need to remove this subtype answer from the scheduled answer list. +err: + MDNS_SERVICE_UNLOCK(); + return ret; +} + +static esp_err_t _mdns_service_subtype_add_for_host(mdns_srv_item_t *service, const char *subtype) +{ + esp_err_t ret = ESP_OK; + mdns_subtype_t *srv_subtype = service->service->subtype; while (srv_subtype) { - ESP_GOTO_ON_FALSE(strcmp(srv_subtype->subtype, subtype) != 0, ESP_ERR_INVALID_ARG, err, TAG, "The same subtype has already been added"); + ESP_GOTO_ON_FALSE(strcmp(srv_subtype->subtype, subtype) != 0, ESP_ERR_INVALID_ARG, err, TAG, "Subtype: %s has already been added", subtype); srv_subtype = srv_subtype->next; } - mdns_service_t *srv = s->service; mdns_subtype_t *subtype_item = (mdns_subtype_t *)malloc(sizeof(mdns_subtype_t)); ESP_GOTO_ON_FALSE(subtype_item, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); subtype_item->subtype = strdup(subtype); ESP_GOTO_ON_FALSE(subtype_item->subtype, ESP_ERR_NO_MEM, out_of_mem, TAG, "Out of memory"); - subtype_item->next = srv->subtype; - srv->subtype = subtype_item; + subtype_item->next = service->service->subtype; + service->service->subtype = subtype_item; err: - MDNS_SERVICE_UNLOCK(); return ret; out_of_mem: - MDNS_SERVICE_UNLOCK(); HOOK_MALLOC_FAILED; free(subtype_item); return ret; } +esp_err_t mdns_service_subtype_add_multiple_items_for_host(const char *instance_name, const char *service, const char *proto, + const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items) +{ + MDNS_SERVICE_LOCK(); + esp_err_t ret = ESP_OK; + int cur_index = 0; + ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && + (num_items > 0), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + for (; cur_index < num_items; cur_index++) { + ret = _mdns_service_subtype_add_for_host(s, subtype[cur_index].subtype); + ESP_GOTO_ON_FALSE(ret != ESP_ERR_NO_MEM, ESP_ERR_NO_MEM, err, TAG, "Out of memory"); + } + + _mdns_announce_all_pcbs(&s, 1, false); +err: + if (ret == ESP_ERR_NO_MEM) { + for (int idx = 0; idx < cur_index; idx++) { + _mdns_service_subtype_remove_for_host(s, subtype[idx].subtype); + } + } + MDNS_SERVICE_UNLOCK(); + return ret; +} + +esp_err_t mdns_service_subtype_add_for_host(const char *instance_name, const char *service, const char *proto, + const char *hostname, const char *subtype) +{ + mdns_subtype_item_t _subtype[1]; + _subtype[0].subtype = subtype; + return mdns_service_subtype_add_multiple_items_for_host(instance_name, service, proto, hostname, _subtype, 1); +} + +esp_err_t mdns_service_subtype_update_multiple_items_for_host(const char *instance_name, const char *service, const char *proto, + const char *hostname, mdns_subtype_item_t subtype[], uint8_t num_items) +{ + MDNS_SERVICE_LOCK(); + esp_err_t ret = ESP_OK; + int cur_index = 0; + ESP_GOTO_ON_FALSE(_mdns_server && _mdns_server->services && !_str_null_or_empty(service) && !_str_null_or_empty(proto) && + (num_items > 0), ESP_ERR_INVALID_ARG, err, TAG, "Invalid state or arguments"); + + mdns_srv_item_t *s = _mdns_get_service_item_instance(instance_name, service, proto, hostname); + ESP_GOTO_ON_FALSE(s, ESP_ERR_NOT_FOUND, err, TAG, "Service doesn't exist"); + + // TODO: find subtype needs to say sendbye + _mdns_free_service_subtype(s->service); + + for (; cur_index < num_items; cur_index++) { + ret = _mdns_service_subtype_add_for_host(s, subtype[cur_index].subtype); + ESP_GOTO_ON_FALSE(ret != ESP_ERR_NO_MEM, ESP_ERR_NO_MEM, err, TAG, "Out of memory"); + } + + _mdns_announce_all_pcbs(&s, 1, false); +err: + if (ret == ESP_ERR_NO_MEM) { + for (int idx = 0; idx < cur_index; idx++) { + _mdns_service_subtype_remove_for_host(s, subtype[idx].subtype); + } + } + MDNS_SERVICE_UNLOCK(); + return ret; +} + esp_err_t mdns_service_instance_name_set_for_host(const char *instance_old, const char *service, const char *proto, const char *host, const char *instance) {