Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion include/netutils/dhcpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ struct dhcpc_state
struct in_addr serverid;
struct in_addr ipaddr;
struct in_addr netmask;
struct in_addr dnsaddr;
struct in_addr dnsaddr[CONFIG_NETDB_DNSSERVER_NAMESERVERS];
uint8_t num_dnsaddr; /* Number of DNS addresses received */
struct in_addr default_router;
uint32_t lease_time; /* Lease expires in this number of seconds */
uint32_t renewal_time; /* Seconds to transition to RENEW state(T1) */
Expand Down
4 changes: 4 additions & 0 deletions include/netutils/netlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,14 @@ int netlib_ifdown(FAR const char *ifname);

#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
int netlib_set_ipv4dnsaddr(FAR const struct in_addr *inaddr);
int netlib_del_ipv4dnsaddr(FAR const struct in_addr *inaddr);
int netlib_del_ipv4dnsaddr_by_index(int index);
#endif

#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETDB_DNSCLIENT)
int netlib_set_ipv6dnsaddr(FAR const struct in6_addr *inaddr);
int netlib_del_ipv6dnsaddr(FAR const struct in6_addr *inaddr);
int netlib_del_ipv6dnsaddr_by_index(int index);
#endif

int netlib_set_mtu(FAR const char *ifname, int mtu);
Expand Down
48 changes: 40 additions & 8 deletions netutils/dhcpc/dhcpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,32 @@ static uint8_t dhcpc_parseoptions(FAR struct dhcpc_state *presult,

case DHCP_OPTION_DNS_SERVER:

/* Get the DNS server address in network order */
/* Get the DNS server addresses in network order.
* DHCP option 6 can contain multiple DNS server addresses,
* each 4 bytes long.
*/

if (optptr + 6 <= end)
if (optptr + 2 <= end)
{
memcpy(&presult->dnsaddr.s_addr, optptr + 2, 4);
uint8_t optlen = *(optptr + 1);
uint8_t num_dns = optlen / 4;
uint8_t i;

/* Limit to configured maximum */

if (num_dns > CONFIG_NETDB_DNSSERVER_NAMESERVERS)
{
num_dns = CONFIG_NETDB_DNSSERVER_NAMESERVERS;
}

presult->num_dnsaddr = 0;
for (i = 0; i < num_dns && (optptr + 2 + i * 4 + 4) <= end;
i++)
{
memcpy(&presult->dnsaddr[i].s_addr, optptr + 2 + i * 4,
4);
presult->num_dnsaddr++;
}
}
else
{
Expand Down Expand Up @@ -946,11 +967,22 @@ int dhcpc_request(FAR void *handle, FAR struct dhcpc_state *presult)
ip4_addr2(presult->netmask.s_addr),
ip4_addr3(presult->netmask.s_addr),
ip4_addr4(presult->netmask.s_addr));
ninfo("Got DNS server %u.%u.%u.%u\n",
ip4_addr1(presult->dnsaddr.s_addr),
ip4_addr2(presult->dnsaddr.s_addr),
ip4_addr3(presult->dnsaddr.s_addr),
ip4_addr4(presult->dnsaddr.s_addr));

/* Print all DNS servers received */

if (presult->num_dnsaddr > 0)
{
uint8_t i;
for (i = 0; i < presult->num_dnsaddr; i++)
{
ninfo("Got DNS server %d: %u.%u.%u.%u\n", i,
ip4_addr1(presult->dnsaddr[i].s_addr),
ip4_addr2(presult->dnsaddr[i].s_addr),
ip4_addr3(presult->dnsaddr[i].s_addr),
ip4_addr4(presult->dnsaddr[i].s_addr));
}
}

ninfo("Got default router %u.%u.%u.%u\n",
ip4_addr1(presult->default_router.s_addr),
ip4_addr2(presult->default_router.s_addr),
Expand Down
4 changes: 2 additions & 2 deletions netutils/netlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if(CONFIG_NETUTILS_NETLIB)
endif()
endif()
if(CONFIG_NETDB_DNSCLIENT)
list(APPEND SRCS netlib_setipv4dnsaddr.c)
list(APPEND SRCS netlib_setipv4dnsaddr.c netlib_delipv4dnsaddr.c)
endif()
if(CONFIG_NET_ROUTE)
list(APPEND SRCS netlib_ipv4route.c netlib_ipv4router.c)
Expand All @@ -70,7 +70,7 @@ if(CONFIG_NETUTILS_NETLIB)
list(APPEND SRCS netlib_autoconfig.c netlib_obtainipv6addr.c)
endif()
if(CONFIG_NETDB_DNSCLIENT)
list(APPEND SRCS netlib_setipv6dnsaddr.c)
list(APPEND SRCS netlib_setipv6dnsaddr.c netlib_delipv6dnsaddr.c)
endif()
if(CONFIG_NET_ROUTE)
list(APPEND SRCS netlib_ipv6route.c netlib_ipv6router.c)
Expand Down
4 changes: 2 additions & 2 deletions netutils/netlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CSRCS += netlib_getarptab.c
endif
endif
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
CSRCS += netlib_setipv4dnsaddr.c
CSRCS += netlib_setipv4dnsaddr.c netlib_delipv4dnsaddr.c
endif
ifeq ($(CONFIG_NET_ROUTE),y)
CSRCS += netlib_ipv4route.c netlib_ipv4router.c
Expand All @@ -71,7 +71,7 @@ CSRCS += netlib_autoconfig.c
CSRCS += netlib_obtainipv6addr.c
endif
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
CSRCS += netlib_setipv6dnsaddr.c
CSRCS += netlib_setipv6dnsaddr.c netlib_delipv6dnsaddr.c
endif
ifeq ($(CONFIG_NET_ROUTE),y)
CSRCS += netlib_ipv6route.c netlib_ipv6router.c
Expand Down
97 changes: 97 additions & 0 deletions netutils/netlib/netlib_delipv4dnsaddr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/****************************************************************************
* apps/netutils/netlib/netlib_delipv4dnsaddr.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <sys/socket.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <netinet/in.h>
#include <nuttx/net/dns.h>

#include "netutils/netlib.h"

#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: netlib_del_ipv4dnsaddr
*
* Description:
* Remove a DNS server IPv4 address from the list by address
*
* Parameters:
* inaddr The address to remove
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/

int netlib_del_ipv4dnsaddr(FAR const struct in_addr *inaddr)
{
struct sockaddr_in addr;
int ret = -EINVAL;

if (inaddr)
{
addr.sin_family = AF_INET;
addr.sin_port = 0;
memcpy(&addr.sin_addr, inaddr, sizeof(struct in_addr));
bzero(&addr.sin_zero, sizeof(addr.sin_zero));

ret = dns_del_nameserver((FAR const struct sockaddr *)&addr,
sizeof(struct sockaddr_in));
}

return ret;
}

/****************************************************************************
* Name: netlib_del_ipv4dnsaddr_by_index
*
* Description:
* Remove a DNS server IPv4 address from the list by index (0-based)
*
* Parameters:
* index: The index of the DNS server to remove (0=first, 1=second, etc.)
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/

int netlib_del_ipv4dnsaddr_by_index(int index)
{
return dns_del_nameserver_by_index(index);
}

#endif /* CONFIG_NET_IPv4 && CONFIG_NETDB_DNSCLIENT */
97 changes: 97 additions & 0 deletions netutils/netlib/netlib_delipv6dnsaddr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/****************************************************************************
* apps/netutils/netlib/netlib_delipv6dnsaddr.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/config.h>

#include <sys/socket.h>
#include <string.h>
#include <errno.h>

#include <netinet/in.h>
#include <nuttx/net/dns.h>

#include "netutils/netlib.h"

#if defined(CONFIG_NET_IPv6) && defined(CONFIG_NETDB_DNSCLIENT)

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: netlib_del_ipv6dnsaddr
*
* Description:
* Remove a DNS server IPv6 address from the list by address
*
* Parameters:
* inaddr The IPv6 address to remove
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/

int netlib_del_ipv6dnsaddr(FAR const struct in6_addr *inaddr)
{
struct sockaddr_in6 addr;
int ret = -EINVAL;

if (inaddr)
{
/* Del the IPv6 DNS server address */

addr.sin6_family = AF_INET6;
addr.sin6_port = 0;
memcpy(&addr.sin6_addr, inaddr, sizeof(struct in6_addr));

ret = dns_del_nameserver((FAR const struct sockaddr *)&addr,
sizeof(struct sockaddr_in6));
}

return ret;
}

