core/net/resolv: IPv6 and mDNS ("Bonjour") support. Major refactor.

This patch updates the DNS resolver to support IPv6 and introduces an
improved API for looking up DNS entries. This patch also adds optional
support for mDNS lookups and responses to the DNS resolver.

Here is a quick summary of the changes:

 * Added support for IPv6 lookups.
 * DNS queries now honor record expiration.
 * Added support for mDNS, compatible with "Bonjour".
 * Implemented a new lookup api, `resolv_lookup2()`, which provides
   more information about the state of the record(error, expired,
   looking-up, etc.).

About mDNS/Bonjour Support
--------------------------

This patch adds basic support for mDNS/Bonjour, which allows you to
refer to the name of a device instead of its IP address. This is
incredibly convenient for IPv6 addresses because they tend to be very
long and difficult to remember. It is especially important for
link-local IPv6 addresses, since not all programs support the '%'
notation for indicating a network interface (required on systems with
more than one network interface to disambiguate).

In other words, instead of typing in this:

 * `http://[fe80::58dc:d7ed:a644:628f%en1]/`

You can type this instead:

 * `http://contiki.local/`

Huge improvement, no?

The convenience extends beyond that: this mechanism can be used for
nodes to talk to each other based on their human-readable names instead
of their IPv6 addresses. So instead of a switch on
`aaaa::58dc:d7ed:a644:628f` triggering an actuator on
`aaaa::ed26:19c1:4bd2:f95b`, `light-switch.local` can trigger the
actuator on `living-room-lights.local`.

What you need to do to be able to look up `.local` names on your
workstation depends on a few factors:

 * Your machine needs to be able to send and receive multicast packets
   to and from the LoWPAN. You can do this easily with the Jackdaw
   firmware on an RZUSBStick. If you have a border router, you will need
   it to bridge the mDNS multicast packets across the border.

 * If you are using a Mac, you win. All Apple devices support mDNS
   lookups.

 * If you are using Windows, you can install Apple's Bonjour for Windows
   package. (This may be already installed on your machine if you have
   installed iTunes) After you install this you can easily do `.local`
   lookups.

 * If you are using a Unix machine, you can install Avahi.

The default hostname is set to `contiki.local.`. You can change the
hostname programmatically by calling `resolv_set_hostname()`. You can
change the default hostname by changing `CONTIKI_CONF_DEFAULT_HOSTNAME`.

You may disable mDNS support by setting `RESOLV_CONF_SUPPORTS_MDNS` to
`0`.

---------------------------------

core/net/resolv: `resolv_lookup2()` -> `resolv_lookup()`

Note that this patch should fix several `resolv_lookup()` bugs
that already existed. There were many cases where `resolv_lookup()`
was being called and the IP address ignored, but later code
assumed that the IP address had been fetched... ANYWAY, those
should be fixed now.

---------------------------------

examples/udp-ipv6: Updated client to use MDNS to lookup the server.

Also updated the Cooja regression test simulation.
This commit is contained in:
Robert Quattlebaum 2012-05-17 08:24:08 -07:00
parent 97e16760c3
commit f145c17039
21 changed files with 1358 additions and 293 deletions

View file

