Skip to content

Commit

Permalink
core/net/resolv: IPv6 and mDNS ("Bonjour") support. Major refactor.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
darconeous committed Mar 10, 2013
1 parent 97e1676 commit f145c17
Show file tree
Hide file tree
Showing 21 changed files with 1,351 additions and 286 deletions.
5 changes: 2 additions & 3 deletions apps/email/email.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions apps/ftp/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions apps/httpd-ws/httpd-ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
6 changes: 2 additions & 4 deletions apps/irc/irc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion apps/shell/shell-irc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion apps/shell/shell-ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion apps/shell/shell-tcpsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion apps/shell/shell-udpsend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions apps/shell/shell-wget.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.", "");
Expand Down
5 changes: 2 additions & 3 deletions apps/telnet/simpletelnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
5 changes: 2 additions & 3 deletions apps/vnc/vnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions apps/webbrowser/webclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down
7 changes: 5 additions & 2 deletions apps/webbrowser/www.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
Loading

0 comments on commit f145c17

Please sign in to comment.