Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions subsys/net/ip/dhcpv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <net/udp.h>
#include "udp_internal.h"
#include <net/dhcpv4.h>
#include <net/dns_resolve.h>

struct dhcp_msg {
u8_t op; /* Message type, 1:BOOTREQUEST, 2:BOOTREPLY */
Expand Down Expand Up @@ -745,6 +746,43 @@ static enum net_verdict parse_options(struct net_if *iface,
net_if_ipv4_set_gw(iface, &router);
break;
}
case DHCPV4_OPTIONS_DNS_SERVER: {
struct in_addr dns;
char dns_string[NET_IPV4_ADDR_LEN];
const char *dns_servers[] = {dns_string, NULL};

/* DNS server option may present 1 or more
* addresses. Each 4 bytes in length. DNS
* servers should be listed in order
* of preference. Hence we choose the first
* and skip the rest.
*/
if (length % 4 != 0 || length < 4) {
NET_ERR("options_dns, bad length");
return NET_DROP;
}

frag = net_frag_read(frag, pos, &pos, 4, dns.s4_addr);
frag = net_frag_skip(frag, pos, &pos, length - 4);
if (!frag && pos) {
NET_ERR("options_dns, short packet");
return NET_DROP;
}

memset(dns_string, 0, sizeof(dns_string));
memcpy(dns_string, net_sprint_ipv4_addr(&dns),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is what I don't like. Convert binary address to string, just for it to be converted back? Nope, we should allow to init resolver with struct in_addr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely won't do, because net_sprint_ipv4_addr() is debugging func, available only when logging is enabled.

@pdxjohnny : I'm in the process of refactoring this patch already.

min(strlen(net_sprint_ipv4_addr(&dns)),
sizeof(dns_string) - 1));

NET_DBG("options_dns: %s", dns_servers[0]);

// TODO Change this to its own context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: this TODO. I guess, for initial version of this feature it's not required.

if (dns_resolve_init(dns_resolve_get_default(), dns_servers) < 0) {
NET_DBG("options_dns, failed to set resolve address");
return NET_DROP;
}
break;
}
case DHCPV4_OPTIONS_LEASE_TIME:
if (length != 4) {
NET_ERR("options_lease_time, bad length");
Expand Down