Skip to content

Commit b3523a6

Browse files
committed
fix(dhcps): Fixed DNS server handling for previous breaking changes
1 parent 0fd3984 commit b3523a6

File tree

4 files changed

+172
-49
lines changed

4 files changed

+172
-49
lines changed

components/esp_netif/lwip/esp_netif_lwip.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ static esp_err_t esp_netif_set_dns_info_api(esp_netif_api_msg_t *msg)
19941994
ESP_LOGD(TAG, "set dns invalid type");
19951995
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
19961996
} else {
1997-
dhcps_dns_setserver(esp_netif->dhcps, &lwip_ip, (dns_type_t)type);
1997+
dhcps_dns_setserver_by_type(esp_netif->dhcps, &lwip_ip, (dns_type_t)type);
19981998
}
19991999
#else
20002000
LOG_NETIF_DISABLED_AND_DO("DHCP Server", return ESP_ERR_NOT_SUPPORTED);
@@ -2053,7 +2053,7 @@ static esp_err_t esp_netif_get_dns_info_api(esp_netif_api_msg_t *msg)
20532053
if (esp_netif && esp_netif->flags & ESP_NETIF_DHCP_SERVER) {
20542054
#if ESP_DHCPS
20552055
ip4_addr_t dns_ip;
2056-
dhcps_dns_getserver(esp_netif->dhcps, &dns_ip, (dns_type_t)type);
2056+
dhcps_dns_getserver_by_type(esp_netif->dhcps, &dns_ip, (dns_type_t)type);
20572057
memcpy(&dns->ip.u_addr.ip4, &dns_ip, sizeof(ip4_addr_t));
20582058
dns->ip.type = ESP_IPADDR_TYPE_V4;
20592059
#else

components/lwip/apps/dhcpserver/dhcpserver.c

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,18 @@ static void get_ip_info(struct netif * netif, ip_info_t *ip_info)
197197
}
198198
}
199199

200+
static inline u8_t* dhcps_option_ip(u8_t *optptr, const ip4_addr_t *ip)
201+
{
202+
LWIP_ASSERT("dhcps_option_ip: optptr must not be NULL", (optptr != NULL));
203+
LWIP_ASSERT("dhcps_option_ip: ip must not be NULL", (ip != NULL));
204+
205+
*optptr++ = ip4_addr1(ip);
206+
*optptr++ = ip4_addr2(ip);
207+
*optptr++ = ip4_addr3(ip);
208+
*optptr++ = ip4_addr4(ip);
209+
return optptr;
210+
}
211+
200212
/******************************************************************************
201213
* FunctionName : dhcps_option_info
202214
* Description : get the DHCP message option info
@@ -422,10 +434,7 @@ static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr)
422434

423435
*optptr++ = DHCP_OPTION_SUBNET_MASK;
424436
*optptr++ = 4;
425-
*optptr++ = ip4_addr1(&dhcps->dhcps_mask);
426-
*optptr++ = ip4_addr2(&dhcps->dhcps_mask);
427-
*optptr++ = ip4_addr3(&dhcps->dhcps_mask);
428-
*optptr++ = ip4_addr4(&dhcps->dhcps_mask);
437+
optptr = dhcps_option_ip(optptr, &dhcps->dhcps_mask);
429438

430439
*optptr++ = DHCP_OPTION_LEASE_TIME;
431440
*optptr++ = 4;
@@ -436,10 +445,7 @@ static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr)
436445

437446
*optptr++ = DHCP_OPTION_SERVER_ID;
438447
*optptr++ = 4;
439-
*optptr++ = ip4_addr1(&ipadd);
440-
*optptr++ = ip4_addr2(&ipadd);
441-
*optptr++ = ip4_addr3(&ipadd);
442-
*optptr++ = ip4_addr4(&ipadd);
448+
optptr = dhcps_option_ip(optptr, &ipadd);
443449

444450
if (dhcps_router_enabled(dhcps->dhcps_offer)) {
445451
ip_info_t if_ip = { 0 };
@@ -450,10 +456,7 @@ static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr)
450456
if (!ip4_addr_isany_val(*gw_ip)) {
451457
*optptr++ = DHCP_OPTION_ROUTER;
452458
*optptr++ = 4;
453-
*optptr++ = ip4_addr1(gw_ip);
454-
*optptr++ = ip4_addr2(gw_ip);
455-
*optptr++ = ip4_addr3(gw_ip);
456-
*optptr++ = ip4_addr4(gw_ip);
459+
optptr = dhcps_option_ip(optptr, gw_ip);
457460
}
458461
}
459462

@@ -467,36 +470,23 @@ static u8_t *add_offer_options(dhcps_t *dhcps, u8_t *optptr)
467470

468471
*optptr++ = DHCP_OPTION_DNS_SERVER;
469472
*optptr++ = size;
470-
471-
*optptr++ = ip4_addr1(&dhcps->dns_server[DNS_TYPE_MAIN]);
472-
*optptr++ = ip4_addr2(&dhcps->dns_server[DNS_TYPE_MAIN]);
473-
*optptr++ = ip4_addr3(&dhcps->dns_server[DNS_TYPE_MAIN]);
474-
*optptr++ = ip4_addr4(&dhcps->dns_server[DNS_TYPE_MAIN]);
473+
optptr = dhcps_option_ip(optptr, &dhcps->dns_server[DNS_TYPE_MAIN]);
475474

476475
if (dhcps->dns_server[DNS_TYPE_BACKUP].addr) {
477-
*optptr++ = ip4_addr1(&dhcps->dns_server[DNS_TYPE_BACKUP]);
478-
*optptr++ = ip4_addr2(&dhcps->dns_server[DNS_TYPE_BACKUP]);
479-
*optptr++ = ip4_addr3(&dhcps->dns_server[DNS_TYPE_BACKUP]);
480-
*optptr++ = ip4_addr4(&dhcps->dns_server[DNS_TYPE_BACKUP]);
476+
optptr = dhcps_option_ip(optptr, &dhcps->dns_server[DNS_TYPE_BACKUP]);
481477
}
482478
#ifdef CONFIG_LWIP_DHCPS_ADD_DNS
483479
} else {
484480
*optptr++ = DHCP_OPTION_DNS_SERVER;
485481
*optptr++ = 4;
486-
*optptr++ = ip4_addr1(&ipadd);
487-
*optptr++ = ip4_addr2(&ipadd);
488-
*optptr++ = ip4_addr3(&ipadd);
489-
*optptr++ = ip4_addr4(&ipadd);
482+
optptr = dhcps_option_ip(optptr, &ipadd);
490483
#endif /* CONFIG_LWIP_DHCPS_ADD_DNS */
491484
}
492485

