dns.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. /**
  2. * @file
  3. * DNS - host name to IP address resolver.
  4. *
  5. * @defgroup dns DNS
  6. * @ingroup callbackstyle_api
  7. *
  8. * Implements a DNS host name to IP address resolver.
  9. *
  10. * The lwIP DNS resolver functions are used to lookup a host name and
  11. * map it to a numerical IP address. It maintains a list of resolved
  12. * hostnames that can be queried with the dns_lookup() function.
  13. * New hostnames can be resolved using the dns_query() function.
  14. *
  15. * The lwIP version of the resolver also adds a non-blocking version of
  16. * gethostbyname() that will work with a raw API application. This function
  17. * checks for an IP address string first and converts it if it is valid.
  18. * gethostbyname() then does a dns_lookup() to see if the name is
  19. * already in the table. If so, the IP is returned. If not, a query is
  20. * issued and the function returns with a ERR_INPROGRESS status. The app
  21. * using the dns client must then go into a waiting state.
  22. *
  23. * Once a hostname has been resolved (or found to be non-existent),
  24. * the resolver code calls a specified callback function (which
  25. * must be implemented by the module that uses the resolver).
  26. *
  27. * Multicast DNS queries are supported for names ending on ".local".
  28. * However, only "One-Shot Multicast DNS Queries" are supported (RFC 6762
  29. * chapter 5.1), this is not a fully compliant implementation of continuous
  30. * mDNS querying!
  31. *
  32. * All functions must be called from TCPIP thread.
  33. *
  34. * @see @ref netconn_common for thread-safe access.
  35. */
  36. /*
  37. * Port to lwIP from uIP
  38. * by Jim Pettinato April 2007
  39. *
  40. * security fixes and more by Simon Goldschmidt
  41. *
  42. * uIP version Copyright (c) 2002-2003, Adam Dunkels.
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms, with or without
  46. * modification, are permitted provided that the following conditions
  47. * are met:
  48. * 1. Redistributions of source code must retain the above copyright
  49. * notice, this list of conditions and the following disclaimer.
  50. * 2. Redistributions in binary form must reproduce the above copyright
  51. * notice, this list of conditions and the following disclaimer in the
  52. * documentation and/or other materials provided with the distribution.
  53. * 3. The name of the author may not be used to endorse or promote
  54. * products derived from this software without specific prior
  55. * written permission.
  56. *
  57. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  58. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  59. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  60. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  61. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  62. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  63. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  64. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  65. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  66. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  67. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  68. */
  69. /*-----------------------------------------------------------------------------
  70. * RFC 1035 - Domain names - implementation and specification
  71. * RFC 2181 - Clarifications to the DNS Specification
  72. *----------------------------------------------------------------------------*/
  73. /** @todo: define good default values (rfc compliance) */
  74. /** @todo: improve answer parsing, more checkings... */
  75. /** @todo: check RFC1035 - 7.3. Processing responses */
  76. /** @todo: one-shot mDNS: dual-stack fallback to another IP version */
  77. /*-----------------------------------------------------------------------------
  78. * Includes
  79. *----------------------------------------------------------------------------*/
  80. #include "lwip/opt.h"
  81. #if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
  82. #include "lwip/def.h"
  83. #include "lwip/udp.h"
  84. #include "lwip/mem.h"
  85. #include "lwip/memp.h"
  86. #include "lwip/dns.h"
  87. #include "lwip/prot/dns.h"
  88. #include <string.h>
  89. /** Random generator function to create random TXIDs and source ports for queries */
  90. #ifndef DNS_RAND_TXID
  91. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_XID) != 0)
  92. #define DNS_RAND_TXID LWIP_RAND
  93. #else
  94. static u16_t dns_txid;
  95. #define DNS_RAND_TXID() (++dns_txid)
  96. #endif
  97. #endif
  98. /** Limits the source port to be >= 1024 by default */
  99. #ifndef DNS_PORT_ALLOWED
  100. #define DNS_PORT_ALLOWED(port) ((port) >= 1024)
  101. #endif
  102. /** DNS maximum number of retries when asking for a name, before "timeout". */
  103. #ifndef DNS_MAX_RETRIES
  104. #define DNS_MAX_RETRIES 4
  105. #endif
  106. /** DNS resource record max. TTL (one week as default) */
  107. #ifndef DNS_MAX_TTL
  108. #define DNS_MAX_TTL 604800
  109. #elif DNS_MAX_TTL > 0x7FFFFFFF
  110. #error DNS_MAX_TTL must be a positive 32-bit value
  111. #endif
  112. #if DNS_TABLE_SIZE > 255
  113. #error DNS_TABLE_SIZE must fit into an u8_t
  114. #endif
  115. #if DNS_MAX_SERVERS > 255
  116. #error DNS_MAX_SERVERS must fit into an u8_t
  117. #endif
  118. /* The number of parallel requests (i.e. calls to dns_gethostbyname
  119. * that cannot be answered from the DNS table.
  120. * This is set to the table size by default.
  121. */
  122. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
  123. #ifndef DNS_MAX_REQUESTS
  124. #define DNS_MAX_REQUESTS DNS_TABLE_SIZE
  125. #else
  126. #if DNS_MAX_REQUESTS > 255
  127. #error DNS_MAX_REQUESTS must fit into an u8_t
  128. #endif
  129. #endif
  130. #else
  131. /* In this configuration, both arrays have to have the same size and are used
  132. * like one entry (used/free) */
  133. #define DNS_MAX_REQUESTS DNS_TABLE_SIZE
  134. #endif
  135. /* The number of UDP source ports used in parallel */
  136. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  137. #ifndef DNS_MAX_SOURCE_PORTS
  138. #define DNS_MAX_SOURCE_PORTS DNS_MAX_REQUESTS
  139. #else
  140. #if DNS_MAX_SOURCE_PORTS > 255
  141. #error DNS_MAX_SOURCE_PORTS must fit into an u8_t
  142. #endif
  143. #endif
  144. #else
  145. #ifdef DNS_MAX_SOURCE_PORTS
  146. #undef DNS_MAX_SOURCE_PORTS
  147. #endif
  148. #define DNS_MAX_SOURCE_PORTS 1
  149. #endif
  150. #if LWIP_IPV4 && LWIP_IPV6
  151. #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) (((t) == LWIP_DNS_ADDRTYPE_IPV6_IPV4) || ((t) == LWIP_DNS_ADDRTYPE_IPV6))
  152. #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)))
  153. #define LWIP_DNS_ADDRTYPE_ARG(x) , x
  154. #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) x
  155. #define LWIP_DNS_SET_ADDRTYPE(x, y) do { x = y; } while(0)
  156. #else
  157. #if LWIP_IPV6
  158. #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 1
  159. #else
  160. #define LWIP_DNS_ADDRTYPE_IS_IPV6(t) 0
  161. #endif
  162. #define LWIP_DNS_ADDRTYPE_MATCH_IP(t, ip) 1
  163. #define LWIP_DNS_ADDRTYPE_ARG(x)
  164. #define LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(x) 0
  165. #define LWIP_DNS_SET_ADDRTYPE(x, y)
  166. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  167. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  168. #define LWIP_DNS_ISMDNS_ARG(x) , x
  169. #else
  170. #define LWIP_DNS_ISMDNS_ARG(x)
  171. #endif
  172. /** DNS query message structure.
  173. No packing needed: only used locally on the stack. */
  174. struct dns_query {
  175. /* DNS query record starts with either a domain name or a pointer
  176. to a name already present somewhere in the packet. */
  177. u16_t type;
  178. u16_t cls;
  179. };
  180. #define SIZEOF_DNS_QUERY 4
  181. /** DNS answer message structure.
  182. No packing needed: only used locally on the stack. */
  183. struct dns_answer {
  184. /* DNS answer record starts with either a domain name or a pointer
  185. to a name already present somewhere in the packet. */
  186. u16_t type;
  187. u16_t cls;
  188. u32_t ttl;
  189. u16_t len;
  190. };
  191. #define SIZEOF_DNS_ANSWER 10
  192. /* maximum allowed size for the struct due to non-packed */
  193. #define SIZEOF_DNS_ANSWER_ASSERT 12
  194. /* DNS table entry states */
  195. typedef enum {
  196. DNS_STATE_UNUSED = 0,
  197. DNS_STATE_NEW = 1,
  198. DNS_STATE_ASKING = 2,
  199. DNS_STATE_DONE = 3
  200. } dns_state_enum_t;
  201. /** DNS table entry */
  202. struct dns_table_entry {
  203. u32_t ttl;
  204. ip_addr_t ipaddr;
  205. u16_t txid;
  206. u8_t state;
  207. u8_t server_idx;
  208. u8_t tmr;
  209. u8_t retries;
  210. u8_t seqno;
  211. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  212. u8_t pcb_idx;
  213. #endif
  214. char name[DNS_MAX_NAME_LENGTH];
  215. #if LWIP_IPV4 && LWIP_IPV6
  216. u8_t reqaddrtype;
  217. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  218. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  219. u8_t is_mdns;
  220. #endif
  221. };
  222. /** DNS request table entry: used when dns_gehostbyname cannot answer the
  223. * request from the DNS table */
  224. struct dns_req_entry {
  225. /* pointer to callback on DNS query done */
  226. dns_found_callback found;
  227. /* argument passed to the callback function */
  228. void *arg;
  229. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
  230. u8_t dns_table_idx;
  231. #endif
  232. #if LWIP_IPV4 && LWIP_IPV6
  233. u8_t reqaddrtype;
  234. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  235. };
  236. #if DNS_LOCAL_HOSTLIST
  237. #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
  238. /** Local host-list. For hostnames in this list, no
  239. * external name resolution is performed */
  240. static struct local_hostlist_entry *local_hostlist_dynamic;
  241. #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
  242. /** Defining this allows the local_hostlist_static to be placed in a different
  243. * linker section (e.g. FLASH) */
  244. #ifndef DNS_LOCAL_HOSTLIST_STORAGE_PRE
  245. #define DNS_LOCAL_HOSTLIST_STORAGE_PRE static
  246. #endif /* DNS_LOCAL_HOSTLIST_STORAGE_PRE */
  247. /** Defining this allows the local_hostlist_static to be placed in a different
  248. * linker section (e.g. FLASH) */
  249. #ifndef DNS_LOCAL_HOSTLIST_STORAGE_POST
  250. #define DNS_LOCAL_HOSTLIST_STORAGE_POST
  251. #endif /* DNS_LOCAL_HOSTLIST_STORAGE_POST */
  252. DNS_LOCAL_HOSTLIST_STORAGE_PRE struct local_hostlist_entry local_hostlist_static[]
  253. DNS_LOCAL_HOSTLIST_STORAGE_POST = DNS_LOCAL_HOSTLIST_INIT;
  254. #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
  255. static void dns_init_local(void);
  256. static err_t dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype));
  257. #endif /* DNS_LOCAL_HOSTLIST */
  258. /* forward declarations */
  259. static void dns_recv(void *s, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
  260. static void dns_check_entries(void);
  261. static void dns_call_found(u8_t idx, ip_addr_t* addr);
  262. /*-----------------------------------------------------------------------------
  263. * Globals
  264. *----------------------------------------------------------------------------*/
  265. /* DNS variables */
  266. static struct udp_pcb *dns_pcbs[DNS_MAX_SOURCE_PORTS];
  267. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  268. static u8_t dns_last_pcb_idx;
  269. #endif
  270. static u8_t dns_seqno;
  271. static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
  272. static struct dns_req_entry dns_requests[DNS_MAX_REQUESTS];
  273. static ip_addr_t dns_servers[DNS_MAX_SERVERS];
  274. #if LWIP_IPV4
  275. const ip_addr_t dns_mquery_v4group = DNS_MQUERY_IPV4_GROUP_INIT;
  276. #endif /* LWIP_IPV4 */
  277. #if LWIP_IPV6
  278. const ip_addr_t dns_mquery_v6group = DNS_MQUERY_IPV6_GROUP_INIT;
  279. #endif /* LWIP_IPV6 */
  280. /**
  281. * Initialize the resolver: set up the UDP pcb and configure the default server
  282. * (if DNS_SERVER_ADDRESS is set).
  283. */
  284. void
  285. dns_init(void)
  286. {
  287. #ifdef DNS_SERVER_ADDRESS
  288. /* initialize default DNS server address */
  289. ip_addr_t dnsserver;
  290. DNS_SERVER_ADDRESS(&dnsserver);
  291. dns_setserver(0, &dnsserver);
  292. #endif /* DNS_SERVER_ADDRESS */
  293. LWIP_ASSERT("sanity check SIZEOF_DNS_QUERY",
  294. sizeof(struct dns_query) == SIZEOF_DNS_QUERY);
  295. LWIP_ASSERT("sanity check SIZEOF_DNS_ANSWER",
  296. sizeof(struct dns_answer) <= SIZEOF_DNS_ANSWER_ASSERT);
  297. LWIP_DEBUGF(DNS_DEBUG, ("dns_init: initializing\n"));
  298. /* if dns client not yet initialized... */
  299. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
  300. if (dns_pcbs[0] == NULL) {
  301. dns_pcbs[0] = udp_new_ip_type(IPADDR_TYPE_ANY);
  302. LWIP_ASSERT("dns_pcbs[0] != NULL", dns_pcbs[0] != NULL);
  303. /* initialize DNS table not needed (initialized to zero since it is a
  304. * global variable) */
  305. LWIP_ASSERT("For implicit initialization to work, DNS_STATE_UNUSED needs to be 0",
  306. DNS_STATE_UNUSED == 0);
  307. /* initialize DNS client */
  308. udp_bind(dns_pcbs[0], IP_ANY_TYPE, 0);
  309. udp_recv(dns_pcbs[0], dns_recv, NULL);
  310. }
  311. #endif
  312. #if DNS_LOCAL_HOSTLIST
  313. dns_init_local();
  314. #endif
  315. }
  316. /**
  317. * @ingroup dns
  318. * Initialize one of the DNS servers.
  319. *
  320. * @param numdns the index of the DNS server to set must be < DNS_MAX_SERVERS
  321. * @param dnsserver IP address of the DNS server to set
  322. */
  323. void
  324. dns_setserver(u8_t numdns, const ip_addr_t *dnsserver)
  325. {
  326. if (numdns < DNS_MAX_SERVERS) {
  327. if (dnsserver != NULL) {
  328. dns_servers[numdns] = (*dnsserver);
  329. } else {
  330. dns_servers[numdns] = *IP_ADDR_ANY;
  331. }
  332. }
  333. }
  334. /**
  335. * @ingroup dns
  336. * Obtain one of the currently configured DNS server.
  337. *
  338. * @param numdns the index of the DNS server
  339. * @return IP address of the indexed DNS server or "ip_addr_any" if the DNS
  340. * server has not been configured.
  341. */
  342. const ip_addr_t*
  343. dns_getserver(u8_t numdns)
  344. {
  345. if (numdns < DNS_MAX_SERVERS) {
  346. return &dns_servers[numdns];
  347. } else {
  348. return IP_ADDR_ANY;
  349. }
  350. }
  351. /**
  352. * The DNS resolver client timer - handle retries and timeouts and should
  353. * be called every DNS_TMR_INTERVAL milliseconds (every second by default).
  354. */
  355. void
  356. dns_tmr(void)
  357. {
  358. LWIP_DEBUGF(DNS_DEBUG, ("dns_tmr: dns_check_entries\n"));
  359. dns_check_entries();
  360. }
  361. #if DNS_LOCAL_HOSTLIST
  362. static void
  363. dns_init_local(void)
  364. {
  365. #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT)
  366. size_t i;
  367. struct local_hostlist_entry *entry;
  368. /* Dynamic: copy entries from DNS_LOCAL_HOSTLIST_INIT to list */
  369. struct local_hostlist_entry local_hostlist_init[] = DNS_LOCAL_HOSTLIST_INIT;
  370. size_t namelen;
  371. for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_init); i++) {
  372. struct local_hostlist_entry *init_entry = &local_hostlist_init[i];
  373. LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL);
  374. namelen = strlen(init_entry->name);
  375. LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
  376. entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
  377. LWIP_ASSERT("mem-error in dns_init_local", entry != NULL);
  378. if (entry != NULL) {
  379. char* entry_name = (char*)entry + sizeof(struct local_hostlist_entry);
  380. MEMCPY(entry_name, init_entry->name, namelen);
  381. entry_name[namelen] = 0;
  382. entry->name = entry_name;
  383. entry->addr = init_entry->addr;
  384. entry->next = local_hostlist_dynamic;
  385. local_hostlist_dynamic = entry;
  386. }
  387. }
  388. #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC && defined(DNS_LOCAL_HOSTLIST_INIT) */
  389. }
  390. /**
  391. * @ingroup dns
  392. * Iterate the local host-list for a hostname.
  393. *
  394. * @param iterator_fn a function that is called for every entry in the local host-list
  395. * @param iterator_arg 3rd argument passed to iterator_fn
  396. * @return the number of entries in the local host-list
  397. */
  398. size_t
  399. dns_local_iterate(dns_found_callback iterator_fn, void *iterator_arg)
  400. {
  401. size_t i;
  402. #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
  403. struct local_hostlist_entry *entry = local_hostlist_dynamic;
  404. i = 0;
  405. while (entry != NULL) {
  406. if (iterator_fn != NULL) {
  407. iterator_fn(entry->name, &entry->addr, iterator_arg);
  408. }
  409. i++;
  410. entry = entry->next;
  411. }
  412. #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
  413. for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
  414. if (iterator_fn != NULL) {
  415. iterator_fn(local_hostlist_static[i].name, &local_hostlist_static[i].addr, iterator_arg);
  416. }
  417. }
  418. #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
  419. return i;
  420. }
  421. /**
  422. * @ingroup dns
  423. * Scans the local host-list for a hostname.
  424. *
  425. * @param hostname Hostname to look for in the local host-list
  426. * @param addr the first IP address for the hostname in the local host-list or
  427. * IPADDR_NONE if not found.
  428. * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 (ATTENTION: no fallback here!)
  429. * - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 (ATTENTION: no fallback here!)
  430. * - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only
  431. * - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only
  432. * @return ERR_OK if found, ERR_ARG if not found
  433. */
  434. err_t
  435. dns_local_lookup(const char *hostname, ip_addr_t *addr, u8_t dns_addrtype)
  436. {
  437. LWIP_UNUSED_ARG(dns_addrtype);
  438. return dns_lookup_local(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype));
  439. }
  440. /* Internal implementation for dns_local_lookup and dns_lookup */
  441. static err_t
  442. dns_lookup_local(const char *hostname, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
  443. {
  444. #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
  445. struct local_hostlist_entry *entry = local_hostlist_dynamic;
  446. while (entry != NULL) {
  447. if ((lwip_stricmp(entry->name, hostname) == 0) &&
  448. LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, entry->addr)) {
  449. if (addr) {
  450. ip_addr_copy(*addr, entry->addr);
  451. }
  452. return ERR_OK;
  453. }
  454. entry = entry->next;
  455. }
  456. #else /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
  457. size_t i;
  458. for (i = 0; i < LWIP_ARRAYSIZE(local_hostlist_static); i++) {
  459. if ((lwip_stricmp(local_hostlist_static[i].name, hostname) == 0) &&
  460. LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, local_hostlist_static[i].addr)) {
  461. if (addr) {
  462. ip_addr_copy(*addr, local_hostlist_static[i].addr);
  463. }
  464. return ERR_OK;
  465. }
  466. }
  467. #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */
  468. return ERR_ARG;
  469. }
  470. #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC
  471. /**
  472. * @ingroup dns
  473. * Remove all entries from the local host-list for a specific hostname
  474. * and/or IP address
  475. *
  476. * @param hostname hostname for which entries shall be removed from the local
  477. * host-list
  478. * @param addr address for which entries shall be removed from the local host-list
  479. * @return the number of removed entries
  480. */
  481. int
  482. dns_local_removehost(const char *hostname, const ip_addr_t *addr)
  483. {
  484. int removed = 0;
  485. struct local_hostlist_entry *entry = local_hostlist_dynamic;
  486. struct local_hostlist_entry *last_entry = NULL;
  487. while (entry != NULL) {
  488. if (((hostname == NULL) || !lwip_stricmp(entry->name, hostname)) &&
  489. ((addr == NULL) || ip_addr_cmp(&entry->addr, addr))) {
  490. struct local_hostlist_entry *free_entry;
  491. if (last_entry != NULL) {
  492. last_entry->next = entry->next;
  493. } else {
  494. local_hostlist_dynamic = entry->next;
  495. }
  496. free_entry = entry;
  497. entry = entry->next;
  498. memp_free(MEMP_LOCALHOSTLIST, free_entry);
  499. removed++;
  500. } else {
  501. last_entry = entry;
  502. entry = entry->next;
  503. }
  504. }
  505. return removed;
  506. }
  507. /**
  508. * @ingroup dns
  509. * Add a hostname/IP address pair to the local host-list.
  510. * Duplicates are not checked.
  511. *
  512. * @param hostname hostname of the new entry
  513. * @param addr IP address of the new entry
  514. * @return ERR_OK if succeeded or ERR_MEM on memory error
  515. */
  516. err_t
  517. dns_local_addhost(const char *hostname, const ip_addr_t *addr)
  518. {
  519. struct local_hostlist_entry *entry;
  520. size_t namelen;
  521. char* entry_name;
  522. LWIP_ASSERT("invalid host name (NULL)", hostname != NULL);
  523. namelen = strlen(hostname);
  524. LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN);
  525. entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST);
  526. if (entry == NULL) {
  527. return ERR_MEM;
  528. }
  529. entry_name = (char*)entry + sizeof(struct local_hostlist_entry);
  530. MEMCPY(entry_name, hostname, namelen);
  531. entry_name[namelen] = 0;
  532. entry->name = entry_name;
  533. ip_addr_copy(entry->addr, *addr);
  534. entry->next = local_hostlist_dynamic;
  535. local_hostlist_dynamic = entry;
  536. return ERR_OK;
  537. }
  538. #endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC*/
  539. #endif /* DNS_LOCAL_HOSTLIST */
  540. /**
  541. * @ingroup dns
  542. * Look up a hostname in the array of known hostnames.
  543. *
  544. * @note This function only looks in the internal array of known
  545. * hostnames, it does not send out a query for the hostname if none
  546. * was found. The function dns_enqueue() can be used to send a query
  547. * for a hostname.
  548. *
  549. * @param name the hostname to look up
  550. * @param addr the hostname's IP address, as u32_t (instead of ip_addr_t to
  551. * better check for failure: != IPADDR_NONE) or IPADDR_NONE if the hostname
  552. * was not found in the cached dns_table.
  553. * @return ERR_OK if found, ERR_ARG if not found
  554. */
  555. static err_t
  556. dns_lookup(const char *name, ip_addr_t *addr LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype))
  557. {
  558. u8_t i;
  559. #if DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN)
  560. #endif /* DNS_LOCAL_HOSTLIST || defined(DNS_LOOKUP_LOCAL_EXTERN) */
  561. #if DNS_LOCAL_HOSTLIST
  562. if (dns_lookup_local(name, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) {
  563. return ERR_OK;
  564. }
  565. #endif /* DNS_LOCAL_HOSTLIST */
  566. #ifdef DNS_LOOKUP_LOCAL_EXTERN
  567. if (DNS_LOOKUP_LOCAL_EXTERN(name, addr, LWIP_DNS_ADDRTYPE_ARG_OR_ZERO(dns_addrtype)) == ERR_OK) {
  568. return ERR_OK;
  569. }
  570. #endif /* DNS_LOOKUP_LOCAL_EXTERN */
  571. /* Walk through name list, return entry if found. If not, return NULL. */
  572. for (i = 0; i < DNS_TABLE_SIZE; ++i) {
  573. if ((dns_table[i].state == DNS_STATE_DONE) &&
  574. (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0) &&
  575. LWIP_DNS_ADDRTYPE_MATCH_IP(dns_addrtype, dns_table[i].ipaddr)) {
  576. LWIP_DEBUGF(DNS_DEBUG, ("dns_lookup: \"%s\": found = ", name));
  577. ip_addr_debug_print(DNS_DEBUG, &(dns_table[i].ipaddr));
  578. LWIP_DEBUGF(DNS_DEBUG, ("\n"));
  579. if (addr) {
  580. ip_addr_copy(*addr, dns_table[i].ipaddr);
  581. }
  582. return ERR_OK;
  583. }
  584. }
  585. return ERR_ARG;
  586. }
  587. /**
  588. * Compare the "dotted" name "query" with the encoded name "response"
  589. * to make sure an answer from the DNS server matches the current dns_table
  590. * entry (otherwise, answers might arrive late for hostname not on the list
  591. * any more).
  592. *
  593. * @param query hostname (not encoded) from the dns_table
  594. * @param p pbuf containing the encoded hostname in the DNS response
  595. * @param start_offset offset into p where the name starts
  596. * @return 0xFFFF: names differ, other: names equal -> offset behind name
  597. */
  598. static u16_t
  599. dns_compare_name(const char *query, struct pbuf* p, u16_t start_offset)
  600. {
  601. int n;
  602. u16_t response_offset = start_offset;
  603. do {
  604. n = pbuf_try_get_at(p, response_offset++);
  605. if (n < 0) {
  606. return 0xFFFF;
  607. }
  608. /** @see RFC 1035 - 4.1.4. Message compression */
  609. if ((n & 0xc0) == 0xc0) {
  610. /* Compressed name: cannot be equal since we don't send them */
  611. return 0xFFFF;
  612. } else {
  613. /* Not compressed name */
  614. while (n > 0) {
  615. int c = pbuf_try_get_at(p, response_offset);
  616. if (c < 0) {
  617. return 0xFFFF;
  618. }
  619. if ((*query) != (u8_t)c) {
  620. return 0xFFFF;
  621. }
  622. ++response_offset;
  623. ++query;
  624. --n;
  625. }
  626. ++query;
  627. }
  628. n = pbuf_try_get_at(p, response_offset);
  629. if (n < 0) {
  630. return 0xFFFF;
  631. }
  632. } while (n != 0);
  633. return response_offset + 1;
  634. }
  635. /**
  636. * Walk through a compact encoded DNS name and return the end of the name.
  637. *
  638. * @param p pbuf containing the name
  639. * @param query_idx start index into p pointing to encoded DNS name in the DNS server response
  640. * @return index to end of the name
  641. */
  642. static u16_t
  643. dns_skip_name(struct pbuf* p, u16_t query_idx)
  644. {
  645. int n;
  646. u16_t offset = query_idx;
  647. do {
  648. n = pbuf_try_get_at(p, offset++);
  649. if (n < 0) {
  650. return 0xFFFF;
  651. }
  652. /** @see RFC 1035 - 4.1.4. Message compression */
  653. if ((n & 0xc0) == 0xc0) {
  654. /* Compressed name: since we only want to skip it (not check it), stop here */
  655. break;
  656. } else {
  657. /* Not compressed name */
  658. if (offset + n >= p->tot_len) {
  659. return 0xFFFF;
  660. }
  661. offset = (u16_t)(offset + n);
  662. }
  663. n = pbuf_try_get_at(p, offset);
  664. if (n < 0) {
  665. return 0xFFFF;
  666. }
  667. } while (n != 0);
  668. return offset + 1;
  669. }
  670. /**
  671. * Send a DNS query packet.
  672. *
  673. * @param idx the DNS table entry index for which to send a request
  674. * @return ERR_OK if packet is sent; an err_t indicating the problem otherwise
  675. */
  676. static err_t
  677. dns_send(u8_t idx)
  678. {
  679. err_t err;
  680. struct dns_hdr hdr;
  681. struct dns_query qry;
  682. struct pbuf *p;
  683. u16_t query_idx, copy_len;
  684. const char *hostname, *hostname_part;
  685. u8_t n;
  686. u8_t pcb_idx;
  687. struct dns_table_entry* entry = &dns_table[idx];
  688. LWIP_DEBUGF(DNS_DEBUG, ("dns_send: dns_servers[%"U16_F"] \"%s\": request\n",
  689. (u16_t)(entry->server_idx), entry->name));
  690. LWIP_ASSERT("dns server out of array", entry->server_idx < DNS_MAX_SERVERS);
  691. if (ip_addr_isany_val(dns_servers[entry->server_idx])
  692. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  693. && !entry->is_mdns
  694. #endif
  695. ) {
  696. /* DNS server not valid anymore, e.g. PPP netif has been shut down */
  697. /* call specified callback function if provided */
  698. dns_call_found(idx, NULL);
  699. /* flush this entry */
  700. entry->state = DNS_STATE_UNUSED;
  701. return ERR_OK;
  702. }
  703. /* if here, we have either a new query or a retry on a previous query to process */
  704. p = pbuf_alloc(PBUF_TRANSPORT, (u16_t)(SIZEOF_DNS_HDR + strlen(entry->name) + 2 +
  705. SIZEOF_DNS_QUERY), PBUF_RAM);
  706. if (p != NULL) {
  707. const ip_addr_t* dst;
  708. u16_t dst_port;
  709. /* fill dns header */
  710. memset(&hdr, 0, SIZEOF_DNS_HDR);
  711. hdr.id = lwip_htons(entry->txid);
  712. hdr.flags1 = DNS_FLAG1_RD;
  713. hdr.numquestions = PP_HTONS(1);
  714. pbuf_take(p, &hdr, SIZEOF_DNS_HDR);
  715. hostname = entry->name;
  716. --hostname;
  717. /* convert hostname into suitable query format. */
  718. query_idx = SIZEOF_DNS_HDR;
  719. do {
  720. ++hostname;
  721. hostname_part = hostname;
  722. for (n = 0; *hostname != '.' && *hostname != 0; ++hostname) {
  723. ++n;
  724. }
  725. copy_len = (u16_t)(hostname - hostname_part);
  726. pbuf_put_at(p, query_idx, n);
  727. pbuf_take_at(p, hostname_part, copy_len, query_idx + 1);
  728. query_idx += n + 1;
  729. } while (*hostname != 0);
  730. pbuf_put_at(p, query_idx, 0);
  731. query_idx++;
  732. /* fill dns query */
  733. if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype)) {
  734. qry.type = PP_HTONS(DNS_RRTYPE_AAAA);
  735. } else {
  736. qry.type = PP_HTONS(DNS_RRTYPE_A);
  737. }
  738. qry.cls = PP_HTONS(DNS_RRCLASS_IN);
  739. pbuf_take_at(p, &qry, SIZEOF_DNS_QUERY, query_idx);
  740. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  741. pcb_idx = entry->pcb_idx;
  742. #else
  743. pcb_idx = 0;
  744. #endif
  745. /* send dns packet */
  746. LWIP_DEBUGF(DNS_DEBUG, ("sending DNS request ID %d for name \"%s\" to server %d\r\n",
  747. entry->txid, entry->name, entry->server_idx));
  748. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  749. if (entry->is_mdns) {
  750. dst_port = DNS_MQUERY_PORT;
  751. #if LWIP_IPV6
  752. if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
  753. {
  754. dst = &dns_mquery_v6group;
  755. }
  756. #endif
  757. #if LWIP_IPV4 && LWIP_IPV6
  758. else
  759. #endif
  760. #if LWIP_IPV4
  761. {
  762. dst = &dns_mquery_v4group;
  763. }
  764. #endif
  765. } else
  766. #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
  767. {
  768. dst_port = DNS_SERVER_PORT;
  769. dst = &dns_servers[entry->server_idx];
  770. }
  771. err = udp_sendto(dns_pcbs[pcb_idx], p, dst, dst_port);
  772. /* free pbuf */
  773. pbuf_free(p);
  774. } else {
  775. err = ERR_MEM;
  776. }
  777. return err;
  778. }
  779. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  780. static struct udp_pcb*
  781. dns_alloc_random_port(void)
  782. {
  783. err_t err;
  784. struct udp_pcb* ret;
  785. ret = udp_new_ip_type(IPADDR_TYPE_ANY);
  786. if (ret == NULL) {
  787. /* out of memory, have to reuse an existing pcb */
  788. return NULL;
  789. }
  790. do {
  791. u16_t port = (u16_t)DNS_RAND_TXID();
  792. if (!DNS_PORT_ALLOWED(port)) {
  793. /* this port is not allowed, try again */
  794. err = ERR_USE;
  795. continue;
  796. }
  797. err = udp_bind(ret, IP_ANY_TYPE, port);
  798. } while (err == ERR_USE);
  799. if (err != ERR_OK) {
  800. udp_remove(ret);
  801. return NULL;
  802. }
  803. udp_recv(ret, dns_recv, NULL);
  804. return ret;
  805. }
  806. /**
  807. * dns_alloc_pcb() - allocates a new pcb (or reuses an existing one) to be used
  808. * for sending a request
  809. *
  810. * @return an index into dns_pcbs
  811. */
  812. static u8_t
  813. dns_alloc_pcb(void)
  814. {
  815. u8_t i;
  816. u8_t idx;
  817. for (i = 0; i < DNS_MAX_SOURCE_PORTS; i++) {
  818. if (dns_pcbs[i] == NULL) {
  819. break;
  820. }
  821. }
  822. if (i < DNS_MAX_SOURCE_PORTS) {
  823. dns_pcbs[i] = dns_alloc_random_port();
  824. if (dns_pcbs[i] != NULL) {
  825. /* succeeded */
  826. dns_last_pcb_idx = i;
  827. return i;
  828. }
  829. }
  830. /* if we come here, creating a new UDP pcb failed, so we have to use
  831. an already existing one */
  832. for (i = 0, idx = dns_last_pcb_idx + 1; i < DNS_MAX_SOURCE_PORTS; i++, idx++) {
  833. if (idx >= DNS_MAX_SOURCE_PORTS) {
  834. idx = 0;
  835. }
  836. if (dns_pcbs[idx] != NULL) {
  837. dns_last_pcb_idx = idx;
  838. return idx;
  839. }
  840. }
  841. return DNS_MAX_SOURCE_PORTS;
  842. }
  843. #endif /* ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0) */
  844. /**
  845. * dns_call_found() - call the found callback and check if there are duplicate
  846. * entries for the given hostname. If there are any, their found callback will
  847. * be called and they will be removed.
  848. *
  849. * @param idx dns table index of the entry that is resolved or removed
  850. * @param addr IP address for the hostname (or NULL on error or memory shortage)
  851. */
  852. static void
  853. dns_call_found(u8_t idx, ip_addr_t* addr)
  854. {
  855. #if ((LWIP_DNS_SECURE & (LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING | LWIP_DNS_SECURE_RAND_SRC_PORT)) != 0)
  856. u8_t i;
  857. #endif
  858. #if LWIP_IPV4 && LWIP_IPV6
  859. if (addr != NULL) {
  860. /* check that address type matches the request and adapt the table entry */
  861. if (IP_IS_V6_VAL(*addr)) {
  862. LWIP_ASSERT("invalid response", LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
  863. dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
  864. } else {
  865. LWIP_ASSERT("invalid response", !LWIP_DNS_ADDRTYPE_IS_IPV6(dns_table[idx].reqaddrtype));
  866. dns_table[idx].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
  867. }
  868. }
  869. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  870. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
  871. for (i = 0; i < DNS_MAX_REQUESTS; i++) {
  872. if (dns_requests[i].found && (dns_requests[i].dns_table_idx == idx)) {
  873. (*dns_requests[i].found)(dns_table[idx].name, addr, dns_requests[i].arg);
  874. /* flush this entry */
  875. dns_requests[i].found = NULL;
  876. }
  877. }
  878. #else
  879. if (dns_requests[idx].found) {
  880. (*dns_requests[idx].found)(dns_table[idx].name, addr, dns_requests[idx].arg);
  881. }
  882. dns_requests[idx].found = NULL;
  883. #endif
  884. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  885. /* close the pcb used unless other request are using it */
  886. for (i = 0; i < DNS_MAX_REQUESTS; i++) {
  887. if (i == idx) {
  888. continue; /* only check other requests */
  889. }
  890. if (dns_table[i].state == DNS_STATE_ASKING) {
  891. if (dns_table[i].pcb_idx == dns_table[idx].pcb_idx) {
  892. /* another request is still using the same pcb */
  893. dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
  894. break;
  895. }
  896. }
  897. }
  898. if (dns_table[idx].pcb_idx < DNS_MAX_SOURCE_PORTS) {
  899. /* if we come here, the pcb is not used any more and can be removed */
  900. udp_remove(dns_pcbs[dns_table[idx].pcb_idx]);
  901. dns_pcbs[dns_table[idx].pcb_idx] = NULL;
  902. dns_table[idx].pcb_idx = DNS_MAX_SOURCE_PORTS;
  903. }
  904. #endif
  905. }
  906. /* Create a query transmission ID that is unique for all outstanding queries */
  907. static u16_t
  908. dns_create_txid(void)
  909. {
  910. u16_t txid;
  911. u8_t i;
  912. again:
  913. txid = (u16_t)DNS_RAND_TXID();
  914. /* check whether the ID is unique */
  915. for (i = 0; i < DNS_TABLE_SIZE; i++) {
  916. if ((dns_table[i].state == DNS_STATE_ASKING) &&
  917. (dns_table[i].txid == txid)) {
  918. /* ID already used by another pending query */
  919. goto again;
  920. }
  921. }
  922. return txid;
  923. }
  924. /**
  925. * dns_check_entry() - see if entry has not yet been queried and, if so, sends out a query.
  926. * Check an entry in the dns_table:
  927. * - send out query for new entries
  928. * - retry old pending entries on timeout (also with different servers)
  929. * - remove completed entries from the table if their TTL has expired
  930. *
  931. * @param i index of the dns_table entry to check
  932. */
  933. static void
  934. dns_check_entry(u8_t i)
  935. {
  936. err_t err;
  937. struct dns_table_entry *entry = &dns_table[i];
  938. LWIP_ASSERT("array index out of bounds", i < DNS_TABLE_SIZE);
  939. switch (entry->state) {
  940. case DNS_STATE_NEW:
  941. /* initialize new entry */
  942. entry->txid = dns_create_txid();
  943. entry->state = DNS_STATE_ASKING;
  944. entry->server_idx = 0;
  945. entry->tmr = 1;
  946. entry->retries = 0;
  947. /* send DNS packet for this entry */
  948. err = dns_send(i);
  949. if (err != ERR_OK) {
  950. LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
  951. ("dns_send returned error: %s\n", lwip_strerr(err)));
  952. }
  953. break;
  954. case DNS_STATE_ASKING:
  955. if (--entry->tmr == 0) {
  956. if (++entry->retries == DNS_MAX_RETRIES) {
  957. if ((entry->server_idx + 1 < DNS_MAX_SERVERS) && !ip_addr_isany_val(dns_servers[entry->server_idx + 1])
  958. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  959. && !entry->is_mdns
  960. #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
  961. ) {
  962. /* change of server */
  963. entry->server_idx++;
  964. entry->tmr = 1;
  965. entry->retries = 0;
  966. } else {
  967. LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": timeout\n", entry->name));
  968. /* call specified callback function if provided */
  969. dns_call_found(i, NULL);
  970. /* flush this entry */
  971. entry->state = DNS_STATE_UNUSED;
  972. break;
  973. }
  974. } else {
  975. /* wait longer for the next retry */
  976. entry->tmr = entry->retries;
  977. }
  978. /* send DNS packet for this entry */
  979. err = dns_send(i);
  980. if (err != ERR_OK) {
  981. LWIP_DEBUGF(DNS_DEBUG | LWIP_DBG_LEVEL_WARNING,
  982. ("dns_send returned error: %s\n", lwip_strerr(err)));
  983. }
  984. }
  985. break;
  986. case DNS_STATE_DONE:
  987. /* if the time to live is nul */
  988. if ((entry->ttl == 0) || (--entry->ttl == 0)) {
  989. LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", entry->name));
  990. /* flush this entry, there cannot be any related pending entries in this state */
  991. entry->state = DNS_STATE_UNUSED;
  992. }
  993. break;
  994. case DNS_STATE_UNUSED:
  995. /* nothing to do */
  996. break;
  997. default:
  998. LWIP_ASSERT("unknown dns_table entry state:", 0);
  999. break;
  1000. }
  1001. }
  1002. /**
  1003. * Call dns_check_entry for each entry in dns_table - check all entries.
  1004. */
  1005. static void
  1006. dns_check_entries(void)
  1007. {
  1008. u8_t i;
  1009. for (i = 0; i < DNS_TABLE_SIZE; ++i) {
  1010. dns_check_entry(i);
  1011. }
  1012. }
  1013. /**
  1014. * Save TTL and call dns_call_found for correct response.
  1015. */
  1016. static void
  1017. dns_correct_response(u8_t idx, u32_t ttl)
  1018. {
  1019. struct dns_table_entry *entry = &dns_table[idx];
  1020. entry->state = DNS_STATE_DONE;
  1021. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name));
  1022. ip_addr_debug_print(DNS_DEBUG, (&(entry->ipaddr)));
  1023. LWIP_DEBUGF(DNS_DEBUG, ("\n"));
  1024. /* read the answer resource record's TTL, and maximize it if needed */
  1025. entry->ttl = ttl;
  1026. if (entry->ttl > DNS_MAX_TTL) {
  1027. entry->ttl = DNS_MAX_TTL;
  1028. }
  1029. dns_call_found(idx, &entry->ipaddr);
  1030. if (entry->ttl == 0) {
  1031. /* RFC 883, page 29: "Zero values are
  1032. interpreted to mean that the RR can only be used for the
  1033. transaction in progress, and should not be cached."
  1034. -> flush this entry now */
  1035. /* entry reused during callback? */
  1036. if (entry->state == DNS_STATE_DONE) {
  1037. entry->state = DNS_STATE_UNUSED;
  1038. }
  1039. }
  1040. }
  1041. /**
  1042. * Receive input function for DNS response packets arriving for the dns UDP pcb.
  1043. */
  1044. static void
  1045. dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  1046. {
  1047. u8_t i;
  1048. u16_t txid;
  1049. u16_t res_idx;
  1050. struct dns_hdr hdr;
  1051. struct dns_answer ans;
  1052. struct dns_query qry;
  1053. u16_t nquestions, nanswers;
  1054. LWIP_UNUSED_ARG(arg);
  1055. LWIP_UNUSED_ARG(pcb);
  1056. LWIP_UNUSED_ARG(port);
  1057. /* is the dns message big enough ? */
  1058. if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY)) {
  1059. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));
  1060. /* free pbuf and return */
  1061. goto memerr;
  1062. }
  1063. /* copy dns payload inside static buffer for processing */
  1064. if (pbuf_copy_partial(p, &hdr, SIZEOF_DNS_HDR, 0) == SIZEOF_DNS_HDR) {
  1065. /* Match the ID in the DNS header with the name table. */
  1066. txid = lwip_htons(hdr.id);
  1067. for (i = 0; i < DNS_TABLE_SIZE; i++) {
  1068. const struct dns_table_entry *entry = &dns_table[i];
  1069. if ((entry->state == DNS_STATE_ASKING) &&
  1070. (entry->txid == txid)) {
  1071. /* We only care about the question(s) and the answers. The authrr
  1072. and the extrarr are simply discarded. */
  1073. nquestions = lwip_htons(hdr.numquestions);
  1074. nanswers = lwip_htons(hdr.numanswers);
  1075. /* Check for correct response. */
  1076. if ((hdr.flags1 & DNS_FLAG1_RESPONSE) == 0) {
  1077. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": not a response\n", entry->name));
  1078. goto memerr; /* ignore this packet */
  1079. }
  1080. if (nquestions != 1) {
  1081. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
  1082. goto memerr; /* ignore this packet */
  1083. }
  1084. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  1085. if (!entry->is_mdns)
  1086. #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
  1087. {
  1088. /* Check whether response comes from the same network address to which the
  1089. question was sent. (RFC 5452) */
  1090. if (!ip_addr_cmp(addr, &dns_servers[entry->server_idx])) {
  1091. goto memerr; /* ignore this packet */
  1092. }
  1093. }
  1094. /* Check if the name in the "question" part match with the name in the entry and
  1095. skip it if equal. */
  1096. res_idx = dns_compare_name(entry->name, p, SIZEOF_DNS_HDR);
  1097. if (res_idx == 0xFFFF) {
  1098. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
  1099. goto memerr; /* ignore this packet */
  1100. }
  1101. /* check if "question" part matches the request */
  1102. if (pbuf_copy_partial(p, &qry, SIZEOF_DNS_QUERY, res_idx) != SIZEOF_DNS_QUERY) {
  1103. goto memerr; /* ignore this packet */
  1104. }
  1105. if ((qry.cls != PP_HTONS(DNS_RRCLASS_IN)) ||
  1106. (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_AAAA))) ||
  1107. (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype) && (qry.type != PP_HTONS(DNS_RRTYPE_A)))) {
  1108. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response not match to query\n", entry->name));
  1109. goto memerr; /* ignore this packet */
  1110. }
  1111. /* skip the rest of the "question" part */
  1112. res_idx += SIZEOF_DNS_QUERY;
  1113. /* Check for error. If so, call callback to inform. */
  1114. if (hdr.flags2 & DNS_FLAG2_ERR_MASK) {
  1115. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in flags\n", entry->name));
  1116. } else {
  1117. while ((nanswers > 0) && (res_idx < p->tot_len)) {
  1118. /* skip answer resource record's host name */
  1119. res_idx = dns_skip_name(p, res_idx);
  1120. if (res_idx == 0xFFFF) {
  1121. goto memerr; /* ignore this packet */
  1122. }
  1123. /* Check for IP address type and Internet class. Others are discarded. */
  1124. if (pbuf_copy_partial(p, &ans, SIZEOF_DNS_ANSWER, res_idx) != SIZEOF_DNS_ANSWER) {
  1125. goto memerr; /* ignore this packet */
  1126. }
  1127. res_idx += SIZEOF_DNS_ANSWER;
  1128. if (ans.cls == PP_HTONS(DNS_RRCLASS_IN)) {
  1129. #if LWIP_IPV4
  1130. if ((ans.type == PP_HTONS(DNS_RRTYPE_A)) && (ans.len == PP_HTONS(sizeof(ip4_addr_t)))) {
  1131. #if LWIP_IPV4 && LWIP_IPV6
  1132. if (!LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
  1133. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1134. {
  1135. ip4_addr_t ip4addr;
  1136. /* read the IP address after answer resource record's header */
  1137. if (pbuf_copy_partial(p, &ip4addr, sizeof(ip4_addr_t), res_idx) != sizeof(ip4_addr_t)) {
  1138. goto memerr; /* ignore this packet */
  1139. }
  1140. ip_addr_copy_from_ip4(dns_table[i].ipaddr, ip4addr);
  1141. pbuf_free(p);
  1142. /* handle correct response */
  1143. dns_correct_response(i, lwip_ntohl(ans.ttl));
  1144. return;
  1145. }
  1146. }
  1147. #endif /* LWIP_IPV4 */
  1148. #if LWIP_IPV6
  1149. if ((ans.type == PP_HTONS(DNS_RRTYPE_AAAA)) && (ans.len == PP_HTONS(sizeof(ip6_addr_t)))) {
  1150. #if LWIP_IPV4 && LWIP_IPV6
  1151. if (LWIP_DNS_ADDRTYPE_IS_IPV6(entry->reqaddrtype))
  1152. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1153. {
  1154. ip6_addr_t ip6addr;
  1155. /* read the IP address after answer resource record's header */
  1156. if (pbuf_copy_partial(p, &ip6addr, sizeof(ip6_addr_t), res_idx) != sizeof(ip6_addr_t)) {
  1157. goto memerr; /* ignore this packet */
  1158. }
  1159. ip_addr_copy_from_ip6(dns_table[i].ipaddr, ip6addr);
  1160. pbuf_free(p);
  1161. /* handle correct response */
  1162. dns_correct_response(i, lwip_ntohl(ans.ttl));
  1163. return;
  1164. }
  1165. }
  1166. #endif /* LWIP_IPV6 */
  1167. }
  1168. /* skip this answer */
  1169. if ((int)(res_idx + lwip_htons(ans.len)) > 0xFFFF) {
  1170. goto memerr; /* ignore this packet */
  1171. }
  1172. res_idx += lwip_htons(ans.len);
  1173. --nanswers;
  1174. }
  1175. #if LWIP_IPV4 && LWIP_IPV6
  1176. if ((entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) ||
  1177. (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
  1178. if (entry->reqaddrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
  1179. /* IPv4 failed, try IPv6 */
  1180. dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV6;
  1181. } else {
  1182. /* IPv6 failed, try IPv4 */
  1183. dns_table[i].reqaddrtype = LWIP_DNS_ADDRTYPE_IPV4;
  1184. }
  1185. pbuf_free(p);
  1186. dns_table[i].state = DNS_STATE_NEW;
  1187. dns_check_entry(i);
  1188. return;
  1189. }
  1190. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1191. LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": error in response\n", entry->name));
  1192. }
  1193. /* call callback to indicate error, clean up memory and return */
  1194. pbuf_free(p);
  1195. dns_call_found(i, NULL);
  1196. dns_table[i].state = DNS_STATE_UNUSED;
  1197. return;
  1198. }
  1199. }
  1200. }
  1201. memerr:
  1202. /* deallocate memory and return */
  1203. pbuf_free(p);
  1204. return;
  1205. }
  1206. /**
  1207. * Queues a new hostname to resolve and sends out a DNS query for that hostname
  1208. *
  1209. * @param name the hostname that is to be queried
  1210. * @param hostnamelen length of the hostname
  1211. * @param found a callback function to be called on success, failure or timeout
  1212. * @param callback_arg argument to pass to the callback function
  1213. * @return err_t return code.
  1214. */
  1215. static err_t
  1216. dns_enqueue(const char *name, size_t hostnamelen, dns_found_callback found,
  1217. void *callback_arg LWIP_DNS_ADDRTYPE_ARG(u8_t dns_addrtype) LWIP_DNS_ISMDNS_ARG(u8_t is_mdns))
  1218. {
  1219. u8_t i;
  1220. u8_t lseq, lseqi;
  1221. struct dns_table_entry *entry = NULL;
  1222. size_t namelen;
  1223. struct dns_req_entry* req;
  1224. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
  1225. u8_t r;
  1226. /* check for duplicate entries */
  1227. for (i = 0; i < DNS_TABLE_SIZE; i++) {
  1228. if ((dns_table[i].state == DNS_STATE_ASKING) &&
  1229. (lwip_strnicmp(name, dns_table[i].name, sizeof(dns_table[i].name)) == 0)) {
  1230. #if LWIP_IPV4 && LWIP_IPV6
  1231. if (dns_table[i].reqaddrtype != dns_addrtype) {
  1232. /* requested address types don't match
  1233. this can lead to 2 concurrent requests, but mixing the address types
  1234. for the same host should not be that common */
  1235. continue;
  1236. }
  1237. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1238. /* this is a duplicate entry, find a free request entry */
  1239. for (r = 0; r < DNS_MAX_REQUESTS; r++) {
  1240. if (dns_requests[r].found == 0) {
  1241. dns_requests[r].found = found;
  1242. dns_requests[r].arg = callback_arg;
  1243. dns_requests[r].dns_table_idx = i;
  1244. LWIP_DNS_SET_ADDRTYPE(dns_requests[r].reqaddrtype, dns_addrtype);
  1245. LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": duplicate request\n", name));
  1246. return ERR_INPROGRESS;
  1247. }
  1248. }
  1249. }
  1250. }
  1251. /* no duplicate entries found */
  1252. #endif
  1253. /* search an unused entry, or the oldest one */
  1254. lseq = 0;
  1255. lseqi = DNS_TABLE_SIZE;
  1256. for (i = 0; i < DNS_TABLE_SIZE; ++i) {
  1257. entry = &dns_table[i];
  1258. /* is it an unused entry ? */
  1259. if (entry->state == DNS_STATE_UNUSED) {
  1260. break;
  1261. }
  1262. /* check if this is the oldest completed entry */
  1263. if (entry->state == DNS_STATE_DONE) {
  1264. u8_t age = dns_seqno - entry->seqno;
  1265. if (age > lseq) {
  1266. lseq = age;
  1267. lseqi = i;
  1268. }
  1269. }
  1270. }
  1271. /* if we don't have found an unused entry, use the oldest completed one */
  1272. if (i == DNS_TABLE_SIZE) {
  1273. if ((lseqi >= DNS_TABLE_SIZE) || (dns_table[lseqi].state != DNS_STATE_DONE)) {
  1274. /* no entry can be used now, table is full */
  1275. LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS entries table is full\n", name));
  1276. return ERR_MEM;
  1277. } else {
  1278. /* use the oldest completed one */
  1279. i = lseqi;
  1280. entry = &dns_table[i];
  1281. }
  1282. }
  1283. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_NO_MULTIPLE_OUTSTANDING) != 0)
  1284. /* find a free request entry */
  1285. req = NULL;
  1286. for (r = 0; r < DNS_MAX_REQUESTS; r++) {
  1287. if (dns_requests[r].found == NULL) {
  1288. req = &dns_requests[r];
  1289. break;
  1290. }
  1291. }
  1292. if (req == NULL) {
  1293. /* no request entry can be used now, table is full */
  1294. LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": DNS request entries table is full\n", name));
  1295. return ERR_MEM;
  1296. }
  1297. req->dns_table_idx = i;
  1298. #else
  1299. /* in this configuration, the entry index is the same as the request index */
  1300. req = &dns_requests[i];
  1301. #endif
  1302. /* use this entry */
  1303. LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i)));
  1304. /* fill the entry */
  1305. entry->state = DNS_STATE_NEW;
  1306. entry->seqno = dns_seqno;
  1307. LWIP_DNS_SET_ADDRTYPE(entry->reqaddrtype, dns_addrtype);
  1308. LWIP_DNS_SET_ADDRTYPE(req->reqaddrtype, dns_addrtype);
  1309. req->found = found;
  1310. req->arg = callback_arg;
  1311. namelen = LWIP_MIN(hostnamelen, DNS_MAX_NAME_LENGTH-1);
  1312. MEMCPY(entry->name, name, namelen);
  1313. entry->name[namelen] = 0;
  1314. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) != 0)
  1315. entry->pcb_idx = dns_alloc_pcb();
  1316. if (entry->pcb_idx >= DNS_MAX_SOURCE_PORTS) {
  1317. /* failed to get a UDP pcb */
  1318. LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": failed to allocate a pcb\n", name));
  1319. entry->state = DNS_STATE_UNUSED;
  1320. req->found = NULL;
  1321. return ERR_MEM;
  1322. }
  1323. LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS pcb %"U16_F"\n", name, (u16_t)(entry->pcb_idx)));
  1324. #endif
  1325. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  1326. entry->is_mdns = is_mdns;
  1327. #endif
  1328. dns_seqno++;
  1329. /* force to send query without waiting timer */
  1330. dns_check_entry(i);
  1331. /* dns query is enqueued */
  1332. return ERR_INPROGRESS;
  1333. }
  1334. /**
  1335. * @ingroup dns
  1336. * Resolve a hostname (string) into an IP address.
  1337. * NON-BLOCKING callback version for use with raw API!!!
  1338. *
  1339. * Returns immediately with one of err_t return codes:
  1340. * - ERR_OK if hostname is a valid IP address string or the host
  1341. * name is already in the local names table.
  1342. * - ERR_INPROGRESS enqueue a request to be sent to the DNS server
  1343. * for resolution if no errors are present.
  1344. * - ERR_ARG: dns client not initialized or invalid hostname
  1345. *
  1346. * @param hostname the hostname that is to be queried
  1347. * @param addr pointer to a ip_addr_t where to store the address if it is already
  1348. * cached in the dns_table (only valid if ERR_OK is returned!)
  1349. * @param found a callback function to be called on success, failure or timeout (only if
  1350. * ERR_INPROGRESS is returned!)
  1351. * @param callback_arg argument to pass to the callback function
  1352. * @return a err_t return code.
  1353. */
  1354. err_t
  1355. dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found,
  1356. void *callback_arg)
  1357. {
  1358. return dns_gethostbyname_addrtype(hostname, addr, found, callback_arg, LWIP_DNS_ADDRTYPE_DEFAULT);
  1359. }
  1360. /**
  1361. * @ingroup dns
  1362. * Like dns_gethostbyname, but returned address type can be controlled:
  1363. * @param hostname the hostname that is to be queried
  1364. * @param addr pointer to a ip_addr_t where to store the address if it is already
  1365. * cached in the dns_table (only valid if ERR_OK is returned!)
  1366. * @param found a callback function to be called on success, failure or timeout (only if
  1367. * ERR_INPROGRESS is returned!)
  1368. * @param callback_arg argument to pass to the callback function
  1369. * @param dns_addrtype - LWIP_DNS_ADDRTYPE_IPV4_IPV6: try to resolve IPv4 first, try IPv6 if IPv4 fails only
  1370. * - LWIP_DNS_ADDRTYPE_IPV6_IPV4: try to resolve IPv6 first, try IPv4 if IPv6 fails only
  1371. * - LWIP_DNS_ADDRTYPE_IPV4: try to resolve IPv4 only
  1372. * - LWIP_DNS_ADDRTYPE_IPV6: try to resolve IPv6 only
  1373. */
  1374. err_t
  1375. dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_callback found,
  1376. void *callback_arg, u8_t dns_addrtype)
  1377. {
  1378. size_t hostnamelen;
  1379. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  1380. u8_t is_mdns;
  1381. #endif
  1382. /* not initialized or no valid server yet, or invalid addr pointer
  1383. * or invalid hostname or invalid hostname length */
  1384. if ((addr == NULL) ||
  1385. (!hostname) || (!hostname[0])) {
  1386. return ERR_ARG;
  1387. }
  1388. #if ((LWIP_DNS_SECURE & LWIP_DNS_SECURE_RAND_SRC_PORT) == 0)
  1389. if (dns_pcbs[0] == NULL) {
  1390. return ERR_ARG;
  1391. }
  1392. #endif
  1393. hostnamelen = strlen(hostname);
  1394. if (hostnamelen >= DNS_MAX_NAME_LENGTH) {
  1395. LWIP_DEBUGF(DNS_DEBUG, ("dns_gethostbyname: name too long to resolve"));
  1396. return ERR_ARG;
  1397. }
  1398. #if LWIP_HAVE_LOOPIF
  1399. if (strcmp(hostname, "localhost") == 0) {
  1400. ip_addr_set_loopback(LWIP_DNS_ADDRTYPE_IS_IPV6(dns_addrtype), addr);
  1401. return ERR_OK;
  1402. }
  1403. #endif /* LWIP_HAVE_LOOPIF */
  1404. /* host name already in octet notation? set ip addr and return ERR_OK */
  1405. if (ipaddr_aton(hostname, addr)) {
  1406. #if LWIP_IPV4 && LWIP_IPV6
  1407. if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) ||
  1408. (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6)))
  1409. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1410. {
  1411. return ERR_OK;
  1412. }
  1413. }
  1414. /* already have this address cached? */
  1415. if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)) == ERR_OK) {
  1416. return ERR_OK;
  1417. }
  1418. #if LWIP_IPV4 && LWIP_IPV6
  1419. if ((dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) || (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV6_IPV4)) {
  1420. /* fallback to 2nd IP type and try again to lookup */
  1421. u8_t fallback;
  1422. if (dns_addrtype == LWIP_DNS_ADDRTYPE_IPV4_IPV6) {
  1423. fallback = LWIP_DNS_ADDRTYPE_IPV6;
  1424. } else {
  1425. fallback = LWIP_DNS_ADDRTYPE_IPV4;
  1426. }
  1427. if (dns_lookup(hostname, addr LWIP_DNS_ADDRTYPE_ARG(fallback)) == ERR_OK) {
  1428. return ERR_OK;
  1429. }
  1430. }
  1431. #else /* LWIP_IPV4 && LWIP_IPV6 */
  1432. LWIP_UNUSED_ARG(dns_addrtype);
  1433. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1434. #if LWIP_DNS_SUPPORT_MDNS_QUERIES
  1435. if (strstr(hostname, ".local") == &hostname[hostnamelen] - 6) {
  1436. is_mdns = 1;
  1437. } else {
  1438. is_mdns = 0;
  1439. }
  1440. if (!is_mdns)
  1441. #endif /* LWIP_DNS_SUPPORT_MDNS_QUERIES */
  1442. {
  1443. /* prevent calling found callback if no server is set, return error instead */
  1444. if (ip_addr_isany_val(dns_servers[0])) {
  1445. return ERR_VAL;
  1446. }
  1447. }
  1448. /* queue query with specified callback */
  1449. return dns_enqueue(hostname, hostnamelen, found, callback_arg LWIP_DNS_ADDRTYPE_ARG(dns_addrtype)
  1450. LWIP_DNS_ISMDNS_ARG(is_mdns));
  1451. }
  1452. #endif /* LWIP_DNS */