Breadk dnsclient.c file into three smaller files: dns_resolver.c, dns_socket.c, and dns_gethostip.c
This commit is contained in:
parent
8b386385ab
commit
a0d6777c23
@ -878,6 +878,8 @@
|
||||
probably incomplete (2014-4-10).
|
||||
* apps/netutils/resolv: Long needed major clean up for coding style
|
||||
and unification of naming conventions (resolv vs dns) (2014-4-11).
|
||||
* aps/netutils/dnsclient and include/netutils/dnsclient.h: File name
|
||||
* apps/netutils/dnsclient and include/netutils/dnsclient.h: File name
|
||||
changes that are part of the overal resolv->dns renaming (2014-4-11).
|
||||
* apps/netutils/dnsclient: Break the one big monolithic file into
|
||||
three smaller files (2014-4-11).
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
############################################################################
|
||||
# apps/netutils/dnsclient/Makefile
|
||||
#
|
||||
# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
# Copyright (C) 2011-2012, 2014 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
@ -39,34 +39,34 @@ include $(APPDIR)/Make.defs
|
||||
|
||||
# DNS Resolver library
|
||||
|
||||
ASRCS =
|
||||
CSRCS =
|
||||
ASRCS =
|
||||
CSRCS =
|
||||
|
||||
ifeq ($(CONFIG_NET_UDP),y)
|
||||
CSRCS = dnsclient.c
|
||||
CSRCS = dns_resolver.c dns_socket.c dns_gethostip.c
|
||||
endif
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||
BIN = ..\..\libapps$(LIBEXT)
|
||||
BIN = ..\..\libapps$(LIBEXT)
|
||||
else
|
||||
ifeq ($(WINTOOL),y)
|
||||
BIN = ..\\..\\libapps$(LIBEXT)
|
||||
BIN = ..\\..\\libapps$(LIBEXT)
|
||||
else
|
||||
BIN = ../../libapps$(LIBEXT)
|
||||
BIN = ../../libapps$(LIBEXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: context depend clean distclean
|
||||
|
91
netutils/dnsclient/dns_gethostip.c
Normal file
91
netutils/dnsclient/dns_gethostip.c
Normal file
@ -0,0 +1,91 @@
|
||||
/****************************************************************************
|
||||
* apps/netutils/dnsclient/dns_gethostip.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2012, 2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Based heavily on portions of uIP:
|
||||
*
|
||||
* Author: Adam Dunkels <adam@dunkels.com>
|
||||
* Copyright (c) 2002-2003, Adam Dunkels.
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior
|
||||
* written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 <apps/netutils/dnsclient.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_gethostip
|
||||
*
|
||||
* Descriptions:
|
||||
* Combines the operations of dns_bind_sock(), dns_query_sock(), and
|
||||
* dns_free_sock() to obtain the the IP address ('ipaddr') associated with
|
||||
* the 'hostname' in one operation.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dns_gethostip(FAR const char *hostname, FAR in_addr_t *ipaddr)
|
||||
{
|
||||
int sockfd = -1;
|
||||
int ret=ERROR;
|
||||
|
||||
dns_bind_sock(&sockfd);
|
||||
if (sockfd >= 0)
|
||||
{
|
||||
ret = dns_query_sock(sockfd, hostname, ipaddr);
|
||||
dns_free_sock(&sockfd);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
123
netutils/dnsclient/dns_resolver.c
Normal file
123
netutils/dnsclient/dns_resolver.c
Normal file
@ -0,0 +1,123 @@
|
||||
/****************************************************************************
|
||||
* apps/netutils/dnsclient/dns_resolver.c
|
||||
* DNS host name to IP address resolver.
|
||||
*
|
||||
* The uIP DNS resolver functions are used to lookup a hostname and
|
||||
* map it to a numerical IP address. It maintains a list of resolved
|
||||
* hostnames that can be queried. New hostnames can be resolved using the
|
||||
* dns_whois() function.
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2012, 2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Based heavily on portions of uIP:
|
||||
*
|
||||
* Author: Adam Dunkels <adam@dunkels.com>
|
||||
* Copyright (c) 2002-2003, Adam Dunkels.
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior
|
||||
* written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 <netinet/in.h>
|
||||
|
||||
#include <apps/netutils/dnsclient.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static int g_sockfd = -1;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_bind
|
||||
*
|
||||
* Description:
|
||||
* Initialize the DNS resolver using an internal, share-able socket.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dns_bind(void)
|
||||
{
|
||||
return dns_bind_sock(&g_sockfd);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_query
|
||||
*
|
||||
* Description:
|
||||
* Using the internal DNS resolver socket, look up the the 'hostname', and
|
||||
* return its IP address in 'ipaddr'
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns zero (OK) if the query was successful.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dns_query(FAR const char *hostname, FAR in_addr_t *ipaddr)
|
||||
{
|
||||
return dns_query_sock(g_sockfd, hostname, ipaddr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_whois
|
||||
*
|
||||
* Description:
|
||||
* Get the binding for 'name' using the DNS server accessed via the DNS
|
||||
* resolvers internal socket.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
int dns_whois(FAR const char *name, FAR struct sockaddr_in6 *addr)
|
||||
#else
|
||||
int dns_whois(FAR const char *name, FAR struct sockaddr_in *addr)
|
||||
#endif
|
||||
{
|
||||
return dns_whois_socket(g_sockfd, name, addr);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/****************************************************************************
|
||||
* apps/netutils/dnsclient/dnsclient.c
|
||||
* apps/netutils/dnsclient/dns_socket.c
|
||||
* DNS host name to IP address resolver.
|
||||
*
|
||||
* The uIP DNS resolver functions are used to lookup a hostname and
|
||||
@ -162,7 +162,6 @@ struct namemap
|
||||
****************************************************************************/
|
||||
|
||||
static uint8_t g_seqno;
|
||||
static int g_sockfd = -1;
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
static struct sockaddr_in6 g_dnsserver;
|
||||
#else
|
||||
@ -174,14 +173,14 @@ static struct sockaddr_in g_dnsserver;
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: parse_name
|
||||
* Name: dns_parse_name
|
||||
*
|
||||
* Description:
|
||||
* Walk through a compact encoded DNS name and return the end of it.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static FAR unsigned char *parse_name(FAR unsigned char *query)
|
||||
static FAR unsigned char *dns_parse_name(FAR unsigned char *query)
|
||||
{
|
||||
unsigned char n;
|
||||
|
||||
@ -201,7 +200,7 @@ static FAR unsigned char *parse_name(FAR unsigned char *query)
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: send_query_socket
|
||||
* Name: dns_send_query
|
||||
*
|
||||
* Description:
|
||||
* Runs through the list of names to see if there are any that have
|
||||
@ -210,11 +209,11 @@ static FAR unsigned char *parse_name(FAR unsigned char *query)
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
static int send_query_socket(int sockfd, FAR const char *name,
|
||||
FAR struct sockaddr_in6 *addr)
|
||||
static int dns_send_query(int sockfd, FAR const char *name,
|
||||
FAR struct sockaddr_in6 *addr)
|
||||
#else
|
||||
static int send_query_socket(int sockfd, FAR const char *name,
|
||||
FAR struct sockaddr_in *addr)
|
||||
static int dns_send_query(int sockfd, FAR const char *name,
|
||||
FAR struct sockaddr_in *addr)
|
||||
#endif
|
||||
{
|
||||
register FAR struct dns_hdr *hdr;
|
||||
@ -263,22 +262,7 @@ static int send_query_socket(int sockfd, FAR const char *name,
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: send_query
|
||||
****************************************************************************/
|
||||
|
||||
#if 0 /* Not used */
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
static int send_query(FAR const char *name, FAR struct sockaddr_in6 *addr)
|
||||
#else
|
||||
static int send_query(FAR const char *name, FAR struct sockaddr_in *addr)
|
||||
#endif
|
||||
{
|
||||
return send_query_socket(g_sockfd, name, addr);
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: recv_response_socket
|
||||
* Name: dns_recv_response
|
||||
*
|
||||
* Description:
|
||||
* Called when new UDP data arrives
|
||||
@ -288,7 +272,7 @@ static int send_query(FAR const char *name, FAR struct sockaddr_in *addr)
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
# error "Not implemented"
|
||||
#else
|
||||
int recv_response_socket(int sockfd, FAR struct sockaddr_in *addr)
|
||||
static int dns_recv_response(int sockfd, FAR struct sockaddr_in *addr)
|
||||
#endif
|
||||
{
|
||||
FAR unsigned char *nameptr;
|
||||
@ -342,7 +326,7 @@ int recv_response_socket(int sockfd, FAR struct sockaddr_in *addr)
|
||||
#ifdef CONFIG_DEBUG_NET
|
||||
{
|
||||
int d = 64;
|
||||
nameptr = parse_name((unsigned char *)buffer + 12) + 4;
|
||||
nameptr = dns_parse_name((unsigned char *)buffer + 12) + 4;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
@ -360,7 +344,7 @@ int recv_response_socket(int sockfd, FAR struct sockaddr_in *addr)
|
||||
}
|
||||
#endif
|
||||
|
||||
nameptr = parse_name((unsigned char *)buffer + 12) + 4;
|
||||
nameptr = dns_parse_name((unsigned char *)buffer + 12) + 4;
|
||||
|
||||
for (; nanswers > 0; nanswers--)
|
||||
{
|
||||
@ -379,7 +363,7 @@ int recv_response_socket(int sockfd, FAR struct sockaddr_in *addr)
|
||||
{
|
||||
/* Not compressed name. */
|
||||
|
||||
nameptr = parse_name(nameptr);
|
||||
nameptr = dns_parse_name(nameptr);
|
||||
}
|
||||
|
||||
ans = (struct dns_answer *)nameptr;
|
||||
@ -418,19 +402,6 @@ int recv_response_socket(int sockfd, FAR struct sockaddr_in *addr)
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: recv_response
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
# error "Not implemented"
|
||||
#else
|
||||
int recv_response(FAR struct sockaddr_in *addr)
|
||||
#endif
|
||||
{
|
||||
return recv_response_socket(g_sockfd, addr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
@ -448,11 +419,19 @@ int dns_bind_sock(FAR int *sockfd)
|
||||
struct timeval tv;
|
||||
int ret;
|
||||
|
||||
if (*sockfd >= 0) dns_free_sock(sockfd);
|
||||
/* If the socket is already open, then close it now */
|
||||
|
||||
if (*sockfd >= 0)
|
||||
{
|
||||
dns_free_sock(sockfd);
|
||||
}
|
||||
|
||||
/* Create a new socket */
|
||||
|
||||
*sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
||||
if (*sockfd < 0)
|
||||
{
|
||||
ndbg("ERROR: socket() failed: %d\n", errno);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
@ -466,6 +445,7 @@ int dns_bind_sock(FAR int *sockfd)
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
ndbg("ERROR: setsockopt() failed: %d\n", errno);
|
||||
close(*sockfd);
|
||||
*sockfd = -1;
|
||||
return ERROR;
|
||||
@ -474,19 +454,6 @@ int dns_bind_sock(FAR int *sockfd)
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_bind
|
||||
*
|
||||
* Description:
|
||||
* Initialize the DNS resolver using an internal, share-able socket.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dns_bind(void)
|
||||
{
|
||||
return dns_bind_sock(&g_sockfd);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_free_sock
|
||||
*
|
||||
@ -570,23 +537,6 @@ int dns_query_sock(int sockfd, FAR const char *hostname, FAR in_addr_t *ipaddr)
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_query
|
||||
*
|
||||
* Description:
|
||||
* Using the internal DNS resolver socket, look up the the 'hostname', and
|
||||
* return its IP address in 'ipaddr'
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns zero (OK) if the query was successful.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dns_query(FAR const char *hostname, FAR in_addr_t *ipaddr)
|
||||
{
|
||||
return dns_query_sock(g_sockfd, hostname, ipaddr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_setserver
|
||||
*
|
||||
@ -654,12 +604,13 @@ int dns_whois_socket(int sockfd, FAR const char *name,
|
||||
|
||||
for (retries = 0; retries < 3; retries++)
|
||||
{
|
||||
if (send_query_socket(sockfd, name, &g_dnsserver) < 0)
|
||||
ret = dns_send_query(sockfd, name, &g_dnsserver);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
ret = recv_response_socket(sockfd, addr);
|
||||
ret = dns_recv_response(sockfd, addr);
|
||||
if (ret >= 0)
|
||||
{
|
||||
/* Response received successfully */
|
||||
@ -677,47 +628,3 @@ int dns_whois_socket(int sockfd, FAR const char *name,
|
||||
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_whois
|
||||
*
|
||||
* Description:
|
||||
* Get the binding for 'name' using the DNS server accessed via the DNS
|
||||
* resolvers internal socket.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
int dns_whois(FAR const char *name, FAR struct sockaddr_in6 *addr)
|
||||
#else
|
||||
int dns_whois(FAR const char *name, FAR struct sockaddr_in *addr)
|
||||
#endif
|
||||
{
|
||||
return dns_whois_socket(g_sockfd, name, addr);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: dns_gethostip
|
||||
*
|
||||
* Descriptions:
|
||||
* Combines the operations of dns_bind_sock(), dns_query_sock(), and
|
||||
* dns_free_sock() to obtain the the IP address ('ipaddr') associated with
|
||||
* the 'hostname' in one operation.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int dns_gethostip(FAR const char *hostname, FAR in_addr_t *ipaddr)
|
||||
{
|
||||
int sockfd = -1;
|
||||
int ret=ERROR;
|
||||
|
||||
dns_bind_sock(&sockfd);
|
||||
if (sockfd >= 0)
|
||||
{
|
||||
ret = dns_query_sock(sockfd, hostname, ipaddr);
|
||||
dns_free_sock(&sockfd);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user