493486
ip4_addr_t broadcast_addr = { .addr = (ipadd.addr & dhcps->dhcps_mask.addr) | ~dhcps->dhcps_mask.addr };
494487
*optptr++ = DHCP_OPTION_BROADCAST_ADDRESS;
495488
*optptr++ = 4;
496-
*optptr++ = ip4_addr1(&broadcast_addr);
497-
*optptr++ = ip4_addr2(&broadcast_addr);
498-
*optptr++ = ip4_addr3(&broadcast_addr);
499-
*optptr++ = ip4_addr4(&broadcast_addr);
489+
optptr = dhcps_option_ip(optptr, &broadcast_addr);
500490

501491
*optptr++ = DHCP_OPTION_INTERFACE_MTU;
502492
*optptr++ = 2;
@@ -1549,17 +1539,22 @@ bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip)
15491539
}
15501540

15511541
/******************************************************************************
1552-
* FunctionName : dhcps_dns_setserver
1553-
* Description : set DNS server address for dhcpserver
1542+
* FunctionName : dhcps_dns_setserver_by_type
1543+
* Description : Set the DNS server address for dhcpserver with a specific type
15541544
* Parameters : dnsserver -- The DNS server address
15551545
* type -- The DNS type
1556-
* Returns : ERR_ARG if invalid handle, ERR_OK on success
1546+
* Returns : ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
15571547
*******************************************************************************/
1558-
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t type)
1548+
err_t dhcps_dns_setserver_by_type(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t type)
15591549
{
15601550
if (dhcps == NULL) {
15611551
return ERR_ARG;
15621552
}
1553+
1554+
if (type >= DNS_TYPE_MAX) {
1555+
return ERR_VAL;
1556+
}
1557+
15631558
if (dnsserver != NULL) {
15641559
dhcps->dns_server[type] = *(ip_2_ip4(dnsserver));
15651560
} else {
@@ -1569,18 +1564,46 @@ err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t
15691564
}
15701565

15711566
/******************************************************************************
1572-
* FunctionName : dhcps_dns_getserver
1573-
* Description : get DNS server address for dhcpserver
1567+
* FunctionName : dhcps_dns_setserver
1568+
* Description : Set the main DNS server address for dhcpserver
1569+
* Parameters : dnsserver -- The DNS server address
1570+
* Returns : ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
1571+
*******************************************************************************/
1572+
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver)
1573+
{
1574+
return dhcps_dns_setserver_by_type(dhcps, dnsserver, DNS_TYPE_MAIN);
1575+
}
1576+
1577+
/******************************************************************************
1578+
* FunctionName : dhcps_dns_getserver_by_type
1579+
* Description : Get the DNS server address for dhcpserver
15741580
* Parameters : dnsserver -- The DNS server address
15751581
* type -- The DNS type
1576-
* Returns : ERR_ARG if invalid handle, ERR_OK on success
1582+
* Returns : ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
15771583
*******************************************************************************/
1578-
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver, dns_type_t type)
1584+
err_t dhcps_dns_getserver_by_type(dhcps_t *dhcps, ip4_addr_t *dnsserver, dns_type_t type)
15791585
{
1580-
if (dhcps) {
1581-
*dnsserver = dhcps->dns_server[type];
1582-
return ERR_OK;
1586+
if ((dhcps == NULL) || (dnsserver == NULL)) {
1587+
return ERR_ARG;
15831588
}
1584-
return ERR_ARG;
1589+
1590+
if (type >= DNS_TYPE_MAX) {
1591+
return ERR_VAL;
1592+
}
1593+
1594+
*dnsserver = dhcps->dns_server[type];
1595+
return ERR_OK;
15851596
}
1597+
1598+
/******************************************************************************
1599+
* FunctionName : dhcps_dns_getserver_by_type
1600+
* Description : Get the main DNS server address for dhcpserver
1601+
* Parameters : dnsserver -- The DNS server address
1602+
* Returns : ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
1603+
*******************************************************************************/
1604+
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver)
1605+
{
1606+
return dhcps_dns_getserver_by_type(dhcps, dnsserver, DNS_TYPE_MAIN);
1607+
}
1608+
15861609
#endif // ESP_DHCPS

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,38 @@ err_t dhcps_set_option_info(dhcps_t *dhcps, u8_t op_id, void *opt_info, u32_t op
168168
bool dhcp_search_ip_on_mac(dhcps_t *dhcps, u8_t *mac, ip4_addr_t *ip);
169169

170170
/**
171-
* @brief Sets DNS server address for the DHCP server
171+
* @brief Sets the DNS server address for the DHCP server
172+
* @param dhcps Pointer to the DHCP handle
173+
* @param dnsserver Address of the DNS server
174+
* @return ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
175+
*/
176+
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver);
177+
178+
/**
179+
* @brief Sets the DNS server address for the DHCP server with a specific type
172180
* @param dhcps Pointer to the DHCP handle
173181
* @param dnsserver Address of the DNS server
174182
* @param type Type of the DNS server
175-
* @return ERR_ARG if invalid handle, ERR_OK on success
183+
* @return ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
176184
*/
177-
err_t dhcps_dns_setserver(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t type);
185+
err_t dhcps_dns_setserver_by_type(dhcps_t *dhcps, const ip_addr_t *dnsserver, dns_type_t type);
178186

