DNS fixes from Darcy Gong

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5268 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2012-10-28 13:20:36 +00:00
parent 2a038d1bc0
commit d943f61d2d
7 changed files with 267 additions and 24 deletions

137
COPYING Normal file
View File

@ -0,0 +1,137 @@
COPYING -- Describes the terms under which Nuttx is distributed. A
copy of the BSD-style licensing is included in this file. In my
words -- I believe that you should free to use NuttX in any
environment, private, private, commercial, open, closed, etc.
provided only that you repect the modest copyright notices as
described in license (below). Please feel free to contact me if you
have any licensing concerns.
NuttX
^^^^^^
License for NuttX in general (authorship of individual files may vary):
Copyright (C) 2007-2012 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.
uIP
^^^
Some lower-level networking middle-ware components of NuttX
derive from uIP which has a similar BSD style license:
Copyright (c) 2001-2003, Adam Dunkels.
All rights reserved.
FreeModbus
^^^^^^^^^
FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
Copyright (c) 2006 Christian Walter <wolti@sil.at>
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.
THTTPD
^^^^^^
Derived from the original THTTPD package:
Copyright © 1995,1998,1999,2000,2001 by Jef Poskanzer <jef@mail.acme.com>.
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.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
cJSON
^^^^^
Derives from the cJSON Project which has an MIT license:
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -387,4 +387,6 @@
* apps/netutils/json: Add a snapshot of the cJSON project. Contributed by
Darcy Gong.
* apps/examples/json: Test example for cJSON from Darcy Gong
* apps/nshlib/nsh_netinit.c: Fix static IP DNS problem (Darcy Gong)
* apps/netutils/resolv/resolv.c: DNS fixes from Darcy Gong.
* COPYING: Licensing information added.

View File

