dns_structs.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2003-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file dns_structs.h
  7. *
  8. * \brief Structures used in dns.c. Exposed to dns.c, and to the unit tests
  9. * that declare DNS_PRIVATE.
  10. */
  11. #ifndef TOR_DNS_STRUCTS_H
  12. #define TOR_DNS_STRUCTS_H
  13. /** Longest hostname we're willing to resolve. */
  14. #define MAX_ADDRESSLEN 256
  15. /** Linked list of connections waiting for a DNS answer. */
  16. typedef struct pending_connection_t {
  17. edge_connection_t *conn;
  18. struct pending_connection_t *next;
  19. } pending_connection_t;
  20. /** Value of 'magic' field for cached_resolve_t. Used to try to catch bad
  21. * pointers and memory stomping. */
  22. #define CACHED_RESOLVE_MAGIC 0x1234F00D
  23. /* Possible states for a cached resolve_t */
  24. /** We are waiting for the resolver system to tell us an answer here.
  25. * When we get one, or when we time out, the state of this cached_resolve_t
  26. * will become "DONE" and we'll possibly add a CACHED
  27. * entry. This cached_resolve_t will be in the hash table so that we will
  28. * know not to launch more requests for this addr, but rather to add more
  29. * connections to the pending list for the addr. */
  30. #define CACHE_STATE_PENDING 0
  31. /** This used to be a pending cached_resolve_t, and we got an answer for it.
  32. * Now we're waiting for this cached_resolve_t to expire. This should
  33. * have no pending connections, and should not appear in the hash table. */
  34. #define CACHE_STATE_DONE 1
  35. /** We are caching an answer for this address. This should have no pending
  36. * connections, and should appear in the hash table. */
  37. #define CACHE_STATE_CACHED 2
  38. /** @name status values for a single DNS request.
  39. *
  40. * @{ */
  41. /** The DNS request is in progress. */
  42. #define RES_STATUS_INFLIGHT 1
  43. /** The DNS request finished and gave an answer */
  44. #define RES_STATUS_DONE_OK 2
  45. /** The DNS request finished and gave an error */
  46. #define RES_STATUS_DONE_ERR 3
  47. /**@}*/
  48. /** A DNS request: possibly completed, possibly pending; cached_resolve
  49. * structs are stored at the OR side in a hash table, and as a linked
  50. * list from oldest to newest.
  51. */
  52. typedef struct cached_resolve_t {
  53. HT_ENTRY(cached_resolve_t) node;
  54. uint32_t magic; /**< Must be CACHED_RESOLVE_MAGIC */
  55. char address[MAX_ADDRESSLEN]; /**< The hostname to be resolved. */
  56. union {
  57. uint32_t addr_ipv4; /**< IPv4 addr for <b>address</b>, if successful.
  58. * (In host order.) */
  59. int err_ipv4; /**< One of DNS_ERR_*, if IPv4 lookup failed. */
  60. } result_ipv4; /**< Outcome of IPv4 lookup */
  61. union {
  62. struct in6_addr addr_ipv6; /**< IPv6 addr for <b>address</b>, if
  63. * successful */
  64. int err_ipv6; /**< One of DNS_ERR_*, if IPv6 lookup failed. */
  65. } result_ipv6; /**< Outcome of IPv6 lookup, if any */
  66. union {
  67. char *hostname; /** A hostname, if PTR lookup happened successfully*/
  68. int err_hostname; /** One of DNS_ERR_*, if PTR lookup failed. */
  69. } result_ptr;
  70. /** @name Status fields
  71. *
  72. * These take one of the RES_STATUS_* values, depending on the state
  73. * of the corresponding lookup.
  74. *
  75. * @{ */
  76. unsigned int res_status_ipv4 : 2;
  77. unsigned int res_status_ipv6 : 2;
  78. unsigned int res_status_hostname : 2;
  79. /**@}*/
  80. uint8_t state; /**< Is this cached entry pending/done/informative? */
  81. time_t expire; /**< Remove items from cache after this time. */
  82. uint32_t ttl_ipv4; /**< What TTL did the nameserver tell us? */
  83. uint32_t ttl_ipv6; /**< What TTL did the nameserver tell us? */
  84. uint32_t ttl_hostname; /**< What TTL did the nameserver tell us? */
  85. /** Connections that want to know when we get an answer for this resolve. */
  86. pending_connection_t *pending_connections;
  87. /** Position of this element in the heap*/
  88. int minheap_idx;
  89. } cached_resolve_t;
  90. #endif /* !defined(TOR_DNS_STRUCTS_H) */