From a91ead8ef59d48285c8dd089bdc9a7778ab52410 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 10:21:53 +0200 Subject: [PATCH 1/9] feat(mdns): Console test for lookup service APIs --- components/mdns/mdns_console.c | 74 ++++++++++++++++++- .../mdns/tests/host_test/pytest_mdns.py | 4 + 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 9fe6d4b3e1..6ad2fdcbf4 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -19,7 +19,10 @@ static void mdns_print_results(mdns_result_t *results) mdns_ip_addr_t *a = NULL; int i = 1; while (r) { - printf("%d: Interface: %s, Type: %s\n", i++, esp_netif_get_ifkey(r->esp_netif), ip_protocol_str[r->ip_protocol]); + if (r->esp_netif) { + printf("%d: Interface: %s, Type: %s, TTL: %" PRIu32 "\n", i++, esp_netif_get_ifkey(r->esp_netif), + ip_protocol_str[r->ip_protocol], r->ttl); + } if (r->instance_name) { printf(" PTR : %s\n", r->instance_name); } @@ -1031,6 +1034,73 @@ static void register_mdns_service_remove_all(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_free) ); } +#define MDNS_MAX_LOOKUP_RESULTS CONFIG_MDNS_MAX_SERVICES + +static struct { + struct arg_str *instance; + struct arg_str *service; + struct arg_str *proto; + struct arg_lit *delegated; + struct arg_end *end; +} mdns_lookup_service_args; + +static esp_err_t lookup_service(const char *instance, const char *service, const char *proto, size_t max_results, + mdns_result_t **result, bool delegated) +{ + if (delegated) { + return mdns_lookup_delegated_service(instance, service, proto, max_results, result); + } + return mdns_lookup_selfhosted_service(instance, service, proto, max_results, result); +} + +static int cmd_mdns_lookup_service(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &mdns_lookup_service_args); + if (nerrors != 0) { + arg_print_errors(stderr, mdns_lookup_service_args.end, argv[0]); + return 1; + } + + if (!mdns_lookup_service_args.instance->sval[0] || !mdns_lookup_service_args.service->sval[0] || !mdns_lookup_service_args.proto->sval[0]) { + printf("ERROR: Bad arguments!\n"); + return 1; + } + mdns_result_t *results = NULL; + esp_err_t err = lookup_service(mdns_lookup_service_args.instance->count ? mdns_lookup_service_args.instance->sval[0] : NULL, + mdns_lookup_service_args.service->sval[0], mdns_lookup_service_args.proto->sval[0], + MDNS_MAX_LOOKUP_RESULTS, &results, mdns_lookup_service_args.delegated->count); + if (err) { + printf("Service lookup failed\n"); + return 1; + } + if (!results) { + printf("No results found!\n"); + return 0; + } + mdns_print_results(results); + mdns_query_results_free(results); + return 0; +} + +static void register_mdns_lookup_service(void) +{ + mdns_lookup_service_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); + mdns_lookup_service_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); + mdns_lookup_service_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_lookup_service_args.delegated = arg_lit0("d", "delegated", "Lookup delegated services"); + mdns_lookup_service_args.end = arg_end(4); + + const esp_console_cmd_t cmd_lookup_service = { + .command = "mdns_service_lookup", + .help = "Lookup registered service", + .hint = NULL, + .func = &cmd_mdns_lookup_service, + .argtable = &mdns_lookup_service_args + }; + + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_lookup_service) ); +} + void mdns_console_register(void) { register_mdns_init(); @@ -1046,6 +1116,8 @@ void mdns_console_register(void) register_mdns_service_txt_remove(); register_mdns_service_remove_all(); + register_mdns_lookup_service(); + #ifdef CONFIG_LWIP_IPV4 register_mdns_query_a(); #endif diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index 6f2a9763b4..20f5074f7f 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -60,11 +60,15 @@ def test_mdns_init(mdns_console, dig_app): def test_add_service(mdns_console, dig_app): mdns_console.send_input('mdns_service_add _http _tcp 80 -i test_service') mdns_console.get_output('MDNS: Service Instance: test_service') + mdns_console.send_input('mdns_service_lookup _http _tcp') + mdns_console.get_output('PTR : test_service') dig_app.check_record('_http._tcp.local', query_type='PTR', expected=True) def test_remove_service(mdns_console, dig_app): mdns_console.send_input('mdns_service_remove _http _tcp') + mdns_console.send_input('mdns_service_lookup _http _tcp') + mdns_console.get_output('No results found!') dig_app.check_record('_http._tcp.local', query_type='PTR', expected=False) From ce7f326af0ff1d6151feb3aa891ae359c48a157d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 11:42:18 +0200 Subject: [PATCH 2/9] feat(mdns): Console test for add/remove delegated host APIs --- components/mdns/mdns_console.c | 91 +++++++++++++++++++ .../esp_netif_linux/esp_netif_linux.c | 13 +++ .../mdns/tests/host_test/pytest_mdns.py | 10 ++ 3 files changed, 114 insertions(+) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 6ad2fdcbf4..02b86b446d 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -1101,6 +1101,95 @@ static void register_mdns_lookup_service(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_lookup_service) ); } +static struct { + struct arg_str *hostname; + struct arg_str *address; + struct arg_end *end; +} mdns_delegate_host_args; + +static int cmd_mdns_delegate_host(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &mdns_delegate_host_args); + if (nerrors != 0) { + arg_print_errors(stderr, mdns_delegate_host_args.end, argv[0]); + return 1; + } + + if (!mdns_delegate_host_args.hostname->sval[0] || !mdns_delegate_host_args.address->sval[0]) { + printf("ERROR: Bad arguments!\n"); + return 1; + } + + mdns_ip_addr_t addr = { .next = NULL}; + esp_netif_str_to_ip4(mdns_delegate_host_args.address->sval[0], &addr.addr.u_addr.ip4); + addr.addr.type = ESP_IPADDR_TYPE_V4; + + esp_err_t err = mdns_delegate_hostname_add(mdns_delegate_host_args.hostname->sval[0], &addr); + if (err) { + printf("mdns_delegate_hostname_add() failed\n"); + return 1; + } + return 0; +} + +static void register_mdns_delegate_host(void) +{ + mdns_delegate_host_args.hostname = arg_str1(NULL, NULL, "", "Delegated hostname"); + mdns_delegate_host_args.address = arg_str1(NULL, NULL, "
", "Delegated hosts address"); + mdns_delegate_host_args.end = arg_end(2); + + const esp_console_cmd_t cmd_delegate_host = { + .command = "mdns_delegate_host", + .help = "Add delegated hostname", + .hint = NULL, + .func = &cmd_mdns_delegate_host, + .argtable = &mdns_delegate_host_args + }; + + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_delegate_host) ); +} + +static struct { + struct arg_str *hostname; + struct arg_end *end; +} mdns_undelegate_host_args; + +static int cmd_mdns_undelegate_host(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &mdns_undelegate_host_args); + if (nerrors != 0) { + arg_print_errors(stderr, mdns_undelegate_host_args.end, argv[0]); + return 1; + } + + if (!mdns_undelegate_host_args.hostname->sval[0]) { + printf("ERROR: Bad arguments!\n"); + return 1; + } + + if (mdns_delegate_hostname_remove(mdns_undelegate_host_args.hostname->sval[0]) != ESP_OK) { + printf("mdns_delegate_hostname_remove() failed\n"); + return 1; + } + return 0; +} + +static void register_mdns_undelegate_host(void) +{ + mdns_undelegate_host_args.hostname = arg_str1(NULL, NULL, "", "Delegated hostname"); + mdns_undelegate_host_args.end = arg_end(2); + + const esp_console_cmd_t cmd_undelegate_host = { + .command = "mdns_undelegate_host", + .help = "Remove delegated hostname", + .hint = NULL, + .func = &cmd_mdns_undelegate_host, + .argtable = &mdns_undelegate_host_args + }; + + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_undelegate_host) ); +} + void mdns_console_register(void) { register_mdns_init(); @@ -1117,6 +1206,8 @@ void mdns_console_register(void) register_mdns_service_remove_all(); register_mdns_lookup_service(); + register_mdns_delegate_host(); + register_mdns_undelegate_host(); #ifdef CONFIG_LWIP_IPV4 register_mdns_query_a(); diff --git a/components/mdns/tests/host_test/components/esp_netif_linux/esp_netif_linux.c b/components/mdns/tests/host_test/components/esp_netif_linux/esp_netif_linux.c index d769ebbe06..93e4386c81 100644 --- a/components/mdns/tests/host_test/components/esp_netif_linux/esp_netif_linux.c +++ b/components/mdns/tests/host_test/components/esp_netif_linux/esp_netif_linux.c @@ -182,3 +182,16 @@ const char *esp_netif_get_ifkey(esp_netif_t *esp_netif) { return esp_netif->if_key; } + +esp_err_t esp_netif_str_to_ip4(const char *src, esp_ip4_addr_t *dst) +{ + if (src == NULL || dst == NULL) { + return ESP_ERR_INVALID_ARG; + } + struct in_addr addr; + if (inet_pton(AF_INET, src, &addr) != 1) { + return ESP_FAIL; + } + dst->addr = addr.s_addr; + return ESP_OK; +} diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index 20f5074f7f..e697b1995f 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -72,5 +72,15 @@ def test_remove_service(mdns_console, dig_app): dig_app.check_record('_http._tcp.local', query_type='PTR', expected=False) +def test_delegate_host(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) + + +def test_undelegate_host(mdns_console, dig_app): + mdns_console.send_input('mdns_undelegate_host delegated') + dig_app.check_record('delegated.local', query_type='A', expected=False) + + if __name__ == '__main__': pytest.main(['-s', 'test_mdns.py']) From 43de7e5c4d7671a87071cdfe1708fd267b7fa2cd Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 12:34:03 +0200 Subject: [PATCH 3/9] 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 9679f6245a..cc359c8032 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 02b86b446d..8faa5e0f56 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 e697b1995f..b45a6f3e78 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']) From 6b9a6ce65bc204b1fe8d887eb5261445a588d8e5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 13:46:37 +0200 Subject: [PATCH 4/9] feat(mdns): Console test for changing TXT records --- components/mdns/tests/host_test/dnsfixture.py | 8 +++++--- components/mdns/tests/host_test/pytest_mdns.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/components/mdns/tests/host_test/dnsfixture.py b/components/mdns/tests/host_test/dnsfixture.py index e3b4051233..6dcf0c99f5 100644 --- a/components/mdns/tests/host_test/dnsfixture.py +++ b/components/mdns/tests/host_test/dnsfixture.py @@ -85,14 +85,16 @@ def parse_answer_section(self, response, query_type): answers.append(full_answer) return answers - def check_record(self, name, query_type, expected=True): + def check_record(self, name, query_type, expected=True, expect=None): output = self.run_query(name, query_type=query_type) answers = self.parse_answer_section(output, query_type) logger.info(f'answers: {answers}') + if expect is None: + expect = name if expected: - assert any(name in answer for answer in answers), f"Expected service '{name}' not found in answer section" + assert any(expect in answer for answer in answers), f"Expected record '{expect}' not found in answer section" else: - assert not any(name in answer for answer in answers), f"Unexpected service '{name}' found in answer section" + assert not any(expect in answer for answer in answers), f"Unexpected record '{expect}' found in answer section" if __name__ == '__main__': diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index b45a6f3e78..04ce9f2305 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -104,5 +104,20 @@ def test_remove_delegated_service(mdns_console, dig_app): dig_app.check_record('_test2._tcp.local', query_type='PTR', expected=False) +def test_update_txt(mdns_console, dig_app): + mdns_console.send_input('mdns_service_txt_set _test _tcp key1 value1') + dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True) + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key1=value1') + mdns_console.send_input('mdns_service_txt_set _test _tcp key2 value2') + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key2=value2') + mdns_console.send_input('mdns_service_txt_remove _test _tcp key2') + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=False, expect='key2=value2') + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key1=value1') + mdns_console.send_input('mdns_service_txt_replace _test _tcp key3=value3 key4=value4') + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=False, expect='key1=value1') + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key3=value3') + dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key4=value4') + + if __name__ == '__main__': pytest.main(['-s', 'test_mdns.py']) From c9a58d7350f0d8da6e8064ae7a708272981569c7 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 15:12:04 +0200 Subject: [PATCH 5/9] feat(mdns): Console test: add/remove TXT recs for delegated srvs --- components/mdns/mdns_console.c | 52 ++++++++++++++++--- .../mdns/tests/host_test/pytest_mdns.py | 36 ++++++++----- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 8faa5e0f56..73325d3f29 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -901,6 +901,8 @@ static void register_mdns_service_port_set(void) static struct { struct arg_str *service; struct arg_str *proto; + struct arg_str *instance; + struct arg_str *host; struct arg_str *txt; struct arg_end *end; } mdns_txt_replace_args; @@ -918,7 +920,16 @@ static int cmd_mdns_service_txt_replace(int argc, char **argv) printf("ERROR: Bad arguments!\n"); return 1; } - + const char *instance = NULL; + if (mdns_txt_replace_args.instance->count && mdns_txt_replace_args.instance->sval[0]) { + instance = mdns_txt_replace_args.instance->sval[0]; + printf("MDNS: Service Instance: %s\n", instance); + } + const char *host = NULL; + if (mdns_txt_replace_args.host->count && mdns_txt_replace_args.host->sval[0]) { + host = mdns_txt_replace_args.host->sval[0]; + printf("MDNS: Service for delegated host: %s\n", host); + } if (mdns_txt_replace_args.txt->count) { items = _convert_items(mdns_txt_replace_args.txt->sval, mdns_txt_replace_args.txt->count); if (!items) { @@ -927,7 +938,7 @@ static int cmd_mdns_service_txt_replace(int argc, char **argv) } } - ESP_ERROR_CHECK( mdns_service_txt_set(mdns_txt_replace_args.service->sval[0], mdns_txt_replace_args.proto->sval[0], items, mdns_txt_replace_args.txt->count) ); + ESP_ERROR_CHECK( mdns_service_txt_set_for_host(instance, mdns_txt_replace_args.service->sval[0], mdns_txt_replace_args.proto->sval[0], host, items, mdns_txt_replace_args.txt->count) ); free(items); return 0; } @@ -936,8 +947,10 @@ static void register_mdns_service_txt_replace(void) { mdns_txt_replace_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); mdns_txt_replace_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); + mdns_txt_replace_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_txt_replace_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); mdns_txt_replace_args.txt = arg_strn(NULL, NULL, "item", 0, 30, "TXT Items (name=value)"); - mdns_txt_replace_args.end = arg_end(2); + mdns_txt_replace_args.end = arg_end(5); const esp_console_cmd_t cmd_txt_set = { .command = "mdns_service_txt_replace", @@ -953,6 +966,8 @@ static void register_mdns_service_txt_replace(void) static struct { struct arg_str *service; struct arg_str *proto; + struct arg_str *instance; + struct arg_str *host; struct arg_str *var; struct arg_str *value; struct arg_end *end; @@ -970,8 +985,18 @@ static int cmd_mdns_service_txt_set(int argc, char **argv) printf("ERROR: Bad arguments!\n"); return 1; } + const char *instance = NULL; + if (mdns_txt_set_args.instance->count && mdns_txt_set_args.instance->sval[0]) { + instance = mdns_txt_set_args.instance->sval[0]; + printf("MDNS: Service Instance: %s\n", instance); + } + const char *host = NULL; + if (mdns_txt_set_args.host->count && mdns_txt_set_args.host->sval[0]) { + host = mdns_txt_set_args.host->sval[0]; + printf("MDNS: Service for delegated host: %s\n", host); + } - ESP_ERROR_CHECK( mdns_service_txt_item_set(mdns_txt_set_args.service->sval[0], mdns_txt_set_args.proto->sval[0], mdns_txt_set_args.var->sval[0], mdns_txt_set_args.value->sval[0]) ); + ESP_ERROR_CHECK( mdns_service_txt_item_set_for_host(instance, mdns_txt_set_args.service->sval[0], mdns_txt_set_args.proto->sval[0], host, mdns_txt_set_args.var->sval[0], mdns_txt_set_args.value->sval[0]) ); return 0; } @@ -981,7 +1006,9 @@ static void register_mdns_service_txt_set(void) mdns_txt_set_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); mdns_txt_set_args.var = arg_str1(NULL, NULL, "", "Item Name"); mdns_txt_set_args.value = arg_str1(NULL, NULL, "", "Item Value"); - mdns_txt_set_args.end = arg_end(2); + mdns_txt_set_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_txt_set_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); + mdns_txt_set_args.end = arg_end(6); const esp_console_cmd_t cmd_txt_set = { .command = "mdns_service_txt_set", @@ -998,6 +1025,8 @@ static struct { struct arg_str *service; struct arg_str *proto; struct arg_str *var; + struct arg_str *instance; + struct arg_str *host; struct arg_end *end; } mdns_txt_remove_args; @@ -1013,8 +1042,15 @@ static int cmd_mdns_service_txt_remove(int argc, char **argv) printf("ERROR: Bad arguments!\n"); return 1; } - - ESP_ERROR_CHECK( mdns_service_txt_item_remove(mdns_txt_remove_args.service->sval[0], mdns_txt_remove_args.proto->sval[0], mdns_txt_remove_args.var->sval[0]) ); + const char *instance = NULL; + if (mdns_txt_remove_args.instance->count && mdns_txt_remove_args.instance->sval[0]) { + instance = mdns_txt_remove_args.instance->sval[0]; + } + const char *host = NULL; + if (mdns_txt_remove_args.host->count && mdns_txt_remove_args.host->sval[0]) { + host = mdns_txt_remove_args.host->sval[0]; + } + ESP_ERROR_CHECK( mdns_service_txt_item_remove_for_host(instance, mdns_txt_remove_args.service->sval[0], mdns_txt_remove_args.proto->sval[0], host, mdns_txt_remove_args.var->sval[0]) ); return 0; } @@ -1023,6 +1059,8 @@ static void register_mdns_service_txt_remove(void) mdns_txt_remove_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); mdns_txt_remove_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); mdns_txt_remove_args.var = arg_str1(NULL, NULL, "", "Item Name"); + mdns_txt_remove_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_txt_remove_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); mdns_txt_remove_args.end = arg_end(2); const esp_console_cmd_t cmd_txt_remove = { diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index 04ce9f2305..5050f51302 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -102,21 +102,33 @@ def test_remove_delegated_service(mdns_console, dig_app): 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) + # add the delegated service again, would be used in the TXT test + mdns_console.send_input('mdns_service_add _test2 _tcp 80 -i extern -h delegated') + mdns_console.get_output('MDNS: Service Instance: extern') + + +def check_txt_for_service(instance, service, proto, mdns_console, dig_app, host=None): + for_host_arg = f'-h {host}' if host is not None else '' + mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} key1 value1') + dig_app.check_record(f'{instance}.{service}.{proto}.local', query_type='SRV', expected=True) + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key1=value1') + mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} key2 value2') + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key2=value2') + mdns_console.send_input(f'mdns_service_txt_remove {service} {proto} {for_host_arg} key2') + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=False, expect='key2=value2') + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key1=value1') + mdns_console.send_input(f'mdns_service_txt_replace {service} {proto} {for_host_arg} key3=value3 key4=value4') + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=False, expect='key1=value1') + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key3=value3') + dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key4=value4') def test_update_txt(mdns_console, dig_app): - mdns_console.send_input('mdns_service_txt_set _test _tcp key1 value1') - dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True) - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key1=value1') - mdns_console.send_input('mdns_service_txt_set _test _tcp key2 value2') - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key2=value2') - mdns_console.send_input('mdns_service_txt_remove _test _tcp key2') - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=False, expect='key2=value2') - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key1=value1') - mdns_console.send_input('mdns_service_txt_replace _test _tcp key3=value3 key4=value4') - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=False, expect='key1=value1') - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key3=value3') - dig_app.check_record('_test._tcp.local', query_type='TXT', expected=True, expect='key4=value4') + check_txt_for_service('local', '_test', '_tcp', mdns_console=mdns_console, dig_app=dig_app) + + +def test_update_delegated_txt(mdns_console, dig_app): + check_txt_for_service('extern', '_test2', '_tcp', mdns_console=mdns_console, dig_app=dig_app, host='delegated') if __name__ == '__main__': From 07b79abf6226d43d8cac1efaea8a4092b2927b30 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 15:52:19 +0200 Subject: [PATCH 6/9] feat(mdns): Console test: set port for (delegated) srvs --- components/mdns/mdns_console.c | 18 ++++++++++++- .../mdns/tests/host_test/pytest_mdns.py | 26 +++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 73325d3f29..55f7a44df5 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -860,6 +860,8 @@ static struct { struct arg_str *service; struct arg_str *proto; struct arg_int *port; + struct arg_str *host; + struct arg_str *instance; struct arg_end *end; } mdns_service_port_set_args; @@ -876,7 +878,19 @@ static int cmd_mdns_service_port_set(int argc, char **argv) return 1; } - ESP_ERROR_CHECK( mdns_service_port_set(mdns_service_port_set_args.service->sval[0], mdns_service_port_set_args.proto->sval[0], mdns_service_port_set_args.port->ival[0]) ); + const char *host = NULL; + if (mdns_service_port_set_args.host->count && mdns_service_port_set_args.host->sval[0]) { + host = mdns_service_port_set_args.host->sval[0]; + } + const char *instance = NULL; + if (mdns_service_port_set_args.instance->count && mdns_service_port_set_args.instance->sval[0]) { + instance = mdns_service_port_set_args.instance->sval[0]; + } + esp_err_t err = mdns_service_port_set_for_host(instance, mdns_service_port_set_args.service->sval[0], mdns_service_port_set_args.proto->sval[0], host, mdns_service_port_set_args.port->ival[0]); + if (err != ESP_OK) { + printf("mdns_service_port_set_for_host() failed with %s\n", esp_err_to_name(err)); + return 1; + } return 0; } @@ -885,6 +899,8 @@ static void register_mdns_service_port_set(void) mdns_service_port_set_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); mdns_service_port_set_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); mdns_service_port_set_args.port = arg_int1(NULL, NULL, "", "Service Port"); + mdns_service_port_set_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); + mdns_service_port_set_args.instance = arg_str0("i", "instance", "", "Instance name"); mdns_service_port_set_args.end = arg_end(2); const esp_console_cmd_t cmd_add = { diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index 5050f51302..7f7c371221 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -107,17 +107,18 @@ def test_remove_delegated_service(mdns_console, dig_app): mdns_console.get_output('MDNS: Service Instance: extern') -def check_txt_for_service(instance, service, proto, mdns_console, dig_app, host=None): +def check_txt_for_service(instance, service, proto, mdns_console, dig_app, host=None, with_inst=False): for_host_arg = f'-h {host}' if host is not None else '' - mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} key1 value1') + for_inst_arg = f'-i {instance}' if with_inst else '' + mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} {for_inst_arg} key1 value1') dig_app.check_record(f'{instance}.{service}.{proto}.local', query_type='SRV', expected=True) dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key1=value1') - mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} key2 value2') + mdns_console.send_input(f'mdns_service_txt_set {service} {proto} {for_host_arg} {for_inst_arg} key2 value2') dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key2=value2') - mdns_console.send_input(f'mdns_service_txt_remove {service} {proto} {for_host_arg} key2') + mdns_console.send_input(f'mdns_service_txt_remove {service} {proto} {for_host_arg} {for_inst_arg} key2') dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=False, expect='key2=value2') dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key1=value1') - mdns_console.send_input(f'mdns_service_txt_replace {service} {proto} {for_host_arg} key3=value3 key4=value4') + mdns_console.send_input(f'mdns_service_txt_replace {service} {proto} {for_host_arg} {for_inst_arg} key3=value3 key4=value4') dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=False, expect='key1=value1') dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key3=value3') dig_app.check_record(f'{service}.{proto}.local', query_type='TXT', expected=True, expect='key4=value4') @@ -125,10 +126,25 @@ def check_txt_for_service(instance, service, proto, mdns_console, dig_app, host= def test_update_txt(mdns_console, dig_app): check_txt_for_service('local', '_test', '_tcp', mdns_console=mdns_console, dig_app=dig_app) + check_txt_for_service('local', '_test', '_tcp', mdns_console=mdns_console, dig_app=dig_app, with_inst=True) def test_update_delegated_txt(mdns_console, dig_app): check_txt_for_service('extern', '_test2', '_tcp', mdns_console=mdns_console, dig_app=dig_app, host='delegated') + check_txt_for_service('extern', '_test2', '_tcp', mdns_console=mdns_console, dig_app=dig_app, host='delegated', with_inst=True) + + +def test_service_port_set(mdns_console, dig_app): + dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True, expect='80') + mdns_console.send_input('mdns_service_port_set _test _tcp 81') + dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True, expect='81') + mdns_console.send_input('mdns_service_port_set _test2 _tcp -h delegated 82') + dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True, expect='82') + mdns_console.send_input('mdns_service_port_set _test2 _tcp -h delegated -i extern 83') + dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True, expect='83') + mdns_console.send_input('mdns_service_port_set _test2 _tcp -h delegated -i invalid_inst 84') + mdns_console.get_output('ESP_ERR_NOT_FOUND') + dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True, expect='83') if __name__ == '__main__': From ee00e97b2bac538d09bcf0ed4e99ba6d99afe303 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 16:48:27 +0200 Subject: [PATCH 7/9] feat(mdns): Console test: add subtype for service --- components/mdns/mdns_console.c | 54 +++++++++++++++++++ .../mdns/tests/host_test/pytest_mdns.py | 8 +++ 2 files changed, 62 insertions(+) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 55f7a44df5..fb8c1db80c 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -1265,6 +1265,59 @@ static void register_mdns_undelegate_host(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_undelegate_host) ); } +static struct { + struct arg_str *service; + struct arg_str *proto; + struct arg_str *sub; + struct arg_str *instance; + struct arg_str *host; + struct arg_end *end; +} mdns_service_subtype_args; + +static int cmd_mdns_service_subtype(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &mdns_service_subtype_args); + if (nerrors != 0) { + arg_print_errors(stderr, mdns_service_subtype_args.end, argv[0]); + return 1; + } + + if (!mdns_service_subtype_args.service->sval[0] || !mdns_service_subtype_args.proto->sval[0] || !mdns_service_subtype_args.sub->sval[0]) { + printf("ERROR: Bad arguments!\n"); + return 1; + } + const char *instance = NULL; + if (mdns_service_subtype_args.instance->count && mdns_service_subtype_args.instance->sval[0]) { + instance = mdns_service_subtype_args.instance->sval[0]; + } + const char *host = NULL; + if (mdns_service_subtype_args.host->count && mdns_service_subtype_args.host->sval[0]) { + host = mdns_service_subtype_args.host->sval[0]; + } + ESP_ERROR_CHECK( mdns_service_subtype_add_for_host(instance, mdns_service_subtype_args.service->sval[0], mdns_service_subtype_args.proto->sval[0], host, mdns_service_subtype_args.sub->sval[0]) ); + return 0; +} + +static void register_mdns_service_subtype_set(void) +{ + mdns_service_subtype_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); + mdns_service_subtype_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); + mdns_service_subtype_args.sub = arg_str1(NULL, NULL, "", "Subtype"); + mdns_service_subtype_args.instance = arg_str0("i", "instance", "", "Instance name"); + mdns_service_subtype_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); + mdns_service_subtype_args.end = arg_end(5); + + const esp_console_cmd_t cmd_service_sub = { + .command = "mdns_service_subtype", + .help = "Adds subtype for service", + .hint = NULL, + .func = &cmd_mdns_service_subtype, + .argtable = &mdns_service_subtype_args + }; + + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_service_sub) ); +} + void mdns_console_register(void) { register_mdns_init(); @@ -1283,6 +1336,7 @@ void mdns_console_register(void) register_mdns_lookup_service(); register_mdns_delegate_host(); register_mdns_undelegate_host(); + register_mdns_service_subtype_set(); #ifdef CONFIG_LWIP_IPV4 register_mdns_query_a(); diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index 7f7c371221..18914194df 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -147,5 +147,13 @@ def test_service_port_set(mdns_console, dig_app): dig_app.check_record('extern._test2._tcp.local', query_type='SRV', expected=True, expect='83') +def test_service_subtype(mdns_console, dig_app): + dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True) + mdns_console.send_input('mdns_service_subtype _test _tcp _subtest -i local') + dig_app.check_record('_subtest._sub._test._tcp.local', query_type='PTR', expected=True) + mdns_console.send_input('mdns_service_subtype _test2 _tcp _subtest2 -i extern -h delegated') + dig_app.check_record('_subtest2._sub._test2._tcp.local', query_type='PTR', expected=True) + + if __name__ == '__main__': pytest.main(['-s', 'test_mdns.py']) From f107dcd1186ba95b9d2838eb45beda21e5bb5cb0 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Mon, 22 Jul 2024 17:12:14 +0200 Subject: [PATCH 8/9] feat(mdns): Console test: set instance for service --- components/mdns/mdns_console.c | 20 ++++++++++++++++-- .../mdns/tests/host_test/pytest_mdns.py | 21 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index fb8c1db80c..17a4127122 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -818,6 +818,8 @@ static struct { struct arg_str *service; struct arg_str *proto; struct arg_str *instance; + struct arg_str *host; + struct arg_str *old_instance; struct arg_end *end; } mdns_service_instance_set_args; @@ -833,8 +835,20 @@ static int cmd_mdns_service_instance_set(int argc, char **argv) printf("ERROR: Bad arguments!\n"); return 1; } + const char *host = NULL; + if (mdns_service_instance_set_args.host->count && mdns_service_instance_set_args.host->sval[0]) { + host = mdns_service_instance_set_args.host->sval[0]; + } + const char *old_instance = NULL; + if (mdns_service_instance_set_args.old_instance->count && mdns_service_instance_set_args.old_instance->sval[0]) { + old_instance = mdns_service_instance_set_args.old_instance->sval[0]; + } + esp_err_t err = mdns_service_instance_name_set_for_host(old_instance, mdns_service_instance_set_args.service->sval[0], mdns_service_instance_set_args.proto->sval[0], host, mdns_service_instance_set_args.instance->sval[0]); + if (err != ESP_OK) { + printf("mdns_service_instance_name_set_for_host() failed with %s\n", esp_err_to_name(err)); + return 1; + } - ESP_ERROR_CHECK( mdns_service_instance_name_set(mdns_service_instance_set_args.service->sval[0], mdns_service_instance_set_args.proto->sval[0], mdns_service_instance_set_args.instance->sval[0]) ); return 0; } @@ -843,7 +857,9 @@ static void register_mdns_service_instance_set(void) mdns_service_instance_set_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); mdns_service_instance_set_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); mdns_service_instance_set_args.instance = arg_str1(NULL, NULL, "", "Instance name"); - mdns_service_instance_set_args.end = arg_end(2); + mdns_service_instance_set_args.host = arg_str0("h", "host", "", "Service for this (delegated) host"); + mdns_service_instance_set_args.old_instance = arg_str0("i", "old_instance", "", "Instance name before update"); + mdns_service_instance_set_args.end = arg_end(4); const esp_console_cmd_t cmd_add = { .command = "mdns_service_instance_set", diff --git a/components/mdns/tests/host_test/pytest_mdns.py b/components/mdns/tests/host_test/pytest_mdns.py index 18914194df..f8b95f5514 100644 --- a/components/mdns/tests/host_test/pytest_mdns.py +++ b/components/mdns/tests/host_test/pytest_mdns.py @@ -155,5 +155,26 @@ def test_service_subtype(mdns_console, dig_app): dig_app.check_record('_subtest2._sub._test2._tcp.local', query_type='PTR', expected=True) +def test_service_set_instance(mdns_console, dig_app): + dig_app.check_record('local._test._tcp.local', query_type='SRV', expected=True) + mdns_console.send_input('mdns_service_instance_set _test _tcp local2') + dig_app.check_record('local2._test._tcp.local', query_type='SRV', expected=True) + mdns_console.send_input('mdns_service_instance_set _test2 _tcp extern2 -h delegated') + mdns_console.send_input('mdns_service_lookup _test2 _tcp -d') + mdns_console.get_output('PTR : extern2') + dig_app.check_record('extern2._test2._tcp.local', query_type='SRV', expected=True) + mdns_console.send_input('mdns_service_instance_set _test2 _tcp extern3 -h delegated -i extern') + mdns_console.get_output('ESP_ERR_NOT_FOUND') + + +def test_service_remove_all(mdns_console, dig_app): + mdns_console.send_input('mdns_service_remove_all') + mdns_console.send_input('mdns_service_lookup _test2 _tcp -d') + mdns_console.get_output('No results found!') + mdns_console.send_input('mdns_service_lookup _test _tcp') + mdns_console.get_output('No results found!') + dig_app.check_record('_test._tcp.local', query_type='PTR', expected=False) + + if __name__ == '__main__': pytest.main(['-s', 'test_mdns.py']) From 1e8ede3396e92dd1ea953f5b717e5a1d26da2aed Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 23 Jul 2024 18:06:22 +0200 Subject: [PATCH 9/9] feat(mdns): Add console command for mdns browsing --- components/mdns/mdns_console.c | 82 ++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/components/mdns/mdns_console.c b/components/mdns/mdns_console.c index 17a4127122..80f009a122 100644 --- a/components/mdns/mdns_console.c +++ b/components/mdns/mdns_console.c @@ -1334,6 +1334,85 @@ static void register_mdns_service_subtype_set(void) ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_service_sub) ); } +static struct { + struct arg_str *service; + struct arg_str *proto; + struct arg_end *end; +} mdns_browse_args; + +static void mdns_browse_notifier(mdns_result_t *result) +{ + if (result) { + mdns_print_results(result); + } +} + +static int cmd_mdns_browse(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &mdns_browse_args); + if (nerrors != 0) { + arg_print_errors(stderr, mdns_browse_args.end, argv[0]); + return 1; + } + + if (!mdns_browse_args.service->sval[0] || !mdns_browse_args.proto->sval[0]) { + printf("ERROR: Bad arguments!\n"); + return 1; + } + mdns_browse_t *handle = mdns_browse_new(mdns_browse_args.service->sval[0], mdns_browse_args.proto->sval[0], mdns_browse_notifier); + return handle ? 0 : 1; +} + +static void register_mdns_browse(void) +{ + mdns_browse_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); + mdns_browse_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); + mdns_browse_args.end = arg_end(2); + + const esp_console_cmd_t cmd_browse = { + .command = "mdns_browse", + .help = "Start browsing", + .hint = NULL, + .func = &cmd_mdns_browse, + .argtable = &mdns_browse_args + }; + + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_browse) ); +} + +static int cmd_mdns_browse_del(int argc, char **argv) +{ + int nerrors = arg_parse(argc, argv, (void **) &mdns_browse_args); + if (nerrors != 0) { + arg_print_errors(stderr, mdns_browse_args.end, argv[0]); + return 1; + } + + if (!mdns_browse_args.service->sval[0] || !mdns_browse_args.proto->sval[0]) { + printf("ERROR: Bad arguments!\n"); + return 1; + } + esp_err_t err = mdns_browse_delete(mdns_browse_args.service->sval[0], mdns_browse_args.proto->sval[0]); + return err == ESP_OK ? 0 : 1; +} + +static void register_mdns_browse_del(void) +{ + mdns_browse_args.service = arg_str1(NULL, NULL, "", "MDNS Service"); + mdns_browse_args.proto = arg_str1(NULL, NULL, "", "IP Protocol"); + mdns_browse_args.end = arg_end(2); + + const esp_console_cmd_t cmd_browse_del = { + .command = "mdns_browse_del", + .help = "Stop browsing", + .hint = NULL, + .func = &cmd_mdns_browse_del, + .argtable = &mdns_browse_args + }; + + ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_browse_del) ); +} + void mdns_console_register(void) { register_mdns_init(); @@ -1354,6 +1433,9 @@ void mdns_console_register(void) register_mdns_undelegate_host(); register_mdns_service_subtype_set(); + register_mdns_browse(); + register_mdns_browse_del(); + #ifdef CONFIG_LWIP_IPV4 register_mdns_query_a(); #endif