dns_structs.h 3.4 KB

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