apps/netutils/netlib and other files: Create netlib wrapper functions around dns_getserver() and dns_setserver() to isolate application code from changes to those interfaces.

This commit is contained in:
Gregory Nutt 2015-07-12 11:53:23 -06:00
parent 812450ebe6
commit ba72767d09
12 changed files with 227 additions and 27 deletions

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/bridge/bridge_main.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2014-2015 Gregory Nutt. All rights reserved.
*
* Authors: Gregory Nutt <gnutt@nuttx.org>
*
@ -55,7 +55,6 @@
#if defined(CONFIG_EXAMPLES_BRIDGE_NET1_DHCPC) || \
defined(CONFIG_EXAMPLES_BRIDGE_NET2_DHCPC)
# include <arpa/inet.h>
# include <nuttx/net/dns.h>
# include <apps/netutils/dhcpc.h>
#endif
@ -169,7 +168,7 @@ printf("NET1: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET1_IFNAME);
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);
@ -290,7 +289,7 @@ printf("NET2: Configuring %s\n", CONFIG_EXAMPLES_BRIDGE_NET2_IFNAME);
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);

View File

@ -2,7 +2,7 @@
* examples/discover/discover_main.c
*
* Copyright (C) 2012 Max Holtzberg. All rights reserved.
* Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2008, 2011-2012, 2015 Gregory Nutt. All rights reserved.
*
* Authors: Max Holtzberg <mh@uvc.de>
* Gregory Nutt <gnutt@nuttx.org>
@ -65,7 +65,6 @@
/* DHCPC may be used in conjunction with any other feature (or not) */
#ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC
# include <nuttx/net/dns.h>
# include <apps/netutils/dhcpc.h>
#endif
@ -167,7 +166,7 @@ int discover_main(int argc, char *argv[])
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);

View File

@ -2,7 +2,7 @@
* examples/tcpecho/tcpecho_main.c
*
* Copyright (C) 2013 Max Holtzberg. All rights reserved.
* Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2008, 2011-2012, 2015 Gregory Nutt. All rights reserved.
*
* Authors: Max Holtzberg <mh@uvc.de>
* Gregory Nutt <gnutt@nuttx.org>
@ -71,7 +71,6 @@
/* DHCPC may be used in conjunction with any other feature (or not) */
#ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC
# include <nuttx/net/dns.h>
# include <apps/netutils/dhcpc.h>
#endif
@ -181,7 +180,7 @@ static int tcpecho_netsetup()
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/webserver/webserver_main.c
*
* Copyright (C) 2007, 2009-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009-2012, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Based on uIP which also has a BSD style license:
@ -70,7 +70,6 @@
/* DHCPC may be used in conjunction with any other feature (or not) */
#ifdef CONFIG_EXAMPLES_WEBSERVER_DHCPC
# include <nuttx/net/dns.h>
# include <apps/netutils/dhcpc.h>
#endif
@ -173,7 +172,7 @@ int webserver_main(int argc, char *argv[])
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/wget/wget_main.c
*
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2011, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -47,7 +47,6 @@
#include <net/if.h>
#include <apps/netutils/netlib.h>
#include <nuttx/net/dns.h>
#include <apps/netutils/webclient.h>
/****************************************************************************

View File

@ -2,6 +2,7 @@
* apps/examples/xmlrpc/xmlrpc_main.c
*
* Copyright (C) 2012 Max Holtzberg. All rights reserved.
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Max Holtzberg <mh@uvc.de>
*
* Based on the embeddable lightweight XML-RPC server code discussed
@ -80,7 +81,6 @@
/* DHCPC may be used in conjunction with any other feature (or not) */
#ifdef CONFIG_EXAMPLES_XMLRPC_DHCPC
# include <nuttx/net/dns.h>
# include <apps/netutils/dhcpc.h>
#endif
@ -341,7 +341,7 @@ static int xmlrpc_netinit(void)
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);

View File

@ -3,7 +3,7 @@
* Various non-standard APIs to support netutils. All non-standard and
* intended only for internal use.
*
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2011m 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Some of these APIs derive from uIP. uIP also has a BSD style license:
@ -72,7 +72,7 @@
#endif
/****************************************************************************
* Public Function Prototypes
* Public Data
****************************************************************************/
#undef EXTERN
@ -84,6 +84,10 @@ extern "C"
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Convert a textual representation of an IP address to a numerical representation.
*
* This function takes a textual representation of an IP address in
@ -103,9 +107,9 @@ extern "C"
bool netlib_ipaddrconv(FAR const char *addrstr, uint8_t *addr);
bool netlib_hwmacconv(FAR const char *hwstr, uint8_t *hw);
#ifdef CONFIG_NET_ETHERNET
/* Get and set IP/MAC addresses (Ethernet L2 only) */
#ifdef CONFIG_NET_ETHERNET
int netlib_setmacaddr(FAR const char *ifname, const uint8_t *macaddr);
int netlib_getmacaddr(FAR const char *ifname, uint8_t *macaddr);
#endif
@ -129,9 +133,9 @@ uint8_t netlib_ipv6netmask2prefix(FAR const uint16_t *mask);
void netlib_prefix2ipv6netmask(uint8_t preflen, FAR struct in6_addr *netmask);
#endif
#ifdef CONFIG_NET_ICMPv6_AUTOCONF
/* ICMPv6 Autoconfiguration */
#ifdef CONFIG_NET_ICMPv6_AUTOCONF
int netlib_icmpv6_autoconfiguration(FAR const char *ifname);
#endif
@ -151,6 +155,13 @@ int netlib_getifstatus(FAR const char *ifname, FAR uint8_t *flags);
int netlib_ifup(FAR const char *ifname);
int netlib_ifdown(FAR const char *ifname);
/* DNS server addressing */
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
int netlib_set_ipv4dnsaddr(FAR const struct in_addr *inaddr);
int netlib_get_ipv4addr(FAR struct in_addr *inaddr);
#endif
#undef EXTERN
#ifdef __cplusplus
}

