mdns: add more mdns result attributes

This commit is contained in:
Jiacheng Guo 2021-04-21 15:34:22 +08:00
parent 1fd56e0b87
commit 76ec76c12c
3 changed files with 75 additions and 33 deletions

View File

@ -75,10 +75,13 @@ typedef struct mdns_result_s {
struct mdns_result_s * next; /*!< next result, or NULL for the last result in the list */
mdns_if_t tcpip_if; /*!< interface index */
uint32_t ttl; /*!< time to live */
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 */
// SRV
char * hostname; /*!< hostname */
uint16_t port; /*!< service port */

View File

@ -41,10 +41,16 @@ static SemaphoreHandle_t _mdns_service_semaphore = NULL;
static void _mdns_search_finish_done(void);
static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * search, mdns_name_t * name, uint16_t type, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char * hostname, uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_item_t * txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol);
static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl);
static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
uint32_t ttl);
static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance,
const char * service_type, const char * proto, mdns_if_t tcpip_if,
mdns_ip_protocol_t ip_protocol, uint32_t ttl);
static bool _mdns_append_host_list_in_services(mdns_out_answer_t ** destination, mdns_srv_item_t * services[], size_t services_len, bool flush, bool bye);
static bool _mdns_append_host_list(mdns_out_answer_t ** destination, bool flush, bool bye);
static void _mdns_remap_self_service_hostname(const char *old_hostname, const char *new_hostname);
@ -3162,7 +3168,8 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
continue;//error
}
if (search_result) {
_mdns_search_result_add_ptr(search_result, name->host, packet->tcpip_if, packet->ip_protocol);
_mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
packet->tcpip_if, packet->ip_protocol, ttl);
} else if ((discovery || ours) && !name->sub && _mdns_name_is_ours(name)) {
if (discovery) {
service = _mdns_get_service_item(name->service, name->proto, NULL);
@ -3189,7 +3196,8 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
result = result->next;
}
if (!result) {
result = _mdns_search_result_add_ptr(search_result, name->host, packet->tcpip_if, packet->ip_protocol);
result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
packet->tcpip_if, packet->ip_protocol, ttl);
if (!result) {
continue;//error
}
@ -3210,7 +3218,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
result->hostname = strdup(name->host);
}
} else {
_mdns_search_result_add_srv(search_result, name->host, port, packet->tcpip_if, packet->ip_protocol);
_mdns_search_result_add_srv(search_result, name->host, port, packet->tcpip_if, packet->ip_protocol, ttl);
}
} else if (ours) {
if (parsed_packet->questions && !parsed_packet->probe) {
@ -3285,7 +3293,8 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
result = result->next;
}
if (!result) {
result = _mdns_search_result_add_ptr(search_result, name->host, packet->tcpip_if, packet->ip_protocol);
result = _mdns_search_result_add_ptr(search_result, name->host, name->service, name->proto,
packet->tcpip_if, packet->ip_protocol, ttl);
if (!result) {
continue;//error
}
@ -3301,7 +3310,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
} else {
_mdns_result_txt_create(data_ptr, data_len, &txt, &txt_value_len, &txt_count);
if (txt_count) {
_mdns_search_result_add_txt(search_result, txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol);
_mdns_search_result_add_txt(search_result, txt, txt_value_len, txt_count, packet->tcpip_if, packet->ip_protocol, ttl);
}
}
} else if (ours) {
@ -3335,7 +3344,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
if (search_result) {
//check for more applicable searches (PTR & A/AAAA at the same time)
while (search_result) {
_mdns_search_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol);
_mdns_search_result_add_ip(search_result, name->host, &ip6, packet->tcpip_if, packet->ip_protocol, ttl);
search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
}
} else if (ours) {
@ -3385,7 +3394,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
if (search_result) {
//check for more applicable searches (PTR & A/AAAA at the same time)
while (search_result) {
_mdns_search_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol);
_mdns_search_result_add_ip(search_result, name->host, &ip, packet->tcpip_if, packet->ip_protocol, ttl);
search_result = _mdns_search_find_from(search_result->next, name, type, packet->tcpip_if, packet->ip_protocol);
}
} else if (ours) {
@ -3699,6 +3708,11 @@ static mdns_ip_addr_t * _mdns_result_addr_create_ip(esp_ip_addr_t * ip)
return a;
}
static inline void _mdns_result_update_ttl(mdns_result_t * r, uint32_t ttl)
{
r->ttl = r->ttl < ttl ? r->ttl : ttl;
}
/**
* @brief Chain new IP to search result
*/
@ -3727,7 +3741,8 @@ static void _mdns_result_add_ip(mdns_result_t * r, esp_ip_addr_t * ip)
/**
* @brief Called from parser to add A/AAAA data to search result
*/
static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char * hostname, esp_ip_addr_t * ip,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
{
mdns_result_t * r = NULL;
mdns_ip_addr_t * a = NULL;
@ -3739,6 +3754,7 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char *
while (r) {
if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol) {
_mdns_result_add_ip(r, ip);
_mdns_result_update_ttl(r, ttl);
return;
}
r = r->next;
@ -3758,18 +3774,21 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char *
return;
}
a->next = r->addr;
r->hostname = strdup(hostname);
r->addr = a;
r->tcpip_if = tcpip_if;
r->ip_protocol = ip_protocol;
r->next = search->result;
r->ttl = ttl;
search->result = r;
search->num_results++;
}
} else if (search->type == MDNS_TYPE_PTR) {
} else if (search->type == MDNS_TYPE_PTR || search->type == MDNS_TYPE_SRV) {
r = search->result;
while (r) {
if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
_mdns_result_add_ip(r, ip);
_mdns_result_update_ttl(r, ttl);
break;
}
r = r->next;
@ -3780,11 +3799,14 @@ static void _mdns_search_result_add_ip(mdns_search_once_t * search, const char *
/**
* @brief Called from parser to add PTR data to search result
*/
static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search, const char * instance,
const char * service_type, const char * proto, mdns_if_t tcpip_if,
mdns_ip_protocol_t ip_protocol, uint32_t ttl)
{
mdns_result_t * r = search->result;
while (r) {
if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name)) {
_mdns_result_update_ttl(r, ttl);
return r;
}
r = r->next;
@ -3798,6 +3820,8 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search,
memset(r, 0 , sizeof(mdns_result_t));
r->instance_name = strdup(instance);
r->service_type = strdup(service_type);
r->proto = strdup(proto);
if (!r->instance_name) {
free(r);
return NULL;
@ -3805,6 +3829,7 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search,
r->tcpip_if = tcpip_if;
r->ip_protocol = ip_protocol;
r->ttl = ttl;
r->next = search->result;
search->result = r;
search->num_results++;
@ -3816,11 +3841,13 @@ static mdns_result_t * _mdns_search_result_add_ptr(mdns_search_once_t * search,
/**
* @brief Called from parser to add SRV data to search result
*/
static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char * hostname, uint16_t port, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
static void _mdns_search_result_add_srv(mdns_search_once_t *search, const char *hostname, uint16_t port,
mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol, uint32_t ttl)
{
mdns_result_t * r = search->result;
while (r) {
if (r->tcpip_if == tcpip_if && r->ip_protocol == ip_protocol && !_str_null_or_empty(r->hostname) && !strcasecmp(hostname, r->hostname)) {
_mdns_result_update_ttl(r, ttl);
return;
}
r = r->next;
@ -3838,9 +3865,15 @@ static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char
free(r);
return;
}
if (search->instance) {
r->instance_name = strdup(search->instance);
}
r->service_type = strdup(search->service);
r->proto = strdup(search->proto);
r->port = port;
r->tcpip_if = tcpip_if;
r->ip_protocol = ip_protocol;
r->ttl = ttl;
r->next = search->result;
search->result = r;
search->num_results++;
@ -3850,7 +3883,9 @@ static void _mdns_search_result_add_srv(mdns_search_once_t * search, const char
/**
* @brief Called from parser to add TXT data to search result
*/
static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_item_t * txt, uint8_t *txt_value_len, size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol)
static void _mdns_search_result_add_txt(mdns_search_once_t *search, mdns_txt_item_t *txt, uint8_t *txt_value_len,
size_t txt_count, mdns_if_t tcpip_if, mdns_ip_protocol_t ip_protocol,
uint32_t ttl)
{
mdns_result_t * r = search->result;
while (r) {
@ -3861,6 +3896,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
r->txt = txt;
r->txt_value_len = txt_value_len;
r->txt_count = txt_count;
_mdns_result_update_ttl(r, ttl);
return;
}
r = r->next;
@ -3878,6 +3914,7 @@ static void _mdns_search_result_add_txt(mdns_search_once_t * search, mdns_txt_it
r->txt_count = txt_count;
r->tcpip_if = tcpip_if;
r->ip_protocol = ip_protocol;
r->ttl = ttl;
r->next = search->result;
search->result = r;
search->num_results++;
@ -3906,12 +3943,12 @@ static mdns_search_once_t * _mdns_search_find_from(mdns_search_once_t * s, mdns_
if (type == MDNS_TYPE_A || type == MDNS_TYPE_AAAA) {
if ((s->type == MDNS_TYPE_ANY && s->service != NULL)
|| (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR))
|| (s->type != MDNS_TYPE_ANY && s->type != type && s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV))
{
s = s->next;
continue;
}
if (s->type != MDNS_TYPE_PTR) {
if (s->type != MDNS_TYPE_PTR && s->type != MDNS_TYPE_SRV) {
if (!strcasecmp(name->host, s->instance)) {
return s;
}
@ -5266,6 +5303,8 @@ void mdns_query_results_free(mdns_result_t * results)
free((char *)(r->hostname));
free((char *)(r->instance_name));
free((char *)(r->service_type));
free((char *)(r->proto));
for (size_t i=0; i<r->txt_count; i++) {
free((char *)(r->txt[i].key));

View File

@ -85,29 +85,30 @@ static const char * if_str[] = {"STA", "AP", "ETH", "MAX"};
/* these strings match mdns_ip_protocol_t enumeration */
static const char * ip_protocol_str[] = {"V4", "V6", "MAX"};
static void mdns_print_results(mdns_result_t * results){
mdns_result_t * r = results;
mdns_ip_addr_t * a = NULL;
static void mdns_print_results(mdns_result_t *results)
{
mdns_result_t *r = results;
mdns_ip_addr_t *a = NULL;
int i = 1, t;
while(r){
printf("%d: Interface: %s, Type: %s\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol]);
if(r->instance_name){
printf(" PTR : %s\n", r->instance_name);
while (r) {
printf("%d: Interface: %s, Type: %s, TTL: %u\n", i++, if_str[r->tcpip_if], ip_protocol_str[r->ip_protocol],
r->ttl);
if (r->instance_name) {
printf(" PTR : %s.%s.%s\n", r->instance_name, r->service_type, r->proto);
}
if(r->hostname){
if (r->hostname) {
printf(" SRV : %s.local:%u\n", r->hostname, r->port);
}
if(r->txt_count){
if (r->txt_count) {
printf(" TXT : [%zu] ", r->txt_count);
for(t=0; t<r->txt_count; t++){
printf("%s=%s(%d); ", r->txt[t].key, r->txt[t].value?r->txt[t].value:"NULL",
r->txt_value_len[t]);
for (t = 0; t < r->txt_count; t++) {
printf("%s=%s(%d); ", r->txt[t].key, r->txt[t].value ? r->txt[t].value : "NULL", r->txt_value_len[t]);
}
printf("\n");
}
a = r->addr;
while(a){
if(a->addr.type == ESP_IPADDR_TYPE_V6){
while (a) {
if (a->addr.type == ESP_IPADDR_TYPE_V6) {
printf(" AAAA: " IPV6STR "\n", IPV62STR(a->addr.u_addr.ip6));
} else {
printf(" A : " IPSTR "\n", IP2STR(&(a->addr.u_addr.ip4)));
@ -116,7 +117,6 @@ static void mdns_print_results(mdns_result_t * results){
}
r = r->next;
}
}
static void query_mdns_service(const char * service_name, const char * proto)