Skip to content

Commit a8339e4

Browse files
committed
fix(mdns): Allow setting instance name only after hostname set
Closes #190
1 parent 12cfcb5 commit a8339e4

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

components/mdns/mdns.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5398,7 +5398,7 @@ esp_err_t mdns_instance_name_set(const char *instance)
53985398
if (!_mdns_server) {
53995399
return ESP_ERR_INVALID_STATE;
54005400
}
5401-
if (_str_null_or_empty(instance) || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
5401+
if (_str_null_or_empty(instance) || _mdns_server->hostname == NULL || strlen(instance) > (MDNS_NAME_BUF_LEN - 1)) {
54025402
return ESP_ERR_INVALID_ARG;
54035403
}
54045404
char *new_instance = strndup(instance, MDNS_NAME_BUF_LEN - 1);

components/mdns/tests/unit_test/main/test_mdns.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ TEST(mdns, query_api_fails_with_expected_err)
115115
TEST_ASSERT_EQUAL(ESP_OK, esp_event_loop_create_default());
116116

117117
TEST_ASSERT_EQUAL(ESP_OK, mdns_init() );
118+
// check it is not possible to register a service or set an instance without configuring the hostname
119+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, mdns_service_add(MDNS_INSTANCE, MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_SERVICE_PORT, NULL, 0));
120+
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, mdns_instance_name_set(MDNS_INSTANCE));
121+
TEST_ASSERT_EQUAL(ESP_OK, mdns_hostname_set(MDNS_HOSTNAME));
122+
// hostname is set, now adding a service and instance should succeed
123+
TEST_ASSERT_EQUAL(ESP_OK, mdns_service_add(MDNS_INSTANCE, MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, MDNS_SERVICE_PORT, NULL, 0));
124+
TEST_ASSERT_EQUAL(ESP_OK, mdns_instance_name_set(MDNS_INSTANCE));
125+
118126
TEST_ASSERT_EQUAL(ESP_OK, mdns_query_ptr(MDNS_SERVICE_NAME, MDNS_SERVICE_PROTO, 10, CONFIG_MDNS_MAX_SERVICES, &results) );
119127
mdns_query_results_free(results);
120128

0 commit comments

Comments
 (0)