Skip to content

Commit 7a259df

Browse files
committed
Merge branch 'cli_ext/dns64server' into 'main'
fix(dns64): Set DNS server with the esp_netif API See merge request espressif/esp-thread-br!152
2 parents cf3a09f + 1126826 commit 7a259df

File tree

1 file changed

+47
-17
lines changed

1 file changed

+47
-17
lines changed

components/esp_ot_cli_extension/src/esp_ot_dns64.c

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,70 @@
99
#include "esp_check.h"
1010
#include "esp_err.h"
1111
#include "esp_event.h"
12+
#include "esp_netif_types.h"
1213
#include "esp_openthread.h"
1314
#include "esp_openthread_dns64.h"
15+
#include "esp_openthread_netif_glue.h"
1416
#include "esp_ot_cli_extension.h"
1517
#include "lwip/dns.h"
1618
#include "openthread/cli.h"
1719
#include "openthread/netdata.h"
1820

1921
#define DNS_SERVER_ALTERNATIVE_INDEX 1
2022

21-
static esp_err_t set_dns64(const ip4_addr_t *dns_server)
23+
static esp_err_t set_dns64(esp_netif_dns_type_t dns_type, const ip4_addr_t *dns_server)
2224
{
23-
ip_addr_t dns_server_addr = {};
24-
25-
dns_server_addr.type = IPADDR_TYPE_V6;
26-
27-
ESP_RETURN_ON_FALSE(esp_openthread_get_nat64_prefix(&dns_server_addr.u_addr.ip6) == ESP_OK, ESP_ERR_NOT_FOUND,
28-
OT_EXT_CLI_TAG, "Cannot find NAT64 prefix");
29-
dns_server_addr.u_addr.ip6.addr[3] = dns_server->addr;
30-
dns_setserver(DNS_SERVER_ALTERNATIVE_INDEX, &dns_server_addr);
31-
25+
esp_netif_t *openthread_netif = esp_openthread_get_netif();
26+
ESP_RETURN_ON_FALSE(openthread_netif, ESP_ERR_ESP_NETIF_IF_NOT_READY, OT_EXT_CLI_TAG,
27+
"openthread netif is not initializd");
28+
ip6_addr_t dns_server_addr = {};
29+
ESP_RETURN_ON_ERROR(esp_openthread_get_nat64_prefix(&dns_server_addr), OT_EXT_CLI_TAG, "Cannot find NAT64 prefix");
30+
dns_server_addr.addr[3] = dns_server->addr;
31+
ESP_RETURN_ON_ERROR(esp_openthread_set_dnsserver_addr_with_type(dns_server_addr, dns_type), OT_EXT_CLI_TAG,
32+
"Failed to set dns server address");
3233
ESP_RETURN_ON_ERROR(esp_event_post(OPENTHREAD_EVENT, OPENTHREAD_EVENT_SET_DNS_SERVER, NULL, 0, 0), OT_EXT_CLI_TAG,
3334
"Failed to post OpenThread set DNS server event");
3435
return ESP_OK;
3536
}
3637

37-
otError esp_openthread_process_dns64_server(void *aContext, uint8_t aArgsLength, char *aArgs[])
38+
static esp_netif_dns_type_t parse_dns_type(const char *type)
3839
{
39-
ESP_RETURN_ON_FALSE(aArgsLength != 0, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG, "dns64server DNS_SERVER_URL");
40-
ip4_addr_t server_addr;
40+
if (strcmp(type, "main") == 0) {
41+
return ESP_NETIF_DNS_MAIN;
42+
}
43+
if (strcmp(type, "backup") == 0) {
44+
return ESP_NETIF_DNS_BACKUP;
45+
}
46+
if (strcmp(type, "fallback") == 0) {
47+
return ESP_NETIF_DNS_FALLBACK;
48+
}
49+
return ESP_NETIF_DNS_MAX;
50+
}
4151

42-
ESP_RETURN_ON_FALSE(ip4addr_aton(aArgs[0], &server_addr) == 1, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
43-
"Invalid DNS server");
44-
ESP_RETURN_ON_FALSE(set_dns64(&server_addr) == ESP_OK, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
45-
"Failed to set DNS server");
52+
otError esp_openthread_process_dns64_server(void *aContext, uint8_t aArgsLength, char *aArgs[])
53+
{
54+
(void)(aContext);
55+
if (aArgsLength == 0) {
56+
otCliOutputFormat("---dns64server command parameter---\n");
57+
otCliOutputFormat("dns64server <dns_server_addr> [dns_type]\n");
58+
otCliOutputFormat("<dns_server_addr> should be an IPv4 address\n");
59+
otCliOutputFormat(
60+
"[dns_type] can be 'main', 'backup', or 'fallback'.If not specified, it defaults to 'backup'\n");
61+
} else {
62+
ESP_RETURN_ON_FALSE(aArgsLength == 2 || aArgsLength == 1, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
63+
"Invalid parameters");
64+
ip4_addr_t server_addr;
65+
ESP_RETURN_ON_FALSE(ip4addr_aton(aArgs[0], &server_addr) == 1, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
66+
"Invalid DNS server");
67+
esp_netif_dns_type_t dns_type = ESP_NETIF_DNS_BACKUP;
68+
if (aArgsLength == 2) {
69+
dns_type = parse_dns_type(aArgs[1]);
70+
ESP_RETURN_ON_FALSE(dns_type != ESP_NETIF_DNS_MAX, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
71+
"Invalid DNS type");
72+
}
73+
ESP_RETURN_ON_FALSE(set_dns64(dns_type, &server_addr) == ESP_OK, OT_ERROR_INVALID_ARGS, OT_EXT_CLI_TAG,
74+
"Failed to set DNS server");
75+
}
4676

4777
return OT_ERROR_NONE;
4878
}

0 commit comments

Comments
 (0)