Integrates uip-nameserver API

On the same commit the src have been uncrustified and some typo
fixes as well as includes missing.
ico
Víctor Ariño 2014-04-16 15:52:22 +02:00 committed by Adam Dunkels
parent 27afb5a2ae
commit ccc0d27da0
15 changed files with 187 additions and 189 deletions

View File

@ -88,7 +88,7 @@ makestrings(void)
uip_getdraddr(&addr);
makeaddr(&addr, gateway);
addrptr = resolv_getserver();
addrptr = uip_nameserver_get(0);
if(addrptr != NULL) {
makeaddr(addrptr, dnsserver);
}
@ -147,7 +147,7 @@ dhcpc_configured(const struct dhcpc_state *s)
uip_sethostaddr(&s->ipaddr);
uip_setnetmask(&s->netmask);
uip_setdraddr(&s->default_router);
resolv_conf(&s->dnsaddr);
uip_nameserver_update(&s->dnsaddr, UIP_NAMESERVER_INFINITE_LIFETIME);
set_statustext("Configured.");
process_post(PROCESS_CURRENT(), SHOWCONFIG, NULL);
}

View File

@ -113,7 +113,7 @@ makestrings(void)
makeaddr(&addr, gateway);
#if UIP_UDP
addrptr = resolv_getserver();
addrptr = uip_nameserver_get(0);
if(addrptr != NULL) {
makeaddr(addrptr, dnsserver);
}
@ -152,7 +152,7 @@ apply_tcpipconfig(void)
#if UIP_UDP
nullterminate(dnsserver);
if(uiplib_ipaddrconv(dnsserver, &addr)) {
resolv_conf(&addr);
uip_nameserver_update(&addr, UIP_NAMESERVER_INFINITE_LIFETIME);
}
#endif /* UIP_UDP */
}

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,20 @@ 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
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 +671,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 +756,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 +766,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 +1053,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 +1434,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

@ -49,12 +49,12 @@
#include <string.h>
/** \brief Nameserver record */
typedef struct uip_namserver_record {
struct uip_namserver_record *next;
typedef struct uip_nameserver_record {
struct uip_nameserver_record *next;
uip_ipaddr_t ip;
uint32_t added;
uint32_t lifetime;
} uip_namserver_record;
} uip_nameserver_record;
/** \brief Initialization flag */
static uint8_t initialized = 0;
@ -63,66 +63,68 @@ static uint8_t initialized = 0;
* @{
*/
LIST(dns);
MEMB(dnsmemb, uip_namserver_record, UIP_NAMESERVER_POOL_SIZE);
MEMB(dnsmemb, uip_nameserver_record, UIP_NAMESERVER_POOL_SIZE);
/** @} */
/** \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)
(((UIP_NAMESERVER_INFINITE_LIFETIME - r->added) <= r->lifetime) ? \
UIP_NAMESERVER_INFINITE_LIFETIME : r->added + r->lifetime)
/*----------------------------------------------------------------------------*/
/**
* Initialize the module variables
*/
static void inline
static CC_INLINE void
init(void)
{
list_init(dns);
memb_init(&dnsmemb);
initialized = 1;
list_init(dns);
memb_init(&dnsmemb);
initialized = 1;
}
/*----------------------------------------------------------------------------*/
void
uip_nameserver_update(uip_ipaddr_t *nameserver, uint32_t lifetime)
{
if(initialized == 0) {
init();
}
register uip_nameserver_record *e;
register uip_namserver_record *e;
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_namserver_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;
}
}
}
}
if(initialized == 0) {
init();
}
/* 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);
}
}
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);
}
}
}
/*----------------------------------------------------------------------------*/
/**
@ -131,66 +133,65 @@ uip_nameserver_update(uip_ipaddr_t *nameserver, uint32_t lifetime)
static void
purge(void)
{
register uip_namserver_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);
}
}
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);
}
}
}
/*----------------------------------------------------------------------------*/
uip_ipaddr_t *
uip_nameserver_get(uint8_t num)
{
uint8_t i;
uip_namserver_record *e = NULL;
uint8_t i;
uip_nameserver_record *e = NULL;
if(initialized == 0) {
return NULL;
}
if(initialized == 0) {
return NULL;
}
purge();
for(i = 1, e = list_head(dns); e != NULL && i <= num;
i++, e = list_item_next(e)) {
}
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;
if(e != NULL) {
return &e->ip;
}
return NULL;
}
/*----------------------------------------------------------------------------*/
uint32_t
uip_nameserver_next_expiration(void)
{
register uip_namserver_record *e = NULL;
uint32_t exp = UIP_NAMESERVER_INFINITE_LIFETIME;
uint32_t t;
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;
}
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;
}
}
purge();
for(e = list_head(dns); e != NULL; e = list_item_next(e)) {
t = DNS_EXPIRATION(e);
if(t < exp) {
exp = t;
}
}
return exp;
return exp;
}
/*----------------------------------------------------------------------------*/
uint16_t
uip_nameserver_count(void)
{
if(initialized == 0) {
return 0;
}
return list_length(dns);
if(initialized == 0) {
return 0;
}
return list_length(dns);
}
/*----------------------------------------------------------------------------*/
/** @} */

