mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-05 21:54:33 +02:00
Merge branch 'feature/add_dns_config' into 'master'
example: Add DNS configure and test for static ip example Closes IDFGH-5523 See merge request espressif/esp-idf!14324
This commit is contained in:
@@ -21,6 +21,12 @@ Set following parameters under Example Configuration Options:
|
|||||||
|
|
||||||
* Set `Static gateway address` of your device static gateway address.
|
* Set `Static gateway address` of your device static gateway address.
|
||||||
|
|
||||||
|
* Set `Choose DNS server` with `Set manual value as DNS server` or `Use gateway address as DNS server` to configure your DNS server.
|
||||||
|
|
||||||
|
* Choose `Set manual value as DNS server` to configure manual DNS server with `Main DNS server address` and `Backup DNS server address`.
|
||||||
|
|
||||||
|
* Set `Enable DNS resolve test` to resolve your host which input in `Domain name to resolve`.
|
||||||
|
|
||||||
### Build and Flash
|
### Build and Flash
|
||||||
|
|
||||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||||
@@ -82,4 +88,5 @@ I (3253) static_ip: connected to ap SSID:myssid password:mypassword
|
|||||||
W (3273) wifi:<ba-add>idx:0 (ifx:0, 34:29:12:43:c5:40), tid:5, ssn:2, winSize:64
|
W (3273) wifi:<ba-add>idx:0 (ifx:0, 34:29:12:43:c5:40), tid:5, ssn:2, winSize:64
|
||||||
I (3303) wifi:AP's beacon interval = 102400 us, DTIM period = 1
|
I (3303) wifi:AP's beacon interval = 102400 us, DTIM period = 1
|
||||||
I (3303) wifi:new:<11,0>, old:<11,2>, ap:<255,255>, sta:<11,0>, prof:1
|
I (3303) wifi:new:<11,0>, old:<11,2>, ap:<255,255>, sta:<11,0>, prof:1
|
||||||
|
I (5203) static_ip: Resolved IPv4 address: 119.9.92.99
|
||||||
```
|
```
|
||||||
|
@@ -35,4 +35,47 @@ menu "Example Configuration"
|
|||||||
default "192.168.4.1"
|
default "192.168.4.1"
|
||||||
help
|
help
|
||||||
Set static gateway address.
|
Set static gateway address.
|
||||||
|
|
||||||
|
choice EXAMPLE_STATIC_DNS_SERVER
|
||||||
|
prompt "Choose DNS server"
|
||||||
|
default EXAMPLE_STATIC_DNS_AUTO
|
||||||
|
help
|
||||||
|
Select auto to make gateway address as DNS server or manual to input your DNS server
|
||||||
|
config EXAMPLE_STATIC_DNS_AUTO
|
||||||
|
bool "Use gateway address as DNS server"
|
||||||
|
help
|
||||||
|
Set DNS server the same as gateway address
|
||||||
|
|
||||||
|
config EXAMPLE_STATIC_DNS_MANUAL
|
||||||
|
bool "Set manual value as DNS server"
|
||||||
|
help
|
||||||
|
Set DNS server with you want
|
||||||
|
endchoice
|
||||||
|
|
||||||
|
config EXAMPLE_STATIC_DNS_SERVER_MAIN
|
||||||
|
string "Main DNS server address"
|
||||||
|
default "192.168.4.1"
|
||||||
|
depends on EXAMPLE_STATIC_DNS_MANUAL
|
||||||
|
help
|
||||||
|
Set main DNS server address.
|
||||||
|
|
||||||
|
config EXAMPLE_STATIC_DNS_SERVER_BACKUP
|
||||||
|
string "Backup DNS server address"
|
||||||
|
default "192.168.4.1"
|
||||||
|
depends on EXAMPLE_STATIC_DNS_MANUAL
|
||||||
|
help
|
||||||
|
Set backup DNS server address. It can be same with the main DNS server address or leave empty.
|
||||||
|
|
||||||
|
config EXAMPLE_STATIC_DNS_RESOLVE_TEST
|
||||||
|
bool "Enable DNS resolve test"
|
||||||
|
default n
|
||||||
|
help
|
||||||
|
Enable it and configure EXAMPLE_STATIC_RESOLVE_DOMAIN to resolve DNS domain name
|
||||||
|
|
||||||
|
config EXAMPLE_STATIC_RESOLVE_DOMAIN
|
||||||
|
string "Domain name to resolve"
|
||||||
|
default "www.espressif.com"
|
||||||
|
depends on EXAMPLE_STATIC_DNS_RESOLVE_TEST
|
||||||
|
help
|
||||||
|
Set domain name for DNS test
|
||||||
endmenu
|
endmenu
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include <netdb.h>
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
/* The examples use configuration that you can set via project configuration menu
|
/* The examples use configuration that you can set via project configuration menu
|
||||||
@@ -27,7 +28,16 @@
|
|||||||
#define EXAMPLE_STATIC_IP_ADDR CONFIG_EXAMPLE_STATIC_IP_ADDR
|
#define EXAMPLE_STATIC_IP_ADDR CONFIG_EXAMPLE_STATIC_IP_ADDR
|
||||||
#define EXAMPLE_STATIC_NETMASK_ADDR CONFIG_EXAMPLE_STATIC_NETMASK_ADDR
|
#define EXAMPLE_STATIC_NETMASK_ADDR CONFIG_EXAMPLE_STATIC_NETMASK_ADDR
|
||||||
#define EXAMPLE_STATIC_GW_ADDR CONFIG_EXAMPLE_STATIC_GW_ADDR
|
#define EXAMPLE_STATIC_GW_ADDR CONFIG_EXAMPLE_STATIC_GW_ADDR
|
||||||
|
#ifdef CONFIG_EXAMPLE_STATIC_DNS_AUTO
|
||||||
|
#define EXAMPLE_MAIN_DNS_SERVER EXAMPLE_STATIC_GW_ADDR
|
||||||
|
#define EXAMPLE_BACKUP_DNS_SERVER "0.0.0.0"
|
||||||
|
#else
|
||||||
|
#define EXAMPLE_MAIN_DNS_SERVER CONFIG_EXAMPLE_STATIC_DNS_SERVER_MAIN
|
||||||
|
#define EXAMPLE_BACKUP_DNS_SERVER CONFIG_EXAMPLE_STATIC_DNS_SERVER_BACKUP
|
||||||
|
#endif
|
||||||
|
#ifdef CONFIG_EXAMPLE_STATIC_DNS_RESOLVE_TEST
|
||||||
|
#define EXAMPLE_RESOLVE_DOMAIN CONFIG_EXAMPLE_STATIC_RESOLVE_DOMAIN
|
||||||
|
#endif
|
||||||
/* FreeRTOS event group to signal when we are connected*/
|
/* FreeRTOS event group to signal when we are connected*/
|
||||||
static EventGroupHandle_t s_wifi_event_group;
|
static EventGroupHandle_t s_wifi_event_group;
|
||||||
|
|
||||||
@@ -41,6 +51,17 @@ static const char *TAG = "static_ip";
|
|||||||
|
|
||||||
static int s_retry_num = 0;
|
static int s_retry_num = 0;
|
||||||
|
|
||||||
|
static esp_err_t example_set_dns_server(esp_netif_t *netif, uint32_t addr, esp_netif_dns_type_t type)
|
||||||
|
{
|
||||||
|
if (addr && (addr != IPADDR_NONE)) {
|
||||||
|
esp_netif_dns_info_t dns;
|
||||||
|
dns.ip.u_addr.ip4.addr = addr;
|
||||||
|
dns.ip.type = IPADDR_TYPE_V4;
|
||||||
|
ESP_ERROR_CHECK(esp_netif_set_dns_info(netif, type, &dns));
|
||||||
|
}
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
static void example_set_static_ip(esp_netif_t *netif)
|
static void example_set_static_ip(esp_netif_t *netif)
|
||||||
{
|
{
|
||||||
if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
|
if (esp_netif_dhcpc_stop(netif) != ESP_OK) {
|
||||||
@@ -57,6 +78,8 @@ static void example_set_static_ip(esp_netif_t *netif)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "Success to set static ip: %s, netmask: %s, gw: %s", EXAMPLE_STATIC_IP_ADDR, EXAMPLE_STATIC_NETMASK_ADDR, EXAMPLE_STATIC_GW_ADDR);
|
ESP_LOGD(TAG, "Success to set static ip: %s, netmask: %s, gw: %s", EXAMPLE_STATIC_IP_ADDR, EXAMPLE_STATIC_NETMASK_ADDR, EXAMPLE_STATIC_GW_ADDR);
|
||||||
|
ESP_ERROR_CHECK(example_set_dns_server(netif, ipaddr_addr(EXAMPLE_MAIN_DNS_SERVER), ESP_NETIF_DNS_MAIN));
|
||||||
|
ESP_ERROR_CHECK(example_set_dns_server(netif, ipaddr_addr(EXAMPLE_BACKUP_DNS_SERVER), ESP_NETIF_DNS_BACKUP));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||||
@@ -150,7 +173,30 @@ void wifi_init_sta(void)
|
|||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||||
}
|
}
|
||||||
|
#ifdef CONFIG_EXAMPLE_STATIC_DNS_RESOLVE_TEST
|
||||||
|
struct addrinfo *address_info;
|
||||||
|
struct addrinfo hints;
|
||||||
|
memset(&hints, 0, sizeof(hints));
|
||||||
|
hints.ai_family = AF_UNSPEC;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
|
||||||
|
int res = getaddrinfo(EXAMPLE_RESOLVE_DOMAIN, NULL, &hints, &address_info);
|
||||||
|
if (res != 0 || address_info == NULL) {
|
||||||
|
ESP_LOGE(TAG, "couldn't get hostname for :%s: "
|
||||||
|
"getaddrinfo() returns %d, addrinfo=%p", EXAMPLE_RESOLVE_DOMAIN, res, address_info);
|
||||||
|
} else {
|
||||||
|
if (address_info->ai_family == AF_INET) {
|
||||||
|
struct sockaddr_in *p = (struct sockaddr_in *)address_info->ai_addr;
|
||||||
|
ESP_LOGI(TAG, "Resolved IPv4 address: %s", ipaddr_ntoa((const ip_addr_t*)&p->sin_addr.s_addr));
|
||||||
|
}
|
||||||
|
#if CONFIG_LWIP_IPV6
|
||||||
|
else if (address_info->ai_family == AF_INET6) {
|
||||||
|
struct sockaddr_in6 *p = (struct sockaddr_in6 *)address_info->ai_addr;
|
||||||
|
ESP_LOGI(TAG, "Resolved IPv6 address: %s", ip6addr_ntoa((const ip6_addr_t*)&p->sin6_addr));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
/* The event will not be processed after unregister */
|
/* The event will not be processed after unregister */
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
|
||||||
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
|
||||||
|
Reference in New Issue
Block a user