From 43de7e5c4d7671a87071cdfe1708fd267b7fa2cd Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 12:34:03 +0200 Subject: [PATCH] feat(mdns): Console test for add/remove delegated service APIs --- components/mdns/mdns.c | 6 ++++- components/mdns/mdns_console.c | 27 ++++++++++++++++--- .../mdns/tests/host_test/pytest_mdns.py | 22 +++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/components/mdns/mdns.c b/components/mdns/mdns.c index 9679f6245..cc359c803 100644 --- a/components/mdns/mdns.c +++ b/components/mdns/mdns.c @@ -6079,7 +6079,7 @@ esp_err_t mdns_instance_name_set(const char *instance) esp_err_t mdns_service_add_for_host(const char *instance, const char *service, const char *proto, const char *hostname, uint16_t port, mdns_txt_item_t txt[], size_t num_items) { - if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !hostname) { + if (!_mdns_server || _str_null_or_empty(service) || _str_null_or_empty(proto) || !port || !_mdns_server->hostname) { return ESP_ERR_INVALID_ARG; } @@ -6089,6 +6089,10 @@ esp_err_t mdns_service_add_for_host(const char *instance, const char *service, c return ESP_ERR_NO_MEM; } + if (!hostname) { + hostname = _mdns_server->hostname; + } + mdns_srv_item_t *item = _mdns_get_service_item_instance(instance, service, proto, hostname); MDNS_SERVICE_UNLOCK(); if (item) { diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 02b86b446..8faa5e0f5 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -697,6 +697,7 @@ static struct { struct arg_str *proto; struct arg_int *port; struct arg_str *instance; + struct arg_str *host; struct arg_str *txt; struct arg_end *end; } mdns_add_args; @@ -718,6 +719,11 @@ static int cmd_mdns_service_add(int argc, char **argv) instance = mdns_add_args.instance->sval[0]; printf("MDNS: Service Instance: %s\n", instance); } + const char *host = NULL; + if (mdns_add_args.host->count && mdns_add_args.host->sval[0]) { + host = mdns_add_args.host->sval[0]; + printf("MDNS: Service for delegated host: %s\n", host); + } mdns_txt_item_t *items = NULL; if (mdns_add_args.txt->count) { 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) } } - 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) ); + ESP_ERROR_CHECK( mdns_service_add_for_host(instance, mdns_add_args.service->sval[0], mdns_add_args.proto->sval[0], + host, mdns_add_args.port->ival[0], items, mdns_add_args.txt->count) ); free(items); return 0; } @@ -739,6 +746,7 @@ static void register_mdns_service_add(void) mdns_add_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); mdns_add_args.port = arg_int1(NULL, NULL, "", "Service Port"); mdns_add_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_add_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); mdns_add_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)"); mdns_add_args.end = arg_end(2); @@ -754,8 +762,10 @@ static void register_mdns_service_add(void) } static struct { + struct arg_str *instance; struct arg_str *service; struct arg_str *proto; + struct arg_str *host; struct arg_end *end; } mdns_remove_args; @@ -772,7 +782,16 @@ static int cmd_mdns_service_remove(int argc, char **argv) return 1; } - ESP_ERROR_CHECK( mdns_service_remove(mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0]) ); + const char *instance = NULL; + if (mdns_remove_args.instance->count && mdns_remove_args.instance->sval[0]) { + instance = mdns_remove_args.instance->sval[0]; + } + const char *host = NULL; + if (mdns_remove_args.host->count && mdns_remove_args.host->sval[0]) { + host = mdns_remove_args.host->sval[0]; + } + + ESP_ERROR_CHECK( mdns_service_remove_for_host(instance, mdns_remove_args.service->sval[0], mdns_remove_args.proto->sval[0], host) ); return 0; } @@ -780,7 +799,9 @@ static void register_mdns_service_remove(void) { mdns_remove_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); mdns_remove_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); - mdns_remove_args.end = arg_end(2); + mdns_remove_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); + mdns_remove_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_remove_args.end = arg_end(4); const esp_console_cmd_t cmd_remove = { .command = "mdns_service_remove", diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index e697b1995..b45a6f3e7 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -82,5 +82,27 @@ def test_undelegate_host(mdns_console, dig_app): dig_app.check_record('delegated.local', query_type='A', expected=False) +def test_add_delegated_service(mdns_console, dig_app): + mdns_console.send_input('mdns_delegate_host delegated 1.2.3.4') + dig_app.check_record('delegated.local', query_type='A', expected=True) + mdns_console.send_input('mdns_service_add _test _tcp 80 -i local') + mdns_console.get_output('MDNS: Service Instance: local') + mdns_console.send_input('mdns_service_add _test2 _tcp 80 -i extern -h delegated') + mdns_console.get_output('MDNS: Service Instance: extern') + mdns_console.send_input('mdns_service_lookup _test _tcp') + mdns_console.get_output('PTR : local') + mdns_console.send_input('mdns_service_lookup _test2 _tcp -d') + mdns_console.get_output('PTR : extern') + dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=True) + dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True) + + +def test_remove_delegated_service(mdns_console, dig_app): + mdns_console.send_input('mdns_service_remove _test2 _tcp -h delegated') + mdns_console.send_input('mdns_service_lookup _test2 _tcp -d') + mdns_console.get_output('No results found!') + dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=False) + + if __name__ == '__main__': pytest.main(['-s', 'test_mdns.py'])