2015-07-10 20:11:40 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/netdb/lib_dnsquery.c
|
2015-07-10 20:11:40 +02:00
|
|
|
* DNS host name to IP address resolver.
|
|
|
|
*
|
2016-05-30 19:52:07 +02:00
|
|
|
* The DNS resolver functions are used to lookup a hostname and map it to a
|
|
|
|
* numerical IP address.
|
2015-07-10 20:11:40 +02:00
|
|
|
*
|
2018-02-26 18:48:57 +01:00
|
|
|
* Copyright (C) 2007, 2009, 2012, 2014-2018 Gregory Nutt. All rights
|
2017-10-11 17:25:43 +02:00
|
|
|
* reserved.
|
2015-07-10 20:11:40 +02:00
|
|
|
* 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:
|
2017-10-11 17:25:43 +02:00
|
|
|
*
|
2015-07-10 20:11:40 +02:00
|
|
|
* 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>
|
|
|
|
|
2016-01-14 17:17:15 +01:00
|
|
|
#include <string.h>
|
2018-11-16 13:56:45 +01:00
|
|
|
#include <time.h>
|
2015-07-10 20:11:40 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2018-11-16 13:56:45 +01:00
|
|
|
#include <assert.h>
|
2015-07-10 20:11:40 +02:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2016-01-14 17:17:15 +01:00
|
|
|
|
2017-10-11 17:25:43 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2015-07-12 17:19:40 +02:00
|
|
|
#include <nuttx/net/dns.h>
|
|
|
|
|
|
|
|
#include "netdb/lib_dns.h"
|
2015-07-10 20:11:40 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-06-23 14:30:33 +02:00
|
|
|
/* Buffer sizes
|
|
|
|
*
|
2018-11-16 13:56:45 +01:00
|
|
|
* The SEND_BUFFER_SIZE depends the configured DNS name size:
|
|
|
|
*
|
2017-06-23 14:30:33 +02:00
|
|
|
* sizeof(DNS query0 = Header (12 bytes) + DNS Name (Variable) +
|
|
|
|
* Query type (2 bytes) + Query Class (2 bytes)
|
2018-11-16 13:56:45 +01:00
|
|
|
*
|
|
|
|
* sizeof(DNS Name) = Encoded length (1 byte) + Maximum length of name +
|
|
|
|
* NUL-terminator (1 byte)
|
2017-06-23 14:30:33 +02:00
|
|
|
*/
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
#define SEND_BUFFER_SIZE (16 + CONFIG_NETDB_DNSCLIENT_NAMESIZE + 2)
|
2015-07-12 17:19:40 +02:00
|
|
|
#define RECV_BUFFER_SIZE CONFIG_NETDB_DNSCLIENT_MAXRESPONSE
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2015-07-12 21:45:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2015-07-13 18:41:32 +02:00
|
|
|
|
2016-01-14 21:18:19 +01:00
|
|
|
struct dns_query_s
|
|
|
|
{
|
|
|
|
int sd; /* DNS server socket */
|
|
|
|
int result; /* Explanation of the failure */
|
|
|
|
FAR const char *hostname; /* Hostname to lookup */
|
2018-11-23 14:09:47 +01:00
|
|
|
FAR union dns_addr_u *addr; /* Location to return host address */
|
|
|
|
FAR int *naddr; /* Number of returned addresses */
|
2016-01-14 21:18:19 +01:00
|
|
|
};
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Query info to check response against. */
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
struct dns_query_info_s
|
|
|
|
{
|
2020-03-30 14:49:28 +02:00
|
|
|
uint16_t id; /* Query ID */
|
|
|
|
uint16_t rectype; /* Queried record type */
|
|
|
|
uint16_t qnamelen; /* Queried hostname length */
|
|
|
|
char qname[CONFIG_NETDB_DNSCLIENT_NAMESIZE + 2]; /* Queried hostname in
|
|
|
|
* encoded format + NUL */
|
2018-11-16 13:56:45 +01:00
|
|
|
};
|
2015-07-13 16:19:31 +02:00
|
|
|
|
2015-07-10 20:11:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_parse_name
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Walk through a compact encoded DNS name and return the end of it.
|
|
|
|
*
|
2018-02-26 18:43:37 +01:00
|
|
|
* Input Parameters:
|
|
|
|
*
|
2018-02-26 18:48:57 +01:00
|
|
|
* query - A pointer to the starting byte of the name entry in the DNS
|
|
|
|
* response.
|
2020-03-30 14:49:28 +02:00
|
|
|
* queryend - A pointer to the byte after the last byte of the response.
|
2018-02-26 18:43:37 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Pointer to the first byte after the parsed name, or the value of
|
|
|
|
* `queryend` if the name did not fit into provided DNS response.
|
|
|
|
*
|
2015-07-10 20:11:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-02-26 18:43:37 +01:00
|
|
|
static FAR uint8_t *dns_parse_name(FAR uint8_t *query, FAR uint8_t *queryend)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2015-07-13 00:23:08 +02:00
|
|
|
uint8_t n;
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-02-26 18:43:37 +01:00
|
|
|
while (query < queryend)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
|
|
|
n = *query++;
|
|
|
|
|
2020-03-30 14:49:28 +02:00
|
|
|
/* Check for a leading or trailing pointer */
|
2018-02-26 18:43:37 +01:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
if ((n & 0xc0) != 0)
|
2018-02-26 18:43:37 +01:00
|
|
|
{
|
|
|
|
/* Eat second pointer byte and terminate search */
|
|
|
|
|
|
|
|
ninfo("Compressed answer\n");
|
|
|
|
query++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for final label with zero-length */
|
|
|
|
|
|
|
|
if (!n)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2018-02-26 18:43:37 +01:00
|
|
|
break;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
2018-02-26 18:43:37 +01:00
|
|
|
|
|
|
|
/* Eat non-empty label */
|
|
|
|
|
|
|
|
query += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query >= queryend)
|
|
|
|
{
|
|
|
|
/* Always return `queryend` in case of errors */
|
|
|
|
|
|
|
|
nerr("ERROR: DNS response is too short\n");
|
|
|
|
query = queryend;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 18:43:37 +01:00
|
|
|
return query;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_alloc_id
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Gets a new ID for query. Function manufactures a reasonably unique ID,
|
|
|
|
* but we accept rare collisions, since they are harmless when resolver
|
|
|
|
* clients retry failed DNS few times.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t dns_alloc_id(void)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
return (uint32_t)ts.tv_nsec + ((uint32_t)ts.tv_nsec >> 16);
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:11:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_send_query
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
static int dns_send_query(int sd, FAR const char *name,
|
2018-11-23 14:09:47 +01:00
|
|
|
FAR union dns_addr_u *uaddr, uint16_t rectype,
|
2018-11-16 13:56:45 +01:00
|
|
|
FAR struct dns_query_info_s *qinfo)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2018-11-14 13:30:49 +01:00
|
|
|
FAR struct dns_header_s *hdr;
|
2015-07-13 00:23:08 +02:00
|
|
|
FAR uint8_t *dest;
|
|
|
|
FAR uint8_t *nptr;
|
2018-11-16 13:56:45 +01:00
|
|
|
FAR char *qname;
|
|
|
|
FAR char *qptr;
|
2015-07-13 00:23:08 +02:00
|
|
|
FAR const char *src;
|
|
|
|
uint8_t buffer[SEND_BUFFER_SIZE];
|
2018-11-16 13:56:45 +01:00
|
|
|
uint16_t id;
|
2015-07-12 19:56:53 +02:00
|
|
|
socklen_t addrlen;
|
2015-07-12 17:19:40 +02:00
|
|
|
int ret;
|
2018-11-16 13:56:45 +01:00
|
|
|
int len;
|
2015-07-10 20:11:40 +02:00
|
|
|
int n;
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Get a new ID for query */
|
2015-07-13 18:41:32 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
id = dns_alloc_id();
|
2015-07-13 18:41:32 +02:00
|
|
|
|
|
|
|
/* Initialize the request header */
|
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
hdr = (FAR struct dns_header_s *)buffer;
|
2018-11-14 13:30:49 +01:00
|
|
|
memset(hdr, 0, sizeof(*hdr));
|
2018-11-16 13:56:45 +01:00
|
|
|
hdr->id = htons(id);
|
2015-07-10 20:11:40 +02:00
|
|
|
hdr->flags1 = DNS_FLAG1_RD;
|
|
|
|
hdr->numquestions = HTONS(1);
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Convert hostname into suitable query format.
|
|
|
|
*
|
|
|
|
* There is space for CONFIG_NETDB_DNSCLIENT_NAMESIZE
|
|
|
|
* plus one pre-pended name length and NUL-terminator
|
|
|
|
* (other pre-pended name lengths replace dots).
|
|
|
|
*/
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
src = name - 1;
|
|
|
|
dest = buffer + sizeof(*hdr);
|
|
|
|
qname = qinfo->qname;
|
|
|
|
len = 0;
|
2015-07-10 20:11:40 +02:00
|
|
|
do
|
2020-03-30 14:49:28 +02:00
|
|
|
{
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Copy the name string to both query and saved info. */
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2015-07-13 00:23:08 +02:00
|
|
|
src++;
|
|
|
|
nptr = dest++;
|
2018-11-16 13:56:45 +01:00
|
|
|
qptr = qname++;
|
|
|
|
len++;
|
|
|
|
|
|
|
|
for (n = 0;
|
2020-03-29 07:45:12 +02:00
|
|
|
*src != '.' && *src != '\0' &&
|
2018-11-16 13:56:45 +01:00
|
|
|
len <= CONFIG_NETDB_DNSCLIENT_NAMESIZE;
|
|
|
|
src++)
|
2015-07-13 00:23:08 +02:00
|
|
|
{
|
2018-11-16 13:56:45 +01:00
|
|
|
*dest++ = *(FAR uint8_t *)src;
|
|
|
|
*qname++ = *(FAR uint8_t *)src;
|
2015-07-13 00:23:08 +02:00
|
|
|
n++;
|
2018-11-16 13:56:45 +01:00
|
|
|
len++;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pre-pend the name length */
|
|
|
|
|
|
|
|
*nptr = n;
|
2018-11-16 13:56:45 +01:00
|
|
|
*qptr = n;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
2018-11-16 13:56:45 +01:00
|
|
|
while (*src != '\0' && len <= CONFIG_NETDB_DNSCLIENT_NAMESIZE);
|
|
|
|
|
|
|
|
/* Add NUL termination */
|
|
|
|
|
|
|
|
*dest++ = '\0';
|
|
|
|
*qname++ = '\0';
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Store name length to saved info */
|
|
|
|
|
|
|
|
DEBUGASSERT(len <= CONFIG_NETDB_DNSCLIENT_NAMESIZE + 1);
|
|
|
|
DEBUGASSERT(qname - qinfo->qname == len + 1);
|
|
|
|
qinfo->qnamelen = len;
|
|
|
|
|
|
|
|
/* Add DNS record type, and DNS class */
|
2015-07-13 00:23:08 +02:00
|
|
|
|
|
|
|
*dest++ = (rectype >> 8); /* DNS record type (big endian) */
|
|
|
|
*dest++ = (rectype & 0xff);
|
|
|
|
*dest++ = (DNS_CLASS_IN >> 8); /* DNS record class (big endian) */
|
|
|
|
*dest++ = (DNS_CLASS_IN & 0xff);
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
qinfo->rectype = htons(rectype);
|
|
|
|
qinfo->id = hdr->id;
|
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
/* Send the request */
|
|
|
|
|
2020-03-29 07:45:12 +02:00
|
|
|
if (uaddr->addr.sa_family == AF_INET)
|
2015-07-12 19:56:53 +02:00
|
|
|
{
|
2020-03-29 07:45:12 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2015-07-12 19:56:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-29 07:45:12 +02:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2015-07-12 19:56:53 +02:00
|
|
|
}
|
2015-07-12 17:19:40 +02:00
|
|
|
|
2020-03-29 07:45:12 +02:00
|
|
|
ret = connect(sd, &uaddr->addr, addrlen);
|
2015-07-12 17:19:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-03-28 18:08:10 +01:00
|
|
|
ret = -errno;
|
2020-03-29 07:45:12 +02:00
|
|
|
nerr("ERROR: connect failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-02-15 17:12:01 +01:00
|
|
|
ret = send(sd, buffer, dest - buffer, 0);
|
2020-03-29 07:45:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-02-15 17:12:01 +01:00
|
|
|
ret = -errno;
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: sendto failed: %d\n", ret);
|
|
|
|
return ret;
|
2015-07-12 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_recv_response
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when new UDP data arrives
|
|
|
|
*
|
2018-11-23 14:09:47 +01:00
|
|
|
* Returned Value:
|
|
|
|
* Returns number of valid IP address responses. Negated errno value is
|
|
|
|
* returned in all other cases.
|
|
|
|
*
|
2015-07-10 20:11:40 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
static int dns_recv_response(int sd, FAR union dns_addr_u *addr, int naddr,
|
2018-11-16 13:56:45 +01:00
|
|
|
FAR struct dns_query_info_s *qinfo)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2015-07-13 00:23:08 +02:00
|
|
|
FAR uint8_t *nameptr;
|
2018-11-16 13:56:45 +01:00
|
|
|
FAR uint8_t *namestart;
|
2018-02-26 18:43:37 +01:00
|
|
|
FAR uint8_t *endofbuffer;
|
2015-07-10 20:11:40 +02:00
|
|
|
char buffer[RECV_BUFFER_SIZE];
|
2015-07-12 17:19:40 +02:00
|
|
|
FAR struct dns_answer_s *ans;
|
|
|
|
FAR struct dns_header_s *hdr;
|
2018-11-16 13:56:45 +01:00
|
|
|
FAR struct dns_question_s *que;
|
|
|
|
uint16_t nquestions;
|
|
|
|
uint16_t nanswers;
|
2018-11-23 14:09:47 +01:00
|
|
|
int naddr_read;
|
2015-07-10 20:11:40 +02:00
|
|
|
int ret;
|
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
if (naddr <= 0)
|
|
|
|
{
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:11:40 +02:00
|
|
|
/* Receive the response */
|
|
|
|
|
2021-02-15 17:12:01 +01:00
|
|
|
ret = recv(sd, buffer, RECV_BUFFER_SIZE, 0);
|
2015-07-10 20:11:40 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-02-15 17:12:01 +01:00
|
|
|
ret = -errno;
|
2020-03-28 18:08:10 +01:00
|
|
|
nerr("ERROR: recv failed: %d\n", ret);
|
|
|
|
return ret;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:30:49 +01:00
|
|
|
if (ret < sizeof(*hdr))
|
2018-02-26 18:43:37 +01:00
|
|
|
{
|
|
|
|
/* DNS header can't fit in received data */
|
|
|
|
|
|
|
|
nerr("ERROR: DNS response is too short\n");
|
|
|
|
return -EILSEQ;
|
|
|
|
}
|
|
|
|
|
2018-02-26 18:48:57 +01:00
|
|
|
hdr = (FAR struct dns_header_s *)buffer;
|
2020-03-30 14:49:28 +02:00
|
|
|
endofbuffer = (FAR uint8_t *)buffer + ret;
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2020-03-29 07:32:03 +02:00
|
|
|
ninfo("ID %d\n", ntohs(hdr->id));
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("Query %d\n", hdr->flags1 & DNS_FLAG1_RESPONSE);
|
|
|
|
ninfo("Error %d\n", hdr->flags2 & DNS_FLAG2_ERR_MASK);
|
|
|
|
ninfo("Num questions %d, answers %d, authrr %d, extrarr %d\n",
|
2020-03-29 07:32:03 +02:00
|
|
|
ntohs(hdr->numquestions), ntohs(hdr->numanswers),
|
|
|
|
ntohs(hdr->numauthrr), ntohs(hdr->numextrarr));
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2015-07-12 17:19:40 +02:00
|
|
|
/* Check for error */
|
2015-07-10 20:11:40 +02:00
|
|
|
|
|
|
|
if ((hdr->flags2 & DNS_FLAG2_ERR_MASK) != 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: DNS reported error: flags2=%02x\n", hdr->flags2);
|
2015-07-12 17:19:40 +02:00
|
|
|
return -EPROTO;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Check for matching ID. */
|
|
|
|
|
|
|
|
if (hdr->id != qinfo->id)
|
|
|
|
{
|
|
|
|
nerr("ERROR: DNS wrong response ID (expected %d, got %d)\n",
|
2020-03-29 07:32:03 +02:00
|
|
|
ntohs(qinfo->id), ntohs(hdr->id));
|
2018-11-16 13:56:45 +01:00
|
|
|
return -EBADMSG;
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:11:40 +02:00
|
|
|
/* We only care about the question(s) and the answers. The authrr
|
|
|
|
* and the extrarr are simply discarded.
|
|
|
|
*/
|
|
|
|
|
2020-03-29 07:32:03 +02:00
|
|
|
nquestions = ntohs(hdr->numquestions);
|
|
|
|
nanswers = ntohs(hdr->numanswers);
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* We only ever send queries with one question. */
|
|
|
|
|
|
|
|
if (nquestions != 1)
|
|
|
|
{
|
|
|
|
nerr("ERROR: DNS wrong number of questions %d\n", nquestions);
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the name in the answer, but do check that it
|
|
|
|
* matches against the name in the question.
|
2015-07-10 20:11:40 +02:00
|
|
|
*/
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
namestart = (uint8_t *)buffer + sizeof(*hdr);
|
|
|
|
nameptr = dns_parse_name(namestart, endofbuffer);
|
|
|
|
if (nameptr == endofbuffer)
|
|
|
|
{
|
|
|
|
return -EILSEQ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since dns_parse_name() skips any pointer bytes,
|
|
|
|
* we cannot compare for equality here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nameptr - namestart < qinfo->qnamelen)
|
2018-02-26 18:43:37 +01:00
|
|
|
{
|
2018-11-16 13:56:45 +01:00
|
|
|
nerr("ERROR: DNS response name wrong length\n");
|
|
|
|
return -EBADMSG;
|
2018-02-26 18:43:37 +01:00
|
|
|
}
|
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* qname is NUL-terminated and we must include NUL to the comparison. */
|
|
|
|
|
|
|
|
if (memcmp(namestart, qinfo->qname, qinfo->qnamelen + 1) != 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: DNS response with wrong name\n");
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate query type and class */
|
|
|
|
|
|
|
|
que = (FAR struct dns_question_s *)nameptr;
|
|
|
|
ninfo("Question: type=%04x, class=%04x\n",
|
2020-03-29 07:32:03 +02:00
|
|
|
ntohs(que->type), ntohs(que->class));
|
2018-11-16 13:56:45 +01:00
|
|
|
|
|
|
|
if (que->type != qinfo->rectype ||
|
|
|
|
que->class != HTONS(DNS_CLASS_IN))
|
|
|
|
{
|
|
|
|
nerr("ERROR: DNS response with wrong question\n");
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip over question */
|
|
|
|
|
|
|
|
nameptr += sizeof(struct dns_question_s);
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
ret = OK;
|
|
|
|
naddr_read = 0;
|
|
|
|
|
2015-07-10 20:11:40 +02:00
|
|
|
for (; nanswers > 0; nanswers--)
|
|
|
|
{
|
2018-02-26 18:43:37 +01:00
|
|
|
/* Each answer starts with a name */
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-02-26 18:43:37 +01:00
|
|
|
nameptr = dns_parse_name(nameptr, endofbuffer);
|
|
|
|
if (nameptr == endofbuffer)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2018-11-23 14:09:47 +01:00
|
|
|
ret = -EILSEQ;
|
2020-03-29 08:34:14 +02:00
|
|
|
nwarn("Further parse returned %d\n", ret);
|
2018-11-23 14:09:47 +01:00
|
|
|
break;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 00:23:08 +02:00
|
|
|
ans = (FAR struct dns_answer_s *)nameptr;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("Answer: type=%04x, class=%04x, ttl=%06x, length=%04x \n",
|
2020-03-29 07:32:03 +02:00
|
|
|
ntohs(ans->type), ntohs(ans->class),
|
|
|
|
(ntohs(ans->ttl[0]) << 16) | ntohs(ans->ttl[1]),
|
|
|
|
ntohs(ans->len));
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Check for IPv4/6 address type and Internet class. Others are
|
|
|
|
* discarded.
|
|
|
|
*/
|
2015-07-12 21:45:52 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-07-13 00:23:08 +02:00
|
|
|
if (ans->type == HTONS(DNS_RECTYPE_A) &&
|
|
|
|
ans->class == HTONS(DNS_CLASS_IN) &&
|
2018-02-26 18:43:37 +01:00
|
|
|
ans->len == HTONS(4) &&
|
2018-03-27 03:31:06 +02:00
|
|
|
nameptr + 10 + 4 <= endofbuffer)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2020-03-29 08:34:14 +02:00
|
|
|
FAR struct sockaddr_in *inaddr;
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
nameptr += 10 + 4;
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("IPv4 address: %d.%d.%d.%d\n",
|
2020-11-23 12:19:10 +01:00
|
|
|
(int)((ans->u.ipv4.s_addr) & 0xff),
|
|
|
|
(int)((ans->u.ipv4.s_addr >> 8) & 0xff),
|
|
|
|
(int)((ans->u.ipv4.s_addr >> 16) & 0xff),
|
|
|
|
(int)((ans->u.ipv4.s_addr >> 24) & 0xff));
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
inaddr = &addr[naddr_read].ipv4;
|
|
|
|
inaddr->sin_family = AF_INET;
|
|
|
|
inaddr->sin_port = 0;
|
|
|
|
inaddr->sin_addr.s_addr = ans->u.ipv4.s_addr;
|
|
|
|
|
|
|
|
if (++naddr_read >= naddr)
|
2015-07-12 21:45:52 +02:00
|
|
|
{
|
2018-11-23 14:09:47 +01:00
|
|
|
ret = -ERANGE;
|
|
|
|
break;
|
2015-07-12 21:45:52 +02:00
|
|
|
}
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
else
|
2015-07-13 00:23:08 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (ans->type == HTONS(DNS_RECTYPE_AAAA) &&
|
|
|
|
ans->class == HTONS(DNS_CLASS_IN) &&
|
2018-02-26 18:43:37 +01:00
|
|
|
ans->len == HTONS(16) &&
|
2018-03-27 03:31:06 +02:00
|
|
|
nameptr + 10 + 16 <= endofbuffer)
|
2015-07-13 00:23:08 +02:00
|
|
|
{
|
2020-03-29 08:34:14 +02:00
|
|
|
FAR struct sockaddr_in6 *inaddr;
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
nameptr += 10 + 16;
|
2015-07-13 00:23:08 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("IPv6 address: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
|
2020-03-29 07:32:03 +02:00
|
|
|
ntohs(ans->u.ipv6.s6_addr[7]), ntohs(ans->u.ipv6.s6_addr[6]),
|
|
|
|
ntohs(ans->u.ipv6.s6_addr[5]), ntohs(ans->u.ipv6.s6_addr[4]),
|
|
|
|
ntohs(ans->u.ipv6.s6_addr[3]), ntohs(ans->u.ipv6.s6_addr[2]),
|
2020-03-30 14:49:28 +02:00
|
|
|
ntohs(ans->u.ipv6.s6_addr[1]), ntohs(*ans->u.ipv6.s6_addr));
|
2015-07-13 00:23:08 +02:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
inaddr = &addr[naddr_read].ipv6;
|
|
|
|
inaddr->sin6_family = AF_INET6;
|
|
|
|
inaddr->sin6_port = 0;
|
|
|
|
memcpy(inaddr->sin6_addr.s6_addr, ans->u.ipv6.s6_addr, 16);
|
|
|
|
|
|
|
|
if (++naddr_read >= naddr)
|
2015-07-13 00:23:08 +02:00
|
|
|
{
|
2018-11-23 14:09:47 +01:00
|
|
|
ret = -ERANGE;
|
|
|
|
break;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-07-12 21:45:52 +02:00
|
|
|
#endif
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2020-03-29 07:32:03 +02:00
|
|
|
nameptr = nameptr + 10 + ntohs(ans->len);
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
if (naddr_read == 0 && ret == OK)
|
|
|
|
{
|
|
|
|
ret = -EADDRNOTAVAIL;
|
|
|
|
}
|
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
return naddr_read > 0 ? naddr_read : ret;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-01-14 21:18:19 +01:00
|
|
|
* Name: dns_query_callback
|
2015-07-10 20:11:40 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-05-11 21:35:56 +02:00
|
|
|
* Using the DNS information and this DNS server address, look up the
|
2016-01-14 21:18:19 +01:00
|
|
|
* hostname.
|
2015-07-10 20:11:40 +02:00
|
|
|
*
|
2015-07-13 18:41:32 +02:00
|
|
|
* Input Parameters:
|
2020-02-22 19:31:14 +01:00
|
|
|
* arg - Query arguments
|
2016-01-14 21:18:19 +01:00
|
|
|
* addr - DNS name server address
|
|
|
|
* addrlen - Length of the DNS name server address.
|
2015-07-13 18:41:32 +02:00
|
|
|
*
|
2015-07-10 20:11:40 +02:00
|
|
|
* Returned Value:
|
2016-01-14 21:18:19 +01:00
|
|
|
* Returns one (1) if the query was successful. Zero is returned in all
|
|
|
|
* other cases. The result field of the query structure is set to a
|
|
|
|
* negated errno value indicate the reason for the last failure (only).
|
2015-07-10 20:11:40 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-14 21:18:19 +01:00
|
|
|
static int dns_query_callback(FAR void *arg, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t addrlen)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2016-01-14 21:18:19 +01:00
|
|
|
FAR struct dns_query_s *query = (FAR struct dns_query_s *)arg;
|
2018-11-16 13:56:45 +01:00
|
|
|
FAR struct dns_query_info_s qinfo;
|
2020-03-29 08:34:14 +02:00
|
|
|
int next = 0;
|
2015-07-12 17:19:40 +02:00
|
|
|
int retries;
|
|
|
|
int ret;
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-16 13:56:45 +01:00
|
|
|
/* Loop while receive timeout errors occur and there are remaining
|
|
|
|
* retries.
|
2020-03-30 14:49:28 +02:00
|
|
|
*/
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2018-11-14 13:30:49 +01:00
|
|
|
for (retries = 0; retries < CONFIG_NETDB_DNSCLIENT_RETRIES; retries++)
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
/* Send the IPv6 query */
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
ret = dns_send_query(query->sd, query->hostname,
|
2020-04-01 11:20:13 +02:00
|
|
|
(FAR union dns_addr_u *)addr,
|
|
|
|
DNS_RECTYPE_AAAA, &qinfo);
|
2020-03-29 08:34:14 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
nerr("ERROR: IPv6 dns_send_query failed: %d\n", ret);
|
2020-03-29 08:34:14 +02:00
|
|
|
query->result = ret;
|
|
|
|
}
|
|
|
|
else
|
2015-07-12 17:19:40 +02:00
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
/* Obtain the IPv6 response */
|
2015-07-13 00:23:08 +02:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
ret = dns_recv_response(query->sd, &query->addr[next],
|
|
|
|
*query->naddr - next, &qinfo);
|
2015-07-13 00:23:08 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-03-29 08:34:14 +02:00
|
|
|
next += ret;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
2020-03-29 08:34:14 +02:00
|
|
|
else
|
2015-07-13 00:23:08 +02:00
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
nerr("ERROR: IPv6 dns_recv_response failed: %d\n", ret);
|
2016-01-14 21:18:19 +01:00
|
|
|
query->result = ret;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
2015-07-12 17:19:40 +02:00
|
|
|
}
|
2020-04-01 11:20:13 +02:00
|
|
|
#endif
|
2015-07-12 17:19:40 +02:00
|
|
|
|
2020-04-01 11:20:13 +02:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
/* Send the IPv4 query */
|
2015-07-12 17:19:40 +02:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
ret = dns_send_query(query->sd, query->hostname,
|
2020-04-01 11:20:13 +02:00
|
|
|
(FAR union dns_addr_u *)addr,
|
|
|
|
DNS_RECTYPE_A, &qinfo);
|
2020-03-29 08:34:14 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
nerr("ERROR: IPv4 dns_send_query failed: %d\n", ret);
|
2020-03-29 08:34:14 +02:00
|
|
|
query->result = ret;
|
|
|
|
}
|
|
|
|
else
|
2015-07-10 20:11:40 +02:00
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
/* Obtain the IPv4 response */
|
2015-07-10 20:11:40 +02:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
ret = dns_recv_response(query->sd, &query->addr[next],
|
|
|
|
*query->naddr - next, &qinfo);
|
2015-07-13 00:23:08 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2020-03-29 08:34:14 +02:00
|
|
|
next += ret;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
2020-03-29 08:34:14 +02:00
|
|
|
else
|
2015-07-13 00:23:08 +02:00
|
|
|
{
|
2020-04-01 11:20:13 +02:00
|
|
|
nerr("ERROR: IPv4 dns_recv_response failed: %d\n", ret);
|
2016-01-14 21:18:19 +01:00
|
|
|
query->result = ret;
|
2015-07-13 00:23:08 +02:00
|
|
|
}
|
2015-07-12 17:19:40 +02:00
|
|
|
}
|
2020-04-01 11:20:13 +02:00
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2020-03-29 08:34:14 +02:00
|
|
|
|
|
|
|
if (next > 0)
|
2016-01-14 21:18:19 +01:00
|
|
|
{
|
2020-03-29 08:34:14 +02:00
|
|
|
#if CONFIG_NETDB_DNSCLIENT_ENTRIES > 0
|
|
|
|
/* Save the answer in the DNS cache */
|
|
|
|
|
|
|
|
dns_save_answer(query->hostname, query->addr, next);
|
|
|
|
#endif
|
|
|
|
/* Return 1 to indicate to (1) stop the traversal, and (2)
|
|
|
|
* indicate that the address was found.
|
2020-03-30 14:49:28 +02:00
|
|
|
*/
|
2016-01-14 21:18:19 +01:00
|
|
|
|
2020-03-29 08:34:14 +02:00
|
|
|
*query->naddr = next;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (query->result != -EAGAIN)
|
|
|
|
{
|
|
|
|
break;
|
2016-01-14 21:18:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dns_query
|
|
|
|
*
|
|
|
|
* Description:
|
2017-05-11 21:35:56 +02:00
|
|
|
* Using the DNS resolver socket (sd), look up the 'hostname', and
|
2016-01-14 21:18:19 +01:00
|
|
|
* return its IP address in 'ipaddr'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sd - The socket descriptor previously initialized by dsn_bind().
|
|
|
|
* hostname - The hostname string to be resolved.
|
2018-11-23 14:09:47 +01:00
|
|
|
* addr - The location to return the IP addresses associated with the
|
|
|
|
* hostname.
|
|
|
|
* naddr - On entry, the count of addresses backing up the 'addr'
|
|
|
|
* pointer. On return, this location will hold the actual count of
|
|
|
|
* the returned addresses.
|
2016-01-14 21:18:19 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Returns zero (OK) if the query was successful.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-23 14:09:47 +01:00
|
|
|
int dns_query(int sd, FAR const char *hostname, FAR union dns_addr_u *addr,
|
|
|
|
FAR int *naddr)
|
2016-01-14 21:18:19 +01:00
|
|
|
{
|
|
|
|
FAR struct dns_query_s query;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the query info structure */
|
|
|
|
|
|
|
|
query.sd = sd;
|
2016-01-14 22:12:42 +01:00
|
|
|
query.result = -EADDRNOTAVAIL;
|
2016-01-14 21:18:19 +01:00
|
|
|
query.hostname = hostname;
|
|
|
|
query.addr = addr;
|
2018-11-23 14:09:47 +01:00
|
|
|
query.naddr = naddr;
|
2016-01-14 21:18:19 +01:00
|
|
|
|
|
|
|
/* Perform the query. dns_foreach_nameserver() will return:
|
|
|
|
*
|
|
|
|
* 1 - The query was successful.
|
|
|
|
* 0 - Look up failed
|
|
|
|
* <0 - Some other failure (?, shouldn't happen)
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = dns_foreach_nameserver(dns_query_callback, &query);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
/* The lookup was successful */
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
ret = query.result;
|
2015-07-12 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2016-01-14 21:18:19 +01:00
|
|
|
return ret;
|
2015-07-10 20:11:40 +02:00
|
|
|
}
|