resolve.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include "lib/net/resolve.h"
  6. #include "lib/net/address.h"
  7. #include "lib/malloc/util_malloc.h"
  8. #include "siphash.h"
  9. #include "ht.h"
  10. #ifdef HAVE_SYS_TYPES_H
  11. #include <sys/types.h>
  12. #endif
  13. #ifdef HAVE_SYS_SOCKET_H
  14. #include <sys/socket.h>
  15. #endif
  16. #ifdef HAVE_NETDB_H
  17. #include <netdb.h>
  18. #endif
  19. #include <string.h>
  20. /** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
  21. * *<b>addr</b> to the proper IP address, in host byte order. Returns 0
  22. * on success, -1 on failure; 1 on transient failure.
  23. *
  24. * (This function exists because standard windows gethostbyname
  25. * doesn't treat raw IP addresses properly.)
  26. */
  27. MOCK_IMPL(int,
  28. tor_lookup_hostname,(const char *name, uint32_t *addr))
  29. {
  30. tor_addr_t myaddr;
  31. int ret;
  32. if ((ret = tor_addr_lookup(name, AF_INET, &myaddr)))
  33. return ret;
  34. if (tor_addr_family(&myaddr) == AF_INET) {
  35. *addr = tor_addr_to_ipv4h(&myaddr);
  36. return ret;
  37. }
  38. return -1;
  39. }
  40. #ifdef USE_SANDBOX_GETADDRINFO
  41. /** True if we should only return cached values */
  42. static int sandbox_getaddrinfo_is_active = 0;
  43. /** Cache entry for getaddrinfo results; used when sandboxing is implemented
  44. * so that we can consult the cache when the sandbox prevents us from doing
  45. * getaddrinfo.
  46. *
  47. * We support only a limited range of getaddrinfo calls, where servname is null
  48. * and hints contains only socktype=SOCK_STREAM, family in INET,INET6,UNSPEC.
  49. */
  50. typedef struct cached_getaddrinfo_item_t {
  51. HT_ENTRY(cached_getaddrinfo_item_t) node;
  52. char *name;
  53. int family;
  54. /** set if no error; otherwise NULL */
  55. struct addrinfo *res;
  56. /** 0 for no error; otherwise an EAI_* value */
  57. int err;
  58. } cached_getaddrinfo_item_t;
  59. static unsigned
  60. cached_getaddrinfo_item_hash(const cached_getaddrinfo_item_t *item)
  61. {
  62. return (unsigned)siphash24g(item->name, strlen(item->name)) + item->family;
  63. }
  64. static unsigned
  65. cached_getaddrinfo_items_eq(const cached_getaddrinfo_item_t *a,
  66. const cached_getaddrinfo_item_t *b)
  67. {
  68. return (a->family == b->family) && 0 == strcmp(a->name, b->name);
  69. }
  70. #define cached_getaddrinfo_item_free(item) \
  71. FREE_AND_NULL(cached_getaddrinfo_item_t, \
  72. cached_getaddrinfo_item_free_, (item))
  73. static void
  74. cached_getaddrinfo_item_free_(cached_getaddrinfo_item_t *item)
  75. {
  76. if (item == NULL)
  77. return;
  78. tor_free(item->name);
  79. if (item->res)
  80. freeaddrinfo(item->res);
  81. tor_free(item);
  82. }
  83. static HT_HEAD(getaddrinfo_cache, cached_getaddrinfo_item_t)
  84. getaddrinfo_cache = HT_INITIALIZER();
  85. HT_PROTOTYPE(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
  86. cached_getaddrinfo_item_hash,
  87. cached_getaddrinfo_items_eq)
  88. HT_GENERATE2(getaddrinfo_cache, cached_getaddrinfo_item_t, node,
  89. cached_getaddrinfo_item_hash,
  90. cached_getaddrinfo_items_eq,
  91. 0.6, tor_reallocarray_, tor_free_)
  92. /** If true, don't try to cache getaddrinfo results. */
  93. static int sandbox_getaddrinfo_cache_disabled = 0;
  94. /** Tell the sandbox layer not to try to cache getaddrinfo results. Used as in
  95. * tor-resolve, when we have no intention of initializing crypto or of
  96. * installing the sandbox.*/
  97. void
  98. sandbox_disable_getaddrinfo_cache(void)
  99. {
  100. sandbox_getaddrinfo_cache_disabled = 1;
  101. }
  102. void
  103. sandbox_freeaddrinfo(struct addrinfo *ai)
  104. {
  105. if (sandbox_getaddrinfo_cache_disabled)
  106. freeaddrinfo(ai);
  107. }
  108. int
  109. sandbox_getaddrinfo(const char *name, const char *servname,
  110. const struct addrinfo *hints,
  111. struct addrinfo **res)
  112. {
  113. int err;
  114. struct cached_getaddrinfo_item_t search, *item;
  115. if (sandbox_getaddrinfo_cache_disabled) {
  116. return getaddrinfo(name, NULL, hints, res);
  117. }
  118. if (servname != NULL) {
  119. log_warn(LD_BUG, "called with non-NULL servname");
  120. return EAI_NONAME;
  121. }
  122. if (name == NULL) {
  123. log_warn(LD_BUG, "called with NULL name");
  124. return EAI_NONAME;
  125. }
  126. *res = NULL;
  127. memset(&search, 0, sizeof(search));
  128. search.name = (char *) name;
  129. search.family = hints ? hints->ai_family : AF_UNSPEC;
  130. item = HT_FIND(getaddrinfo_cache, &getaddrinfo_cache, &search);
  131. if (! sandbox_getaddrinfo_is_active) {
  132. /* If the sandbox is not turned on yet, then getaddrinfo and store the
  133. result. */
  134. err = getaddrinfo(name, NULL, hints, res);
  135. log_info(LD_NET,"(Sandbox) getaddrinfo %s.", err ? "failed" : "succeeded");
  136. if (! item) {
  137. item = tor_malloc_zero(sizeof(*item));
  138. item->name = tor_strdup(name);
  139. item->family = hints ? hints->ai_family : AF_UNSPEC;
  140. HT_INSERT(getaddrinfo_cache, &getaddrinfo_cache, item);
  141. }
  142. if (item->res) {
  143. freeaddrinfo(item->res);
  144. item->res = NULL;
  145. }
  146. item->res = *res;
  147. item->err = err;
  148. return err;
  149. }
  150. /* Otherwise, the sandbox is on. If we have an item, yield its cached
  151. result. */
  152. if (item) {
  153. *res = item->res;
  154. return item->err;
  155. }
  156. /* getting here means something went wrong */
  157. log_err(LD_BUG,"(Sandbox) failed to get address %s!", name);
  158. return EAI_NONAME;
  159. }
  160. int
  161. sandbox_add_addrinfo(const char *name)
  162. {
  163. struct addrinfo *res;
  164. struct addrinfo hints;
  165. int i;
  166. static const int families[] = { AF_INET, AF_INET6, AF_UNSPEC };
  167. memset(&hints, 0, sizeof(hints));
  168. hints.ai_socktype = SOCK_STREAM;
  169. for (i = 0; i < 3; ++i) {
  170. hints.ai_family = families[i];
  171. res = NULL;
  172. (void) sandbox_getaddrinfo(name, NULL, &hints, &res);
  173. if (res)
  174. sandbox_freeaddrinfo(res);
  175. }
  176. return 0;
  177. }
  178. void
  179. sandbox_free_getaddrinfo_cache(void)
  180. {
  181. cached_getaddrinfo_item_t **next, **item, *this;
  182. for (item = HT_START(getaddrinfo_cache, &getaddrinfo_cache);
  183. item;
  184. item = next) {
  185. this = *item;
  186. next = HT_NEXT_RMV(getaddrinfo_cache, &getaddrinfo_cache, item);
  187. cached_getaddrinfo_item_free(this);
  188. }
  189. HT_CLEAR(getaddrinfo_cache, &getaddrinfo_cache);
  190. }
  191. void
  192. sandbox_make_getaddrinfo_cache_active(void)
  193. {
  194. sandbox_getaddrinfo_is_active = 1;
  195. }
  196. #endif