dns.c 51 KB

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