Skip to content

Commit 43de7e5

Browse files
committed
feat(mdns): Console test for add/remove delegated service APIs
1 parent ce7f326 commit 43de7e5

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

components/mdns/mdns.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6079,7 +6079,7 @@ esp_err_t mdns_instance_name_set(const char *instance)
60796079
esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *hostname,
60806080
uint16_t port, mdns_txt_item_t txt[], size_t num_items)
60816081
{
6082-
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !hostname) {
6082+
if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !_mdns_server->hostname) {
60836083
return ESP_ERR_INVALID_ARG;
60846084
}
60856085

@@ -6089,6 +6089,10 @@ esp_err_t mdns_service_add_for_host(const char *instance, const char *service, c
60896089
return ESP_ERR_NO_MEM;
60906090
}
60916091

6092+
if (!hostname) {
6093+
hostname = _mdns_server->hostname;
6094+
}
6095+
60926096
mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname);
60936097
MDNS_SERVICE_UNLOCK();
60946098
if (item) {

components/mdns/mdns_console.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ static struct {
697697
struct arg_str *proto;
698698
struct arg_int *port;
699699
struct arg_str *instance;
700+
struct arg_str *host;
700701
struct arg_str *txt;
701702
struct arg_end *end;
702703
} mdns_add_args;
@@ -718,6 +719,11 @@ static int cmd_mdns_service_add(int argc, char **argv)
718719
instance = mdns_add_args.instance->sval[0];
719720
printf("MDNS: Service Instance: %s\n", instance);
720721
}
722+
const char *host = NULL;
723+
if (mdns_add_args.host->count && mdns_add_args.host->sval[0]) {
724+
host = mdns_add_args.host->sval[0];
725+
printf("MDNS: Service for delegated host: %s\n", host);
726+
}
721727
mdns_txt_item_t *items = NULL;
722728
if (mdns_add_args.txt->count) {
723729
items = _convert_items(mdns_add_args.txt->sval, mdns_add_args.txt->count);
@@ -728,7 +734,8 @@ static int cmd_mdns_service_add(int argc, char **argv)
728734
}
729735
}
730736

731-
ESP_ERROR_CHECK( mdns_service_add(instance, mdns_add_args.service->sval[0], mdns_add_args.proto->sval[0], mdns_add_args.port->ival[0], items, mdns_add_args.txt->count) );
737+
ESP_ERROR_CHECK( mdns_service_add_for_host(instance, mdns_add_args.service->sval[0], mdns_add_args.proto->sval[0],
738+
host, mdns_add_args.port->ival[0], items, mdns_add_args.txt->count) );
732739
free(items);
733740
return 0;
734741
}
@@ -739,6 +746,7 @@ static void register_mdns_service_add(void)
739746
mdns_add_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
740747
mdns_add_args.port = arg_int1(NULL, NULL, "<port>", "Service Port");
741748
mdns_add_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
749+
mdns_add_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
742750
mdns_add_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)");
743751
mdns_add_args.end = arg_end(2);
744752

@@ -754,8 +762,10 @@ static void register_mdns_service_add(void)
754762
}
755763

756764
static struct {
765+
struct arg_str *instance;
757766
struct arg_str *service;
758767
struct arg_str *proto;
768+
struct arg_str *host;
759769
struct arg_end *end;
760770
} mdns_remove_args;
761771

@@ -772,15 +782,26 @@ static int cmd_mdns_service_remove(int argc, char **argv)
772782
return 1;
773783
}
774784

775-
ESP_ERROR_CHECK( mdns_service_remove(mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0]) );
785+
const char *instance = NULL;
786+
if (mdns_remove_args.instance->count && mdns_remove_args.instance->sval[0]) {
787+
instance = mdns_remove_args.instance->sval[0];
788+
}
789+
const char *host = NULL;
790+
if (mdns_remove_args.host->count && mdns_remove_args.host->sval[0]) {
791+
host = mdns_remove_args.host->sval[0];
792+
}
793+
794+
ESP_ERROR_CHECK( mdns_service_remove_for_host(instance, mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0], host) );
776795
return 0;
777796
}
778797

779798
static void register_mdns_service_remove(void)
780799
{
781800
mdns_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
782801
mdns_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
783-
mdns_remove_args.end = arg_end(2);
802+
mdns_remove_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
803+
mdns_remove_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
804+
mdns_remove_args.end = arg_end(4);
784805

785806
const esp_console_cmd_t cmd_remove = {
786807
.command = "mdns_service_remove",

components/mdns/tests/host_test/pytest_mdns.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,27 @@ def test_undelegate_host(mdns_console, dig_app):
8282
dig_app.check_record('delegated.local', query_type='A', expected=False)
8383

8484

85+
def test_add_delegated_service(mdns_console, dig_app):
86+
mdns_console.send_input('mdns_delegate_host delegated 1.2.3.4')
87+
dig_app.check_record('delegated.local', query_type='A', expected=True)
88+
mdns_console.send_input('mdns_service_add _test _tcp 80 -i local')
89+
mdns_console.get_output('MDNS: Service Instance: local')
90+
mdns_console.send_input('mdns_service_add _test2 _tcp 80 -i extern -h delegated')
91+
mdns_console.get_output('MDNS: Service Instance: extern')
92+
mdns_console.send_input('mdns_service_lookup _test _tcp')
93+
mdns_console.get_output('PTR : local')
94+
mdns_console.send_input('mdns_service_lookup _test2 _tcp -d')
95+
mdns_console.get_output('PTR : extern')
96+
dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=True)
97+
dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True)
98+
99+
100+
def test_remove_delegated_service(mdns_console, dig_app):
101+
mdns_console.send_input('mdns_service_remove _test2 _tcp -h delegated')
102+
mdns_console.send_input('mdns_service_lookup _test2 _tcp -d')
103+
mdns_console.get_output('No results found!')
104+
dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=False)
105+
106+
85107
if __name__ == '__main__':
86108
pytest.main(['-s', 'test_mdns.py'])

0 commit comments

Comments
 (0)