102#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0)
103#define DNS_RAND_TXID LWIP_RAND
105static u16_t dns_txid;
106#define DNS_RAND_TXID() (++dns_txid)
111#ifndef DNS_PORT_ALLOWED
112#define DNS_PORT_ALLOWED(port) ((port) >= 1024)
117#define DNS_MAX_TTL 604800
118#elif DNS_MAX_TTL > 0x7FFFFFFF
119#error DNS_MAX_TTL must be a positive 32-bit value
122#if DNS_TABLE_SIZE > 255
123#error DNS_TABLE_SIZE must fit into an u8_t
125#if DNS_MAX_SERVERS > 255
126#error DNS_MAX_SERVERS must fit into an u8_t
133#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
134#ifndef DNS_MAX_REQUESTS
135#define DNS_MAX_REQUESTS DNS_TABLE_SIZE
137#if DNS_MAX_REQUESTS > 255
138#error DNS_MAX_REQUESTS must fit into an u8_t
144#define DNS_MAX_REQUESTS DNS_TABLE_SIZE
148#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
149#ifndef DNS_MAX_SOURCE_PORTS
150#define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS
152#if DNS_MAX_SOURCE_PORTS > 255
153#error DNS_MAX_SOURCE_PORTS must fit into an u8_t
157#ifdef DNS_MAX_SOURCE_PORTS
158#undef DNS_MAX_SOURCE_PORTS
160#define DNS_MAX_SOURCE_PORTS 1
163#if LWIP_IPV4 && LWIP_IPV6
164#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6))
165#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)))
166#define LWIP_DNS_ADDRTYPE_ARG(x) , x
167#define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x
168#define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0)
171#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1
173#define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0
175#define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1
176#define LWIP_DNS_ADDRTYPE_ARG(x)
177#define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0
178#define LWIP_DNS_SET_ADDRTYPE(x, y)
181#if LWIP_DNS_SUPPORT_MDNS_QUERIES
182#define LWIP_DNS_ISMDNS_ARG(x) , x
184#define LWIP_DNS_ISMDNS_ARG(x)
195#define SIZEOF_DNS_QUERY 4
207#define SIZEOF_DNS_ANSWER 10
209#define SIZEOF_DNS_ANSWER_ASSERT 12
213 DNS_STATE_UNUSED = 0,
215 DNS_STATE_ASKING = 2,
220struct dns_table_entry {
229#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
233#if LWIP_IPV4 && LWIP_IPV6
236#if LWIP_DNS_SUPPORT_MDNS_QUERIES
243struct dns_req_entry {
248#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
251#if LWIP_IPV4 && LWIP_IPV6
256#if DNS_LOCAL_HOSTLIST
258#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
261static struct local_hostlist_entry *local_hostlist_dynamic;
266#ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE
267#define DNS_LOCAL_HOSTLIST_STORAGE_PRE static
271#ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST
272#define DNS_LOCAL_HOSTLIST_STORAGE_POST
274DNS_LOCAL_HOSTLIST_STORAGE_PRE
struct local_hostlist_entry local_hostlist_static[]
275 DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;
279static void dns_init_local(
void);
280static err_t dns_lookup_local(
const char *hostname,
size_t hostnamelen, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype));
285static void dns_recv(
void *s,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr, u16_t port);
286static void dns_check_entries(
void);
287static void dns_call_found(u8_t idx, ip_addr_t *addr);
294static struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS];
295#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
296static u8_t dns_last_pcb_idx;
298static u8_t dns_seqno;
300static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS];
301static ip_addr_t dns_servers[DNS_MAX_SERVERS];
304const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
307const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
317#ifdef DNS_SERVER_ADDRESS
320 DNS_SERVER_ADDRESS(&dnsserver);
321 dns_setserver(0, &dnsserver);
324 LWIP_ASSERT(
"sanity check SIZEOF_DNS_QUERY",
325 sizeof(
struct dns_query) == SIZEOF_DNS_QUERY);
326 LWIP_ASSERT(
"sanity check SIZEOF_DNS_ANSWER",
327 sizeof(
struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT);
329 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_init: initializing\n"));
332#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
333 if (dns_pcbs[0] == NULL) {
335 LWIP_ASSERT(
"dns_pcbs[0] != NULL", dns_pcbs[0] != NULL);
339 LWIP_ASSERT(
"For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",
340 DNS_STATE_UNUSED == 0);
343 udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0);
344 udp_recv(dns_pcbs[0], dns_recv, NULL);
348#if DNS_LOCAL_HOSTLIST
361dns_setserver(u8_t numdns,
const ip_addr_t *dnsserver)
363 if (numdns < DNS_MAX_SERVERS) {
364 if (dnsserver != NULL) {
365 dns_servers[numdns] = (*dnsserver);
381dns_getserver(u8_t numdns)
383 if (numdns < DNS_MAX_SERVERS) {
384 return &dns_servers[numdns];
397 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_tmr: dns_check_entries\n"));
401#if DNS_LOCAL_HOSTLIST
405#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT)
407 struct local_hostlist_entry *entry;
409 struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;
411 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) {
412 struct local_hostlist_entry *init_entry = &local_hostlist_init[i];
413 LWIP_ASSERT(
"invalid host name (NULL)", init_entry->name != NULL);
414 namelen = strlen(init_entry->name);
415 LWIP_ASSERT(
"namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
416 entry = (
struct local_hostlist_entry *)
memp_malloc(MEMP_LOCALHOSTLIST);
417 LWIP_ASSERT(
"mem-error in dns_init_local", entry != NULL);
419 char *entry_name = (
char *)entry +
sizeof(
struct local_hostlist_entry);
420 MEMCPY(entry_name, init_entry->name, namelen);
421 entry_name[namelen] = 0;
422 entry->name = entry_name;
423 entry->addr = init_entry->addr;
424 entry->next = local_hostlist_dynamic;
425 local_hostlist_dynamic = entry;
443#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
444 struct local_hostlist_entry *entry = local_hostlist_dynamic;
446 while (entry != NULL) {
447 if (iterator_fn != NULL) {
448 iterator_fn(entry->name, &entry->addr, iterator_arg);
454 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
455 if (iterator_fn != NULL) {
456 iterator_fn(local_hostlist_static[i].name, &local_hostlist_static[i].addr, iterator_arg);
477dns_local_lookup(
const char *hostname, ip_addr_t *addr, u8_t dns_addrtype)
481 if ((addr == NULL) ||
482 (!hostname) || (!hostname[0])) {
485 hostnamelen = strlen(hostname);
486 if (hostname[hostnamelen - 1] ==
'.') {
490 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_local_lookup: name too long to resolve\n"));
493 return dns_lookup_local(hostname, hostnamelen, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype));
498dns_lookup_local(
const char *hostname,
size_t hostnamelen, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
500#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
501 struct local_hostlist_entry *entry = local_hostlist_dynamic;
502 while (entry != NULL) {
503 if ((
lwip_strnicmp(entry->name, hostname, hostnamelen) == 0) &&
504 !entry->name[hostnamelen] &&
505 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) {
507 ip_addr_copy(*addr, entry->addr);
515 for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
516 if ((
lwip_strnicmp(local_hostlist_static[i].name, hostname, hostnamelen) == 0) &&
517 !local_hostlist_static[i].name[hostnamelen] &&
518 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) {
520 ip_addr_copy(*addr, local_hostlist_static[i].addr);
529#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
541dns_local_removehost(
const char *hostname,
const ip_addr_t *addr)
544 struct local_hostlist_entry *entry = local_hostlist_dynamic;
545 struct local_hostlist_entry *last_entry = NULL;
546 while (entry != NULL) {
547 if (((hostname == NULL) || !
lwip_stricmp(entry->name, hostname)) &&
548 ((addr == NULL) || ip_addr_eq(&entry->addr, addr))) {
549 struct local_hostlist_entry *free_entry;
550 if (last_entry != NULL) {
551 last_entry->next = entry->next;
553 local_hostlist_dynamic = entry->next;
557 memp_free(MEMP_LOCALHOSTLIST, free_entry);
577dns_local_addhost(
const char *hostname,
const ip_addr_t *addr)
579 struct local_hostlist_entry *entry;
582 LWIP_ASSERT(
"invalid host name (NULL)", hostname != NULL);
583 namelen = strlen(hostname);
584 LWIP_ASSERT(
"namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
585 entry = (
struct local_hostlist_entry *)
memp_malloc(MEMP_LOCALHOSTLIST);
589 entry_name = (
char *)entry +
sizeof(
struct local_hostlist_entry);
590 MEMCPY(entry_name, hostname, namelen);
591 entry_name[namelen] = 0;
592 entry->name = entry_name;
593 ip_addr_copy(entry->addr, *addr);
594 entry->next = local_hostlist_dynamic;
595 local_hostlist_dynamic = entry;
618dns_lookup(
const char *name,
size_t hostnamelen, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
622#if DNS_LOCAL_HOSTLIST
623 if (dns_lookup_local(name, hostnamelen, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) ==
ERR_OK) {
627#ifdef DNS_LOOKUP_LOCAL_EXTERN
628 if (DNS_LOOKUP_LOCAL_EXTERN(name, hostnamelen, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) ==
ERR_OK) {
636 if ((dns_table[i].state == DNS_STATE_DONE) &&
638 !dns_table[i].name[namelen] &&
639 LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) {
640 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_lookup: \"%s\": found = ", name));
641 ip_addr_debug_print_val(
DNS_DEBUG, dns_table[i].ipaddr);
644 ip_addr_copy(*addr, dns_table[i].ipaddr);
670dns_compare_name(
const char *query,
struct pbuf *p, u16_t start_offset)
673 u16_t response_offset = start_offset;
677 if ((n < 0) || (response_offset == 0xFFFF)) {
683 if ((n & 0xc0) == 0xc0) {
693 if (lwip_tolower((*query)) != lwip_tolower((u8_t)c)) {
696 if (response_offset == 0xFFFF) {
712 if (response_offset == 0xFFFF) {
716 return (u16_t)(response_offset + 1);
727dns_skip_name(
struct pbuf *p, u16_t query_idx)
730 u16_t offset = query_idx;
734 if ((n < 0) || (offset == 0)) {
738 if ((n & 0xc0) == 0xc0) {
743 if (offset + n >= p->tot_len) {
746 offset = (u16_t)(offset + n);
754 if (offset == 0xFFFF) {
757 return (u16_t)(offset + 1);
771 struct dns_query qry;
773 u16_t query_idx, copy_len;
774 const char *hostname, *hostname_part;
777 struct dns_table_entry *entry = &dns_table[idx];
779 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_send: dns_servers[%"U16_F
"] \"%s\": request\n",
780 (u16_t)(entry->server_idx), entry->name));
781 LWIP_ASSERT(
"dns server out of array", entry->server_idx < DNS_MAX_SERVERS);
782 if (ip_addr_isany_val(dns_servers[entry->server_idx])
789 dns_call_found(idx, NULL);
791 entry->state = DNS_STATE_UNUSED;
796 p = pbuf_alloc(
PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 +
799 const ip_addr_t *dst;
802 memset(&hdr, 0, SIZEOF_DNS_HDR);
803 hdr.id = lwip_htons(entry->txid);
804 hdr.flags1 = DNS_FLAG1_RD;
805 hdr.numquestions = PP_HTONS(1);
806 pbuf_take(p, &hdr, SIZEOF_DNS_HDR);
807 hostname = entry->name;
811 query_idx = SIZEOF_DNS_HDR;
814 hostname_part = hostname;
815 for (n = 0; *hostname !=
'.' && *hostname != 0; ++hostname) {
818 copy_len = (u16_t)(hostname - hostname_part);
819 if (query_idx + n + 1 > 0xFFFF) {
821 goto overflow_return;
824 pbuf_take_at(p, hostname_part, copy_len, (u16_t)(query_idx + 1));
825 query_idx = (u16_t)(query_idx + n + 1);
826 }
while (*hostname != 0);
831 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
832 qry.type = PP_HTONS(DNS_RRTYPE_AAAA);
834 qry.type = PP_HTONS(DNS_RRTYPE_A);
836 qry.cls = PP_HTONS(DNS_RRCLASS_IN);
839#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
840 pcb_idx = entry->pcb_idx;
845 LWIP_DEBUGF(
DNS_DEBUG, (
"sending DNS request ID %d for name \"%s\" to server %d\r\n",
846 entry->txid, entry->name, entry->server_idx));
847#if LWIP_DNS_SUPPORT_MDNS_QUERIES
848 if (entry->is_mdns) {
851 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
852 dst = &dns_mquery_v6group;
855#if LWIP_IPV4 && LWIP_IPV6
860 dst = &dns_mquery_v4group;
867 dst = &dns_servers[entry->server_idx];
869 err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port);
883#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
884static struct udp_pcb *
885dns_alloc_random_port(
void)
896 u16_t port = (u16_t)DNS_RAND_TXID();
897 if (DNS_PORT_ALLOWED(port)) {
898 err = udp_bind(pcb, IP_ANY_TYPE, port);
908 udp_recv(pcb, dns_recv, NULL);
924 for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) {
925 if (dns_pcbs[i] == NULL) {
929 if (i < DNS_MAX_SOURCE_PORTS) {
930 dns_pcbs[i] = dns_alloc_random_port();
931 if (dns_pcbs[i] != NULL) {
933 dns_last_pcb_idx = i;
939 for (i = 0, idx = (u8_t)(dns_last_pcb_idx + 1); i < DNS_MAX_SOURCE_PORTS; i++, idx++) {
940 if (idx >= DNS_MAX_SOURCE_PORTS) {
943 if (dns_pcbs[idx] != NULL) {
944 dns_last_pcb_idx = idx;
948 return DNS_MAX_SOURCE_PORTS;
961dns_call_found(u8_t idx, ip_addr_t *addr)
963#if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0)
967#if LWIP_IPV4 && LWIP_IPV6
970 if (IP_IS_V6_VAL(*addr)) {
971 LWIP_ASSERT(
"invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
972 dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
974 LWIP_ASSERT(
"invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
975 dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
980#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
981 for (i = 0; i < DNS_MAX_REQUESTS; i++) {
982 if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) {
983 (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg);
985 dns_requests[i].found = NULL;
989 if (dns_requests[idx].found) {
990 (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg);
992 dns_requests[idx].found = NULL;
994#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
996 for (i = 0; i < DNS_MAX_REQUESTS; i++) {
1000 if (dns_table[i].state == DNS_STATE_ASKING) {
1001 if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) {
1003 dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
1008 if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) {
1010 udp_remove(dns_pcbs[dns_table[idx].pcb_idx]);
1011 dns_pcbs[dns_table[idx].pcb_idx] = NULL;
1012 dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
1019dns_create_txid(
void)
1025 txid = (u16_t)DNS_RAND_TXID();
1029 if ((dns_table[i].state == DNS_STATE_ASKING) &&
1030 (dns_table[i].txid == txid)) {
1043dns_backupserver_available(
struct dns_table_entry *pentry)
1048 if ((pentry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[pentry->server_idx + 1])) {
1066dns_check_entry(u8_t i)
1069 struct dns_table_entry *entry = &dns_table[i];
1073 switch (entry->state) {
1076 entry->txid = dns_create_txid();
1077 entry->state = DNS_STATE_ASKING;
1078 entry->server_idx = 0;
1086 (
"dns_send returned error: %s\n", lwip_strerr(err)));
1089 case DNS_STATE_ASKING:
1090 if (--entry->tmr == 0) {
1092 if (dns_backupserver_available(entry)
1098 entry->server_idx++;
1102 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_check_entry: \"%s\": timeout\n", entry->name));
1104 dns_call_found(i, NULL);
1106 entry->state = DNS_STATE_UNUSED;
1111 entry->tmr = entry->retries;
1118 (
"dns_send returned error: %s\n", lwip_strerr(err)));
1122 case DNS_STATE_DONE:
1124 if ((entry->ttl == 0) || (--entry->ttl == 0)) {
1125 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_check_entry: \"%s\": flush\n", entry->name));
1127 entry->state = DNS_STATE_UNUSED;
1130 case DNS_STATE_UNUSED:
1134 LWIP_ASSERT(
"unknown dns_table entry state:", 0);
1143dns_check_entries(
void)
1156dns_correct_response(u8_t idx, u32_t ttl)
1158 struct dns_table_entry *entry = &dns_table[idx];
1160 entry->state = DNS_STATE_DONE;
1162 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": response = ", entry->name));
1163 ip_addr_debug_print_val(
DNS_DEBUG, entry->ipaddr);
1168 if (entry->ttl > DNS_MAX_TTL) {
1169 entry->ttl = DNS_MAX_TTL;
1171 dns_call_found(idx, &entry->ipaddr);
1173 if (entry->ttl == 0) {
1179 if (entry->state == DNS_STATE_DONE) {
1180 entry->state = DNS_STATE_UNUSED;
1189dns_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr, u16_t port)
1195 struct dns_answer ans;
1196 struct dns_query qry;
1197 u16_t nquestions, nanswers;
1204 if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) {
1205 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: pbuf too small\n"));
1213 txid = lwip_htons(hdr.id);
1215 struct dns_table_entry *entry = &dns_table[i];
1216 if ((entry->state == DNS_STATE_ASKING) &&
1217 (entry->txid == txid)) {
1221 nquestions = lwip_htons(hdr.numquestions);
1222 nanswers = lwip_htons(hdr.numanswers);
1225 if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) {
1226 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": not a response\n", entry->name));
1229 if (nquestions != 1) {
1230 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": response not match to query\n", entry->name));
1234#if LWIP_DNS_SUPPORT_MDNS_QUERIES
1235 if (!entry->is_mdns)
1240 if (!ip_addr_eq(addr, &dns_servers[entry->server_idx])) {
1247 res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR);
1248 if (res_idx == 0xFFFF) {
1249 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": response not match to query\n", entry->name));
1254 if (
pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) {
1257 if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) ||
1258 (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) ||
1259 (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) {
1260 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": response not match to query\n", entry->name));
1264 if (res_idx + SIZEOF_DNS_QUERY > 0xFFFF) {
1267 res_idx = (u16_t)(res_idx + SIZEOF_DNS_QUERY);
1270 if (hdr.flags2 & DNS_FLAG2_ERR_MASK) {
1271 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": error in flags\n", entry->name));
1276 if (dns_backupserver_available(entry)) {
1287 while ((nanswers > 0) && (res_idx < p->tot_len)) {
1289 res_idx = dns_skip_name(p, res_idx);
1290 if (res_idx == 0xFFFF) {
1295 if (
pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) {
1298 if (res_idx + SIZEOF_DNS_ANSWER > 0xFFFF) {
1301 res_idx = (u16_t)(res_idx + SIZEOF_DNS_ANSWER);
1303 if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) {
1305 if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(
sizeof(
ip4_addr_t)))) {
1306#if LWIP_IPV4 && LWIP_IPV6
1307 if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
1315 ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr);
1318 dns_correct_response(i, lwip_ntohl(ans.ttl));
1324 if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(
sizeof(ip6_addr_p_t)))) {
1325#if LWIP_IPV4 && LWIP_IPV6
1326 if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
1329 ip6_addr_p_t ip6addr;
1331 if (
pbuf_copy_partial(p, &ip6addr,
sizeof(ip6_addr_p_t), res_idx) !=
sizeof(ip6_addr_p_t)) {
1335 ip_addr_copy_from_ip6_packed(dns_table[i].ipaddr, ip6addr);
1338 dns_correct_response(i, lwip_ntohl(ans.ttl));
1345 if ((
int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) {
1348 res_idx = (u16_t)(res_idx + lwip_htons(ans.len));
1351#if LWIP_IPV4 && LWIP_IPV6
1352 if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) ||
1353 (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
1354 if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
1356 dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
1359 dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
1362 dns_table[i].state = DNS_STATE_NEW;
1367 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_recv: \"%s\": error in response\n", entry->name));
1371 dns_call_found(i, NULL);
1372 dns_table[i].state = DNS_STATE_UNUSED;
1395 void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns))
1399 struct dns_table_entry *entry = NULL;
1401 struct dns_req_entry *req;
1402#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
1408#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
1411 if ((dns_table[i].state == DNS_STATE_ASKING) &&
1413 !dns_table[i].name[namelen]) {
1414#if LWIP_IPV4 && LWIP_IPV6
1415 if (dns_table[i].reqaddrtype != dns_addrtype) {
1423 for (r = 0; r < DNS_MAX_REQUESTS; r++) {
1424 if (dns_requests[r].found == NULL) {
1425 dns_requests[r].found = found;
1426 dns_requests[r].arg = callback_arg;
1427 dns_requests[r].dns_table_idx = i;
1428 LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype);
1429 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": duplicate request\n", name));
1442 entry = &dns_table[i];
1444 if (entry->state == DNS_STATE_UNUSED) {
1448 if (entry->state == DNS_STATE_DONE) {
1449 u8_t age = (u8_t)(dns_seqno - entry->seqno);
1459 if ((lseqi >=
DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {
1461 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": DNS entries table is full\n", name));
1466 entry = &dns_table[i];
1470#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
1473 for (r = 0; r < DNS_MAX_REQUESTS; r++) {
1474 if (dns_requests[r].found == NULL) {
1475 req = &dns_requests[r];
1481 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": DNS request entries table is full\n", name));
1484 req->dns_table_idx = i;
1487 req = &dns_requests[i];
1491 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": use DNS entry %"U16_F
"\n", name, (u16_t)(i)));
1494 entry->state = DNS_STATE_NEW;
1495 entry->seqno = dns_seqno;
1496 LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype);
1497 LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype);
1499 req->arg = callback_arg;
1500 MEMCPY(entry->name, name, namelen);
1501 entry->name[namelen] = 0;
1503#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
1504 entry->pcb_idx = dns_alloc_pcb();
1505 if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) {
1507 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": failed to allocate a pcb\n", name));
1508 entry->state = DNS_STATE_UNUSED;
1512 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_enqueue: \"%s\": use DNS pcb %"U16_F
"\n", name, (u16_t)(entry->pcb_idx)));
1515#if LWIP_DNS_SUPPORT_MDNS_QUERIES
1516 entry->is_mdns = is_mdns;
1552 return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT);
1570dns_gethostbyname_addrtype(
const char *hostname, ip_addr_t *addr,
dns_found_callback found,
1571 void *callback_arg, u8_t dns_addrtype)
1574#if LWIP_DNS_SUPPORT_MDNS_QUERIES
1579 if ((addr == NULL) ||
1580 (!hostname) || (!hostname[0])) {
1583#if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
1584 if (dns_pcbs[0] == NULL) {
1588 hostnamelen = strlen(hostname);
1589 if (hostname[hostnamelen - 1] ==
'.') {
1593 LWIP_DEBUGF(
DNS_DEBUG, (
"dns_gethostbyname: name too long to resolve\n"));
1599 if (strcmp(hostname,
"localhost") == 0) {
1600 ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr);
1606 if (ipaddr_aton(hostname, addr)) {
1607#if LWIP_IPV4 && LWIP_IPV6
1608 if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) ||
1609 (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6)))
1616 if (dns_lookup(hostname, hostnamelen, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) ==
ERR_OK) {
1619#if LWIP_IPV4 && LWIP_IPV6
1620 if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
1623 if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
1624 fallback = LWIP_DNS_ADDRTYPE_IPV6;
1626 fallback = LWIP_DNS_ADDRTYPE_IPV4;
1628 if (dns_lookup(hostname, hostnamelen, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) ==
ERR_OK) {
1636#if LWIP_DNS_SUPPORT_MDNS_QUERIES
1637 if (strstr(hostname,
".local") == &hostname[hostnamelen] - 6) {
1647 if (ip_addr_isany_val(dns_servers[0])) {
1653 return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)
1654 LWIP_DNS_ISMDNS_ARG(is_mdns));
void(*) dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
#define LWIP_UNUSED_ARG(x)
#define LWIP_DBG_LEVEL_WARNING
#define DNS_MAX_NAME_LENGTH
#define LWIP_DNS_SUPPORT_MDNS_QUERIES
#define MEMCPY(dst, src, len)
int pbuf_try_get_at(const struct pbuf *p, u16_t offset)
u8 pbuf_free(struct pbuf *p)
u16_t pbuf_copy_partial(const struct pbuf *p, void *dataptr, u16_t len, u16_t offset)
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)
int lwip_stricmp(const char *str1, const char *str2)
int lwip_strnicmp(const char *str1, const char *str2, size_t len)
void * memp_malloc(memp_t type)
void memp_free(memp_t type, void *mem)
struct ip4_addr ip4_addr_t