Skip to content

Commit

Permalink
mdns: Fix null-service issue when parsing packets
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak authored and suren-gabrielyan-espressif committed May 27, 2022
1 parent ec03fec commit 034c55e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions components/mdns/mdns.c
Expand Up @@ -158,6 +158,9 @@ static char * _mdns_mangle_name(char* in) {
static bool _mdns_service_match(const mdns_service_t * srv, const char * service, const char * proto,
const char * hostname)
{
if (!service || !proto) {
return false;
}
return !strcasecmp(srv->service, service) && !strcasecmp(srv->proto, proto) &&
(_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
}
Expand Down Expand Up @@ -289,6 +292,12 @@ static bool _mdns_instance_name_match(const char *lhs, const char *rhs)
static bool _mdns_service_match_instance(const mdns_service_t *srv, const char *instance, const char *service,
const char *proto, const char *hostname)
{
// service and proto must be supplied, if not this instance won't match
if (!service || !proto) {
return false;
}
// instance==NULL -> _mdns_instance_name_match() will check the default instance
// hostname==NULL -> matches if instance, service and proto matches
return !strcasecmp(srv->service, service) && _mdns_instance_name_match(srv->instance, instance) &&
!strcasecmp(srv->proto, proto) && (_str_null_or_empty(hostname) || !strcasecmp(srv->hostname, hostname));
}
Expand Down Expand Up @@ -1610,7 +1619,7 @@ static void _mdns_create_answer_from_parsed_packet(mdns_parsed_packet_t *parsed_
shared = q->type == MDNS_TYPE_PTR || q->type == MDNS_TYPE_SDPTR || !parsed_packet->probe;
if (q->type == MDNS_TYPE_SRV || q->type == MDNS_TYPE_TXT) {
mdns_srv_item_t *service = _mdns_get_service_item_instance(q->host, q->service, q->proto, NULL);
if (!_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
if (service == NULL || !_mdns_create_answer_from_service(packet, service->service, q, shared, send_flush)) {
_mdns_free_tx_packet(packet);
return;
}
Expand Down Expand Up @@ -3382,8 +3391,7 @@ void mdns_parse_packet(mdns_rx_packet_t * packet)
_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);
if (discovery && (service = _mdns_get_service_item(name->service, name->proto, NULL))) {
_mdns_remove_parsed_question(parsed_packet, MDNS_TYPE_SDPTR, service);
} else if (service && parsed_packet->questions && !parsed_packet->probe) {
_mdns_remove_parsed_question(parsed_packet, type, service);
Expand Down

0 comments on commit 034c55e

Please sign in to comment.