View File

@ -48,6 +48,9 @@ CSRCS += netlib_setifstatus.c netlib_getifstatus.c
ifeq ($(CONFIG_NET_IPv4),y)
CSRCS += netlib_setipv4addr.c netlib_getipv4addr.c
CSRCS += netlib_setdripv4addr.c netlib_setipv4netmask.c
ifeq ($(CONFIG_NETDB_DNSCLIENT),y)
CSRCS += netlib_setipv4dnsaddr.c netlib_getipv4dnsaddr.c
endif
endif
ifeq ($(CONFIG_NET_IPv6),y)

View File

@ -0,0 +1,98 @@
/****************************************************************************
* netutils/netlib/netlib_getipv4addr.c
*
* Copyright (C) 2007-2009, 2011, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <netinet/in.h>
#include <nuttx/net/dns.h>
#include <apps/netutils/netlib.h>
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netlib_get_ipv4addr
*
* Description:
* Get the DNS server IPv4address
*
* Parameters:
* ipaddr The location to return the IPv4 address
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/
int netlib_get_ipv4dnsaddr(FAR struct in_addr *inaddr)
{
struct sockaddr_in addr;
socklen_t addrlen;
int ret = -EINVAL;
if (inaddr)
{
addrlen = sizeof(struct sockaddr_in);
ret = dns_getserver((FAR struct sockaddr *)&addr, &addrlen);
if (ret >= 0)
{
/* Sanity check */
DEBUGASSERT(addr.sin_family == AF_INET &&
addrlen == sizeof(sizeof(struct sockaddr_in)));
memcpy(inaddr, &addr.sin_addr, sizeof(struct in_addr));
}
}
return ret;
}
#endif /* CONFIG_NET_IPv4 && CONFIG_NETDB_DNSCLIENT */

View File

@ -0,0 +1,94 @@
/****************************************************************************
* netutils/netlib/netlib_setipv4dnsaddr.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* 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 <apps/netutils/netlib.h>
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NETDB_DNSCLIENT)
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netlib_set_ipv4dnsaddr
*
* Description:
* Set the DNS server IPv4 address
*
* Parameters:
* inaddr The address to set
*
* Return:
* Zero (OK) is returned on success; A negated errno value is returned
* on failure.
*
****************************************************************************/
int netlib_set_ipv4dnsaddr(FAR const struct in_addr *inaddr)
{
struct sockaddr_in addr;
int ret = -EINVAL;
if (inaddr)
{
/* Set the IPv4 DNS server address */
addr.sin_family = AF_INET;
addr.sin_port = 0;
memcpy(&addr.sin_addr, inaddr, sizeof(struct in_addr));
ret = dns_setserver((FAR const struct sockaddr *)&addr,
sizeof(struct sockaddr_in));
}
return ret;
}
#endif /* CONFIG_NET_IPv4 && CONFIG_NETDB_DNSCLIENT */

View File

@ -562,7 +562,7 @@ static int ifconfig_callback(FAR struct net_driver_s *dev, void *arg)
nsh_output(vtbl, "Mask:%s\n", inet_ntoa(addr));
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
dns_getserver(&addr);
netlib_get_ipv4dnsaddr(&addr);
nsh_output(vtbl, "\tDNSaddr:%s\n", inet_ntoa(addr));
#endif
#endif
@ -1216,7 +1216,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
addr.s_addr = gip;
}
dns_setserver(&addr);
netlib_set_ipv4dnsaddr(&addr);
}
#endif /* CONFIG_NET_IPv4 */
#endif /* CONFIG_NSH_DHCPC || CONFIG_NSH_DNS */
@ -1256,7 +1256,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);

View File

@ -69,7 +69,6 @@
#include <apps/netutils/netlib.h>
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
# include <nuttx/net/dns.h>
# include <apps/netutils/dhcpc.h>
#endif
@ -289,7 +288,7 @@ static void nsh_netinit_configure(void)
#if defined(CONFIG_NSH_DNS)
addr.s_addr = HTONL(CONFIG_NSH_DNSIPADDR);
dns_setserver(&addr);
netlib_set_ipv4dnsaddr(&addr);
#endif
#if defined(CONFIG_NSH_DHCPC)
@ -323,7 +322,7 @@ static void nsh_netinit_configure(void)
if (ds.dnsaddr.s_addr != 0)
{
dns_setserver(&ds.dnsaddr);
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
}
dhcpc_close(handle);