View File

@ -49,7 +49,11 @@
* @{
*/
/** \brief Number of Nameservers to keep */
#define UIP_NAMESERVER_POOL_SIZE 2
#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
/** @} */
@ -77,7 +81,7 @@ 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. (\sa UIP_ND6_RDNSS_POOL_SIZE)
* up to the pool size.
*/
uip_ipaddr_t *
uip_nameserver_get(uint8_t num);

View File

@ -720,23 +720,23 @@ uip_nd6_ra_output(uip_ipaddr_t * dest)
#if UIP_ND6_RA_RDNSS
if(uip_nameserver_count() > 0) {
uint8_t i = 0;
uip_ipaddr_t *ip = &UIP_ND6_OPT_RDNSS_BUF->ip;
uip_ipaddr_t *dns = NULL;
UIP_ND6_OPT_RDNSS_BUF->type = UIP_ND6_OPT_RDNSS;
UIP_ND6_OPT_RDNSS_BUF->reserved = 0;
UIP_ND6_OPT_RDNSS_BUF->lifetime = uip_nameserver_next_expiration();
if(UIP_ND6_OPT_RDNSS_BUF->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) {
UIP_ND6_OPT_RDNSS_BUF->lifetime -= clock_seconds();
}
while((dns = uip_nameserver_get(i)) != NULL) {
uip_ipaddr_copy(ip++, dns);
i++;
}
UIP_ND6_OPT_RDNSS_BUF->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1);
PRINTF("%d nameservers reported\n", i);
uip_len += UIP_ND6_OPT_RDNSS_BUF->len << 3;
nd6_opt_offset += UIP_ND6_OPT_RDNSS_BUF->len << 3;
uint8_t i = 0;
uip_ipaddr_t *ip = &UIP_ND6_OPT_RDNSS_BUF->ip;
uip_ipaddr_t *dns = NULL;
UIP_ND6_OPT_RDNSS_BUF->type = UIP_ND6_OPT_RDNSS;
UIP_ND6_OPT_RDNSS_BUF->reserved = 0;
UIP_ND6_OPT_RDNSS_BUF->lifetime = uip_nameserver_next_expiration();
if(UIP_ND6_OPT_RDNSS_BUF->lifetime != UIP_NAMESERVER_INFINITE_LIFETIME) {
UIP_ND6_OPT_RDNSS_BUF->lifetime -= clock_seconds();
}
while((dns = uip_nameserver_get(i)) != NULL) {
uip_ipaddr_copy(ip++, dns);
i++;
}
UIP_ND6_OPT_RDNSS_BUF->len = UIP_ND6_OPT_RDNSS_LEN + (i << 1);
PRINTF("%d nameservers reported\n", i);
uip_len += UIP_ND6_OPT_RDNSS_BUF->len << 3;
nd6_opt_offset += UIP_ND6_OPT_RDNSS_BUF->len << 3;
}
#endif /* UIP_ND6_RA_RDNSS */
@ -965,20 +965,20 @@ ra_input(void)
break;
#if UIP_ND6_RA_RDNSS
case UIP_ND6_OPT_RDNSS:
if(UIP_ND6_RA_BUF->flags_reserved & (UIP_ND6_O_FLAG << 6)){
PRINTF("Processing RDNSS option\n");
uint8_t naddr = (UIP_ND6_OPT_RDNSS_BUF->len - 1) / 2;
uip_ipaddr_t *ip = (uip_ipaddr_t *)(&UIP_ND6_OPT_RDNSS_BUF->ip);
PRINTF("got %d nameservers\n", naddr);
while(naddr-- > 0) {
PRINTF(" nameserver: ");
PRINT6ADDR(ip);
PRINTF(" lifetime: %lx\n", uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime));
uip_nameserver_update(ip, uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime));
ip++;
}
}
break;
if(UIP_ND6_RA_BUF->flags_reserved & (UIP_ND6_O_FLAG << 6)) {
PRINTF("Processing RDNSS option\n");
uint8_t naddr = (UIP_ND6_OPT_RDNSS_BUF->len - 1) / 2;
uip_ipaddr_t *ip = (uip_ipaddr_t *)(&UIP_ND6_OPT_RDNSS_BUF->ip);
PRINTF("got %d nameservers\n", naddr);
while(naddr-- > 0) {
PRINTF(" nameserver: ");
PRINT6ADDR(ip);
PRINTF(" lifetime: %lx\n", uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime));
uip_nameserver_update(ip, uip_ntohl(UIP_ND6_OPT_RDNSS_BUF->lifetime));
ip++;
}
}
break;
#endif /* UIP_ND6_RA_RDNSS */
default:
PRINTF("ND option not supported in RA");

