Skip to content

Commit

Permalink
Merge pull request #1 from Eltako/bugfix/fix-mdns-nullptr-access
Browse files Browse the repository at this point in the history
[mdns] Add function to check whether mdns has been startet and a hostname has been set
  • Loading branch information
Caldwell1988 authored and dbahrdt committed Jul 23, 2024
2 parents 5d69d3f + 39545f0 commit 6f51baf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/mdns/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: "1.3.2"
description: mDNS
url: https://github.com/espressif/esp-protocols/tree/master/components/mdns
url: https://github.com/Eltako/esp-protocols/tree/bugfix/fix-mdns-nullptr-access/components/mdns
dependencies:
idf:
version: ">=5.0"
12 changes: 12 additions & 0 deletions components/mdns/include/mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ esp_err_t mdns_init(void);
*/
void mdns_free(void);

/**
* @brief Check whether mDNS is running
*
*/
bool mdns_is_initialized();

/**
* @brief Check whether mDNS has a hostname set
*
*/
bool mdns_has_hostname();

/**
* @brief Set the hostname for mDNS server
* required if you want to advertise services
Expand Down
10 changes: 9 additions & 1 deletion components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static const char *MDNS_SUB_STR = "_sub";

mdns_server_t *_mdns_server = NULL;
static mdns_host_item_t *_mdns_host_list = NULL;
static mdns_host_item_t _mdns_self_host;
static mdns_host_item_t _mdns_self_host = {.hostname = NULL, .address_list = NULL, .next = NULL};

static const char *TAG = "mdns";

Expand Down Expand Up @@ -5896,6 +5896,14 @@ void mdns_free(void)
_mdns_server = NULL;
}

bool mdns_is_initialized() {
return _mdns_server != NULL;
}

bool mdns_has_hostname() {
return mdns_is_initialized() && _mdns_server->hostname != NULL && _mdns_self_host.hostname != NULL;
}

esp_err_t mdns_hostname_set(const char *hostname)
{
if (!_mdns_server) {
Expand Down

0 comments on commit 6f51baf

Please sign in to comment.