dns.c 52 KB

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