/****************************************************************************
* Name: netlib_del_ipv6dnsaddr_by_index
*
* Description:
* Remove a DNS server IPv6 address from the list by index (0-based)
*
* Parameters:
* index:The index of the DNS server to remove (0=first, 1=second, etc.)
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/

int netlib_del_ipv6dnsaddr_by_index(int index)
{
return dns_del_nameserver_by_index(index);
}

#endif /* CONFIG_NET_IPv6 && CONFIG_NETDB_DNSCLIENT */
20 changes: 15 additions & 5 deletions netutils/netlib/netlib_obtainipv4addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,23 @@ static int dhcp_setup_result(FAR const char *ifname,
}

#ifdef CONFIG_NETDB_DNSCLIENT
if (ds->dnsaddr.s_addr != 0)
/* Set all received DNS server addresses */

if (ds->num_dnsaddr > 0)
{
ret = netlib_set_ipv4dnsaddr(&ds->dnsaddr);
if (ret < 0)
uint8_t i;
for (i = 0; i < ds->num_dnsaddr; i++)
{
nerr("ERROR: set the DNS server address failed: %d\n", ret);
return ret;
if (ds->dnsaddr[i].s_addr != 0)
{
ret = netlib_set_ipv4dnsaddr(&ds->dnsaddr[i]);
if (ret < 0)
{
nerr("ERROR: set DNS server %d address failed: %d\n",
i, ret);
return ret;
}
}
}
}
#endif
Expand Down
Loading