Skip to content

Commit ce7f326

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

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

components/mdns/mdns_console.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,95 @@ static void register_mdns_lookup_service(void)
11011101
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_lookup_service) );
11021102
}
11031103

1104+
static struct {
1105+
struct arg_str *hostname;
1106+
struct arg_str *address;
1107+
struct arg_end *end;
1108+
} mdns_delegate_host_args;
1109+
1110+
static int cmd_mdns_delegate_host(int argc, char **argv)
1111+
{
1112+
int nerrors = arg_parse(argc, argv, (void **) &mdns_delegate_host_args);
1113+
if (nerrors != 0) {
1114+
arg_print_errors(stderr, mdns_delegate_host_args.end, argv[0]);
1115+
return 1;
1116+
}
1117+
1118+
if (!mdns_delegate_host_args.hostname->sval[0] || !mdns_delegate_host_args.address->sval[0]) {
1119+
printf("ERROR: Bad arguments!\n");
1120+
return 1;
1121+
}
1122+
1123+
mdns_ip_addr_t addr = { .next = NULL};
1124+
esp_netif_str_to_ip4(mdns_delegate_host_args.address->sval[0], &addr.addr.u_addr.ip4);
1125+
addr.addr.type = ESP_IPADDR_TYPE_V4;
1126+
1127+
esp_err_t err = mdns_delegate_hostname_add(mdns_delegate_host_args.hostname->sval[0], &addr);
1128+
if (err) {
1129+
printf("mdns_delegate_hostname_add() failed\n");
1130+
return 1;
1131+
}
1132+
return 0;
1133+
}
1134+
1135+
static void register_mdns_delegate_host(void)
1136+
{
1137+
mdns_delegate_host_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Delegated hostname");
1138+
mdns_delegate_host_args.address = arg_str1(NULL, NULL, "<address>", "Delegated hosts address");
1139+
mdns_delegate_host_args.end = arg_end(2);
1140+
1141+
const esp_console_cmd_t cmd_delegate_host = {
1142+
.command = "mdns_delegate_host",
1143+
.help = "Add delegated hostname",
1144+
.hint = NULL,
1145+
.func = &cmd_mdns_delegate_host,
1146+
.argtable = &mdns_delegate_host_args
1147+
};
1148+
1149+
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_delegate_host) );
1150+
}
1151+
1152+
static struct {
1153+
struct arg_str *hostname;
1154+
struct arg_end *end;
1155+
} mdns_undelegate_host_args;
1156+
1157+
static int cmd_mdns_undelegate_host(int argc, char **argv)
1158+
{
1159+
int nerrors = arg_parse(argc, argv, (void **) &mdns_undelegate_host_args);
1160+
if (nerrors != 0) {
1161+
arg_print_errors(stderr, mdns_undelegate_host_args.end, argv[0]);
1162+
return 1;
1163+
}
1164+
1165+
if (!mdns_undelegate_host_args.hostname->sval[0]) {
1166+
printf("ERROR: Bad arguments!\n");
1167+
return 1;
1168+
}
1169+
1170+
if (mdns_delegate_hostname_remove(mdns_undelegate_host_args.hostname->sval[0]) != ESP_OK) {
1171+
printf("mdns_delegate_hostname_remove() failed\n");
1172+
return 1;
1173+
}
1174+
return 0;
1175+
}
1176+
1177+
static void register_mdns_undelegate_host(void)
1178+
{
1179+
mdns_undelegate_host_args.hostname = arg_str1(NULL, NULL, "<hostname>", "Delegated hostname");
1180+
mdns_undelegate_host_args.end = arg_end(2);
1181+
1182+
const esp_console_cmd_t cmd_undelegate_host = {
1183+
.command = "mdns_undelegate_host",
1184+
.help = "Remove delegated hostname",
1185+
.hint = NULL,
1186+
.func = &cmd_mdns_undelegate_host,
1187+
.argtable = &mdns_undelegate_host_args
1188+
};
1189+
1190+
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd_undelegate_host) );
1191+
}
1192+
11041193
void mdns_console_register(void)
11051194
{
11061195
register_mdns_init();
@@ -1117,6 +1206,8 @@ void mdns_console_register(void)
11171206
register_mdns_service_remove_all();
11181207

11191208
register_mdns_lookup_service();
1209+
register_mdns_delegate_host();
1210+
register_mdns_undelegate_host();
11201211

11211212
#ifdef CONFIG_LWIP_IPV4
11221213
register_mdns_query_a();

components/mdns/tests/host_test/components/esp_netif_linux/esp_netif_linux.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,16 @@ const char *esp_netif_get_ifkey(esp_netif_t *esp_netif)
182182
{
183183
return esp_netif->if_key;
184184
}
185+
186+
esp_err_t esp_netif_str_to_ip4(const char *src, esp_ip4_addr_t *dst)
187+
{
188+
if (src == NULL || dst == NULL) {
189+
return ESP_ERR_INVALID_ARG;
190+
}
191+
struct in_addr addr;
192+
if (inet_pton(AF_INET, src, &addr) != 1) {
193+
return ESP_FAIL;
194+
}
195+
dst->addr = addr.s_addr;
196+
return ESP_OK;
197+
}

components/mdns/tests/host_test/pytest_mdns.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,15 @@ def test_remove_service(mdns_console, dig_app):
7272
dig_app.check_record('_http._tcp.local', query_type='PTR', expected=False)
7373

7474

75+
def test_delegate_host(mdns_console, dig_app):
76+
mdns_console.send_input('mdns_delegate_host delegated 1.2.3.4')
77+
dig_app.check_record('delegated.local', query_type='A', expected=True)
78+
79+
80+
def test_undelegate_host(mdns_console, dig_app):
81+
mdns_console.send_input('mdns_undelegate_host delegated')
82+
dig_app.check_record('delegated.local', query_type='A', expected=False)
83+
84+
7585
if __name__ == '__main__':
7686
pytest.main(['-s', 'test_mdns.py'])

0 commit comments

Comments
 (0)