mirror of
https://github.com/espressif/esp-protocols.git
synced 2025-07-23 23:37:31 +02:00
feat(mdns): Decouple main module from mdns-networking
Removed sharing the data via the global variable `_mdns_server`, added API `mdns_is_netif_ready()`
This commit is contained in:
@ -1358,7 +1358,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
|
||||
} else if (answer->type == MDNS_TYPE_A) {
|
||||
if (answer->host == &_mdns_self_host) {
|
||||
esp_netif_ip_info_t if_ip_info;
|
||||
if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].pcb && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) {
|
||||
if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V4) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V4].state != PCB_DUP) {
|
||||
return 0;
|
||||
}
|
||||
if (esp_netif_get_ip_info(_mdns_get_esp_netif(tcpip_if), &if_ip_info)) {
|
||||
@ -1387,8 +1387,7 @@ static uint8_t _mdns_append_answer(uint8_t *packet, uint16_t *index, mdns_out_an
|
||||
if (answer->host == &_mdns_self_host) {
|
||||
struct esp_ip6_addr if_ip6s[NETIF_IPV6_MAX_NUMS];
|
||||
uint8_t count = 0;
|
||||
if (!_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].pcb &&
|
||||
_mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) {
|
||||
if (!mdns_is_netif_ready(tcpip_if, MDNS_IP_PROTOCOL_V6) && _mdns_server->interfaces[tcpip_if].pcbs[MDNS_IP_PROTOCOL_V6].state != PCB_DUP) {
|
||||
return 0;
|
||||
}
|
||||
count = esp_netif_get_all_ip6(_mdns_get_esp_netif(tcpip_if), if_ip6s);
|
||||
@ -2275,7 +2274,7 @@ static void _mdns_send_bye(mdns_srv_item_t **services, size_t len, bool include_
|
||||
|
||||
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
|
||||
for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
|
||||
if (_mdns_server->interfaces[i].pcbs[j].pcb && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) {
|
||||
if (mdns_is_netif_ready(i, j) && _mdns_server->interfaces[i].pcbs[j].state == PCB_RUNNING) {
|
||||
_mdns_pcb_send_bye((mdns_if_t)i, (mdns_ip_protocol_t)j, services, len, include_ip);
|
||||
}
|
||||
}
|
||||
@ -2289,7 +2288,7 @@ static void _mdns_announce_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protoco
|
||||
{
|
||||
mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol];
|
||||
size_t i;
|
||||
if (_pcb->pcb) {
|
||||
if (mdns_is_netif_ready(tcpip_if, ip_protocol)) {
|
||||
if (PCB_STATE_IS_PROBING(_pcb)) {
|
||||
_mdns_init_pcb_probe(tcpip_if, ip_protocol, services, len, include_ip);
|
||||
} else if (PCB_STATE_IS_ANNOUNCING(_pcb)) {
|
||||
@ -2333,7 +2332,7 @@ static void _mdns_probe_all_pcbs(mdns_srv_item_t **services, size_t len, bool pr
|
||||
uint8_t i, j;
|
||||
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
|
||||
for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
|
||||
if (_mdns_server->interfaces[i].pcbs[j].pcb) {
|
||||
if (mdns_is_netif_ready(i, j)) {
|
||||
mdns_pcb_t *_pcb = &_mdns_server->interfaces[i].pcbs[j];
|
||||
if (clear_old_probe) {
|
||||
free(_pcb->probe_services);
|
||||
@ -2628,7 +2627,7 @@ static void _mdns_remove_scheduled_service_packets(mdns_service_t *service)
|
||||
|
||||
|
||||
mdns_pcb_t *_pcb = &_mdns_server->interfaces[q->tcpip_if].pcbs[q->ip_protocol];
|
||||
if (_pcb->pcb) {
|
||||
if (mdns_is_netif_ready(q->tcpip_if, q->ip_protocol)) {
|
||||
if (PCB_STATE_IS_PROBING(_pcb)) {
|
||||
uint8_t i;
|
||||
//check if we are probing this service
|
||||
@ -2829,6 +2828,22 @@ static int _mdns_check_txt_collision(mdns_service_t *service, const uint8_t *dat
|
||||
return 0;//same
|
||||
}
|
||||
|
||||
static esp_err_t mdns_pcb_deinit_local(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_proto)
|
||||
{
|
||||
esp_err_t err = _mdns_pcb_deinit(tcpip_if, ip_proto);
|
||||
mdns_pcb_t *_pcb = &_mdns_server->interfaces[tcpip_if].pcbs[ip_proto];
|
||||
if (_pcb == NULL || err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
free(_pcb->probe_services);
|
||||
_pcb->state = PCB_OFF;
|
||||
_pcb->probe_ip = false;
|
||||
_pcb->probe_services = NULL;
|
||||
_pcb->probe_services_len = 0;
|
||||
_pcb->probe_running = false;
|
||||
_pcb->failed_probes = 0;
|
||||
return ESP_OK;
|
||||
}
|
||||
/**
|
||||
* @brief Set interface as duplicate if another is found on the same subnet
|
||||
*/
|
||||
@ -2840,11 +2855,11 @@ static void _mdns_dup_interface(mdns_if_t tcpip_if)
|
||||
return; // no other interface found
|
||||
}
|
||||
for (i = 0; i < MDNS_IP_PROTOCOL_MAX; i++) {
|
||||
if (_mdns_server->interfaces[other_if].pcbs[i].pcb) {
|
||||
if (mdns_is_netif_ready(other_if, i)) {
|
||||
//stop this interface and mark as dup
|
||||
if (_mdns_server->interfaces[tcpip_if].pcbs[i].pcb) {
|
||||
if (mdns_is_netif_ready(tcpip_if, i)) {
|
||||
_mdns_clear_pcb_tx_queue_head(tcpip_if, i);
|
||||
_mdns_pcb_deinit(tcpip_if, i);
|
||||
mdns_pcb_deinit_local(tcpip_if, i);
|
||||
}
|
||||
_mdns_server->interfaces[tcpip_if].pcbs[i].state = PCB_DUP;
|
||||
_mdns_announce_pcb(other_if, i, NULL, 0, true);
|
||||
@ -3984,8 +3999,9 @@ clear_rx_packet:
|
||||
*/
|
||||
void _mdns_enable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
|
||||
{
|
||||
if (!_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
|
||||
if (!mdns_is_netif_ready(tcpip_if, ip_protocol)) {
|
||||
if (_mdns_pcb_init(tcpip_if, ip_protocol)) {
|
||||
_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].failed_probes = 0;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -3999,9 +4015,9 @@ void _mdns_disable_pcb(mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
|
||||
{
|
||||
_mdns_clean_netif_ptr(tcpip_if);
|
||||
|
||||
if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb) {
|
||||
if (mdns_is_netif_ready(tcpip_if, ip_protocol)) {
|
||||
_mdns_clear_pcb_tx_queue_head(tcpip_if, ip_protocol);
|
||||
_mdns_pcb_deinit(tcpip_if, ip_protocol);
|
||||
mdns_pcb_deinit_local(tcpip_if, ip_protocol);
|
||||
mdns_if_t other_if = _mdns_get_other_if (tcpip_if);
|
||||
if (other_if != MDNS_MAX_INTERFACES && _mdns_server->interfaces[other_if].pcbs[ip_protocol].state == PCB_DUP) {
|
||||
_mdns_server->interfaces[other_if].pcbs[ip_protocol].state = PCB_OFF;
|
||||
@ -4669,7 +4685,7 @@ static mdns_tx_packet_t *_mdns_create_search_packet(mdns_search_once_t *search,
|
||||
static void _mdns_search_send_pcb(mdns_search_once_t *search, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
|
||||
{
|
||||
mdns_tx_packet_t *packet = NULL;
|
||||
if (_mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].pcb && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) {
|
||||
if (mdns_is_netif_ready(tcpip_if, ip_protocol) && _mdns_server->interfaces[tcpip_if].pcbs[ip_protocol].state > PCB_INIT) {
|
||||
packet = _mdns_create_search_packet(search, tcpip_if, ip_protocol);
|
||||
if (!packet) {
|
||||
return;
|
||||
@ -5483,7 +5499,7 @@ void mdns_free(void)
|
||||
_mdns_service_task_stop();
|
||||
for (i = 0; i < MDNS_MAX_INTERFACES; i++) {
|
||||
for (j = 0; j < MDNS_IP_PROTOCOL_MAX; j++) {
|
||||
_mdns_pcb_deinit(i, j);
|
||||
mdns_pcb_deinit_local(i, j);
|
||||
}
|
||||
}
|
||||
free((char *)_mdns_server->hostname);
|
||||
|
Reference in New Issue
Block a user