|
9 | 9 | #include "esp_check.h" |
10 | 10 | #include "esp_err.h" |
11 | 11 | #include "esp_event.h" |
| 12 | +#include "esp_netif_types.h" |
12 | 13 | #include "esp_openthread.h" |
13 | 14 | #include "esp_openthread_dns64.h" |
| 15 | +#include "esp_openthread_netif_glue.h" |
14 | 16 | #include "esp_ot_cli_extension.h" |
15 | 17 | #include "lwip/dns.h" |
16 | 18 | #include "openthread/cli.h" |
17 | 19 | #include "openthread/netdata.h" |
18 | 20 |
|
19 | 21 | #define DNS_SERVER_ALTERNATIVE_INDEX 1 |
20 | 22 |
|
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) |
22 | 24 | { |
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"); |
32 | 33 | ESP_RETURN_ON_ERROR(esp_event_post(OPENTHREAD_EVENT, OPENTHREAD_EVENT_SET_DNS_SERVER, NULL, 0, 0), OT_EXT_CLI_TAG, |
33 | 34 | "Failed to post OpenThread set DNS server event"); |
34 | 35 | return ESP_OK; |
35 | 36 | } |
36 | 37 |
|
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) |
38 | 39 | { |
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 | +} |
41 | 51 |
|
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 | + } |
46 | 76 |
|
47 | 77 | return OT_ERROR_NONE; |
48 | 78 | } |
0 commit comments