118 #ifndef DNS_RAND_TXID 119 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0) 120 #define DNS_RAND_TXID LWIP_RAND 122 static u16_t dns_txid;
123 #define DNS_RAND_TXID() (++dns_txid) 128 #ifndef DNS_PORT_ALLOWED 129 #define DNS_PORT_ALLOWED(port) ((port) >= 1024) 133 #ifndef DNS_MAX_RETRIES 134 #define DNS_MAX_RETRIES 4 139 #define DNS_MAX_TTL 604800 140 #elif DNS_MAX_TTL > 0x7FFFFFFF 141 #error DNS_MAX_TTL must be a positive 32-bit value 144 #if DNS_TABLE_SIZE > 255 145 #error DNS_TABLE_SIZE must fit into an u8_t 147 #if DNS_MAX_SERVERS > 255 148 #error DNS_MAX_SERVERS must fit into an u8_t 155 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 156 #ifndef DNS_MAX_REQUESTS 157 #define DNS_MAX_REQUESTS DNS_TABLE_SIZE 159 #if DNS_MAX_REQUESTS > 255 160 #error DNS_MAX_REQUESTS must fit into an u8_t 166 #define DNS_MAX_REQUESTS DNS_TABLE_SIZE 170 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 171 #ifndef DNS_MAX_SOURCE_PORTS 172 #define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS 174 #if DNS_MAX_SOURCE_PORTS > 255 175 #error DNS_MAX_SOURCE_PORTS must fit into an u8_t 179 #ifdef DNS_MAX_SOURCE_PORTS 180 #undef DNS_MAX_SOURCE_PORTS 182 #define DNS_MAX_SOURCE_PORTS 1 185 #if LWIP_IPV4 && LWIP_IPV6 186 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6)) 187 #define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) (IP_IS_V6_VAL(ip) ? LWIP_DNS_ADDRTYPE_IS_IPV6(t) : (!LWIP_DNS_ADDRTYPE_IS_IPV6(t))) 188 #define LWIP_DNS_ADDRTYPE_ARG(x) , x 189 #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x 190 #define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0) 193 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1 195 #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0 197 #define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1 198 #define LWIP_DNS_ADDRTYPE_ARG(x) 199 #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0 200 #define LWIP_DNS_SET_ADDRTYPE(x, y) 203 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 204 #define LWIP_DNS_ISMDNS_ARG(x) , x 206 #define LWIP_DNS_ISMDNS_ARG(x) 217 #define SIZEOF_DNS_QUERY 4 229 #define SIZEOF_DNS_ANSWER 10 231 #define SIZEOF_DNS_ANSWER_ASSERT 12 235 DNS_STATE_UNUSED = 0,
237 DNS_STATE_ASKING = 2,
242 struct dns_table_entry {
251 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 255 #if LWIP_IPV4 && LWIP_IPV6 258 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 265 struct dns_req_entry {
267 dns_found_callback found;
270 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 273 #if LWIP_IPV4 && LWIP_IPV6 278 #if DNS_LOCAL_HOSTLIST 280 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 283 static struct local_hostlist_entry *local_hostlist_dynamic;
288 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE 289 #define DNS_LOCAL_HOSTLIST_STORAGE_PRE static 293 #ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST 294 #define DNS_LOCAL_HOSTLIST_STORAGE_POST 296 DNS_LOCAL_HOSTLIST_STORAGE_PRE
struct local_hostlist_entry local_hostlist_static[]
297 DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;
301 static void dns_init_local(
void);
302 static err_t dns_lookup_local(
const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype));
307 static void dns_recv(
void *s,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr, u16_t port);
308 static void dns_check_entries(
void);
309 static void dns_call_found(u8_t idx, ip_addr_t* addr);
316 static struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS];
317 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 318 static u8_t dns_last_pcb_idx;
320 static u8_t dns_seqno;
322 static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS];
326 const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
329 const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
339 #ifdef DNS_SERVER_ADDRESS 342 DNS_SERVER_ADDRESS(&dnsserver);
343 dns_setserver(0, &dnsserver);
346 LWIP_ASSERT(
"sanity check SIZEOF_DNS_QUERY",
347 sizeof(
struct dns_query) == SIZEOF_DNS_QUERY);
348 LWIP_ASSERT(
"sanity check SIZEOF_DNS_ANSWER",
349 sizeof(
struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT);
354 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) 355 if (dns_pcbs[0] == NULL) {
357 LWIP_ASSERT(
"dns_pcbs[0] != NULL", dns_pcbs[0] != NULL);
361 LWIP_ASSERT(
"For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",
362 DNS_STATE_UNUSED == 0);
365 udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0);
366 udp_recv(dns_pcbs[0], dns_recv, NULL);
370 #if DNS_LOCAL_HOSTLIST 383 dns_setserver(u8_t numdns,
const ip_addr_t *dnsserver)
386 if (dnsserver != NULL) {
387 dns_servers[numdns] = (*dnsserver);
389 dns_servers[numdns] = *IP_ADDR_ANY;
403 dns_getserver(u8_t numdns)
406 return &dns_servers[numdns];
423 #if DNS_LOCAL_HOSTLIST 427 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) 429 struct local_hostlist_entry *entry;
431 struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;
433 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) {
434 struct local_hostlist_entry *init_entry = &local_hostlist_init[i];
435 LWIP_ASSERT(
"invalid host name (NULL)", init_entry->name != NULL);
436 namelen = strlen(init_entry->name);
437 LWIP_ASSERT(
"namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
438 entry = (
struct local_hostlist_entry *)
memp_malloc(MEMP_LOCALHOSTLIST);
439 LWIP_ASSERT(
"mem-error in dns_init_local", entry != NULL);
441 char* entry_name = (
char*)entry +
sizeof(
struct local_hostlist_entry);
442 MEMCPY(entry_name, init_entry->name, namelen);
443 entry_name[namelen] = 0;
444 entry->name = entry_name;
445 entry->addr = init_entry->addr;
446 entry->next = local_hostlist_dynamic;
447 local_hostlist_dynamic = entry;
462 dns_local_iterate(dns_found_callback iterator_fn,
void *iterator_arg)
465 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 466 struct local_hostlist_entry *entry = local_hostlist_dynamic;
468 while (entry != NULL) {
469 if (iterator_fn != NULL) {
470 iterator_fn(entry->name, &entry->addr, iterator_arg);
476 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
477 if (iterator_fn != NULL) {
478 iterator_fn(local_hostlist_static[i].name, &local_hostlist_static[i].addr, iterator_arg);
499 dns_local_lookup(
const char *hostname, ip_addr_t *addr, u8_t dns_addrtype)
502 return dns_lookup_local(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype));
507 dns_lookup_local(
const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
509 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 510 struct local_hostlist_entry *entry = local_hostlist_dynamic;
511 while (entry != NULL) {
513 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) {
515 ip_addr_copy(*addr, entry->addr);
523 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
524 if ((
lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) &&
525 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) {
527 ip_addr_copy(*addr, local_hostlist_static[i].addr);
536 #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC 548 dns_local_removehost(
const char *hostname,
const ip_addr_t *addr)
551 struct local_hostlist_entry *entry = local_hostlist_dynamic;
552 struct local_hostlist_entry *last_entry = NULL;
553 while (entry != NULL) {
554 if (((hostname == NULL) || !
lwip_stricmp(entry->name, hostname)) &&
555 ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) {
556 struct local_hostlist_entry *free_entry;
557 if (last_entry != NULL) {
558 last_entry->next = entry->next;
560 local_hostlist_dynamic = entry->next;
564 memp_free(MEMP_LOCALHOSTLIST, free_entry);
584 dns_local_addhost(
const char *hostname,
const ip_addr_t *addr)
586 struct local_hostlist_entry *entry;
589 LWIP_ASSERT(
"invalid host name (NULL)", hostname != NULL);
590 namelen = strlen(hostname);
591 LWIP_ASSERT(
"namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
592 entry = (
struct local_hostlist_entry *)
memp_malloc(MEMP_LOCALHOSTLIST);
596 entry_name = (
char*)entry +
sizeof(
struct local_hostlist_entry);
597 MEMCPY(entry_name, hostname, namelen);
598 entry_name[namelen] = 0;
599 entry->name = entry_name;
600 ip_addr_copy(entry->addr, *addr);
601 entry->next = local_hostlist_dynamic;
602 local_hostlist_dynamic = entry;
624 dns_lookup(
const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
627 #if DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) 629 #if DNS_LOCAL_HOSTLIST 630 if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) ==
ERR_OK) {
634 #ifdef DNS_LOOKUP_LOCAL_EXTERN 635 if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) ==
ERR_OK) {
642 if ((dns_table[i].state == DNS_STATE_DONE) &&
643 (
lwip_strnicmp(name, dns_table[i].name,
sizeof(dns_table[i].name)) == 0) &&
644 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) {
646 ip_addr_debug_print(
DNS_DEBUG, &(dns_table[i].ipaddr));
649 ip_addr_copy(*addr, dns_table[i].ipaddr);
670 dns_compare_name(
const char *query,
struct pbuf* p, u16_t start_offset)
673 u16_t response_offset = start_offset;
681 if ((n & 0xc0) == 0xc0) {
691 if ((*query) != (u8_t)c) {
706 return response_offset + 1;
717 dns_skip_name(
struct pbuf* p, u16_t query_idx)
720 u16_t offset = query_idx;
728 if ((n & 0xc0) == 0xc0) {
733 if (offset + n >= p->
tot_len) {
736 offset = (u16_t)(offset + n);
758 struct dns_query qry;
760 u16_t query_idx, copy_len;
761 const char *hostname, *hostname_part;
764 struct dns_table_entry* entry = &dns_table[idx];
767 (u16_t)(entry->server_idx), entry->name));
768 LWIP_ASSERT(
"dns server out of array", entry->server_idx <
DNS_MAX_SERVERS);
769 if (ip_addr_isany_val(dns_servers[entry->server_idx])
776 dns_call_found(idx, NULL);
778 entry->state = DNS_STATE_UNUSED;
786 const ip_addr_t* dst;
789 memset(&hdr, 0, SIZEOF_DNS_HDR);
790 hdr.id = lwip_htons(entry->txid);
791 hdr.flags1 = DNS_FLAG1_RD;
792 hdr.numquestions = PP_HTONS(1);
794 hostname = entry->name;
798 query_idx = SIZEOF_DNS_HDR;
801 hostname_part = hostname;
802 for (n = 0; *hostname !=
'.' && *hostname != 0; ++hostname) {
805 copy_len = (u16_t)(hostname - hostname_part);
807 pbuf_take_at(p, hostname_part, copy_len, query_idx + 1);
809 }
while (*hostname != 0);
814 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
815 qry.type = PP_HTONS(DNS_RRTYPE_AAAA);
817 qry.type = PP_HTONS(DNS_RRTYPE_A);
819 qry.cls = PP_HTONS(DNS_RRCLASS_IN);
822 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 823 pcb_idx = entry->pcb_idx;
829 entry->txid, entry->name, entry->server_idx));
830 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 831 if (entry->is_mdns) {
834 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
836 dst = &dns_mquery_v6group;
839 #if LWIP_IPV4 && LWIP_IPV6 844 dst = &dns_mquery_v4group;
851 dst = &dns_servers[entry->server_idx];
853 err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port);
864 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 865 static struct udp_pcb*
866 dns_alloc_random_port(
void)
877 u16_t port = (u16_t)DNS_RAND_TXID();
878 if (!DNS_PORT_ALLOWED(port)) {
883 err = udp_bind(ret, IP_ANY_TYPE, port);
889 udp_recv(ret, dns_recv, NULL);
905 for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) {
906 if (dns_pcbs[i] == NULL) {
910 if (i < DNS_MAX_SOURCE_PORTS) {
911 dns_pcbs[i] = dns_alloc_random_port();
912 if (dns_pcbs[i] != NULL) {
914 dns_last_pcb_idx = i;
920 for (i = 0, idx = dns_last_pcb_idx + 1; i < DNS_MAX_SOURCE_PORTS; i++, idx++) {
921 if (idx >= DNS_MAX_SOURCE_PORTS) {
924 if (dns_pcbs[idx] != NULL) {
925 dns_last_pcb_idx = idx;
929 return DNS_MAX_SOURCE_PORTS;
942 dns_call_found(u8_t idx, ip_addr_t* addr)
944 #if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0) 948 #if LWIP_IPV4 && LWIP_IPV6 951 if (IP_IS_V6_VAL(*addr)) {
952 LWIP_ASSERT(
"invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
953 dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
955 LWIP_ASSERT(
"invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
956 dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
961 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 962 for (i = 0; i < DNS_MAX_REQUESTS; i++) {
963 if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) {
964 (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg);
966 dns_requests[i].found = NULL;
970 if (dns_requests[idx].found) {
971 (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg);
973 dns_requests[idx].found = NULL;
975 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 977 for (i = 0; i < DNS_MAX_REQUESTS; i++) {
981 if (dns_table[i].state == DNS_STATE_ASKING) {
982 if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) {
984 dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
989 if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) {
991 udp_remove(dns_pcbs[dns_table[idx].pcb_idx]);
992 dns_pcbs[dns_table[idx].pcb_idx] = NULL;
993 dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
1000 dns_create_txid(
void)
1006 txid = (u16_t)DNS_RAND_TXID();
1010 if ((dns_table[i].state == DNS_STATE_ASKING) &&
1011 (dns_table[i].txid == txid)) {
1030 dns_check_entry(u8_t i)
1033 struct dns_table_entry *entry = &dns_table[i];
1035 LWIP_ASSERT(
"array index out of bounds", i < DNS_TABLE_SIZE);
1037 switch (entry->state) {
1040 entry->txid = dns_create_txid();
1041 entry->state = DNS_STATE_ASKING;
1042 entry->server_idx = 0;
1050 (
"dns_send returned error: %s\n", lwip_strerr(err)));
1053 case DNS_STATE_ASKING:
1054 if (--entry->tmr == 0) {
1055 if (++entry->retries == DNS_MAX_RETRIES) {
1056 if ((entry->server_idx + 1 <
DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[entry->server_idx + 1])
1062 entry->server_idx++;
1068 dns_call_found(i, NULL);
1070 entry->state = DNS_STATE_UNUSED;
1075 entry->tmr = entry->retries;
1082 (
"dns_send returned error: %s\n", lwip_strerr(err)));
1086 case DNS_STATE_DONE:
1088 if ((entry->ttl == 0) || (--entry->ttl == 0)) {
1091 entry->state = DNS_STATE_UNUSED;
1094 case DNS_STATE_UNUSED:
1098 LWIP_ASSERT(
"unknown dns_table entry state:", 0);
1107 dns_check_entries(
void)
1120 dns_correct_response(u8_t idx, u32_t ttl)
1122 struct dns_table_entry *entry = &dns_table[idx];
1124 entry->state = DNS_STATE_DONE;
1127 ip_addr_debug_print(
DNS_DEBUG, (&(entry->ipaddr)));
1132 if (entry->ttl > DNS_MAX_TTL) {
1133 entry->ttl = DNS_MAX_TTL;
1135 dns_call_found(idx, &entry->ipaddr);
1137 if (entry->ttl == 0) {
1143 if (entry->state == DNS_STATE_DONE) {
1144 entry->state = DNS_STATE_UNUSED;
1152 dns_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr, u16_t port)
1158 struct dns_answer ans;
1159 struct dns_query qry;
1160 u16_t nquestions, nanswers;
1167 if (p->
tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) {
1176 txid = lwip_htons(hdr.id);
1178 const struct dns_table_entry *entry = &dns_table[i];
1179 if ((entry->state == DNS_STATE_ASKING) &&
1180 (entry->txid == txid)) {
1184 nquestions = lwip_htons(hdr.numquestions);
1185 nanswers = lwip_htons(hdr.numanswers);
1188 if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) {
1192 if (nquestions != 1) {
1197 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 1198 if (!entry->is_mdns)
1203 if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) {
1210 res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR);
1211 if (res_idx == 0xFFFF) {
1217 if (
pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) {
1220 if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) ||
1221 (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) ||
1222 (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) {
1227 res_idx += SIZEOF_DNS_QUERY;
1230 if (hdr.flags2 & DNS_FLAG2_ERR_MASK) {
1233 while ((nanswers > 0) && (res_idx < p->tot_len)) {
1235 res_idx = dns_skip_name(p, res_idx);
1236 if (res_idx == 0xFFFF) {
1241 if (
pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) {
1244 res_idx += SIZEOF_DNS_ANSWER;
1246 if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) {
1248 if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(
sizeof(ip4_addr_t)))) {
1249 #if LWIP_IPV4 && LWIP_IPV6 1250 if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
1255 if (
pbuf_copy_partial(p, &ip4addr,
sizeof(ip4_addr_t), res_idx) !=
sizeof(ip4_addr_t)) {
1258 ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr);
1261 dns_correct_response(i, lwip_ntohl(ans.ttl));
1267 if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(
sizeof(ip6_addr_t)))) {
1268 #if LWIP_IPV4 && LWIP_IPV6 1269 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
1274 if (
pbuf_copy_partial(p, &ip6addr,
sizeof(ip6_addr_t), res_idx) !=
sizeof(ip6_addr_t)) {
1277 ip_addr_copy_from_ip6(dns_table[i].ipaddr, ip6addr);
1280 dns_correct_response(i, lwip_ntohl(ans.ttl));
1287 if ((
int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) {
1290 res_idx += lwip_htons(ans.len);
1293 #if LWIP_IPV4 && LWIP_IPV6 1294 if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) ||
1295 (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
1296 if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
1298 dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
1301 dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
1304 dns_table[i].state = DNS_STATE_NEW;
1313 dns_call_found(i, NULL);
1314 dns_table[i].state = DNS_STATE_UNUSED;
1336 dns_enqueue(
const char *name,
size_t hostnamelen, dns_found_callback found,
1337 void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns))
1341 struct dns_table_entry *entry = NULL;
1343 struct dns_req_entry* req;
1345 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 1349 if ((dns_table[i].state == DNS_STATE_ASKING) &&
1350 (
lwip_strnicmp(name, dns_table[i].name,
sizeof(dns_table[i].name)) == 0)) {
1351 #if LWIP_IPV4 && LWIP_IPV6 1352 if (dns_table[i].reqaddrtype != dns_addrtype) {
1360 for (r = 0; r < DNS_MAX_REQUESTS; r++) {
1361 if (dns_requests[r].found == 0) {
1362 dns_requests[r].found = found;
1363 dns_requests[r].arg = callback_arg;
1364 dns_requests[r].dns_table_idx = i;
1365 LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype);
1379 entry = &dns_table[i];
1381 if (entry->state == DNS_STATE_UNUSED) {
1385 if (entry->state == DNS_STATE_DONE) {
1386 u8_t age = dns_seqno - entry->seqno;
1395 if (i == DNS_TABLE_SIZE) {
1396 if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {
1403 entry = &dns_table[i];
1407 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0) 1410 for (r = 0; r < DNS_MAX_REQUESTS; r++) {
1411 if (dns_requests[r].found == NULL) {
1412 req = &dns_requests[r];
1421 req->dns_table_idx = i;
1424 req = &dns_requests[i];
1431 entry->state = DNS_STATE_NEW;
1432 entry->seqno = dns_seqno;
1433 LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype);
1434 LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype);
1436 req->arg = callback_arg;
1438 MEMCPY(entry->name, name, namelen);
1439 entry->name[namelen] = 0;
1441 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) 1442 entry->pcb_idx = dns_alloc_pcb();
1443 if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) {
1446 entry->state = DNS_STATE_UNUSED;
1450 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": use DNS pcb %"U16_F
"\n", name, (u16_t)(entry->pcb_idx)));
1453 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 1454 entry->is_mdns = is_mdns;
1487 dns_gethostbyname(
const char *hostname, ip_addr_t *addr, dns_found_callback found,
1490 return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT);
1508 dns_gethostbyname_addrtype(
const char *hostname, ip_addr_t *addr, dns_found_callback found,
1509 void *callback_arg, u8_t dns_addrtype)
1512 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 1517 if ((addr == NULL) ||
1518 (!hostname) || (!hostname[0])) {
1521 #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0) 1522 if (dns_pcbs[0] == NULL) {
1526 hostnamelen = strlen(hostname);
1533 #if LWIP_HAVE_LOOPIF 1534 if (strcmp(hostname,
"localhost") == 0) {
1535 ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr);
1541 if (ipaddr_aton(hostname, addr)) {
1542 #if LWIP_IPV4 && LWIP_IPV6 1543 if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) ||
1544 (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6)))
1551 if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) ==
ERR_OK) {
1554 #if LWIP_IPV4 && LWIP_IPV6 1555 if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
1558 if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
1559 fallback = LWIP_DNS_ADDRTYPE_IPV6;
1561 fallback = LWIP_DNS_ADDRTYPE_IPV4;
1563 if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) ==
ERR_OK) {
1571 #if LWIP_DNS_SUPPORT_MDNS_QUERIES 1572 if (strstr(hostname,
".local") == &hostname[hostnamelen] - 6) {
1582 if (ip_addr_isany_val(dns_servers[0])) {
1588 return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)
1589 LWIP_DNS_ISMDNS_ARG(is_mdns));
#define LWIP_DNS_SUPPORT_MDNS_QUERIES
void memp_free(memp_t type, void *mem)
err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
void pbuf_put_at(struct pbuf *p, u16_t offset, u8_t data)
#define DNS_MAX_NAME_LENGTH
int lwip_stricmp(const char *str1, const char *str2)
#define LWIP_DEBUGF(debug, message)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
#define LWIP_DBG_LEVEL_WARNING
#define LWIP_UNUSED_ARG(x)
u8_t pbuf_free(struct pbuf *p)
void * memp_malloc(memp_t type)
int pbuf_try_get_at(const struct pbuf *p, u16_t offset)
int lwip_strnicmp(const char *str1, const char *str2, size_t len)