179187
/**
180-
* @brief Gets DNS server associated with this DHCP server
188+
* @brief Gets the DNS server associated with this DHCP server
189+
* @param dhcps Pointer to the DHCP handle
190+
* @param dnsserver Address of the DNS server
191+
* @return ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
192+
*/
193+
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver);
194+
195+
/**
196+
* @brief Gets the DNS server associated with this DHCP server with a specific type
181197
* @param dhcps Pointer to the DHCP handle
182198
* @param dnsserver Address of the DNS server
183199
* @param type Type of the DNS server
184-
* @return ERR_ARG if invalid handle, ERR_OK on success
200+
* @return ERR_ARG if invalid handle, ERR_VAL if invalid type, ERR_OK on success
185201
*/
186-
err_t dhcps_dns_getserver(dhcps_t *dhcps, ip4_addr_t *dnsserver, dns_type_t type);
202+
err_t dhcps_dns_getserver_by_type(dhcps_t *dhcps, ip4_addr_t *dnsserver, dns_type_t type);
187203

188204
/**
189205
* @brief Sets callback on assigning an IP to the connected client

components/lwip/test_apps/main/lwip_test.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,24 @@ TEST(lwip, dhcp_server_init_deinit)
144144
dhcps_delete(dhcps);
145145
}
146146

147+
typedef enum
148+
{
149+
DNS_CALLBACK_TYPE_GET = 0, /**< DNS main server address*/
150+
DNS_CALLBACK_TYPE_SET, /**< DNS backup server address (Wi-Fi STA and Ethernet only) */
151+
} dns_callback_type_t;
152+
153+
typedef struct dhcps_dns_options_ {
154+
dns_callback_type_t cb_type;
155+
ip_addr_t *dnsserver;
156+
dns_type_t type;
157+
} dhcps_dns_options_t;
158+
147159
struct dhcps_api {
148160
EventGroupHandle_t event;
161+
dhcps_t *dhcps;
149162
ip4_addr_t netmask;
150163
ip4_addr_t ip;
164+
dhcps_dns_options_t dns_options;
151165
err_t ret_start;
152166
err_t ret_stop;
153167
};
@@ -220,6 +234,75 @@ TEST(lwip, dhcp_server_start_stop_localhost)
220234
dhcps_test_net_classes(0xC0A8C808, 0xFFFFFFF8, false);
221235
}
222236