View File

@ -147,13 +147,6 @@
#define UIP_ND6_RA_RDNSS UIP_CONF_ND6_RA_RDNSS
#endif
/** \brief Number of DNS to hold on the node */
#if UIP_ND6_RA_RDNSS
#define UIP_ND6_RDNSS_POOL_SIZE 2
#else
#define UIP_ND6_RDNSS_POOL_SIZE 0
#endif
#ifndef UIP_CONF_ND6_RA_DNSSL
#define UIP_ND6_RA_DNSSL 0
#else

View File

@ -117,7 +117,7 @@ makestrings(void)
makeaddr(&addr, gateway);
#if WITH_DNS
addrptr = resolv_getserver();
addrptr = uip_nameserver_get(0);
if(addrptr != NULL) {
makeaddr(addrptr, dnsserver);
}
@ -245,7 +245,7 @@ dhcpc_configured(const struct dhcpc_state *s)
uip_setnetmask(&s->netmask);
uip_setdraddr(&s->default_router);
#if WITH_DNS
resolv_conf(&s->dnsaddr);
uip_nameserver_update(&s->dnsaddr, UIP_NAMESERVER_INFINITE_LIFETIME);
#endif /* WITH_DNS */
set_statustext("Configured.");
@ -261,7 +261,7 @@ dhcpc_unconfigured(const struct dhcpc_state *s)
uip_setnetmask(&nulladdr);
uip_setdraddr(&nulladdr);
#if WITH_DNS
resolv_conf(&nulladdr);
uip_nameserver_update(&nulladdr, UIP_NAMESERVER_INFINITE_LIFETIME);
#endif /* WITH_DNS */
set_statustext("Unconfigured.");

View File

@ -105,7 +105,7 @@ config_read(char *filename)
uip_setnetmask(&config.netmask);
uip_setdraddr(&config.draddr);
#if WITH_DNS
resolv_conf(&config.resolvaddr);
uip_nameserver_update(&config.resolvaddr, UIP_NAMESERVER_INFINITE_LIFETIME);
#endif /* WITH_DNS */
return &config.ethernetcfg;

View File

@ -104,7 +104,7 @@ main(void)
uip_setdraddr(&addr);
uip_ipaddr(&addr, 192,168,0,1);
resolv_conf(&addr);
uip_nameserver_update(&addr, UIP_NAMESERVER_INFINITE_LIFETIME);
ethernet_config = &config;
}

View File

@ -100,7 +100,7 @@ main(void)
uip_setdraddr(&addr);
uip_ipaddr(&addr, 192,168,0,1);
resolv_conf(&addr);
uip_nameserver_update(&addr, UIP_NAMESERVER_INFINITE_LIFETIME);
ethernet_config = &config;
}

View File

@ -100,7 +100,7 @@ main(void)
uip_setdraddr(&addr);
uip_ipaddr(&addr, 192,168,0,1);
resolv_conf(&addr);
uip_nameserver_update(&addr, UIP_NAMESERVER_INFINITE_LIFETIME);
ethernet_config = &config;
}

View File

@ -98,7 +98,7 @@ main(void)
uip_setdraddr(&addr);
uip_ipaddr(&addr, 192,168,0,1);
resolv_conf(&addr);
uip_nameserver_update(&addr, UIP_NAMESERVER_INFINITE_LIFETIME);
ethernet_config = &config;
}

View File

@ -140,7 +140,7 @@ main(int argc, char **argv)
log_message("Def. Router: ", inet_ntoa(*(struct in_addr*)&addr));
uip_ipaddr(&addr, 192,168,0,1);
resolv_conf(&addr);
uip_nameserver_update(&addr, UIP_NAMESERVER_INFINITE_LIFETIME);
log_message("DNS Server: ", inet_ntoa(*(struct in_addr*)&addr));
}