69 #include "lib/random.h" 72 #define DEBUG CONTIKI_TARGET_COOJA 82 #define NULL (void *)0 85 #if !defined(__SDCC) && defined(SDCC_REVISION) 90 #define DEBUG_PRINTF(...) printf(__VA_ARGS__) 92 #define DEBUG_PRINTF(...) do { } while(0) 95 #if DEBUG || VERBOSE_DEBUG 96 #define PRINTF(...) printf(__VA_ARGS__) 98 #define PRINTF(...) do { } while(0) 103 strncasecmp(
const char *s1,
const char *s2,
size_t n)
106 return strncmp(s1, s2, n);
109 strcasecmp(
const char *s1,
const char *s2)
112 return strcmp(s1, s2);
115 int strcasecmp(
const char *s1,
const char *s2);
116 int strncasecmp(
const char *s1,
const char *s2,
size_t n);
119 #define UIP_UDP_BUF ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN]) 125 #ifndef RESOLV_CONF_SUPPORTS_MDNS 126 #define RESOLV_CONF_SUPPORTS_MDNS 1 129 #ifndef RESOLV_CONF_MDNS_INCLUDE_GLOBAL_V6_ADDRS 130 #define RESOLV_CONF_MDNS_INCLUDE_GLOBAL_V6_ADDRS 0 134 #ifndef RESOLV_CONF_MAX_RETRIES 135 #define RESOLV_CONF_MAX_RETRIES 4 138 #ifndef RESOLV_CONF_MAX_MDNS_RETRIES 139 #define RESOLV_CONF_MAX_MDNS_RETRIES 3 142 #ifndef RESOLV_CONF_MAX_DOMAIN_NAME_SIZE 143 #define RESOLV_CONF_MAX_DOMAIN_NAME_SIZE 32 146 #ifdef RESOLV_CONF_AUTO_REMOVE_TRAILING_DOTS 147 #define RESOLV_AUTO_REMOVE_TRAILING_DOTS RESOLV_CONF_AUTO_REMOVE_TRAILING_DOTS 149 #define RESOLV_AUTO_REMOVE_TRAILING_DOTS RESOLV_CONF_SUPPORTS_MDNS 152 #ifdef RESOLV_CONF_VERIFY_ANSWER_NAMES 153 #define RESOLV_VERIFY_ANSWER_NAMES RESOLV_CONF_VERIFY_ANSWER_NAMES 155 #define RESOLV_VERIFY_ANSWER_NAMES RESOLV_CONF_SUPPORTS_MDNS 158 #ifdef RESOLV_CONF_SUPPORTS_RECORD_EXPIRATION 159 #define RESOLV_SUPPORTS_RECORD_EXPIRATION RESOLV_CONF_SUPPORTS_RECORD_EXPIRATION 161 #define RESOLV_SUPPORTS_RECORD_EXPIRATION 1 164 #if RESOLV_CONF_SUPPORTS_MDNS && !RESOLV_VERIFY_ANSWER_NAMES 165 #error RESOLV_CONF_SUPPORTS_MDNS cannot be set without RESOLV_CONF_VERIFY_ANSWER_NAMES 168 #if !defined(CONTIKI_TARGET_NAME) && defined(BOARD) 169 #define stringy2(x) #x 170 #define stringy(x) stringy2(x) 171 #define CONTIKI_TARGET_NAME stringy(BOARD) 174 #ifndef CONTIKI_CONF_DEFAULT_HOSTNAME 175 #ifdef CONTIKI_TARGET_NAME 176 #define CONTIKI_CONF_DEFAULT_HOSTNAME "contiki-"CONTIKI_TARGET_NAME 178 #define CONTIKI_CONF_DEFAULT_HOSTNAME "contiki" 183 #define DNS_TYPE_CNAME 5 184 #define DNS_TYPE_PTR 12 185 #define DNS_TYPE_MX 15 186 #define DNS_TYPE_TXT 16 187 #define DNS_TYPE_AAAA 28 188 #define DNS_TYPE_SRV 33 189 #define DNS_TYPE_ANY 255 190 #define DNS_TYPE_NSEC 47 192 #define NATIVE_DNS_TYPE DNS_TYPE_AAAA 194 #define DNS_CLASS_IN 1 195 #define DNS_CLASS_ANY 255 202 #define MDNS_PORT 5353 205 #ifndef MDNS_RESPONDER_PORT 206 #define MDNS_RESPONDER_PORT 5354 212 uint8_t flags1, flags2;
213 #define DNS_FLAG1_RESPONSE 0x80 214 #define DNS_FLAG1_OPCODE_STATUS 0x10 215 #define DNS_FLAG1_OPCODE_INVERSE 0x08 216 #define DNS_FLAG1_OPCODE_STANDARD 0x00 217 #define DNS_FLAG1_AUTHORATIVE 0x04 218 #define DNS_FLAG1_TRUNC 0x02 219 #define DNS_FLAG1_RD 0x01 220 #define DNS_FLAG2_RA 0x80 221 #define DNS_FLAG2_ERR_MASK 0x0f 222 #define DNS_FLAG2_ERR_NONE 0x00 223 #define DNS_FLAG2_ERR_NAME 0x03 224 uint16_t numquestions;
242 #define STATE_UNUSED 0 243 #define STATE_ERROR 1 245 #define STATE_ASKING 3 252 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 253 unsigned long expiration;
258 #if RESOLV_CONF_SUPPORTS_MDNS 259 int is_mdns:1, is_probe:1;
261 char name[RESOLV_CONF_MAX_DOMAIN_NAME_SIZE + 1];
264 #ifndef UIP_CONF_RESOLV_ENTRIES 265 #define RESOLV_ENTRIES 4 267 #define RESOLV_ENTRIES UIP_CONF_RESOLV_ENTRIES 270 static struct namemap names[RESOLV_ENTRIES];
272 static uint8_t seqno;
276 static struct etimer retry;
280 PROCESS(resolv_process,
"DNS resolver");
282 static void resolv_found(
char *name, uip_ipaddr_t *
ipaddr);
285 struct dns_question {
290 #if RESOLV_CONF_SUPPORTS_MDNS 291 static char resolv_hostname[RESOLV_CONF_MAX_DOMAIN_NAME_SIZE + 1];
294 MDNS_STATE_WAIT_BEFORE_PROBE,
299 static uint8_t mdns_state;
301 static const uip_ipaddr_t resolv_mdns_addr =
302 { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb } };
306 static int mdns_needs_host_announce;
308 PROCESS(mdns_probe_process,
"mDNS probe");
312 #if RESOLV_VERIFY_ANSWER_NAMES || VERBOSE_DEBUG 321 decode_name(
const unsigned char *query,
char *dest,
322 const unsigned char *packet)
324 int len = RESOLV_CONF_MAX_DOMAIN_NAME_SIZE;
326 unsigned char n = *query++;
332 const uint16_t offset = query[0] + ((n & ~0xC0) << 8);
335 query = packet + offset;
370 dns_name_isequal(
const unsigned char *queryptr,
const char *name,
371 const unsigned char *packet)
373 unsigned char n = *queryptr++;
380 queryptr = packet + queryptr[0] + ((n & ~0xC0) << 8);
389 if(tolower((
unsigned int)*name++) != tolower((
unsigned int)*queryptr++)) {
396 if((n != 0) && (*name++ !=
'.')) {
410 static unsigned char *
411 skip_name(
unsigned char *query)
415 DEBUG_PRINTF(
"resolver: skip name: ");
420 DEBUG_PRINTF(
"<skip-to-%d>", query[0] + ((n & ~0xC0) << 8));
428 DEBUG_PRINTF(
"%c", *query);
433 }
while(*query != 0);
440 static unsigned char *
441 encode_name(
unsigned char *query,
const char *nameptr)
451 nptr = (
char *)query;
453 for(n = 0; *nameptr !=
'.' && *nameptr != 0; ++nameptr) {
459 }
while(*nameptr != 0);
467 #if RESOLV_CONF_SUPPORTS_MDNS 471 mdns_announce_requested(
void)
473 mdns_needs_host_announce = 1;
479 start_name_collision_check(clock_time_t after)
487 static unsigned char *
488 mdns_write_announce_records(
unsigned char *queryptr, uint8_t *count)
492 for(i = 0; i < UIP_DS6_ADDR_NB; ++i) {
494 #
if !RESOLV_CONF_MDNS_INCLUDE_GLOBAL_V6_ADDRS
499 queryptr = encode_name(queryptr, resolv_hostname);
503 *queryptr++ =
sizeof(
struct dns_hdr);
506 *queryptr++ = (uint8_t) ((NATIVE_DNS_TYPE) >> 8);
507 *queryptr++ = (uint8_t) ((NATIVE_DNS_TYPE));
509 *queryptr++ = (uint8_t) ((DNS_CLASS_IN | 0x8000) >> 8);
510 *queryptr++ = (uint8_t) ((DNS_CLASS_IN | 0x8000));
518 *queryptr++ =
sizeof(uip_ipaddr_t);
521 queryptr +=
sizeof(uip_ipaddr_t);
532 mdns_prep_host_announce_packet(
void)
534 static const struct {
549 sizeof(
struct dns_hdr),
560 unsigned char *queryptr;
562 uint8_t total_answers = 0;
566 struct dns_hdr *hdr = (
struct dns_hdr *)
uip_appdata;
569 memset((
void *)hdr, 0,
sizeof(*hdr));
571 hdr->flags1 |= DNS_FLAG1_RESPONSE | DNS_FLAG1_AUTHORATIVE;
573 queryptr = (
unsigned char *)
uip_appdata +
sizeof(*hdr);
575 queryptr = mdns_write_announce_records(queryptr, &total_answers);
581 queryptr = encode_name(queryptr, resolv_hostname);
585 *queryptr++ =
sizeof(*hdr);
588 memcpy((
void *)queryptr, (
void *)&nsec_record,
sizeof(nsec_record));
590 queryptr +=
sizeof(nsec_record);
594 ((uint8_t*)&hdr->numanswers)[1] = total_answers;
595 ((uint8_t*)&hdr->numextrarr)[1] = 1;
602 try_next_server(
struct namemap *namemapptr)
605 printf(
"server %d\n", namemapptr->server);
607 namemapptr->server++;
609 namemapptr->retries = 0;
612 namemapptr->server = 0;
627 register struct dns_hdr *hdr;
629 register struct namemap *namemapptr;
631 for(i = 0; i < RESOLV_ENTRIES; ++i) {
632 namemapptr = &names[i];
633 if(namemapptr->state == STATE_NEW || namemapptr->state == STATE_ASKING) {
635 if(namemapptr->state == STATE_ASKING) {
636 if(--namemapptr->tmr == 0) {
637 #if RESOLV_CONF_SUPPORTS_MDNS 638 if(++namemapptr->retries ==
639 (namemapptr->is_mdns ? RESOLV_CONF_MAX_MDNS_RETRIES :
642 if(++namemapptr->retries == RESOLV_CONF_MAX_RETRIES)
647 if(try_next_server(namemapptr) == 0) {
649 namemapptr->state = STATE_ERROR;
651 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 656 resolv_found(namemapptr->name, NULL);
660 namemapptr->tmr = namemapptr->retries * namemapptr->retries * 3;
662 #if RESOLV_CONF_SUPPORTS_MDNS 663 if(namemapptr->is_probe) {
675 namemapptr->state = STATE_ASKING;
677 namemapptr->retries = 0;
680 memset(hdr, 0,
sizeof(
struct dns_hdr));
682 namemapptr->id = hdr->id;
683 #if RESOLV_CONF_SUPPORTS_MDNS 684 if(!namemapptr->is_mdns || namemapptr->is_probe) {
685 hdr->flags1 = DNS_FLAG1_RD;
687 if(namemapptr->is_mdns) {
691 hdr->flags1 = DNS_FLAG1_RD;
694 query = (
unsigned char *)
uip_appdata +
sizeof(*hdr);
695 query = encode_name(query, namemapptr->name);
696 #if RESOLV_CONF_SUPPORTS_MDNS 697 if(namemapptr->is_probe) {
698 *query++ = (uint8_t) ((DNS_TYPE_ANY) >> 8);
699 *query++ = (uint8_t) ((DNS_TYPE_ANY));
703 *query++ = (uint8_t) ((NATIVE_DNS_TYPE) >> 8);
704 *query++ = (uint8_t) ((NATIVE_DNS_TYPE));
706 *query++ = (uint8_t) ((DNS_CLASS_IN) >> 8);
707 *query++ = (uint8_t) ((DNS_CLASS_IN));
708 #if RESOLV_CONF_SUPPORTS_MDNS 709 if(namemapptr->is_mdns) {
710 if(namemapptr->is_probe) {
718 query = mdns_write_announce_records(query, &count);
723 &resolv_mdns_addr,
UIP_HTONS(MDNS_PORT));
725 PRINTF(
"resolver: (i=%d) Sent MDNS %s for \"%s\".\n", i,
726 namemapptr->is_probe?
"probe":
"request",namemapptr->name);
730 (
const uip_ipaddr_t *)
734 PRINTF(
"resolver: (i=%d) Sent DNS request for \"%s\".\n", i,
738 uip_udp_packet_sendto(resolv_conn, uip_appdata,
739 (query - (uint8_t *) uip_appdata),
742 PRINTF(
"resolver: (i=%d) Sent DNS request for \"%s\".\n", i,
756 uint8_t nquestions, nanswers;
760 register struct namemap *namemapptr = NULL;
762 struct dns_answer *ans;
764 register struct dns_hdr const *hdr = (
struct dns_hdr *)uip_appdata;
766 unsigned char *queryptr = (
unsigned char *)hdr +
sizeof(*hdr);
768 const uint8_t is_request = ((hdr->flags1 & ~1) == 0) && (hdr->flags2 == 0);
773 nquestions = (uint8_t) uip_ntohs(hdr->numquestions);
774 nanswers = (uint8_t) uip_ntohs(hdr->numanswers);
776 queryptr = (
unsigned char *)hdr +
sizeof(*hdr);
780 (
"resolver: flags1=0x%02X flags2=0x%02X nquestions=%d, nanswers=%d, nauthrr=%d, nextrarr=%d\n",
781 hdr->flags1, hdr->flags2, (uint8_t) nquestions, (uint8_t) nanswers,
782 (uint8_t) uip_ntohs(hdr->numauthrr),
783 (uint8_t) uip_ntohs(hdr->numextrarr));
785 if(is_request && (nquestions == 0)) {
787 DEBUG_PRINTF(
"resolver: Skipping request with no questions.\n");
793 for(; nquestions > 0;
794 queryptr = skip_name(queryptr) +
sizeof(
struct dns_question),
797 #if RESOLV_CONF_SUPPORTS_MDNS 807 struct dns_question *question = (
struct dns_question *)skip_name(queryptr);
809 #if !ARCH_DOESNT_NEED_ALIGNED_STRUCTS 810 static struct dns_question aligned;
811 memcpy(&aligned, question,
sizeof(aligned));
815 DEBUG_PRINTF(
"resolver: Question %d: type=%d class=%d\n", ++i,
818 if(((uip_ntohs(question->class) & 0x7FFF) != DNS_CLASS_IN) ||
819 ((question->type !=
UIP_HTONS(DNS_TYPE_ANY)) &&
820 (question->type !=
UIP_HTONS(NATIVE_DNS_TYPE)))) {
825 if(!dns_name_isequal(queryptr, resolv_hostname, uip_appdata)) {
829 PRINTF(
"resolver: THIS IS A REQUEST FOR US!!!\n");
831 if(mdns_state == MDNS_STATE_READY) {
835 if(UIP_UDP_BUF->srcport ==
UIP_HTONS(MDNS_PORT)) {
836 mdns_announce_requested();
838 uip_udp_packet_sendto(resolv_conn, uip_appdata,
839 mdns_prep_host_announce_packet(),
840 &UIP_UDP_BUF->srcipaddr,
841 UIP_UDP_BUF->srcport);
846 PRINTF(
"resolver: But we are still probing. Waiting...\n");
851 nauthrr = (uint8_t)uip_ntohs(hdr->numauthrr);
873 #if RESOLV_CONF_SUPPORTS_MDNS 874 if(UIP_UDP_BUF->srcport ==
UIP_HTONS(MDNS_PORT) &&
885 for(i = 0; i < RESOLV_ENTRIES; ++i) {
886 namemapptr = &names[i];
887 if(namemapptr->state == STATE_ASKING &&
888 namemapptr->id == hdr->id) {
893 if(i >= RESOLV_ENTRIES || i < 0 || namemapptr->state != STATE_ASKING) {
894 PRINTF(
"resolver: DNS response has bad ID (%04X) \n", uip_ntohs(hdr->id));
898 PRINTF(
"resolver: Incoming response for \"%s\".\n", namemapptr->name);
901 namemapptr->state = STATE_ERROR;
903 namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
905 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 911 if(namemapptr->err != 0) {
912 namemapptr->state = STATE_ERROR;
913 resolv_found(namemapptr->name, NULL);
921 while(nanswers > 0) {
922 ans = (
struct dns_answer *)skip_name(queryptr);
924 #if !ARCH_DOESNT_NEED_ALIGNED_STRUCTS 926 static struct dns_answer aligned;
927 memcpy(&aligned, ans,
sizeof(aligned));
934 decode_name(queryptr, debug_name, uip_appdata);
935 DEBUG_PRINTF(
"resolver: Answer %d: \"%s\", type %d, class %d, ttl %d, length %d\n",
936 ++i, debug_name, uip_ntohs(ans->type),
937 uip_ntohs(ans->class) & 0x7FFF,
938 (
int)((uint32_t) uip_ntohs(ans->ttl[0]) << 16) | (uint32_t)
939 uip_ntohs(ans->ttl[1]), uip_ntohs(ans->len));
945 if(((uip_ntohs(ans->class) & 0x7FFF) != DNS_CLASS_IN) ||
946 (ans->len !=
UIP_HTONS(
sizeof(uip_ipaddr_t)))) {
947 goto skip_to_next_answer;
950 if(ans->type !=
UIP_HTONS(NATIVE_DNS_TYPE)) {
951 goto skip_to_next_answer;
954 #if RESOLV_CONF_SUPPORTS_MDNS 955 if(UIP_UDP_BUF->srcport ==
UIP_HTONS(MDNS_PORT) &&
957 int8_t available_i = RESOLV_ENTRIES;
959 DEBUG_PRINTF(
"resolver: MDNS query.\n");
964 for(i = 0; i < RESOLV_ENTRIES; ++i) {
965 namemapptr = &names[i];
966 if(dns_name_isequal(queryptr, namemapptr->name, uip_appdata)) {
969 if((namemapptr->state == STATE_UNUSED)
970 #
if RESOLV_SUPPORTS_RECORD_EXPIRATION
971 || (namemapptr->state == STATE_DONE &&
clock_seconds() > namemapptr->expiration)
977 if(i == RESOLV_ENTRIES) {
978 DEBUG_PRINTF(
"resolver: Unsolicited MDNS response.\n");
980 namemapptr = &names[i];
981 if(!decode_name(queryptr, namemapptr->name, uip_appdata)) {
982 DEBUG_PRINTF(
"resolver: MDNS name too big to cache.\n");
984 goto skip_to_next_answer;
987 if(i == RESOLV_ENTRIES) {
989 (
"resolver: Not enough room to keep track of unsolicited MDNS answer.\n");
991 if(dns_name_isequal(queryptr, resolv_hostname, uip_appdata)) {
993 resolv_found(resolv_hostname, (uip_ipaddr_t *) ans->ipaddr);
996 goto skip_to_next_answer;
998 namemapptr = &names[i];
1016 DEBUG_PRINTF(
"resolver: Answer for \"%s\" is usable.\n", namemapptr->name);
1018 namemapptr->state = STATE_DONE;
1019 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 1020 namemapptr->expiration = (uint32_t) uip_ntohs(ans->ttl[0]) << 16 |
1021 (uint32_t) uip_ntohs(ans->ttl[1]);
1027 resolv_found(namemapptr->name, &namemapptr->ipaddr);
1030 skip_to_next_answer:
1031 queryptr = (
unsigned char *)skip_name(queryptr) + 10 +
uip_htons(ans->len);
1037 #if RESOLV_CONF_SUPPORTS_MDNS 1038 if(nanswers == 0 && UIP_UDP_BUF->srcport !=
UIP_HTONS(MDNS_PORT)
1044 if(try_next_server(namemapptr)) {
1045 namemapptr->state = STATE_ASKING;
1046 process_post(&resolv_process, PROCESS_EVENT_TIMER, NULL);
1052 #if RESOLV_CONF_SUPPORTS_MDNS 1060 strncpy(resolv_hostname, hostname, RESOLV_CONF_MAX_DOMAIN_NAME_SIZE);
1063 if(strlen(resolv_hostname) < 7 ||
1064 strcasecmp(resolv_hostname + strlen(resolv_hostname) - 6,
".local") != 0) {
1065 strncat(resolv_hostname,
".local", RESOLV_CONF_MAX_DOMAIN_NAME_SIZE - strlen(resolv_hostname));
1068 PRINTF(
"resolver: hostname changed to \"%s\"\n", resolv_hostname);
1070 start_name_collision_check(0);
1080 return resolv_hostname;
1088 static struct etimer delay;
1091 mdns_state = MDNS_STATE_WAIT_BEFORE_PROBE;
1093 PRINTF(
"mdns-probe: Process (re)started.\n");
1097 PRINTF(
"mdns-probe: Probing will begin in %ld clocks.\n",
1098 (
long)*(clock_time_t *) data);
1109 mdns_state = MDNS_STATE_PROBING;
1114 }
while(strcasecmp(resolv_hostname, data) != 0);
1116 mdns_state = MDNS_STATE_READY;
1117 mdns_announce_requested();
1119 PRINTF(
"mdns-probe: Finished probing.\n");
1132 memset(names, 0,
sizeof(names));
1136 PRINTF(
"resolver: Process started.\n");
1138 resolv_conn =
udp_new(NULL, 0, NULL);
1140 #if RESOLV_CONF_SUPPORTS_MDNS 1141 PRINTF(
"resolver: Supports MDNS.\n");
1144 uip_ds6_maddr_add(&resolv_mdns_addr);
1152 if(ev == PROCESS_EVENT_TIMER) {
1160 #if RESOLV_CONF_SUPPORTS_MDNS 1161 if(mdns_needs_host_announce) {
1164 PRINTF(
"resolver: Announcing that we are \"%s\".\n",
1167 memset(uip_appdata, 0,
sizeof(
struct dns_hdr));
1169 len = mdns_prep_host_announce_packet();
1171 uip_udp_packet_sendto(resolv_conn, uip_appdata,
1172 len, &resolv_mdns_addr,
UIP_HTONS(MDNS_PORT));
1174 mdns_needs_host_announce = 0;
1189 #if RESOLV_CONF_SUPPORTS_MDNS 1190 if(mdns_needs_host_announce) {
1202 static uint8_t initialized = 0;
1209 #if RESOLV_AUTO_REMOVE_TRAILING_DOTS 1211 remove_trailing_dots(
const char *name) {
1212 static char dns_name_without_dots[RESOLV_CONF_MAX_DOMAIN_NAME_SIZE + 1];
1213 size_t len = strlen(name);
1215 if(len && name[len - 1] ==
'.') {
1216 strncpy(dns_name_without_dots, name, RESOLV_CONF_MAX_DOMAIN_NAME_SIZE);
1217 while(len && (dns_name_without_dots[len - 1] ==
'.')) {
1218 dns_name_without_dots[--len] = 0;
1220 name = dns_name_without_dots;
1225 #define remove_trailing_dots(x) (x) 1238 uint8_t lseq, lseqi;
1240 register struct namemap *nameptr = 0;
1247 name = remove_trailing_dots(name);
1249 for(i = 0; i < RESOLV_ENTRIES; ++i) {
1250 nameptr = &names[i];
1251 if(0 == strcasecmp(nameptr->name, name)) {
1254 if((nameptr->state == STATE_UNUSED)
1255 #
if RESOLV_SUPPORTS_RECORD_EXPIRATION
1256 || (nameptr->state == STATE_DONE &&
clock_seconds() > nameptr->expiration)
1261 }
else if(seqno - nameptr->seqno > lseq) {
1262 lseq = seqno - nameptr->seqno;
1267 if(i == RESOLV_ENTRIES) {
1269 nameptr = &names[i];
1272 PRINTF(
"resolver: Starting query for \"%s\".\n", name);
1274 memset(nameptr, 0,
sizeof(*nameptr));
1276 strncpy(nameptr->name, name,
sizeof(nameptr->name) - 1);
1277 nameptr->state = STATE_NEW;
1278 nameptr->seqno = seqno;
1281 #if RESOLV_CONF_SUPPORTS_MDNS 1283 size_t name_len = strlen(name);
1285 const char local_suffix[] =
"local";
1287 if((name_len > (
sizeof(local_suffix) - 1)) &&
1288 (0 == strcasecmp(name + name_len - (
sizeof(local_suffix) - 1), local_suffix))) {
1289 PRINTF(
"resolver: Using MDNS to look up \"%s\".\n", name);
1290 nameptr->is_mdns = 1;
1292 nameptr->is_mdns = 0;
1295 nameptr->is_probe = (mdns_state == MDNS_STATE_PROBING) &&
1296 (0 == strcmp(nameptr->name, resolv_hostname));
1319 struct namemap *nameptr;
1322 name = remove_trailing_dots(name);
1324 #if UIP_CONF_LOOPBACK_INTERFACE 1325 if(strcmp(name,
"localhost")) {
1326 static uip_ipaddr_t loopback =
1327 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } };
1330 *ipaddr = &loopback;
1337 for(i = 0; i < RESOLV_ENTRIES; ++i) {
1338 nameptr = &names[i];
1340 if(strcasecmp(name, nameptr->name) == 0) {
1341 switch (nameptr->state) {
1344 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 1357 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 1366 *ipaddr = &nameptr->ipaddr;
1378 PRINTF(
"resolver: Found \"%s\" in cache.\n", name);
1382 (
"resolver: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x \n",
1383 ((uint8_t *) addr)[0], ((uint8_t *) addr)[1], ((uint8_t *) addr)[2],
1384 ((uint8_t *) addr)[3], ((uint8_t *) addr)[4], ((uint8_t *) addr)[5],
1385 ((uint8_t *) addr)[6], ((uint8_t *) addr)[7], ((uint8_t *) addr)[8],
1386 ((uint8_t *) addr)[9], ((uint8_t *) addr)[10],
1387 ((uint8_t *) addr)[11], ((uint8_t *) addr)[12],
1388 ((uint8_t *) addr)[13], ((uint8_t *) addr)[14],
1389 ((uint8_t *) addr)[15]);
1393 DEBUG_PRINTF(
"resolver: \"%s\" is NOT cached.\n", name);
1406 resolv_found(
char *name, uip_ipaddr_t *
ipaddr)
1408 #if RESOLV_CONF_SUPPORTS_MDNS 1409 if(strncasecmp(resolv_hostname, name, strlen(resolv_hostname)) == 0 &&
1411 && !uip_ds6_is_my_addr(ipaddr)
1415 if(mdns_state == MDNS_STATE_PROBING) {
1419 PRINTF(
"resolver: Name collision detected for \"%s\".\n", name);
1422 resolv_hostname[strlen(resolv_hostname) - 6] = 0;
1425 for(i = 0; i < 3; ++i) {
1428 char append_str[4] =
"-XX";
1430 append_str[2] = (((val & 0xF) > 9) ?
'a' :
'0') + (val & 0xF);
1432 append_str[1] = (((val & 0xF) > 9) ?
'a' :
'0') + (val & 0xF);
1433 strncat(resolv_hostname, append_str,
1434 sizeof(resolv_hostname) - strlen(resolv_hostname) - 1);
1438 strncat(resolv_hostname,
".local", RESOLV_CONF_MAX_DOMAIN_NAME_SIZE - strlen(resolv_hostname));
1441 }
else if(mdns_state == MDNS_STATE_READY) {
1446 PRINTF(
"resolver: Possible name collision, probing...\n");
1447 start_name_collision_check(0);
1455 PRINTF(
"resolver: Found address for \"%s\".\n", name);
1457 (
"resolver: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x \n",
1458 ((uint8_t *) ipaddr)[0], ((uint8_t *) ipaddr)[1],
1459 ((uint8_t *) ipaddr)[2], ((uint8_t *) ipaddr)[3],
1460 ((uint8_t *) ipaddr)[4], ((uint8_t *) ipaddr)[5],
1461 ((uint8_t *) ipaddr)[6], ((uint8_t *) ipaddr)[7],
1462 ((uint8_t *) ipaddr)[8], ((uint8_t *) ipaddr)[9],
1463 ((uint8_t *) ipaddr)[10], ((uint8_t *) ipaddr)[11],
1464 ((uint8_t *) ipaddr)[12], ((uint8_t *) ipaddr)[13],
1465 ((uint8_t *) ipaddr)[14], ((uint8_t *) ipaddr)[15]);
1467 PRINTF(
"resolver: Unable to retrieve address for \"%s\".\n", name);
process_event_t resolv_event_found
Event that is broadcasted when a DNS name has been resolved.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
uip_lladdr_t uip_lladdr
Host L2 address.
Header for the Contiki/uIP interface.
#define PROCESS(name, strname)
Declare a process.
void tcpip_poll_udp(struct uip_udp_conn *conn)
Cause a specified UDP connection to be polled.
Hostname is fresh and usable.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
uIP DNS resolver code header file.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define PROCESS_END()
Define the end of a process.
process_event_t tcpip_event
The uIP event.
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
static void newdata(void)
Hostname was found, but it's status has expired.
unsigned long clock_seconds(void)
Get the current value of the platform seconds.
Header file for module for sending UDP packets through uIP.
#define RESOLV_CONF_MAX_RETRIES
The maximum number of retries when asking for a name.
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Header file for IPv6-related data structures.
void process_exit(struct process *p)
Cause a process to exit.
#define UIP_LLADDR_LEN
802.15.4 address
void resolv_set_hostname(const char *hostname)
Changes the local hostname advertised by MDNS.
#define CLOCK_SECOND
A second, measured in system clock time.
resolv_status_t resolv_lookup(const char *name, uip_ipaddr_t **ipaddr)
Look up a hostname in the array of known hostnames.
#define uip_newdata()
Is new incoming data available?
struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
uIP Name Server interface
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
This hostname is in the process of being resolved.
process_event_t process_alloc_event(void)
Allocate a global event number.
Hostname was not found in the cache.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
uip_ds6_netif_t uip_ds6_if
The single interface.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
const char * resolv_get_hostname(void)
Returns the local hostname being advertised via MDNS.
#define uip_udp_bind(conn, port)
Bind a UDP connection to a local port.
#define uip_poll()
Is the connection being polled by uIP?
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
void resolv_query(const char *name)
Queues a name so that a question for the name will be sent out.
uip_ipaddr_t * uip_nameserver_get(uint8_t num)
Get a Nameserver ip address given in RA.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
void * uip_appdata
Pointer to the application data in the packet buffer.
void process_start(struct process *p, process_data_t data)
Start a process.
Representation of a uIP UDP connection.
The server has returned a not-found response for this domain name.