237+
static void dhcps_test_dns_options_api(void* ctx)
238+
{
239+
struct netif *netif;
240+
struct dhcps_api *api = ctx;
241+
242+
NETIF_FOREACH(netif) {
243+
if (netif->name[0] == 'l' && netif->name[1] == 'o') {
244+
break;
245+
}
246+
}
247+
TEST_ASSERT_NOT_NULL(netif);
248+
249+
if (api->dns_options.cb_type == DNS_CALLBACK_TYPE_GET) {
250+
api->ret_start = dhcps_dns_getserver_by_type(api->dhcps,
251+
ip_2_ip4(api->dns_options.dnsserver),
252+
api->dns_options.type);
253+
} else {
254+
api->ret_start = dhcps_dns_setserver_by_type(api->dhcps,
255+
api->dns_options.dnsserver,
256+
api->dns_options.type);
257+
}
258+
xEventGroupSetBits(api->event, 1);
259+
}
260+
261+
static void dhcps_test_dns_options(dns_callback_type_t cb_type,
262+
dhcps_t *dhcps, ip_addr_t *dnsserver,
263+
dns_type_t type, bool pass)
264+
{
265+
struct dhcps_api api = {
266+
.dhcps = dhcps,
267+
.dns_options.cb_type = cb_type,
268+
.dns_options.dnsserver = dnsserver,
269+
.dns_options.type = type,
270+
.ret_start = ERR_IF,
271+
.event = xEventGroupCreate()
272+
};
273+
274+
tcpip_callback(dhcps_test_dns_options_api, &api);
275+
xEventGroupWaitBits(api.event, 1, true, true, pdMS_TO_TICKS(5000));
276+
vEventGroupDelete(api.event);
277+
278+
TEST_ASSERT((api.ret_start == ERR_OK) == pass);
279+
}
280+
281+
TEST(lwip, dhcp_server_dns_options)
282+
{
283+
test_case_uses_tcpip();
284+
285+
// Class C: IP: 192.168.4.1
286+
ip_addr_t ip = IPADDR4_INIT_BYTES(192, 168, 4, 1);
287+
288+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_SET, NULL, &ip, DNS_TYPE_MAIN, false);
289+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_GET, NULL, &ip, DNS_TYPE_MAIN, false);
290+
291+
dhcps_t *dhcps = dhcps_new();
292+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_SET, dhcps, NULL, DNS_TYPE_MAIN, true);
293+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_GET, dhcps, NULL, DNS_TYPE_MAIN, false);
294+
295+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_SET, dhcps, &ip, DNS_TYPE_MAX, false);
296+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_GET, dhcps, &ip, DNS_TYPE_MAX, false);
297+
298+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_SET, dhcps, &ip, DNS_TYPE_MAIN, true);
299+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_GET, dhcps, &ip, DNS_TYPE_MAIN, true);
300+
301+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_SET, dhcps, &ip, DNS_TYPE_BACKUP, true);
302+
dhcps_test_dns_options(DNS_CALLBACK_TYPE_GET, dhcps, &ip, DNS_TYPE_BACKUP, true);
303+
304+
dhcps_delete(dhcps);
305+
}
223306

224307
int test_sntp_server_create(void)
225308
{
@@ -324,6 +407,7 @@ TEST_GROUP_RUNNER(lwip)
324407
RUN_TEST_CASE(lwip, localhost_ping_test)
325408
RUN_TEST_CASE(lwip, dhcp_server_init_deinit)
326409
RUN_TEST_CASE(lwip, dhcp_server_start_stop_localhost)
410+
RUN_TEST_CASE(lwip, dhcp_server_dns_options)
327411
RUN_TEST_CASE(lwip, sntp_client_time_2015)
328412
RUN_TEST_CASE(lwip, sntp_client_time_2048)
329413
}

0 commit comments

Comments
 (0)