2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2014-04-11 21:43:15 +02:00
|
|
|
* apps/netutils/dnsclient/dns_socket.c
|
2011-03-19 22:04:13 +01:00
|
|
|
* 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
|
2014-04-11 20:25:32 +02:00
|
|
|
* hostnames that can be queried. New hostnames can be resolved using the
|
|
|
|
* dns_whois() function.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2014-04-11 20:25:32 +02:00
|
|
|
* Copyright (C) 2007, 2009, 2012, 2014 Gregory Nutt. All rights reserved.
|
2012-02-18 19:13:30 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2012-10-20 15:47:30 +02:00
|
|
|
#include <assert.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <netinet/in.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-04-11 20:41:13 +02:00
|
|
|
#include <apps/netutils/dnsclient.h>
|
2014-07-03 00:04:25 +02:00
|
|
|
#include <apps/netutils/netlib.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-04-11 20:25:32 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
#ifndef CONFIG_NETUTILS_DNSCLIENT_ENTRIES
|
|
|
|
# define RESOLV_ENTRIES 4
|
|
|
|
#else /* CONFIG_NETUTILS_DNSCLIENT_ENTRIES */
|
|
|
|
# define RESOLV_ENTRIES CONFIG_NETUTILS_DNSCLIENT_ENTRIES
|
|
|
|
#endif /* CONFIG_NETUTILS_DNSCLIENT_ENTRIES */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#ifndef NULL
|
2014-04-11 20:25:32 +02:00
|
|
|
# define NULL (void *)0
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif /* NULL */
|
|
|
|
|
|
|
|
/* The maximum number of retries when asking for a name */
|
|
|
|
|
|
|
|
#define MAX_RETRIES 8
|
|
|
|
|
|
|
|
#define DNS_FLAG1_RESPONSE 0x80
|
|
|
|
#define DNS_FLAG1_OPCODE_STATUS 0x10
|
|
|
|
#define DNS_FLAG1_OPCODE_INVERSE 0x08
|
|
|
|
#define DNS_FLAG1_OPCODE_STANDARD 0x00
|
|
|
|
#define DNS_FLAG1_AUTHORATIVE 0x04
|
|
|
|
#define DNS_FLAG1_TRUNC 0x02
|
|
|
|
#define DNS_FLAG1_RD 0x01
|
|
|
|
#define DNS_FLAG2_RA 0x80
|
|
|
|
#define DNS_FLAG2_ERR_MASK 0x0f
|
|
|
|
#define DNS_FLAG2_ERR_NONE 0x00
|
|
|
|
#define DNS_FLAG2_ERR_NAME 0x03
|
|
|
|
|
|
|
|
#define SEND_BUFFER_SIZE 64
|
2012-10-20 15:47:30 +02:00
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_DNSCLIENT_MAXRESPONSE
|
|
|
|
# define RECV_BUFFER_SIZE CONFIG_NETUTILS_DNSCLIENT_MAXRESPONSE
|
2012-10-20 15:47:30 +02:00
|
|
|
#else
|
|
|
|
# define RECV_BUFFER_SIZE 96
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-04-11 20:25:32 +02:00
|
|
|
# define ADDRLEN sizeof(struct sockaddr_in6)
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2014-04-11 20:25:32 +02:00
|
|
|
# define ADDRLEN sizeof(struct sockaddr_in)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* The DNS message header */
|
|
|
|
|
|
|
|
struct dns_hdr
|
|
|
|
{
|
|
|
|
uint16_t id;
|
2014-04-11 20:25:32 +02:00
|
|
|
uint8_t flags1;
|
|
|
|
uint8_t flags2;
|
2011-03-19 22:04:13 +01:00
|
|
|
uint16_t numquestions;
|
|
|
|
uint16_t numanswers;
|
|
|
|
uint16_t numauthrr;
|
|
|
|
uint16_t numextrarr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The DNS answer message structure */
|
|
|
|
|
|
|
|
struct dns_answer
|
|
|
|
{
|
|
|
|
/* DNS answer record starts with either a domain name or a pointer
|
|
|
|
* to a name already present somewhere in the packet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
uint16_t type;
|
|
|
|
uint16_t class;
|
|
|
|
uint16_t ttl[2];
|
|
|
|
uint16_t len;
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct in6_addr ipaddr;
|
|
|
|
#else
|
|
|
|
struct in_addr ipaddr;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct namemap
|
|
|
|
{
|
|
|
|
uint8_t state;
|
|
|
|
uint8_t tmr;
|
|
|
|
uint8_t retries;
|
|
|
|
uint8_t seqno;
|
|
|
|
uint8_t err;
|
|
|
|
char name[32];
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct in6_addr ipaddr;
|
|
|
|
#else
|
|
|
|
struct in_addr ipaddr;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t g_seqno;
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
static struct sockaddr_in6 g_dnsserver;
|
|
|
|
#else
|
|
|
|
static struct sockaddr_in g_dnsserver;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/****************************************************************************
|
2014-04-11 21:43:15 +02:00
|
|
|
* Name: dns_parse_name
|
2014-04-11 20:25:32 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Walk through a compact encoded DNS name and return the end of it.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-04-11 21:43:15 +02:00
|
|
|
static FAR unsigned char *dns_parse_name(FAR unsigned char *query)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
unsigned char n;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
n = *query++;
|
|
|
|
|
2014-04-12 21:09:48 +02:00
|
|
|
while (n > 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
++query;
|
|
|
|
--n;
|
|
|
|
}
|
|
|
|
}
|
2014-04-12 21:09:48 +02:00
|
|
|
while (*query != 0);
|
2014-04-11 20:25:32 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return query + 1;
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/****************************************************************************
|
2014-04-11 21:43:15 +02:00
|
|
|
* Name: dns_send_query
|
2014-04-11 20:25:32 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Runs through the list of names to see if there are any that have
|
|
|
|
* not yet been queried and, if so, sends out a query.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-04-11 21:43:15 +02:00
|
|
|
static int dns_send_query(int sockfd, FAR const char *name,
|
|
|
|
FAR struct sockaddr_in6 *addr)
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2014-04-11 21:43:15 +02:00
|
|
|
static int dns_send_query(int sockfd, FAR const char *name,
|
|
|
|
FAR struct sockaddr_in *addr)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
{
|
2014-04-11 20:25:32 +02:00
|
|
|
register FAR struct dns_hdr *hdr;
|
|
|
|
FAR char *query;
|
|
|
|
FAR char *nptr;
|
|
|
|
FAR const char *nameptr;
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t seqno = g_seqno++;
|
|
|
|
static unsigned char endquery[] = {0, 0, 1, 0, 1};
|
|
|
|
char buffer[SEND_BUFFER_SIZE];
|
|
|
|
int n;
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
hdr = (FAR struct dns_hdr*)buffer;
|
2011-03-19 22:04:13 +01:00
|
|
|
memset(hdr, 0, sizeof(struct dns_hdr));
|
|
|
|
hdr->id = htons(seqno);
|
|
|
|
hdr->flags1 = DNS_FLAG1_RD;
|
|
|
|
hdr->numquestions = HTONS(1);
|
|
|
|
query = buffer + 12;
|
|
|
|
|
|
|
|
/* Convert hostname into suitable query format. */
|
|
|
|
|
|
|
|
nameptr = name - 1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nameptr++;
|
|
|
|
nptr = query++;
|
|
|
|
for (n = 0; *nameptr != '.' && *nameptr != 0; nameptr++)
|
|
|
|
{
|
|
|
|
*query++ = *nameptr;
|
|
|
|
n++;
|
|
|
|
}
|
2014-04-11 20:25:32 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
*nptr = n;
|
|
|
|
}
|
2014-04-12 21:09:48 +02:00
|
|
|
while (*nameptr != 0);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
memcpy(query, endquery, 5);
|
2012-10-20 15:47:30 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
DEBUGASSERT(((struct sockaddr *)addr)->sa_family == AF_INET6);
|
|
|
|
#else
|
|
|
|
DEBUGASSERT(((struct sockaddr *)addr)->sa_family == AF_INET);
|
|
|
|
#endif
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
return sendto(sockfd, buffer, query + 5 - buffer,
|
|
|
|
0, (struct sockaddr*)addr, ADDRLEN);
|
2012-10-28 14:20:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/****************************************************************************
|
2014-04-11 21:43:15 +02:00
|
|
|
* Name: dns_recv_response
|
2014-04-11 20:25:32 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when new UDP data arrives
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-04-11 20:25:32 +02:00
|
|
|
# error "Not implemented"
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2014-04-11 21:43:15 +02:00
|
|
|
static int dns_recv_response(int sockfd, FAR struct sockaddr_in *addr)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
{
|
2014-04-11 20:25:32 +02:00
|
|
|
FAR unsigned char *nameptr;
|
2011-03-19 22:04:13 +01:00
|
|
|
char buffer[RECV_BUFFER_SIZE];
|
2014-04-11 20:25:32 +02:00
|
|
|
FAR struct dns_answer *ans;
|
|
|
|
FAR struct dns_hdr *hdr;
|
2013-10-05 20:05:51 +02:00
|
|
|
#if 0 /* Not used */
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t nquestions;
|
2013-10-05 20:05:51 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t nanswers;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Receive the response */
|
|
|
|
|
2012-10-28 14:20:36 +01:00
|
|
|
ret = recv(sockfd, buffer, RECV_BUFFER_SIZE, 0);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
hdr = (FAR struct dns_hdr *)buffer;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-10-20 15:47:30 +02:00
|
|
|
ndbg("ID %d\n", htons(hdr->id));
|
|
|
|
ndbg("Query %d\n", hdr->flags1 & DNS_FLAG1_RESPONSE);
|
|
|
|
ndbg("Error %d\n", hdr->flags2 & DNS_FLAG2_ERR_MASK);
|
|
|
|
ndbg("Num questions %d, answers %d, authrr %d, extrarr %d\n",
|
2011-03-19 22:04:13 +01:00
|
|
|
htons(hdr->numquestions), htons(hdr->numanswers),
|
|
|
|
htons(hdr->numauthrr), htons(hdr->numextrarr));
|
|
|
|
|
|
|
|
/* Check for error. If so, call callback to inform */
|
|
|
|
|
|
|
|
if ((hdr->flags2 & DNS_FLAG2_ERR_MASK) != 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We only care about the question(s) and the answers. The authrr
|
|
|
|
* and the extrarr are simply discarded.
|
|
|
|
*/
|
|
|
|
|
2013-10-05 20:05:51 +02:00
|
|
|
#if 0 /* Not used */
|
2011-03-19 22:04:13 +01:00
|
|
|
nquestions = htons(hdr->numquestions);
|
2013-10-05 20:05:51 +02:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
nanswers = htons(hdr->numanswers);
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/* Skip the name in the question. TODO: This should really be
|
|
|
|
* checked against the name in the question, to be sure that they
|
2011-03-19 22:04:13 +01:00
|
|
|
* match.
|
2012-10-20 15:47:30 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_NET
|
|
|
|
{
|
|
|
|
int d = 64;
|
2014-04-11 21:43:15 +02:00
|
|
|
nameptr = dns_parse_name((unsigned char *)buffer + 12) + 4;
|
2012-10-20 15:47:30 +02:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ndbg("%02X %02X %02X %02X %02X %02X %02X %02X \n",
|
|
|
|
nameptr[0],nameptr[1],nameptr[2],nameptr[3],
|
|
|
|
nameptr[4],nameptr[5],nameptr[6],nameptr[7]);
|
|
|
|
|
|
|
|
nameptr += 8;
|
|
|
|
d -= 8;
|
|
|
|
if (d < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-04-11 21:43:15 +02:00
|
|
|
nameptr = dns_parse_name((unsigned char *)buffer + 12) + 4;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
for (; nanswers > 0; nanswers--)
|
|
|
|
{
|
|
|
|
/* The first byte in the answer resource record determines if it
|
|
|
|
* is a compressed record or a normal one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*nameptr & 0xc0)
|
|
|
|
{
|
|
|
|
/* Compressed name. */
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
nameptr += 2;
|
|
|
|
ndbg("Compressed answer\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not compressed name. */
|
2014-04-11 20:25:32 +02:00
|
|
|
|
2014-04-11 21:43:15 +02:00
|
|
|
nameptr = dns_parse_name(nameptr);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ans = (struct dns_answer *)nameptr;
|
2012-10-20 15:47:30 +02:00
|
|
|
ndbg("Answer: type %x, class %x, ttl %x, length %x \n", /* 0x%08X\n", */
|
|
|
|
htons(ans->type), htons(ans->class),
|
|
|
|
(htons(ans->ttl[0]) << 16) | htons(ans->ttl[1]),
|
|
|
|
htons(ans->len) /* , ans->ipaddr.s_addr */);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Check for IP address type and Internet class. Others are discarded. */
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
if (ans->type == HTONS(1) &&
|
|
|
|
ans->class == HTONS(1) &&
|
|
|
|
ans->len == HTONS(4))
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2014-04-11 20:25:32 +02:00
|
|
|
ans->ipaddr.s_addr = *(FAR uint32_t *)(nameptr + 10);
|
|
|
|
|
2012-10-20 15:47:30 +02:00
|
|
|
ndbg("IP address %d.%d.%d.%d\n",
|
|
|
|
(ans->ipaddr.s_addr ) & 0xff,
|
|
|
|
(ans->ipaddr.s_addr >> 8 ) & 0xff,
|
|
|
|
(ans->ipaddr.s_addr >> 16 ) & 0xff,
|
|
|
|
(ans->ipaddr.s_addr >> 24 ) & 0xff);
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/* TODO: we should really check that this IP address is the one
|
2011-03-19 22:04:13 +01:00
|
|
|
* we want.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr->sin_addr.s_addr = ans->ipaddr.s_addr;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nameptr = nameptr + 10 + htons(ans->len);
|
|
|
|
}
|
|
|
|
}
|
2012-10-20 15:47:30 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-20 15:47:30 +02:00
|
|
|
/****************************************************************************
|
2014-04-11 20:25:32 +02:00
|
|
|
* Name: dns_bind_sock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the DNS resolver using the caller provided socket.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dns_bind_sock(FAR int *sockfd)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int ret;
|
|
|
|
|
2014-04-11 21:43:15 +02:00
|
|
|
/* If the socket is already open, then close it now */
|
|
|
|
|
|
|
|
if (*sockfd >= 0)
|
|
|
|
{
|
|
|
|
dns_free_sock(sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new socket */
|
2014-04-11 20:25:32 +02:00
|
|
|
|
|
|
|
*sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (*sockfd < 0)
|
|
|
|
{
|
2014-04-11 21:43:15 +02:00
|
|
|
ndbg("ERROR: socket() failed: %d\n", errno);
|
2014-04-11 20:25:32 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up a receive timeout */
|
|
|
|
|
|
|
|
tv.tv_sec = 30;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
ret = setsockopt(*sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv,
|
|
|
|
sizeof(struct timeval));
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-04-11 21:43:15 +02:00
|
|
|
ndbg("ERROR: setsockopt() failed: %d\n", errno);
|
2014-04-11 20:25:32 +02:00
|
|
|
close(*sockfd);
|
|
|
|
*sockfd = -1;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_free_sock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release the DNS resolver by closing the socket.
|
|
|
|
*
|
2012-10-20 15:47:30 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
int dns_free_sock(FAR int *sockfd)
|
|
|
|
{
|
|
|
|
if (*sockfd >= 0)
|
|
|
|
{
|
|
|
|
close(*sockfd);
|
|
|
|
*sockfd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_query_sock
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Using the DNS resolver socket (sockfd), 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_sock(int sockfd, FAR const char *hostname, FAR in_addr_t *ipaddr)
|
2012-10-20 15:47:30 +02:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_HAVE_GETHOSTBYNAME
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
FAR struct hostent *he;
|
2012-10-20 15:47:30 +02:00
|
|
|
|
|
|
|
nvdbg("Getting address of %s\n", hostname);
|
|
|
|
he = gethostbyname(hostname);
|
|
|
|
if (!he)
|
|
|
|
{
|
|
|
|
ndbg("gethostbyname failed: %d\n", h_errno);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvdbg("Using IP address %04x%04x\n",
|
|
|
|
(uint16_t)he->h_addr[1], (uint16_t)he->h_addr[0]);
|
|
|
|
|
|
|
|
memcpy(ipaddr, he->h_addr, sizeof(in_addr_t));
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
# ifdef CONFIG_NET_IPv6
|
|
|
|
struct sockaddr_in6 addr;
|
|
|
|
# else
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/* First check if the host is an IP address. */
|
|
|
|
|
2014-07-03 00:04:25 +02:00
|
|
|
if (!netlib_ipaddrconv(hostname, (uint8_t*)ipaddr))
|
2012-10-20 15:47:30 +02:00
|
|
|
{
|
|
|
|
/* 'host' does not point to a valid address string. Try to resolve
|
|
|
|
* the host name to an IP address.
|
|
|
|
*/
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
if (dns_whois_socket(sockfd, hostname, &addr) < 0)
|
2012-10-20 15:47:30 +02:00
|
|
|
{
|
|
|
|
/* Needs to set the errno here */
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the host address -- Needs fixed for IPv6 */
|
|
|
|
|
|
|
|
*ipaddr = addr.sin_addr.s_addr;
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
return OK;
|
2012-10-20 15:47:30 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_setserver
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Configure which DNS server to use for queries
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
void dns_setserver(FAR const struct in6_addr *dnsserver)
|
|
|
|
#else
|
|
|
|
void dns_setserver(FAR const struct in_addr *dnsserver)
|
|
|
|
#endif
|
2012-10-28 14:20:36 +01:00
|
|
|
{
|
2014-04-11 20:25:32 +02:00
|
|
|
g_dnsserver.sin_family = AF_INET;
|
|
|
|
g_dnsserver.sin_port = HTONS(53);
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
memcpy(&g_dnsserver.sin6_addr, dnsserver, ADDRLEN);
|
|
|
|
#else
|
|
|
|
g_dnsserver.sin_addr.s_addr = dnsserver->s_addr;
|
|
|
|
#endif
|
|
|
|
}
|
2012-10-28 14:20:36 +01:00
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_getserver
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Obtain the currently configured DNS server.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2012-10-28 14:20:36 +01:00
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
void dns_getserver(FAR struct in6_addr *dnsserver)
|
|
|
|
#else
|
|
|
|
void dns_getserver(FAR struct in_addr *dnsserver)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
memcpy(dnsserver, &g_dnsserver.sin6_addr, ADDRLEN);
|
|
|
|
#else
|
|
|
|
dnsserver->s_addr = g_dnsserver.sin_addr.s_addr;
|
|
|
|
#endif
|
2012-10-28 14:20:36 +01:00
|
|
|
}
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_whois_socket
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the binding for 'name' using the DNS server accessed via 'sockfd'
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-04-11 20:25:32 +02:00
|
|
|
int dns_whois_socket(int sockfd, FAR const char *name,
|
|
|
|
FAR struct sockaddr_in6 *addr)
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2014-04-11 20:25:32 +02:00
|
|
|
int dns_whois_socket(int sockfd, FAR const char *name,
|
|
|
|
FAR struct sockaddr_in *addr)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int retries;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Loop while receive timeout errors occur and there are remaining retries */
|
|
|
|
|
|
|
|
for (retries = 0; retries < 3; retries++)
|
|
|
|
{
|
2014-04-11 21:43:15 +02:00
|
|
|
ret = dns_send_query(sockfd, name, &g_dnsserver);
|
|
|
|
if (ret < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-11 21:43:15 +02:00
|
|
|
ret = dns_recv_response(sockfd, addr);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Response received successfully */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
/* Some failure other than receive timeout occurred */
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|