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);
123 #ifndef RESOLV_CONF_SUPPORTS_MDNS 124 #define RESOLV_CONF_SUPPORTS_MDNS 1 127 #ifndef RESOLV_CONF_MDNS_INCLUDE_GLOBAL_V6_ADDRS 128 #define RESOLV_CONF_MDNS_INCLUDE_GLOBAL_V6_ADDRS 0 132 #ifndef RESOLV_CONF_MAX_RETRIES 133 #define RESOLV_CONF_MAX_RETRIES 4 136 #ifndef RESOLV_CONF_MAX_MDNS_RETRIES 137 #define RESOLV_CONF_MAX_MDNS_RETRIES 3 140 #ifndef RESOLV_CONF_MAX_DOMAIN_NAME_SIZE 141 #define RESOLV_CONF_MAX_DOMAIN_NAME_SIZE 32 144 #ifdef RESOLV_CONF_AUTO_REMOVE_TRAILING_DOTS 145 #define RESOLV_AUTO_REMOVE_TRAILING_DOTS RESOLV_CONF_AUTO_REMOVE_TRAILING_DOTS 147 #define RESOLV_AUTO_REMOVE_TRAILING_DOTS RESOLV_CONF_SUPPORTS_MDNS 150 #ifdef RESOLV_CONF_VERIFY_ANSWER_NAMES 151 #define RESOLV_VERIFY_ANSWER_NAMES RESOLV_CONF_VERIFY_ANSWER_NAMES 153 #define RESOLV_VERIFY_ANSWER_NAMES RESOLV_CONF_SUPPORTS_MDNS 156 #ifdef RESOLV_CONF_SUPPORTS_RECORD_EXPIRATION 157 #define RESOLV_SUPPORTS_RECORD_EXPIRATION RESOLV_CONF_SUPPORTS_RECORD_EXPIRATION 159 #define RESOLV_SUPPORTS_RECORD_EXPIRATION 1 162 #if RESOLV_CONF_SUPPORTS_MDNS && !RESOLV_VERIFY_ANSWER_NAMES 163 #error RESOLV_CONF_SUPPORTS_MDNS cannot be set without RESOLV_CONF_VERIFY_ANSWER_NAMES 166 #if !defined(CONTIKI_TARGET_NAME) && defined(BOARD) 167 #define stringy2(x) #x 168 #define stringy(x) stringy2(x) 169 #define CONTIKI_TARGET_NAME stringy(BOARD) 172 #ifndef CONTIKI_CONF_DEFAULT_HOSTNAME 173 #ifdef CONTIKI_TARGET_NAME 174 #define CONTIKI_CONF_DEFAULT_HOSTNAME "contiki-"CONTIKI_TARGET_NAME 176 #define CONTIKI_CONF_DEFAULT_HOSTNAME "contiki" 181 #define DNS_TYPE_CNAME 5 182 #define DNS_TYPE_PTR 12 183 #define DNS_TYPE_MX 15 184 #define DNS_TYPE_TXT 16 185 #define DNS_TYPE_AAAA 28 186 #define DNS_TYPE_SRV 33 187 #define DNS_TYPE_ANY 255 188 #define DNS_TYPE_NSEC 47 190 #define NATIVE_DNS_TYPE DNS_TYPE_AAAA 192 #define DNS_CLASS_IN 1 193 #define DNS_CLASS_ANY 255 200 #define MDNS_PORT 5353 203 #ifndef MDNS_RESPONDER_PORT 204 #define MDNS_RESPONDER_PORT 5354 210 uint8_t flags1, flags2;
211 #define DNS_FLAG1_RESPONSE 0x80 212 #define DNS_FLAG1_OPCODE_STATUS 0x10 213 #define DNS_FLAG1_OPCODE_INVERSE 0x08 214 #define DNS_FLAG1_OPCODE_STANDARD 0x00 215 #define DNS_FLAG1_AUTHORATIVE 0x04 216 #define DNS_FLAG1_TRUNC 0x02 217 #define DNS_FLAG1_RD 0x01 218 #define DNS_FLAG2_RA 0x80 219 #define DNS_FLAG2_ERR_MASK 0x0f 220 #define DNS_FLAG2_ERR_NONE 0x00 221 #define DNS_FLAG2_ERR_NAME 0x03 222 uint16_t numquestions;
240 #define STATE_UNUSED 0 241 #define STATE_ERROR 1 243 #define STATE_ASKING 3 250 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 251 unsigned long expiration;
256 #if RESOLV_CONF_SUPPORTS_MDNS 257 int is_mdns:1, is_probe:1;
259 char name[RESOLV_CONF_MAX_DOMAIN_NAME_SIZE + 1];
262 #ifndef UIP_CONF_RESOLV_ENTRIES 263 #define RESOLV_ENTRIES 4 265 #define RESOLV_ENTRIES UIP_CONF_RESOLV_ENTRIES 268 static struct namemap names[RESOLV_ENTRIES];
270 static uint8_t seqno;
274 static struct etimer retry;
278 PROCESS(resolv_process,
"DNS resolver");
280 static void resolv_found(
char *name, uip_ipaddr_t *
ipaddr);
283 struct dns_question {
288 #if RESOLV_CONF_SUPPORTS_MDNS 289 static char resolv_hostname[RESOLV_CONF_MAX_DOMAIN_NAME_SIZE + 1];
292 MDNS_STATE_WAIT_BEFORE_PROBE,
297 static uint8_t mdns_state;
299 static const uip_ipaddr_t resolv_mdns_addr =
300 { { 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb } };
304 static int mdns_needs_host_announce;
306 PROCESS(mdns_probe_process,
"mDNS probe");
310 #if RESOLV_VERIFY_ANSWER_NAMES || VERBOSE_DEBUG 319 decode_name(
const unsigned char *query,
char *dest,
320 const unsigned char *packet)
322 int len = RESOLV_CONF_MAX_DOMAIN_NAME_SIZE;
324 unsigned char n = *query++;
330 const uint16_t offset = query[0] + ((n & ~0xC0) << 8);
333 query = packet + offset;
368 dns_name_isequal(
const unsigned char *queryptr,
const char *name,
369 const unsigned char *packet)
371 unsigned char n = *queryptr++;
378 queryptr = packet + queryptr[0] + ((n & ~0xC0) << 8);
387 if(tolower((
unsigned int)*name++) != tolower((
unsigned int)*queryptr++)) {
394 if((n != 0) && (*name++ !=
'.')) {
408 static unsigned char *
409 skip_name(
unsigned char *query)
413 DEBUG_PRINTF(
"resolver: skip name: ");
418 DEBUG_PRINTF(
"<skip-to-%d>", query[0] + ((n & ~0xC0) << 8));
426 DEBUG_PRINTF(
"%c", *query);
431 }
while(*query != 0);
438 static unsigned char *
439 encode_name(
unsigned char *query,
const char *nameptr)
449 nptr = (
char *)query;
451 for(n = 0; *nameptr !=
'.' && *nameptr != 0; ++nameptr) {
457 }
while(*nameptr != 0);
465 #if RESOLV_CONF_SUPPORTS_MDNS 469 mdns_announce_requested(
void)
471 mdns_needs_host_announce = 1;
477 start_name_collision_check(clock_time_t after)
485 static unsigned char *
486 mdns_write_announce_records(
unsigned char *queryptr, uint8_t *count)
490 for(i = 0; i < UIP_DS6_ADDR_NB; ++i) {
492 #
if !RESOLV_CONF_MDNS_INCLUDE_GLOBAL_V6_ADDRS
497 queryptr = encode_name(queryptr, resolv_hostname);
501 *queryptr++ =
sizeof(
struct dns_hdr);
504 *queryptr++ = (uint8_t) ((NATIVE_DNS_TYPE) >> 8);
505 *queryptr++ = (uint8_t) ((NATIVE_DNS_TYPE));
507 *queryptr++ = (uint8_t) ((DNS_CLASS_IN | 0x8000) >> 8);
508 *queryptr++ = (uint8_t) ((DNS_CLASS_IN | 0x8000));
516 *queryptr++ =
sizeof(uip_ipaddr_t);
519 queryptr +=
sizeof(uip_ipaddr_t);
530 mdns_prep_host_announce_packet(
void)
532 static const struct {
547 sizeof(
struct dns_hdr),
558 unsigned char *queryptr;
560 uint8_t total_answers = 0;
564 struct dns_hdr *hdr = (
struct dns_hdr *)
uip_appdata;
567 memset((
void *)hdr, 0,
sizeof(*hdr));
569 hdr->flags1 |= DNS_FLAG1_RESPONSE | DNS_FLAG1_AUTHORATIVE;
571 queryptr = (
unsigned char *)
uip_appdata +
sizeof(*hdr);
573 queryptr = mdns_write_announce_records(queryptr, &total_answers);
579 queryptr = encode_name(queryptr, resolv_hostname);
583 *queryptr++ =
sizeof(*hdr);
586 memcpy((
void *)queryptr, (
void *)&nsec_record,
sizeof(nsec_record));
588 queryptr +=
sizeof(nsec_record);
592 ((uint8_t*)&hdr->numanswers)[1] = total_answers;
593 ((uint8_t*)&hdr->numextrarr)[1] = 1;
600 try_next_server(
struct namemap *namemapptr)
603 printf(
"server %d\n", namemapptr->server);
605 namemapptr->server++;
607 namemapptr->retries = 0;
610 namemapptr->server = 0;
625 register struct dns_hdr *hdr;
627 register struct namemap *namemapptr;
629 for(i = 0; i < RESOLV_ENTRIES; ++i) {
630 namemapptr = &names[i];
631 if(namemapptr->state == STATE_NEW || namemapptr->state == STATE_ASKING) {
633 if(namemapptr->state == STATE_ASKING) {
634 if(--namemapptr->tmr == 0) {
635 #if RESOLV_CONF_SUPPORTS_MDNS 636 if(++namemapptr->retries ==
637 (namemapptr->is_mdns ? RESOLV_CONF_MAX_MDNS_RETRIES :
640 if(++namemapptr->retries == RESOLV_CONF_MAX_RETRIES)
645 if(try_next_server(namemapptr) == 0) {
647 namemapptr->state = STATE_ERROR;
649 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 654 resolv_found(namemapptr->name, NULL);
658 namemapptr->tmr = namemapptr->retries * namemapptr->retries * 3;
660 #if RESOLV_CONF_SUPPORTS_MDNS 661 if(namemapptr->is_probe) {
673 namemapptr->state = STATE_ASKING;
675 namemapptr->retries = 0;
678 memset(hdr, 0,
sizeof(
struct dns_hdr));
680 namemapptr->id = hdr->id;
681 #if RESOLV_CONF_SUPPORTS_MDNS 682 if(!namemapptr->is_mdns || namemapptr->is_probe) {
683 hdr->flags1 = DNS_FLAG1_RD;
685 if(namemapptr->is_mdns) {
689 hdr->flags1 = DNS_FLAG1_RD;
692 query = (
unsigned char *)
uip_appdata +
sizeof(*hdr);
693 query = encode_name(query, namemapptr->name);
694 #if RESOLV_CONF_SUPPORTS_MDNS 695 if(namemapptr->is_probe) {
696 *query++ = (uint8_t) ((DNS_TYPE_ANY) >> 8);
697 *query++ = (uint8_t) ((DNS_TYPE_ANY));
701 *query++ = (uint8_t) ((NATIVE_DNS_TYPE) >> 8);
702 *query++ = (uint8_t) ((NATIVE_DNS_TYPE));
704 *query++ = (uint8_t) ((DNS_CLASS_IN) >> 8);
705 *query++ = (uint8_t) ((DNS_CLASS_IN));
706 #if RESOLV_CONF_SUPPORTS_MDNS 707 if(namemapptr->is_mdns) {
708 if(namemapptr->is_probe) {
716 query = mdns_write_announce_records(query, &count);
721 &resolv_mdns_addr,
UIP_HTONS(MDNS_PORT));
723 PRINTF(
"resolver: (i=%d) Sent MDNS %s for \"%s\".\n", i,
724 namemapptr->is_probe?
"probe":
"request",namemapptr->name);
728 (
const uip_ipaddr_t *)
732 PRINTF(
"resolver: (i=%d) Sent DNS request for \"%s\".\n", i,
736 uip_udp_packet_sendto(resolv_conn, uip_appdata,
737 (query - (uint8_t *) uip_appdata),
740 PRINTF(
"resolver: (i=%d) Sent DNS request for \"%s\".\n", i,
754 uint8_t nquestions, nanswers;
758 register struct namemap *namemapptr = NULL;
760 struct dns_answer *ans;
762 register struct dns_hdr const *hdr = (
struct dns_hdr *)uip_appdata;
764 unsigned char *queryptr = (
unsigned char *)hdr +
sizeof(*hdr);
766 const uint8_t is_request = ((hdr->flags1 & ~1) == 0) && (hdr->flags2 == 0);
771 nquestions = (uint8_t) uip_ntohs(hdr->numquestions);
772 nanswers = (uint8_t) uip_ntohs(hdr->numanswers);
774 queryptr = (
unsigned char *)hdr +
sizeof(*hdr);
778 (
"resolver: flags1=0x%02X flags2=0x%02X nquestions=%d, nanswers=%d, nauthrr=%d, nextrarr=%d\n",
779 hdr->flags1, hdr->flags2, (uint8_t) nquestions, (uint8_t) nanswers,
780 (uint8_t) uip_ntohs(hdr->numauthrr),
781 (uint8_t) uip_ntohs(hdr->numextrarr));
783 if(is_request && (nquestions == 0)) {
785 DEBUG_PRINTF(
"resolver: Skipping request with no questions.\n");
791 for(; nquestions > 0;
792 queryptr = skip_name(queryptr) +
sizeof(
struct dns_question),
795 #if RESOLV_CONF_SUPPORTS_MDNS 805 struct dns_question *question = (
struct dns_question *)skip_name(queryptr);
807 #if !ARCH_DOESNT_NEED_ALIGNED_STRUCTS 808 static struct dns_question aligned;
809 memcpy(&aligned, question,
sizeof(aligned));
813 DEBUG_PRINTF(
"resolver: Question %d: type=%d class=%d\n", ++i,
816 if(((uip_ntohs(question->class) & 0x7FFF) != DNS_CLASS_IN) ||
817 ((question->type !=
UIP_HTONS(DNS_TYPE_ANY)) &&
818 (question->type !=
UIP_HTONS(NATIVE_DNS_TYPE)))) {
823 if(!dns_name_isequal(queryptr, resolv_hostname, uip_appdata)) {
827 PRINTF(
"resolver: THIS IS A REQUEST FOR US!!!\n");
829 if(mdns_state == MDNS_STATE_READY) {
833 if(UIP_UDP_BUF->srcport ==
UIP_HTONS(MDNS_PORT)) {
834 mdns_announce_requested();
836 uip_udp_packet_sendto(resolv_conn, uip_appdata,
837 mdns_prep_host_announce_packet(),
839 UIP_UDP_BUF->srcport);
844 PRINTF(
"resolver: But we are still probing. Waiting...\n");
849 nauthrr = (uint8_t)uip_ntohs(hdr->numauthrr);
871 #if RESOLV_CONF_SUPPORTS_MDNS 872 if(UIP_UDP_BUF->srcport ==
UIP_HTONS(MDNS_PORT) &&
883 for(i = 0; i < RESOLV_ENTRIES; ++i) {
884 namemapptr = &names[i];
885 if(namemapptr->state == STATE_ASKING &&
886 namemapptr->id == hdr->id) {
891 if(i >= RESOLV_ENTRIES || i < 0 || namemapptr->state != STATE_ASKING) {
892 PRINTF(
"resolver: DNS response has bad ID (%04X) \n", uip_ntohs(hdr->id));
896 PRINTF(
"resolver: Incoming response for \"%s\".\n", namemapptr->name);
899 namemapptr->state = STATE_ERROR;
901 namemapptr->err = hdr->flags2 & DNS_FLAG2_ERR_MASK;
903 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 909 if(namemapptr->err != 0) {
910 namemapptr->state = STATE_ERROR;
911 resolv_found(namemapptr->name, NULL);
919 while(nanswers > 0) {
920 ans = (
struct dns_answer *)skip_name(queryptr);
922 #if !ARCH_DOESNT_NEED_ALIGNED_STRUCTS 924 static struct dns_answer aligned;
925 memcpy(&aligned, ans,
sizeof(aligned));
932 decode_name(queryptr, debug_name, uip_appdata);
933 DEBUG_PRINTF(
"resolver: Answer %d: \"%s\", type %d, class %d, ttl %d, length %d\n",
934 ++i, debug_name, uip_ntohs(ans->type),
935 uip_ntohs(ans->class) & 0x7FFF,
936 (
int)((uint32_t) uip_ntohs(ans->ttl[0]) << 16) | (uint32_t)
937 uip_ntohs(ans->ttl[1]), uip_ntohs(ans->len));
943 if(((uip_ntohs(ans->class) & 0x7FFF) != DNS_CLASS_IN) ||
944 (ans->len !=
UIP_HTONS(
sizeof(uip_ipaddr_t)))) {
945 goto skip_to_next_answer;
948 if(ans->type !=
UIP_HTONS(NATIVE_DNS_TYPE)) {
949 goto skip_to_next_answer;
952 #if RESOLV_CONF_SUPPORTS_MDNS 953 if(UIP_UDP_BUF->srcport ==
UIP_HTONS(MDNS_PORT) &&
955 int8_t available_i = RESOLV_ENTRIES;
957 DEBUG_PRINTF(
"resolver: MDNS query.\n");
962 for(i = 0; i < RESOLV_ENTRIES; ++i) {
963 namemapptr = &names[i];
964 if(dns_name_isequal(queryptr, namemapptr->name, uip_appdata)) {
967 if((namemapptr->state == STATE_UNUSED)
968 #
if RESOLV_SUPPORTS_RECORD_EXPIRATION
969 || (namemapptr->state == STATE_DONE &&
clock_seconds() > namemapptr->expiration)
975 if(i == RESOLV_ENTRIES) {
976 DEBUG_PRINTF(
"resolver: Unsolicited MDNS response.\n");
978 namemapptr = &names[i];
979 if(!decode_name(queryptr, namemapptr->name, uip_appdata)) {
980 DEBUG_PRINTF(
"resolver: MDNS name too big to cache.\n");
982 goto skip_to_next_answer;
985 if(i == RESOLV_ENTRIES) {
987 (
"resolver: Not enough room to keep track of unsolicited MDNS answer.\n");
989 if(dns_name_isequal(queryptr, resolv_hostname, uip_appdata)) {
991 resolv_found(resolv_hostname, (uip_ipaddr_t *) ans->ipaddr);
994 goto skip_to_next_answer;
996 namemapptr = &names[i];
1014 DEBUG_PRINTF(
"resolver: Answer for \"%s\" is usable.\n", namemapptr->name);
1016 namemapptr->state = STATE_DONE;
1017 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 1018 namemapptr->expiration = (uint32_t) uip_ntohs(ans->ttl[0]) << 16 |
1019 (uint32_t) uip_ntohs(ans->ttl[1]);
1025 resolv_found(namemapptr->name, &namemapptr->ipaddr);
1028 skip_to_next_answer:
1029 queryptr = (
unsigned char *)skip_name(queryptr) + 10 +
uip_htons(ans->len);
1035 #if RESOLV_CONF_SUPPORTS_MDNS 1036 if(nanswers == 0 && UIP_UDP_BUF->srcport !=
UIP_HTONS(MDNS_PORT)
1042 if(try_next_server(namemapptr)) {
1043 namemapptr->state = STATE_ASKING;
1044 process_post(&resolv_process, PROCESS_EVENT_TIMER, NULL);
1050 #if RESOLV_CONF_SUPPORTS_MDNS 1058 strncpy(resolv_hostname, hostname, RESOLV_CONF_MAX_DOMAIN_NAME_SIZE);
1061 if(strlen(resolv_hostname) < 7 ||
1062 strcasecmp(resolv_hostname + strlen(resolv_hostname) - 6,
".local") != 0) {
1063 strncat(resolv_hostname,
".local", RESOLV_CONF_MAX_DOMAIN_NAME_SIZE - strlen(resolv_hostname));
1066 PRINTF(
"resolver: hostname changed to \"%s\"\n", resolv_hostname);
1068 start_name_collision_check(0);
1078 return resolv_hostname;
1086 static struct etimer delay;
1089 mdns_state = MDNS_STATE_WAIT_BEFORE_PROBE;
1091 PRINTF(
"mdns-probe: Process (re)started.\n");
1095 PRINTF(
"mdns-probe: Probing will begin in %ld clocks.\n",
1096 (
long)*(clock_time_t *) data);
1107 mdns_state = MDNS_STATE_PROBING;
1112 }
while(strcasecmp(resolv_hostname, data) != 0);
1114 mdns_state = MDNS_STATE_READY;
1115 mdns_announce_requested();
1117 PRINTF(
"mdns-probe: Finished probing.\n");
1130 memset(names, 0,
sizeof(names));
1134 PRINTF(
"resolver: Process started.\n");
1136 resolv_conn =
udp_new(NULL, 0, NULL);
1138 #if RESOLV_CONF_SUPPORTS_MDNS 1139 PRINTF(
"resolver: Supports MDNS.\n");
1142 uip_ds6_maddr_add(&resolv_mdns_addr);
1150 if(ev == PROCESS_EVENT_TIMER) {
1158 #if RESOLV_CONF_SUPPORTS_MDNS 1159 if(mdns_needs_host_announce) {
1162 PRINTF(
"resolver: Announcing that we are \"%s\".\n",
1165 memset(uip_appdata, 0,
sizeof(
struct dns_hdr));
1167 len = mdns_prep_host_announce_packet();
1169 uip_udp_packet_sendto(resolv_conn, uip_appdata,
1170 len, &resolv_mdns_addr,
UIP_HTONS(MDNS_PORT));
1172 mdns_needs_host_announce = 0;
1187 #if RESOLV_CONF_SUPPORTS_MDNS 1188 if(mdns_needs_host_announce) {
1200 static uint8_t initialized = 0;
1207 #if RESOLV_AUTO_REMOVE_TRAILING_DOTS 1209 remove_trailing_dots(
const char *name) {
1210 static char dns_name_without_dots[RESOLV_CONF_MAX_DOMAIN_NAME_SIZE + 1];
1211 size_t len = strlen(name);
1213 if(len && name[len - 1] ==
'.') {
1214 strncpy(dns_name_without_dots, name, RESOLV_CONF_MAX_DOMAIN_NAME_SIZE);
1215 while(len && (dns_name_without_dots[len - 1] ==
'.')) {
1216 dns_name_without_dots[--len] = 0;
1218 name = dns_name_without_dots;
1223 #define remove_trailing_dots(x) (x) 1236 uint8_t lseq, lseqi;
1238 register struct namemap *nameptr = 0;
1245 name = remove_trailing_dots(name);
1247 for(i = 0; i < RESOLV_ENTRIES; ++i) {
1248 nameptr = &names[i];
1249 if(0 == strcasecmp(nameptr->name, name)) {
1252 if((nameptr->state == STATE_UNUSED)
1253 #
if RESOLV_SUPPORTS_RECORD_EXPIRATION
1254 || (nameptr->state == STATE_DONE &&
clock_seconds() > nameptr->expiration)
1259 }
else if(seqno - nameptr->seqno > lseq) {
1260 lseq = seqno - nameptr->seqno;
1265 if(i == RESOLV_ENTRIES) {
1267 nameptr = &names[i];
1270 PRINTF(
"resolver: Starting query for \"%s\".\n", name);
1272 memset(nameptr, 0,
sizeof(*nameptr));
1274 strncpy(nameptr->name, name,
sizeof(nameptr->name) - 1);
1275 nameptr->state = STATE_NEW;
1276 nameptr->seqno = seqno;
1279 #if RESOLV_CONF_SUPPORTS_MDNS 1281 size_t name_len = strlen(name);
1283 const char local_suffix[] =
"local";
1285 if((name_len > (
sizeof(local_suffix) - 1)) &&
1286 (0 == strcasecmp(name + name_len - (
sizeof(local_suffix) - 1), local_suffix))) {
1287 PRINTF(
"resolver: Using MDNS to look up \"%s\".\n", name);
1288 nameptr->is_mdns = 1;
1290 nameptr->is_mdns = 0;
1293 nameptr->is_probe = (mdns_state == MDNS_STATE_PROBING) &&
1294 (0 == strcmp(nameptr->name, resolv_hostname));
1317 struct namemap *nameptr;
1320 name = remove_trailing_dots(name);
1322 #if UIP_CONF_LOOPBACK_INTERFACE 1323 if(strcmp(name,
"localhost")) {
1324 static uip_ipaddr_t loopback =
1325 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } };
1328 *ipaddr = &loopback;
1335 for(i = 0; i < RESOLV_ENTRIES; ++i) {
1336 nameptr = &names[i];
1338 if(strcasecmp(name, nameptr->name) == 0) {
1339 switch (nameptr->state) {
1342 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 1355 #if RESOLV_SUPPORTS_RECORD_EXPIRATION 1364 *ipaddr = &nameptr->ipaddr;
1376 PRINTF(
"resolver: Found \"%s\" in cache.\n", name);
1380 (
"resolver: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x \n",
1381 ((uint8_t *) addr)[0], ((uint8_t *) addr)[1], ((uint8_t *) addr)[2],
1382 ((uint8_t *) addr)[3], ((uint8_t *) addr)[4], ((uint8_t *) addr)[5],
1383 ((uint8_t *) addr)[6], ((uint8_t *) addr)[7], ((uint8_t *) addr)[8],
1384 ((uint8_t *) addr)[9], ((uint8_t *) addr)[10],
1385 ((uint8_t *) addr)[11], ((uint8_t *) addr)[12],
1386 ((uint8_t *) addr)[13], ((uint8_t *) addr)[14],
1387 ((uint8_t *) addr)[15]);
1391 DEBUG_PRINTF(
"resolver: \"%s\" is NOT cached.\n", name);
1404 resolv_found(
char *name, uip_ipaddr_t *
ipaddr)
1406 #if RESOLV_CONF_SUPPORTS_MDNS 1407 if(strncasecmp(resolv_hostname, name, strlen(resolv_hostname)) == 0 &&
1409 && !uip_ds6_is_my_addr(ipaddr)
1413 if(mdns_state == MDNS_STATE_PROBING) {
1417 PRINTF(
"resolver: Name collision detected for \"%s\".\n", name);
1420 resolv_hostname[strlen(resolv_hostname) - 6] = 0;
1423 for(i = 0; i < 3; ++i) {
1426 char append_str[4] =
"-XX";
1428 append_str[2] = (((val & 0xF) > 9) ?
'a' :
'0') + (val & 0xF);
1430 append_str[1] = (((val & 0xF) > 9) ?
'a' :
'0') + (val & 0xF);
1431 strncat(resolv_hostname, append_str,
1432 sizeof(resolv_hostname) - strlen(resolv_hostname) - 1);
1436 strncat(resolv_hostname,
".local", RESOLV_CONF_MAX_DOMAIN_NAME_SIZE - strlen(resolv_hostname));
1439 }
else if(mdns_state == MDNS_STATE_READY) {
1444 PRINTF(
"resolver: Possible name collision, probing...\n");
1445 start_name_collision_check(0);
1453 PRINTF(
"resolver: Found address for \"%s\".\n", name);
1455 (
"resolver: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x \n",
1456 ((uint8_t *) ipaddr)[0], ((uint8_t *) ipaddr)[1],
1457 ((uint8_t *) ipaddr)[2], ((uint8_t *) ipaddr)[3],
1458 ((uint8_t *) ipaddr)[4], ((uint8_t *) ipaddr)[5],
1459 ((uint8_t *) ipaddr)[6], ((uint8_t *) ipaddr)[7],
1460 ((uint8_t *) ipaddr)[8], ((uint8_t *) ipaddr)[9],
1461 ((uint8_t *) ipaddr)[10], ((uint8_t *) ipaddr)[11],
1462 ((uint8_t *) ipaddr)[12], ((uint8_t *) ipaddr)[13],
1463 ((uint8_t *) ipaddr)[14], ((uint8_t *) ipaddr)[15]);
1465 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.
#define UIP_IP_BUF
Direct access to IPv6 header.
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.