Merge pull request #999 from adamdunkels/lebrush-rdnss-support-rebased

RDNSS support, again
This commit is contained in:
Nicolas Tsiftes 2015-03-30 17:21:23 +02:00
commit 8f50d9709d
16 changed files with 472 additions and 63 deletions

View file

@ -65,6 +65,7 @@
#include "net/ip/tcpip.h"
#include "net/ip/resolv.h"
#include "net/ip/uip-udp-packet.h"
#include "net/ip/uip-nameserver.h"
#include "lib/random.h"
#ifndef DEBUG
@ -227,17 +228,6 @@ struct dns_hdr {
uint16_t numextrarr;
};
/** These default values for the DNS server are Google's public DNS:
* <https://developers.google.com/speed/public-dns/docs/using>
*/
static uip_ipaddr_t resolv_default_dns_server =
#if NETSTACK_CONF_WITH_IPV6
{ { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 } };
#else /* NETSTACK_CONF_WITH_IPV6 */
{ { 8, 8, 8, 8 } };
#endif /* NETSTACK_CONF_WITH_IPV6 */
/** \internal The DNS answer message structure. */
struct dns_answer {
/* DNS answer record starts with either a domain name or a pointer
@ -269,6 +259,7 @@ struct namemap {
#endif /* RESOLV_SUPPORTS_RECORD_EXPIRATION */
uip_ipaddr_t ipaddr;
uint8_t err;
uint8_t server;
#if RESOLV_CONF_SUPPORTS_MDNS
int is_mdns:1, is_probe:1;
#endif
@ -637,6 +628,21 @@ mdns_prep_host_announce_packet(void)
}
#endif /* RESOLV_CONF_SUPPORTS_MDNS */
/*---------------------------------------------------------------------------*/
static char
try_next_server(struct namemap *namemapptr)
{
#if VERBOSE_DEBUG
printf("server %d\n", namemapptr->server);
#endif
namemapptr->server++;
if(uip_nameserver_get(namemapptr->server) != NULL) {
namemapptr->retries = 0;
return 1;
}
namemapptr->server = 0;
return 0;
}
/*---------------------------------------------------------------------------*/
/** \internal
* 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.
@ -666,16 +672,20 @@ check_entries(void)
if(++namemapptr->retries == RESOLV_CONF_MAX_RETRIES)
#endif /* RESOLV_CONF_SUPPORTS_MDNS */
{
/* STATE_ERROR basically means "not found". */
namemapptr->state = STATE_ERROR;
/* Try the next server (if possible) before failing. Otherwise
simply mark the entry as failed. */
if(try_next_server(namemapptr) == 0) {
/* STATE_ERROR basically means "not found". */
namemapptr->state = STATE_ERROR;
#if RESOLV_SUPPORTS_RECORD_EXPIRATION
/* Keep the "not found" error valid for 30 seconds */
namemapptr->expiration = clock_seconds() + 30;
/* Keep the "not found" error valid for 30 seconds */
namemapptr->expiration = clock_seconds() + 30;
#endif /* RESOLV_SUPPORTS_RECORD_EXPIRATION */
resolv_found(namemapptr->name, NULL);
continue;
resolv_found(namemapptr->name, NULL);
continue;
}
}
namemapptr->tmr = namemapptr->retries * namemapptr->retries * 3;
@ -747,7 +757,9 @@ check_entries(void)
} else {
uip_udp_packet_sendto(resolv_conn, uip_appdata,
(query - (uint8_t *) uip_appdata),
&resolv_default_dns_server, UIP_HTONS(DNS_PORT));
(const uip_ipaddr_t *)
uip_nameserver_get(namemapptr->server),
UIP_HTONS(DNS_PORT));
PRINTF("resolver: (i=%d) Sent DNS request for \"%s\".\n", i,
namemapptr->name);
@ -755,7 +767,8 @@ check_entries(void)
#else /* RESOLV_CONF_SUPPORTS_MDNS */
uip_udp_packet_sendto(resolv_conn, uip_appdata,
(query - (uint8_t *) uip_appdata),
&resolv_default_dns_server, UIP_HTONS(DNS_PORT));
uip_nameserver_get(namemapptr->server),
UIP_HTONS(DNS_PORT));
PRINTF("resolver: (i=%d) Sent DNS request for \"%s\".\n", i,
namemapptr->name);
#endif /* RESOLV_CONF_SUPPORTS_MDNS */
@ -1041,11 +1054,28 @@ newdata(void)
uip_ipaddr_copy(&namemapptr->ipaddr, (uip_ipaddr_t *) ans->ipaddr);
resolv_found(namemapptr->name, &namemapptr->ipaddr);
break;
skip_to_next_answer:
queryptr = (unsigned char *)skip_name(queryptr) + 10 + uip_htons(ans->len);
--nanswers;
}
/* Got to this point there's no answer, try next nameserver if available
since this one doesn't know the answer */
#if RESOLV_CONF_SUPPORTS_MDNS
if(nanswers == 0 && UIP_UDP_BUF->srcport != UIP_HTONS(MDNS_PORT)
&& hdr->id != 0)
#else
if(nanswers == 0)
#endif
{
if(try_next_server(namemapptr)) {
namemapptr->state = STATE_ASKING;
process_post(&resolv_process, PROCESS_EVENT_TIMER, NULL);
}
}
}
/*---------------------------------------------------------------------------*/
#if RESOLV_CONF_SUPPORTS_MDNS
@ -1405,31 +1435,6 @@ resolv_lookup(const char *name, uip_ipaddr_t ** ipaddr)
return ret;
}
/*---------------------------------------------------------------------------*/
/**
* Obtain the currently configured DNS server.
*
* \return A pointer to a 4-byte representation of the IP address of
* the currently configured DNS server or NULL if no DNS server has
* been configured.
*/
uip_ipaddr_t *
resolv_getserver(void)
{
return &resolv_default_dns_server;
}
/*---------------------------------------------------------------------------*/
/**
* Configure a DNS server.
*
* \param dnsserver A pointer to a 4-byte representation of the IP
* address of the DNS server to be configured.
*/
void
resolv_conf(const uip_ipaddr_t * dnsserver)
{
uip_ipaddr_copy(&resolv_default_dns_server, dnsserver);
}
/*---------------------------------------------------------------------------*/
/** \internal
* Callback function which is called when a hostname is found.
*

View file

@ -57,11 +57,6 @@
*/
CCIF extern process_event_t resolv_event_found;
/* Functions. */
CCIF void resolv_conf(const uip_ipaddr_t * dnsserver);
CCIF uip_ipaddr_t *resolv_getserver(void);
enum {
/** Hostname is fresh and usable. This response is cached and will eventually
* expire to RESOLV_STATUS_EXPIRED.*/
@ -95,6 +90,7 @@ enum {
typedef uint8_t resolv_status_t;
/* Functions. */
CCIF resolv_status_t resolv_lookup(const char *name, uip_ipaddr_t ** ipaddr);
CCIF void resolv_query(const char *name);

View file

@ -0,0 +1,234 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* uIP Name Server interface
* \author Víctor Ariño <victor.arino@tado.com>
*/
/*
* Copyright (c) 2014, tado° GmbH.
* 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. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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.
*
* This file is part of the Contiki operating system.
*
*/
#include "contiki.h"
#include "contiki-net.h"
#include "lib/list.h"
#include "lib/memb.h"
#include <string.h>
/** \brief Nameserver record */
typedef struct uip_nameserver_record {
struct uip_nameserver_record *next;
uip_ipaddr_t ip;
uint32_t added;
uint32_t lifetime;
} uip_nameserver_record;
/** \brief Initialization flag */
static uint8_t initialized = 0;
/** \name List and memory block
* @{
*/
#if UIP_NAMESERVER_POOL_SIZE > 1
LIST(dns);
MEMB(dnsmemb, uip_nameserver_record, UIP_NAMESERVER_POOL_SIZE);
#else /* UIP_NAMESERVER_POOL_SIZE > 1 */
static uip_ipaddr_t serveraddr;
static uint32_t serverlifetime;
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
/** @} */
/** \brief Expiration time in seconds */
#define DNS_EXPIRATION(r) \
(((UIP_NAMESERVER_INFINITE_LIFETIME - r->added) <= r->lifetime) ? \
UIP_NAMESERVER_INFINITE_LIFETIME : r->added + r->lifetime)
/*----------------------------------------------------------------------------*/
/**
* Initialize the module variables
*/
#if UIP_NAMESERVER_POOL_SIZE > 1
static CC_INLINE void
init(void)
{
list_init(dns);
memb_init(&dnsmemb);
initialized = 1;
}
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
/*----------------------------------------------------------------------------*/
void
uip_nameserver_update(uip_ipaddr_t *nameserver, uint32_t lifetime)
{
#if UIP_NAMESERVER_POOL_SIZE > 1
register uip_nameserver_record *e;
if(initialized == 0) {
init();
}
for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
if(uip_ipaddr_cmp(&e->ip, nameserver)) {
break;
/* RFC6106: In case there's no more space, the new servers should replace
* the the eldest ones */
}
}
if(e == NULL) {
if((e = memb_alloc(&dnsmemb)) != NULL) {
list_add(dns, e);
} else {
uip_nameserver_record *p;
for(e = list_head(dns), p = list_head(dns); p != NULL;
p = list_item_next(p)) {
if(DNS_EXPIRATION(p) < DNS_EXPIRATION(e)) {
e = p;
}
}
}
}
/* RFC6106: In case the entry is existing the expiration time must be
* updated. Otherwise, new entries are added. */
if(e != NULL) {
if(lifetime == 0) {
memb_free(&dnsmemb, e);
list_remove(dns, e);
} else {
e->added = clock_seconds();
e->lifetime = lifetime;
uip_ipaddr_copy(&e->ip, nameserver);
}
}
#else /* UIP_NAMESERVER_POOL_SIZE > 1 */
uip_ipaddr_copy(&serveraddr, nameserver);
serverlifetime = lifetime;
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
}
/*----------------------------------------------------------------------------*/
#if UIP_NAMESERVER_POOL_SIZE > 1
/**
* Purge expired records
*/
static void
purge(void)
{
register uip_nameserver_record *e = NULL;
uint32_t time = clock_seconds();
for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
if(DNS_EXPIRATION(e) < time) {
list_remove(dns, e);
memb_free(&dnsmemb, e);
e = list_head(dns);
}
}
}
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
/*----------------------------------------------------------------------------*/
uip_ipaddr_t *
uip_nameserver_get(uint8_t num)
{
#if UIP_NAMESERVER_POOL_SIZE > 1
uint8_t i;
uip_nameserver_record *e = NULL;
if(initialized == 0) {
return NULL;
}
purge();
for(i = 1, e = list_head(dns); e != NULL && i <= num;
i++, e = list_item_next(e)) {
}
if(e != NULL) {
return &e->ip;
}
return NULL;
#else /* UIP_NAMESERVER_POOL_SIZE > 1 */
if(num > 0) {
return NULL;
}
return &serveraddr;
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
}
/*----------------------------------------------------------------------------*/
uint32_t
uip_nameserver_next_expiration(void)
{
#if UIP_NAMESERVER_POOL_SIZE > 1
register uip_nameserver_record *e = NULL;
uint32_t exp = UIP_NAMESERVER_INFINITE_LIFETIME;
uint32_t t;
if(initialized == 0 || list_length(dns) == 0) {
return 0;
}
purge();
for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
t = DNS_EXPIRATION(e);
if(t < exp) {
exp = t;
}
}
return exp;
#else /* UIP_NAMESERVER_POOL_SIZE > 1 */
return serverlifetime;
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
}
/*----------------------------------------------------------------------------*/
uint16_t
uip_nameserver_count(void)
{
#if UIP_NAMESERVER_POOL_SIZE > 1
if(initialized == 0) {
return 0;
}
return list_length(dns);
#else /* UIP_NAMESERVER_POOL_SIZE > 1 */
#if NETSTACK_CONF_WITH_IPV6
if(uip_is_addr_unspecified(&serveraddr)) {
#else /* NETSTACK_CONF_WITH_IPV6 */
if(uip_ipaddr_cmp(&serveraddr, &uip_all_zeroes_addr)) {
#endif /* NETSTACK_CONF_WITH_IPV6 */
return 0;
} else {
return 1;
}
#endif /* UIP_NAMESERVER_POOL_SIZE > 1 */
}
/*----------------------------------------------------------------------------*/
/** @} */

View file

@ -0,0 +1,101 @@
/**
* \addtogroup uip6
* @{
*/
/**
* \file
* uIP Name Server interface
* \author Víctor Ariño <victor.arino@tado.com>
*/
/*
* Copyright (c) 2014, tado° GmbH.
* 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. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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.
*
* This file is part of the Contiki operating system.
*
*/
#ifndef UIP_NAMESERVER_H_
#define UIP_NAMESERVER_H_
/**
* \name General
* @{
*/
/** \brief Number of Nameservers to keep */
#ifndef UIP_CONF_NAMESERVER_POOL_SIZE
#define UIP_NAMESERVER_POOL_SIZE 1
#else /* UIP_CONF_NAMESERVER_POOL_SIZE */
#define UIP_NAMESERVER_POOL_SIZE UIP_CONF_NAMESERVER_POOL_SIZE
#endif /* UIP_CONF_NAMESERVER_POOL_SIZE */
/** \brief Infinite Lifetime indicator */
#define UIP_NAMESERVER_INFINITE_LIFETIME 0xFFFFFFFF
/** @} */
/**
* \name Nameserver maintenance
* @{
*/
/**
* \brief Insert or update a nameserver into/from the pool
*
* The list is kept according to the RFC6106, which indicates that new entries
* will replace old ones (with lower lifetime) and existing entries will update
* their lifetimes.
*
* \param nameserver Pointer to the nameserver ip address
* \param lifetime Life time of the given address. Minimum is 0, which is
* considered to remove an entry. Maximum is 0xFFFFFFFF which
* is considered infinite.
*/
void uip_nameserver_update(uip_ipaddr_t *nameserver, uint32_t lifetime);
/**
* \brief Get a Nameserver ip address given in RA
*
* \param num The number of the nameserver to obtain, starting at 0 and going
* up to the pool size.
*/
uip_ipaddr_t *uip_nameserver_get(uint8_t num);
/**
* \brief Get next expiration time
*
* The least expiration time is returned
*/
uint32_t uip_nameserver_next_expiration(void);
/**
* \brief Get the number of recorded name servers
*/
uint16_t uip_nameserver_count(void);
/** @} */
#endif /* UIP_NAMESERVER_H_ */
/** @} */