Skip to content

Commit 12ae722

Browse files
committed
feat(dhcps): Support for multiple DNS servers
1 parent cb3ac74 commit 12ae722

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

components/esp_netif/lwip/esp_netif_lwip.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -1984,11 +1984,14 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
19841984
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
19851985
#if ESP_DHCPS
19861986
// if DHCP server configured to set DNS in dhcps API
1987-
if (type != ESP_NETIF_DNS_MAIN) {
1987+
if (type >= ESP_NETIF_DNS_FALLBACK)
1988+
{
19881989
ESP_LOGD(TAG, "set dns invalid type");
19891990
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
1990-
} else {
1991-
dhcps_dns_setserver(esp_netif->dhcps, &lwip_ip);
1991+
}
1992+
else
1993+
{
1994+
dhcps_dns_setserver(esp_netif->dhcps, &lwip_ip, (dns_type_t)type);
19921995
}
19931996
#else
19941997
LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED);
@@ -2047,7 +2050,7 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
20472050
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
20482051
#if ESP_DHCPS
20492052
ip4_addr_t dns_ip;
2050-
dhcps_dns_getserver(esp_netif->dhcps, &dns_ip);
2053+
dhcps_dns_getserver(esp_netif->dhcps, &dns_ip, (dns_type_t)type);
20512054
memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t));
20522055
dns->ip.type = ESP_IPADDR_TYPE_V4;
20532056
#else

components/lwip/apps/dhcpserver/dhcpserver.c

+37-16
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct dhcps_t {
126126
struct netif *dhcps_netif;
127127
ip4_addr_t broadcast_dhcps;
128128
ip4_addr_t server_address;
129-
ip4_addr_t dns_server;
129+
ip4_addr_t dns_server[DNS_TYPE_MAX];
130130
ip4_addr_t client_address;
131131
ip4_addr_t client_address_plus;
132132
ip4_addr_t dhcps_mask;
@@ -155,7 +155,11 @@ dhcps_t *dhcps_new(void)
155155
return NULL;
156156
}
157157
dhcps->dhcps_netif = NULL;
158-
dhcps->dns_server.addr = 0;
158+
159+
for (int i = 0; i < DNS_TYPE_MAX; i++)
160+
{
161+
dhcps->dns_server[i].addr = 0;
162+
}
159163
#ifdef USE_CLASS_B_NET
160164
dhcps->dhcps_mask.addr = PP_HTONL(LWIP_MAKEU32(255, 240, 0, 0));
161165
#else
@@ -454,16 +458,31 @@ static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr)
454458
}
455459
}
456460

