Skip to content

Commit

Permalink
feat(mdns): Console test for add/remove delegated service APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Aug 15, 2024
1 parent ce7f326 commit 43de7e5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
6 changes: 5 additions & 1 deletion components/mdns/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
27 changes: 24 additions & 3 deletions components/mdns/mdns_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -739,6 +746,7 @@ static void register_mdns_service_add(void)
mdns_add_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
mdns_add_args.port = arg_int1(NULL, NULL, "<port>", "Service Port");
mdns_add_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
mdns_add_args.host = arg_str0("h", "host", "<hostname>", "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);

Expand All @@ -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;

Expand All @@ -772,15 +782,26 @@ 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;
}

static void register_mdns_service_remove(void)
{
mdns_remove_args.service = arg_str1(NULL, NULL, "<service>", "MDNS Service");
mdns_remove_args.proto = arg_str1(NULL, NULL, "<proto>", "IP Protocol");
mdns_remove_args.end = arg_end(2);
mdns_remove_args.host = arg_str0("h", "host", "<hostname>", "Service for this (delegated) host");
mdns_remove_args.instance = arg_str0("i", "instance", "<instance>", "Instance name");
mdns_remove_args.end = arg_end(4);

const esp_console_cmd_t cmd_remove = {
.command = "mdns_service_remove",
Expand Down
22 changes: 22 additions & 0 deletions components/mdns/tests/host_test/pytest_mdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])

0 comments on commit 43de7e5

Please sign in to comment.