@ -63,19 +63,30 @@ extern "C" {
/* Functions. */
EXTERN int resolv_init(void);
EXTERN int resolv_create(int *sockfd);
EXTERN int resolv_release(int *sockfd);
EXTERN int resolv_gethostip_socket(int sockfd, const char *hostname, in_addr_t *ipaddr);
EXTERN int resolv_gethostip(const char *hostname, in_addr_t *ipaddr);
#ifdef CONFIG_NET_IPv6
EXTERN void resolv_conf(FAR const struct in6_addr *dnsserver);
EXTERN void resolv_getserver(FAR const struct in_addr *dnsserver);
EXTERN int resolv_query(FAR const char *name, FAR struct sockaddr_in6 *addr);
EXTERN int resolv_query_socket(int sockfd, FAR const char *name, FAR struct sockaddr_in6 *addr);
#else
EXTERN void resolv_conf(FAR const struct in_addr *dnsserver);
EXTERN void resolv_getserver(FAR struct in_addr *dnsserver);
EXTERN int resolv_query(FAR const char *name, FAR struct sockaddr_in *addr);
EXTERN int resolv_query_socket(int sockfd, FAR const char *name, FAR struct sockaddr_in *addr);
#endif
EXTERN int dns_gethostip(const char *hostname, in_addr_t *ipaddr);
#define dns_init resolv_init
#define dns_bind resolv_create
#define dns_query resolv_gethostip_socket
#define dns_free resolv_release
#define dns_setserver resolv_conf
#define dns_getserver resolv_getserver
#define dns_whois resolv_query

View File

@ -36,7 +36,7 @@
#include <string.h>
#include <stdio.h>
#include <apps/math.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>

View File

@ -202,9 +202,9 @@ static unsigned char *parse_name(unsigned char *query)
*/
#ifdef CONFIG_NET_IPv6
static int send_query(const char *name, struct sockaddr_in6 *addr)
static int send_query_socket(int sockfd, const char *name, struct sockaddr_in6 *addr)
#else
static int send_query(const char *name, struct sockaddr_in *addr)
static int send_query_socket(int sockfd, const char *name, struct sockaddr_in *addr)
#endif
{
register struct dns_hdr *hdr;
@ -247,7 +247,16 @@ static int send_query(const char *name, struct sockaddr_in *addr)
DEBUGASSERT(((struct sockaddr *)addr)->sa_family == AF_INET);
#endif
return sendto(g_sockfd, buffer, query + 5 - buffer, 0, (struct sockaddr*)addr, ADDRLEN);
return sendto(sockfd, buffer, query + 5 - buffer, 0, (struct sockaddr*)addr, ADDRLEN);
}
#ifdef CONFIG_NET_IPv6
static int send_query(const char *name, struct sockaddr_in6 *addr)
#else
static int send_query(const char *name, struct sockaddr_in *addr)
#endif
{
return send_query_socket(g_sockfd, name, addr);
}
/* Called when new UDP data arrives */
@ -255,7 +264,7 @@ static int send_query(const char *name, struct sockaddr_in *addr)
#ifdef CONFIG_NET_IPv6
#error "Not implemented"
#else
int recv_response(struct sockaddr_in *addr)
int recv_response_socket(int sockfd, struct sockaddr_in *addr)
#endif
{
unsigned char *nameptr;
@ -268,7 +277,7 @@ int recv_response(struct sockaddr_in *addr)
/* Receive the response */
ret = recv(g_sockfd, buffer, RECV_BUFFER_SIZE, 0);
ret = recv(sockfd, buffer, RECV_BUFFER_SIZE, 0);
if (ret < 0)
{
return ret;
@ -377,15 +386,24 @@ int recv_response(struct sockaddr_in *addr)
return ERROR;
}
#ifdef CONFIG_NET_IPv6
#error "Not implemented"
#else
int recv_response(struct sockaddr_in *addr)
#endif
{
return recv_response_socket(g_sockfd, addr);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: dns_gethostip
* Name: resolv_gethostip
****************************************************************************/
int dns_gethostip(const char *hostname, in_addr_t *ipaddr)
int resolv_gethostip_socket(int sockfd, const char *hostname, in_addr_t *ipaddr)
{
#ifdef CONFIG_HAVE_GETHOSTBYNAME
@ -421,7 +439,7 @@ int dns_gethostip(const char *hostname, in_addr_t *ipaddr)
* the host name to an IP address.
*/
if (resolv_query(hostname, &addr) < 0)
if (resolv_query_socket(sockfd, hostname, &addr) < 0)
{
/* Needs to set the errno here */
@ -437,12 +455,32 @@ int dns_gethostip(const char *hostname, in_addr_t *ipaddr)
#endif
}
int resolv_gethostip(const char *hostname, in_addr_t *ipaddr)
{
return resolv_gethostip_socket(g_sockfd, hostname, ipaddr);
}
int dns_gethostip(const char *hostname, in_addr_t *ipaddr)
{
int sockfd = -1;
int ret=ERROR;
resolv_create(&sockfd);
if (sockfd >= 0)
{
ret = resolv_gethostip_socket(sockfd, hostname, ipaddr);
resolv_release(&sockfd);
}
return ret;
}
/* Get the binding for name. */
#ifdef CONFIG_NET_IPv6
int resolv_query(FAR const char *name, FAR struct sockaddr_in6 *addr)
int resolv_query_socket(int sockfd, FAR const char *name, FAR struct sockaddr_in6 *addr)
#else
int resolv_query(FAR const char *name, FAR struct sockaddr_in *addr)
int resolv_query_socket(int sockfd, FAR const char *name, FAR struct sockaddr_in *addr)
#endif
{
int retries;
@ -452,12 +490,12 @@ int resolv_query(FAR const char *name, FAR struct sockaddr_in *addr)
for (retries = 0; retries < 3; retries++)
{
if (send_query(name, &g_dnsserver) < 0)
if (send_query_socket(sockfd, name, &g_dnsserver) < 0)
{
return ERROR;
}
ret = recv_response(addr);
ret = recv_response_socket(sockfd, addr);
if (ret >= 0)
{
/* Response received successfully */
@ -476,6 +514,15 @@ int resolv_query(FAR const char *name, FAR struct sockaddr_in *addr)
return ERROR;
}
#ifdef CONFIG_NET_IPv6
int resolv_query(FAR const char *name, FAR struct sockaddr_in6 *addr)
#else
int resolv_query(FAR const char *name, FAR struct sockaddr_in *addr)
#endif
{
return resolv_query_socket(g_sockfd, name, addr);
}
/* Obtain the currently configured DNS server. */
#ifdef CONFIG_NET_IPv6
@ -508,13 +555,29 @@ void resolv_conf(const struct in_addr *dnsserver)
#endif
}
/* Initalize the resolver. */
/* Release the resolver. */
int resolv_init(void)
int resolv_release(int *sockfd)
{
if (*sockfd >= 0)
{
close(*sockfd);
*sockfd = -1;
}
return OK;
}
/* Create the resolver. */
int resolv_create(int *sockfd)
{
struct timeval tv;
g_sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (g_sockfd < 0)
if (*sockfd >= 0) resolv_release(sockfd);
*sockfd = socket(PF_INET, SOCK_DGRAM, 0);
if (*sockfd < 0)
{
return ERROR;
}
@ -523,12 +586,19 @@ int resolv_init(void)
tv.tv_sec = 30;
tv.tv_usec = 0;
if (setsockopt(g_sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) < 0)
if (setsockopt(*sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) < 0)
{
close(g_sockfd);
g_sockfd = -1;
close(*sockfd);
*sockfd = -1;
return ERROR;
}
return OK;
}
/* Initalize the resolver. */
int resolv_init(void)
{
return resolv_create(&g_sockfd);
}

View File

@ -490,7 +490,7 @@ config NSH_DHCPC
config NSH_IPADDR
hex "Target IP address"
default 0x10000002
default 0xa0000002
depends on NSH_LIBRARY && NET && !NSH_DHCPC
---help---
If NSH_DHCPC is NOT set, then the static IP address must be provided.
@ -499,7 +499,7 @@ config NSH_IPADDR
config NSH_DRIPADDR
hex "Router IP address"
default 0x10000001
default 0xa0000001
depends on NSH_LIBRARY && NET && !NSH_DHCPC
---help---
Default router IP address (aka, Gateway). This is a 32-bit integer
@ -513,6 +513,21 @@ config NSH_NETMASK
Network mask. This is a 32-bit integer value in host order. So, as
an example, 0xffffff00 would be 255.255.255.0.
config NSH_DNS
bool "Use DNS"
default n
depends on NSH_LIBRARY && NET && NET_UDP && NET_BROADCAST
---help---
Configure to use a DNS.
config NSH_DNSIPADDR
hex "DNS IP address"
default 0xa0000001
depends on NSH_DNS
---help---
Configure the DNS address. This is a 32-bit integer value in host
order. So, as an example, 0xa0000001 would be 10.0.0.1.
config NSH_NOMAC
bool "Hardware has no MAC address"
default n

View File

@ -60,6 +60,10 @@
* Definitions
****************************************************************************/
#if defined(CONFIG_NSH_DRIPADDR) && !defined(CONFIG_NSH_DNSIPADDR)
# define CONFIG_NSH_DNSIPADDR CONFIG_NSH_DRIPADDR
#endif
/****************************************************************************
* Private Types
****************************************************************************/
@ -125,10 +129,14 @@ int nsh_netinit(void)
addr.s_addr = HTONL(CONFIG_NSH_NETMASK);
uip_setnetmask("eth0", &addr);
#if defined(CONFIG_NSH_DHCPC)
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
/* Set up the resolver */
resolv_init();
#if defined(CONFIG_NSH_DNS)
addr.s_addr = HTONL(CONFIG_NSH_DNSIPADDR);
resolv_conf(&addr.s_addr);
#endif
#endif
#if defined(CONFIG_NSH_DHCPC)