461+
// In order of preference
462+
*optptr++ = DHCP_OPTION_DNS_SERVER;
457463
if (dhcps_dns_enabled(dhcps->dhcps_dns)) {
458-
*optptr++ = DHCP_OPTION_DNS_SERVER;
459-
*optptr++ = 4;
460-
*optptr++ = ip4_addr1(&dhcps->dns_server);
461-
*optptr++ = ip4_addr2(&dhcps->dns_server);
462-
*optptr++ = ip4_addr3(&dhcps->dns_server);
463-
*optptr++ = ip4_addr4(&dhcps->dns_server);
464+
uint8_t size = 4;
465+
466+
if (dhcps->dns_server[DNS_TYPE_BACKUP].addr)
467+
{
468+
size += 4;
469+
}
470+
*optptr++ = size;
471+
472+
*optptr++ = ip4_addr1(&dhcps->dns_server[DNS_TYPE_MAIN]);
473+
*optptr++ = ip4_addr2(&dhcps->dns_server[DNS_TYPE_MAIN]);
474+
*optptr++ = ip4_addr3(&dhcps->dns_server[DNS_TYPE_MAIN]);
475+
*optptr++ = ip4_addr4(&dhcps->dns_server[DNS_TYPE_MAIN]);
476+
477+
if (dhcps->dns_server[DNS_TYPE_BACKUP].addr)
478+
{
479+
*optptr++ = ip4_addr1(&dhcps->dns_server[DNS_TYPE_BACKUP]);
480+
*optptr++ = ip4_addr2(&dhcps->dns_server[DNS_TYPE_BACKUP]);
481+
*optptr++ = ip4_addr3(&dhcps->dns_server[DNS_TYPE_BACKUP]);
482+
*optptr++ = ip4_addr4(&dhcps->dns_server[DNS_TYPE_BACKUP]);
483+
}
464484
#ifdef CONFIG_LWIP_DHCPS_ADD_DNS
465485
}else {
466-
*optptr++ = DHCP_OPTION_DNS_SERVER;
467486
*optptr++ = 4;
468487
*optptr++ = ip4_addr1(&ipadd);
469488
*optptr++ = ip4_addr2(&ipadd);
@@ -1535,31 +1554,33 @@ bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip)
15351554
* FunctionName : dhcps_dns_setserver
15361555
* Description : set DNS server address for dhcpserver
15371556
* Parameters : dnsserver -- The DNS server address
1557+
* type -- The DNS type
15381558
* Returns : ERR_ARG if invalid handle, ERR_OK on success
15391559
*******************************************************************************/
1540-
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver)
1560+
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t type)
15411561
{
15421562
if (dhcps == NULL) {
15431563
return ERR_ARG;
15441564
}
15451565
if (dnsserver != NULL) {
1546-
dhcps->dns_server = *(ip_2_ip4(dnsserver));
1566+
dhcps->dns_server[type] = *(ip_2_ip4(dnsserver));
15471567
} else {
1548-
dhcps->dns_server = *(ip_2_ip4(IP_ADDR_ANY));
1568+
dhcps->dns_server[type] = *(ip_2_ip4(IP_ADDR_ANY));
15491569
}
15501570
return ERR_OK;
15511571
}
15521572

15531573
/******************************************************************************
15541574
* FunctionName : dhcps_dns_getserver
15551575
* Description : get DNS server address for dhcpserver
1556-
* Parameters : none
1557-
* Returns : ip4_addr_t
1576+
* Parameters : dnsserver -- The DNS server address
1577+
* type -- The DNS type
1578+
* Returns : ERR_ARG if invalid handle, ERR_OK on success
15581579
*******************************************************************************/
1559-
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver)
1580+
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver, dns_type_t type)
15601581
{
15611582
if (dhcps) {
1562-
*dnsserver = dhcps->dns_server;
1583+
*dnsserver = dhcps->dns_server[type];
15631584
return ERR_OK;
15641585
}
15651586
return ERR_ARG;

components/lwip/include/apps/dhcpserver/dhcpserver.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -47,6 +47,13 @@ enum dhcps_offer_option{
4747
OFFER_END
4848
};
4949

50+
typedef enum
51+
{
52+
DNS_TYPE_MAIN = 0, /**< DNS main server address*/
53+
DNS_TYPE_BACKUP, /**< DNS backup server address (Wi-Fi STA and Ethernet only) */
54+
DNS_TYPE_MAX
55+
} dns_type_t;
56+
5057
/** @brief DHCP server's description of compile time configuration values in dhcpserver.c
5158
*
5259
* - DHCPS_DEBUG: Prints very detailed debug messages if set to 1, hardcoded to 0
@@ -164,17 +171,19 @@ bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip);
164171
* @brief Sets DNS server address for the DHCP server
165172
* @param dhcps Pointer to the DHCP handle
166173
* @param dnsserver Address of the DNS server
174+
* @param type Type of the DNS server
167175
* @return ERR_ARG if invalid handle, ERR_OK on success
168176
*/
169-
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver);
177+
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t type);
170178

171179
/**
172180
* @brief Gets DNS server associated with this DHCP server
173181
* @param dhcps Pointer to the DHCP handle
174182
* @param dnsserver Address of the DNS server
183+
* @param type Type of the DNS server
175184
* @return ERR_ARG if invalid handle, ERR_OK on success
176185
*/
177-
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver);
186+
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver, dns_type_t type);
178187

179188
/**
180189
* @brief Sets callback on assigning an IP to the connected client

0 commit comments

Comments
 (0)