@ -191,8 +191,7 @@ applyconfig(void)
addrptr = &addr;
#if UIP_UDP
if(uiplib_ipaddrconv(smtpserver, &addr) == 0) {
addrptr = resolv_lookup(smtpserver);
if(addrptr == NULL) {
if(resolv_lookup(smtpserver, &addrptr) != RESOLV_STATUS_CACHED) {
resolv_query(smtpserver);
ctk_label_set_text(&statuslabel, "Resolving host...");
return;
@ -334,7 +333,7 @@ PROCESS_THREAD(email_process, ev, data)
#if UIP_UDP
} else if(ev == resolv_event_found) {
if(strcmp(data, smtpserver) == 0) {
if(resolv_lookup(smtpserver) != NULL) {
if(resolv_lookup(smtpserver, NULL) == RESOLV_STATUS_CACHED) {
applyconfig();
ctk_label_set_text(&statuslabel, "");
} else {

View file

@ -434,7 +434,7 @@ PROCESS_THREAD(ftp_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
(ipaddrptr = resolv_lookup((char *)data)) != NULL) {
resolv_lookup((char *)data, &ipaddrptr) == RESOLV_STATUS_CACHED) {
connection = ftpc_connect(ipaddrptr, UIP_HTONS(21));
show_statustext("Connecting to ", hostname);
} else {
@ -508,8 +508,7 @@ PROCESS_THREAD(ftp_process, ev, data)
ptractive = 1;
#if UIP_UDP
if(uiplib_ipaddrconv(hostname, &ipaddr) == 0) {
ipaddrptr = resolv_lookup(hostname);
if(ipaddrptr == NULL) {
if(resolv_lookup(hostname, &ipaddrptr) != RESOLV_STATUS_CACHED) {
resolv_query(hostname);
show_statustext("Resolving host ", hostname);
break;

View file

@ -378,9 +378,7 @@ httpd_ws_request(char request_type, const char *host_ip, const char *host_hdr,
ipaddr = &addr;
if(uiplib_ipaddrconv(host_ip, &addr) == 0) {
#if 0 && UIP_UDP
ipaddr = resolv_lookup(host_ip);
if(ipaddr == NULL) {
if(resolv_lookup(host, &ipaddr) != RESOLV_STATUS_CACHED) {
return NULL;
}
#else /* UIP_UDP */

View file

@ -246,8 +246,7 @@ PROCESS_THREAD(irc_process, ev, data)
ipaddr = &serveraddr;
#if UIP_UDP
if(uiplib_ipaddrconv(server, &serveraddr) == 0) {
ipaddr = resolv_lookup(server);
if(ipaddr == NULL) {
if(resolv_lookup(server, &ipaddr) != RESOLV_STATUS_CACHED) {
resolv_query(server);
} else {
uip_ipaddr_copy(&serveraddr, ipaddr);
@ -264,8 +263,7 @@ PROCESS_THREAD(irc_process, ev, data)
#if UIP_UDP
} else if(ev == resolv_event_found) {
ipaddr = resolv_lookup(server);
if(ipaddr == NULL) {
if(resolv_lookup(server, &ipaddr) != RESOLV_STATUS_CACHED) {
ircc_text_output(&s, server, "hostname not found");
} else {
uip_ipaddr_copy(&serveraddr, ipaddr);

View file

@ -157,7 +157,7 @@ PROCESS_THREAD(shell_irc_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
resolv_lookup((char *)data) != NULL) {
resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
uip_ipaddr_copy(serveraddr, ipaddr);
ircc_connect(&s, server, serveraddr, nick);
} else {

View file

@ -166,7 +166,7 @@ PROCESS_THREAD(shell_ping_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
resolv_lookup((char *)data) != NULL) {
resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
uip_ipaddr_copy(serveraddr, ipaddr);
telnet_connect(&s, server, serveraddr, nick);
} else {

View file

@ -173,7 +173,7 @@ PROCESS_THREAD(shell_tcpsend_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
resolv_lookup((char *)data) != NULL) {
resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
uip_ipaddr_copy(serveraddr, ipaddr);
telnet_connect(&s, server, serveraddr, nick);
} else {

View file

@ -123,7 +123,7 @@ PROCESS_THREAD(shell_udpsend_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
resolv_lookup((char *)data) != NULL) {
resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
uip_ipaddr_copy(serveraddr, ipaddr);
telnet_connect(&s, server, serveraddr, nick);
} else {

View file

@ -130,12 +130,14 @@ open_url(char *url)
/* Try to lookup the hostname. If it fails, we initiate a hostname
lookup and print out an informative message on the statusbar. */
if(uiplib_ipaddrconv(host, &addr) == 0) {
uip_ipaddr_t *addrptr;
shell_output_str(&wget_command, "Not an IP address", "");
if(resolv_lookup(host) == NULL) {
if(resolv_lookup(host, &addrptr) == RESOLV_STATUS_UNCACHED) {
shell_output_str(&wget_command, "Not resolved", "");
resolv_query(host);
return;
}
uip_ipaddr_copy(&addr, addrptr);
}
#else /* UIP_UDP */
uiplib_ipaddrconv(host, &addr);
@ -171,7 +173,7 @@ PROCESS_THREAD(shell_wget_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
resolv_lookup((char *)data) != NULL) {
resolv_lookup((char *)data, NULL) == RESOLV_STATUS_CACHED) {
open_url(url);
} else {
shell_output_str(&wget_command, "Host not found.", "");

View file

@ -163,8 +163,7 @@ connect(void)
addrptr = &addr;
#if UIP_UDP
if(uiplib_ipaddrconv(telnethost, &addr) == 0) {
addrptr = resolv_lookup(telnethost);
if(addrptr == NULL) {
if(resolv_lookup(telnethost, &addrptr) == RESOLV_STATUS_UNCACHED) {
resolv_query(telnethost);
show("Resolving host...");
return;
@ -252,7 +251,7 @@ PROCESS_THREAD(simpletelnet_process, ev, data)
#if UIP_UDP
} else if(ev == resolv_event_found) {
if(strcmp(data, telnethost) == 0) {
if(resolv_lookup(telnethost) != NULL) {
if(resolv_lookup(telnethost, NULL) == RESOLV_STATUS_CACHED) {
connect();
} else {
show("Host not found");

View file

@ -134,8 +134,7 @@ connect(void)
addrptr = &addr;
if(uiplib_ipaddrconv(host, &addr) == 0) {
addrptr = resolv_lookup(host);
if(addrptr == NULL) {
if(resolv_lookup(host, &addrptr) == RESOLV_STATUS_UNCACHED) {
resolv_query(host);
show("Resolving host...");
return;
@ -198,7 +197,7 @@ PROCESS_THREAD(vnc_process, ev, data)
LOADER_UNLOAD();
} else if(ev == resolv_event_found) {
if(strcmp(data, host) == 0) {
if(resolv_lookup(host) != NULL) {
if(resolv_lookup(host, NULL) == RESOLV_STATUS_CACHED) {
connect();
} else {
show("Host not found");

View file

@ -140,9 +140,7 @@ webclient_get(const char *host, uint16_t port, const char *file)
ipaddr = &addr;
if(uiplib_ipaddrconv(host, &addr) == 0) {
#if UIP_UDP
ipaddr = resolv_lookup(host);
if(ipaddr == NULL) {
if(resolv_lookup(host,&ipaddr) != RESOLV_STATUS_CACHED) {
return 0;
}
#else /* UIP_UDP */
@ -486,7 +484,7 @@ webclient_appcall(void *state)
init_connection();
}*/
#if UIP_UDP
if(resolv_lookup(s.host) == NULL) {
if(resolv_lookup(s.host, NULL) != RESOLV_STATUS_CACHED) {
resolv_query(s.host);
}
#endif /* UIP_UDP */

View file

@ -348,11 +348,13 @@ open_url(void)
/* Try to lookup the hostname. If it fails, we initiate a hostname
lookup and print out an informative message on the statusbar. */
if(uiplib_ipaddrconv(host, &addr) == 0) {
if(resolv_lookup(host) == NULL) {
uip_ipaddr_t *addrptr;
if(resolv_lookup(host, &addrptr) != RESOLV_STATUS_CACHED) {
resolv_query(host);
show_statustext("Resolving host...");
return;
}
uip_ipaddr_copy(&addr, addrptr);
}
#else /* UIP_UDP */
uiplib_ipaddrconv(host, &addr);
@ -553,7 +555,8 @@ PROCESS_THREAD(www_process, ev, data)
#if UIP_UDP
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL && resolv_lookup((char *)data) != NULL) {
if((char *)data != NULL &&
resolv_lookup((char *)data, NULL) == RESOLV_STATUS_CACHED) {
open_url();
} else {
show_statustext("Host not found");

File diff suppressed because it is too large Load diff

View file

@ -40,6 +40,16 @@
#define __RESOLV_H__
#include "contiki.h"
#include "uip.h"
/** If RESOLV_CONF_SUPPORTS_MDNS is set, then queries
* for domain names in the `local` TLD will use MDNS and
* will respond to MDNS queries for this device's hostname,
* as described by draft-cheshire-dnsext-multicastdns.
*/
#ifndef RESOLV_CONF_SUPPORTS_MDNS
#define RESOLV_CONF_SUPPORTS_MDNS (1)
#endif
/**
* Event that is broadcasted when a DNS name has been resolved.
@ -47,11 +57,53 @@
CCIF extern process_event_t resolv_event_found;
/* Functions. */
CCIF void resolv_conf(const uip_ipaddr_t *dnsserver);
CCIF void resolv_conf(const uip_ipaddr_t * dnsserver);
CCIF uip_ipaddr_t *resolv_getserver(void);
CCIF uip_ipaddr_t *resolv_lookup(const char *name);
enum {
/** Hostname is fresh and usable. This response is cached and will eventually
* expire to RESOLV_STATUS_EXPIRED.*/
RESOLV_STATUS_CACHED = 0,
/** Hostname was not found in the cache. Use resolv_query() to look it up. */
RESOLV_STATUS_UNCACHED,
/** Hostname was found, but it's status has expired. The address returned
* should not be used. Use resolv_query() to freshen it up.
*/
RESOLV_STATUS_EXPIRED,
/** The server has returned a not-found response for this domain name.
* This response is cached for the period described in the server.
* You may issue a new query at any time using resolv_query(), but
* you will generally want to wait until this domain's status becomes
* RESOLV_STATUS_EXPIRED.
*/
RESOLV_STATUS_NOT_FOUND,
/** This hostname is in the process of being resolved. Try again soon. */
RESOLV_STATUS_RESOLVING,
/** Some sort of server error was encountered while trying to look up this
* record. This response is cached and will eventually expire to
* RESOLV_STATUS_EXPIRED.
*/
RESOLV_STATUS_ERROR,
};
typedef uint8_t resolv_status_t;
CCIF resolv_status_t resolv_lookup(const char *name, uip_ipaddr_t ** ipaddr);
CCIF void resolv_query(const char *name);
#if RESOLV_CONF_SUPPORTS_MDNS
CCIF void resolv_set_hostname(const char *hostname);
CCIF const char *resolv_get_hostname(void);
#endif
PROCESS_NAME(resolv_process);
#endif /* __RESOLV_H__ */

View file

@ -75,9 +75,7 @@ json_ws_udp_setup(const char *host, uint16_t port)
ipaddr = &server_ipaddr;
if(uiplib_ipaddrconv(host, &server_ipaddr) == 0) {
#if 0 && UIP_UDP
ipaddr = resolv_lookup(host);
if(ipaddr == NULL) {
if(resolv_lookup(host, &ipaddr) != RESOLV_STATUS_CACHED) {
return 0;
}
#else /* UIP_UDP */

View file

@ -2,7 +2,7 @@ CONTIKI_PROJECT = sky-shell-webserver
all: $(CONTIKI_PROJECT)
PROJECT_SOURCEFILES = webserver-nogui.c
HTTPD_CFS=1
CFLAGS = -DWITH_UIP=1
CFLAGS = -DWITH_UIP=1 -DRESOLV_CONF_SUPPORTS_MDNS=0
DEFINES=NETSTACK_MAC=nullmac_driver,NETSTACK_RDC=nullrdc_driver
SMALL=1

View file

@ -30,8 +30,10 @@
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#include "net/resolv.h"
#include <string.h>
#include <stdbool.h>
#define DEBUG DEBUG_PRINT
#include "net/uip-debug.h"
@ -42,7 +44,7 @@
static struct uip_udp_conn *client_conn;
/*---------------------------------------------------------------------------*/
PROCESS(udp_client_process, "UDP client process");
AUTOSTART_PROCESSES(&udp_client_process);
AUTOSTART_PROCESSES(&resolv_process,&udp_client_process);
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
@ -102,20 +104,43 @@ set_global_address(void)
}
#endif /* UIP_CONF_ROUTER */
/*---------------------------------------------------------------------------*/
static void
static resolv_status_t
set_connection_address(uip_ipaddr_t *ipaddr)
{
#ifndef UDP_CONNECTION_ADDR
#if RESOLV_CONF_SUPPORTS_MDNS
#define UDP_CONNECTION_ADDR contiki-udp-server.local
#elif UIP_CONF_ROUTER
#define UDP_CONNECTION_ADDR aaaa:0:0:0:0212:7404:0004:0404
#else
#define UDP_CONNECTION_ADDR fe80:0:0:0:6466:6666:6666:6666
#endif
#endif /* !UDP_CONNECTION_ADDR */
#define _QUOTEME(x) #x
#define QUOTEME(x) _QUOTEME(x)
#ifdef UDP_CONNECTION_ADDR
resolv_status_t status = RESOLV_STATUS_ERROR;
if(uiplib_ipaddrconv(QUOTEME(UDP_CONNECTION_ADDR), ipaddr) == 0) {
PRINTF("UDP client failed to parse address '%s'\n", QUOTEME(UDP_CONNECTION_ADDR));
uip_ipaddr_t *resolved_addr = NULL;
status = resolv_lookup(QUOTEME(UDP_CONNECTION_ADDR),&resolved_addr);
if(status == RESOLV_STATUS_UNCACHED || status == RESOLV_STATUS_EXPIRED) {
PRINTF("Attempting to look up %s\n",QUOTEME(UDP_CONNECTION_ADDR));
resolv_query(QUOTEME(UDP_CONNECTION_ADDR));
status = RESOLV_STATUS_RESOLVING;
} else if(status == RESOLV_STATUS_CACHED && resolved_addr != NULL) {
PRINTF("Lookup of \"%s\" succeded!\n",QUOTEME(UDP_CONNECTION_ADDR));
} else {
PRINTF("Lookup of \"%s\" failed. status = %d\n",QUOTEME(UDP_CONNECTION_ADDR),status);
}
#elif UIP_CONF_ROUTER
uip_ip6addr(ipaddr,0xaaaa,0,0,0,0x0212,0x7404,0x0004,0x0404);
#else
uip_ip6addr(ipaddr,0xfe80,0,0,0,0x6466,0x6666,0x6666,0x6666);
#endif /* UDP_CONNECTION_ADDR */
if(resolved_addr)
uip_ipaddr_copy(ipaddr, resolved_addr);
} else {
status = RESOLV_STATUS_CACHED;
}
return status;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(udp_client_process, ev, data)
@ -132,7 +157,17 @@ PROCESS_THREAD(udp_client_process, ev, data)
print_local_addresses();
set_connection_address(&ipaddr);
static resolv_status_t status = RESOLV_STATUS_UNCACHED;
while(status != RESOLV_STATUS_CACHED) {
status = set_connection_address(&ipaddr);
if(status == RESOLV_STATUS_RESOLVING) {
PROCESS_WAIT_EVENT_UNTIL(ev == resolv_event_found);
} else if(status != RESOLV_STATUS_CACHED) {
PRINTF("Can't get connection address.\n");
PROCESS_YIELD();
}
}
/* new connection with remote host */
client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL);

View file

@ -43,7 +43,7 @@
static struct uip_udp_conn *server_conn;
PROCESS(udp_server_process, "UDP server process");
AUTOSTART_PROCESSES(&udp_server_process);
AUTOSTART_PROCESSES(&resolv_process,&udp_server_process);
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
@ -94,6 +94,10 @@ PROCESS_THREAD(udp_server_process, ev, data)
PROCESS_BEGIN();
PRINTF("UDP server started\n");
#if RESOLV_CONF_SUPPORTS_MDNS
resolv_set_hostname("contiki-udp-server");
#endif
#if UIP_CONF_ROUTER
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);

View file

@ -113,15 +113,16 @@ start_get(void)
#if UIP_UDP
/* First check if the host is an IP address. */
if(uiplib_ipaddrconv(host, &addr) == 0) {
uip_ipaddr_t *addrptr;
/* Try to lookup the hostname. If it fails, we initiate a hostname
lookup and print out an informative message on the
statusbar. */
if(resolv_lookup(host) == NULL) {
if(resolv_lookup(host, &addrptr) != RESOLV_STATUS_CACHED) {
resolv_query(host);
puts("Resolving host...");
return;
}
uip_ipaddr_copy(&addr, addrptr);
}
#else /* UIP_UDP */
uiplib_ipaddrconv(host, &addr);
@ -184,7 +185,7 @@ PROCESS_THREAD(wget_process, ev, data)
} else if(ev == resolv_event_found) {
/* Either found a hostname, or not. */
if((char *)data != NULL &&
resolv_lookup((char *)data) != NULL) {
resolv_lookup((char *)data, NULL) == RESOLV_STATUS_CACHED) {
start_get();
} else {
puts("Host not found");

View file

@ -49,7 +49,7 @@ make udp-server.cooja TARGET=cooja</commands>
<identifier>mtype512</identifier>
<description>Sender</description>
<contikiapp>[CONTIKI_DIR]/examples/udp-ipv6/udp-client.c</contikiapp>
<commands>make udp-client.cooja TARGET=cooja DEFINES=UDP_CONNECTION_ADDR=fe80::201:1:1:1</commands>
<commands>make udp-client.cooja TARGET=cooja</commands>
<moteinterface>se.sics.cooja.interfaces.Position</moteinterface>
<moteinterface>se.sics.cooja.interfaces.Battery</moteinterface>
<moteinterface>se.sics.cooja.contikimote.interfaces.ContikiVib</moteinterface>