Skip to content

Commit 5ef0508

Browse files
M-Haziq94nashif
authored andcommitted
net: dhcpv4_server: dns server option
dhcv4 server was not providing the dns server details to the client because dns option was handled. Added the dns server option to be send to the client as response form dhcpv4 server Signed-off-by: Muhammad Haziq <[email protected]>
1 parent 2e3778a commit 5ef0508

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

subsys/net/lib/dhcpv4/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,15 @@ config NET_DHCPV4_SERVER_NAK_UNRECOGNIZED_REQUESTS
162162
will send a NAK, informing the client it should proceed with a full
163163
procedure.
164164

165+
config NET_DHCPV4_SERVER_OPTION_DNS_ADDRESS
166+
string "DNS Server address"
167+
default DNS_SERVER1 if DNS_SERVER_IP_ADDRESSES
168+
default ""
169+
help
170+
This configuration option specifies the DNS server address that the DHCPv4
171+
server should provide to clients when they request an IP address. The
172+
DHCPv4 server adds this DNS server address in the DHCP offer and
173+
acknowledgment messages sent to the clients, allowing them to use the
174+
specified DNS server for name resolution.
175+
165176
endif # NET_DHCPV4_SERVER

subsys/net/lib/dhcpv4/dhcpv4_server.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LOG_MODULE_REGISTER(net_dhcpv4_server, CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL);
3030
#define DHCPV4_OPTIONS_SERVER_ID_SIZE 6
3131
#define DHCPV4_OPTIONS_SUBNET_MASK_SIZE 6
3232
#define DHCPV4_OPTIONS_ROUTER_SIZE 6
33+
#define DHCPV4_OPTIONS_DNS_SERVER_SIZE 6
3334
#define DHCPV4_OPTIONS_CLIENT_ID_MIN_SIZE 2
3435

3536
#define ADDRESS_RESERVED_TIMEOUT K_SECONDS(30)
@@ -398,6 +399,29 @@ static uint8_t *dhcpv4_encode_router_option(uint8_t *buf, size_t *buflen,
398399
return buf + DHCPV4_OPTIONS_ROUTER_SIZE;
399400
}
400401

402+
static uint8_t *dhcpv4_encode_dns_server_option(uint8_t *buf, size_t *buflen)
403+
{
404+
struct in_addr dns_address;
405+
406+
if (buf == NULL || *buflen < DHCPV4_OPTIONS_DNS_SERVER_SIZE) {
407+
return NULL;
408+
}
409+
410+
if (net_addr_pton(AF_INET, CONFIG_NET_DHCPV4_SERVER_OPTION_DNS_ADDRESS, &dns_address)) {
411+
LOG_ERR("Invalid DNS server address: %s",
412+
CONFIG_NET_DHCPV4_SERVER_OPTION_DNS_ADDRESS);
413+
return NULL;
414+
}
415+
416+
buf[0] = DHCPV4_OPTIONS_DNS_SERVER;
417+
buf[1] = sizeof(struct in_addr);
418+
memcpy(&buf[2], dns_address.s4_addr, sizeof(struct in_addr));
419+
420+
*buflen -= DHCPV4_OPTIONS_DNS_SERVER_SIZE;
421+
422+
return buf + DHCPV4_OPTIONS_DNS_SERVER_SIZE;
423+
}
424+
401425
static uint8_t *dhcpv4_encode_end_option(uint8_t *buf, size_t *buflen)
402426
{
403427
if (buf == NULL || *buflen < 1) {
@@ -498,6 +522,13 @@ static uint8_t *dhcpv4_encode_requested_params(
498522
goto out;
499523
}
500524
break;
525+
526+
case DHCPV4_OPTIONS_DNS_SERVER:
527+
buf = dhcpv4_encode_dns_server_option(buf, buflen);
528+
if (buf == NULL) {
529+
goto out;
530+
}
531+
break;
501532
/* Others - just ignore. */
502533
default:
503534
break;

0 commit comments

Comments
 (0)