private-lib-async-dns.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #define DNS_MAX 96 /* Maximum host name */
  25. #define DNS_RECURSION_LIMIT 3
  26. #define DNS_PACKET_LEN 1400 /* Buffer size for DNS packet */
  27. #define MAX_CACHE_ENTRIES 10 /* Dont cache more than that */
  28. #define DNS_QUERY_TIMEOUT 30 /* Query timeout, seconds */
  29. /*
  30. * ... when we completed a query then the query object is destroyed and a
  31. * cache object below is created with the results in getaddrinfo format
  32. * appended to the allocation
  33. */
  34. typedef struct lws_adns_cache {
  35. lws_sorted_usec_list_t sul; /* for cache TTL management */
  36. lws_dll2_t list;
  37. struct lws_adns_cache *firstcache;
  38. struct lws_adns_cache *chain;
  39. struct addrinfo *results;
  40. uint8_t flags; /* b0 = has ipv4, b1 = has ipv6 */
  41. char refcount;
  42. char incomplete;
  43. /* name, and then result struct addrinfos overallocated here */
  44. } lws_adns_cache_t;
  45. /*
  46. * these objects are used while a query is ongoing...
  47. */
  48. typedef struct {
  49. lws_sorted_usec_list_t sul; /* per-query write retry timer */
  50. lws_dll2_t list;
  51. lws_dll2_owner_t wsi_adns;
  52. lws_async_dns_cb_t standalone_cb; /* if not associated to wsi */
  53. struct lws_context *context;
  54. void *opaque;
  55. struct addrinfo **last;
  56. lws_async_dns_t *dns;
  57. lws_adns_cache_t *firstcache;
  58. lws_async_dns_retcode_t ret;
  59. uint16_t tid;
  60. uint16_t qtype;
  61. uint16_t retry;
  62. uint8_t tsi;
  63. #if defined(LWS_WITH_IPV6)
  64. uint8_t sent[2];
  65. #else
  66. uint8_t sent[1];
  67. #endif
  68. uint8_t asked;
  69. uint8_t responded;
  70. uint8_t recursion;
  71. /* name overallocated here */
  72. } lws_adns_q_t;
  73. enum {
  74. DHO_TID,
  75. DHO_FLAGS = 2,
  76. DHO_NQUERIES = 4,
  77. DHO_NANSWERS = 6,
  78. DHO_NAUTH = 8,
  79. DHO_NOTHER = 10,
  80. DHO_SIZEOF = 12 /* last */
  81. };
  82. void
  83. lws_adns_q_destroy(lws_adns_q_t *q);
  84. void
  85. sul_cb_expire(struct lws_sorted_usec_list *sul);
  86. void
  87. lws_adns_cache_destroy(lws_adns_cache_t *c);
  88. int
  89. lws_async_dns_complete(lws_adns_q_t *q, lws_adns_cache_t *c);
  90. lws_adns_cache_t *
  91. lws_adns_get_cache(lws_async_dns_t *dns, const char *name);
  92. void
  93. lws_adns_parse_udp(lws_async_dns_t *dns, const uint8_t *pkt, size_t len);
  94. lws_adns_q_t *
  95. lws_adns_get_query(lws_async_dns_t *dns, adns_query_type_t qtype,
  96. lws_dll2_owner_t *owner, uint16_t tid, const char *name);
  97. void
  98. lws_async_dns_trim_cache(lws_async_dns_t *dns);
  99. int
  100. lws_async_dns_get_new_tid(struct lws_context *context, lws_adns_q_t *q);