ares_private.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #ifndef __ARES_PRIVATE_H
  2. #define __ARES_PRIVATE_H
  3. /* Copyright 1998 by the Massachusetts Institute of Technology.
  4. * Copyright (C) 2004-2010 by Daniel Stenberg
  5. *
  6. * Permission to use, copy, modify, and distribute this
  7. * software and its documentation for any purpose and without
  8. * fee is hereby granted, provided that the above copyright
  9. * notice appear in all copies and that both that copyright
  10. * notice and this permission notice appear in supporting
  11. * documentation, and that the name of M.I.T. not be used in
  12. * advertising or publicity pertaining to distribution of the
  13. * software without specific, written prior permission.
  14. * M.I.T. makes no representations about the suitability of
  15. * this software for any purpose. It is provided "as is"
  16. * without express or implied warranty.
  17. */
  18. /*
  19. * Define WIN32 when build target is Win32 API
  20. */
  21. #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  22. #define WIN32
  23. #endif
  24. #ifdef HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #ifdef WATT32
  28. #include <tcp.h>
  29. #include <sys/ioctl.h>
  30. #define writev(s,v,c) writev_s(s,v,c)
  31. #define HAVE_WRITEV 1
  32. #endif
  33. #define DEFAULT_TIMEOUT 5000 /* milliseconds */
  34. #define DEFAULT_TRIES 4
  35. #ifndef INADDR_NONE
  36. #define INADDR_NONE 0xffffffff
  37. #endif
  38. #ifdef CARES_EXPOSE_STATICS
  39. /* Make some internal functions visible for testing */
  40. #define STATIC_TESTABLE
  41. #else
  42. #define STATIC_TESTABLE static
  43. #endif
  44. /* By using a double cast, we can get rid of the bogus warning of
  45. * warning: cast from 'const struct sockaddr *' to 'const struct sockaddr_in6 *' increases required alignment from 1 to 4 [-Wcast-align]
  46. */
  47. #define CARES_INADDR_CAST(type, var) ((type)((void *)var))
  48. #if defined(WIN32) && !defined(WATT32)
  49. #define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
  50. #define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
  51. #define WIN_DNSCLIENT "Software\\Policies\\Microsoft\\System\\DNSClient"
  52. #define WIN_NT_DNSCLIENT "Software\\Policies\\Microsoft\\Windows NT\\DNSClient"
  53. #define NAMESERVER "NameServer"
  54. #define DHCPNAMESERVER "DhcpNameServer"
  55. #define DATABASEPATH "DatabasePath"
  56. #define WIN_PATH_HOSTS "\\hosts"
  57. #define SEARCHLIST_KEY "SearchList"
  58. #define PRIMARYDNSSUFFIX_KEY "PrimaryDNSSuffix"
  59. #define INTERFACES_KEY "Interfaces"
  60. #define DOMAIN_KEY "Domain"
  61. #define DHCPDOMAIN_KEY "DhcpDomain"
  62. #elif defined(WATT32)
  63. #define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
  64. #elif defined(NETWARE)
  65. #define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
  66. #define PATH_HOSTS "sys:/etc/hosts"
  67. #elif defined(__riscos__)
  68. #define PATH_HOSTS "InetDBase:Hosts"
  69. #else
  70. #define PATH_RESOLV_CONF "/etc/resolv.conf"
  71. #ifdef ETC_INET
  72. #define PATH_HOSTS "/etc/inet/hosts"
  73. #else
  74. #define PATH_HOSTS "/etc/hosts"
  75. #endif
  76. #endif
  77. #define ARES_ID_KEY_LEN 31
  78. #include "ares_ipv6.h"
  79. #include "ares_llist.h"
  80. #ifndef HAVE_GETENV
  81. # include "ares_getenv.h"
  82. # define getenv(ptr) ares_getenv(ptr)
  83. #endif
  84. #include "ares_strdup.h"
  85. #include "ares_strsplit.h"
  86. #ifndef HAVE_STRCASECMP
  87. # include "ares_strcasecmp.h"
  88. # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
  89. #endif
  90. #ifndef HAVE_STRNCASECMP
  91. # include "ares_strcasecmp.h"
  92. # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
  93. #endif
  94. #ifndef HAVE_WRITEV
  95. # include "ares_writev.h"
  96. # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
  97. #endif
  98. /********* EDNS defines section ******/
  99. #define EDNSPACKETSZ 1280 /* Reasonable UDP payload size, as suggested
  100. in RFC2671 */
  101. #define MAXENDSSZ 4096 /* Maximum (local) limit for edns packet size */
  102. #define EDNSFIXEDSZ 11 /* Size of EDNS header */
  103. /********* EDNS defines section ******/
  104. struct ares_addr {
  105. int family;
  106. union {
  107. struct in_addr addr4;
  108. struct ares_in6_addr addr6;
  109. } addr;
  110. int udp_port; /* stored in network order */
  111. int tcp_port; /* stored in network order */
  112. };
  113. #define addrV4 addr.addr4
  114. #define addrV6 addr.addr6
  115. struct query;
  116. struct send_request {
  117. /* Remaining data to send */
  118. const unsigned char *data;
  119. size_t len;
  120. /* The query for which we're sending this data */
  121. struct query* owner_query;
  122. /* The buffer we're using, if we have our own copy of the packet */
  123. unsigned char *data_storage;
  124. /* Next request in queue */
  125. struct send_request *next;
  126. };
  127. struct server_state {
  128. struct ares_addr addr;
  129. ares_socket_t udp_socket;
  130. ares_socket_t tcp_socket;
  131. /* Mini-buffer for reading the length word */
  132. unsigned char tcp_lenbuf[2];
  133. int tcp_lenbuf_pos;
  134. int tcp_length;
  135. /* Buffer for reading actual TCP data */
  136. unsigned char *tcp_buffer;
  137. int tcp_buffer_pos;
  138. /* TCP output queue */
  139. struct send_request *qhead;
  140. struct send_request *qtail;
  141. /* Which incarnation of this connection is this? We don't want to
  142. * retransmit requests into the very same socket, but if the server
  143. * closes on us and we re-open the connection, then we do want to
  144. * re-send. */
  145. int tcp_connection_generation;
  146. /* Circular, doubly-linked list of outstanding queries to this server */
  147. struct list_node queries_to_server;
  148. /* Link back to owning channel */
  149. ares_channel channel;
  150. /* Is this server broken? We mark connections as broken when a
  151. * request that is queued for sending times out.
  152. */
  153. int is_broken;
  154. };
  155. /* State to represent a DNS query */
  156. struct query {
  157. /* Query ID from qbuf, for faster lookup, and current timeout */
  158. unsigned short qid;
  159. struct timeval timeout;
  160. /*
  161. * Links for the doubly-linked lists in which we insert a query.
  162. * These circular, doubly-linked lists that are hash-bucketed based
  163. * the attributes we care about, help making most important
  164. * operations O(1).
  165. */
  166. struct list_node queries_by_qid; /* hopefully in same cache line as qid */
  167. struct list_node queries_by_timeout;
  168. struct list_node queries_to_server;
  169. struct list_node all_queries;
  170. /* Query buf with length at beginning, for TCP transmission */
  171. unsigned char *tcpbuf;
  172. int tcplen;
  173. /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
  174. const unsigned char *qbuf;
  175. int qlen;
  176. ares_callback callback;
  177. void *arg;
  178. /* Query status */
  179. int try_count; /* Number of times we tried this query already. */
  180. int server; /* Server this query has last been sent to. */
  181. struct query_server_info *server_info; /* per-server state */
  182. int using_tcp;
  183. int error_status;
  184. int timeouts; /* number of timeouts we saw for this request */
  185. };
  186. /* Per-server state for a query */
  187. struct query_server_info {
  188. int skip_server; /* should we skip server, due to errors, etc? */
  189. int tcp_connection_generation; /* into which TCP connection did we send? */
  190. };
  191. /* An IP address pattern; matches an IP address X if X & mask == addr */
  192. #define PATTERN_MASK 0x1
  193. #define PATTERN_CIDR 0x2
  194. struct apattern {
  195. union
  196. {
  197. struct in_addr addr4;
  198. struct ares_in6_addr addr6;
  199. } addr;
  200. union
  201. {
  202. struct in_addr addr4;
  203. struct ares_in6_addr addr6;
  204. unsigned short bits;
  205. } mask;
  206. int family;
  207. unsigned short type;
  208. };
  209. typedef struct rc4_key
  210. {
  211. unsigned char state[256];
  212. unsigned char x;
  213. unsigned char y;
  214. } rc4_key;
  215. struct ares_channeldata {
  216. /* Configuration data */
  217. int flags;
  218. int timeout; /* in milliseconds */
  219. int maxtimeout; /* in milliseconds */
  220. int jitter; /* in .001 */
  221. unsigned int jitter_rand_state;
  222. int tries;
  223. int ndots;
  224. int rotate; /* if true, all servers specified are used */
  225. int udp_port; /* stored in network order */
  226. int tcp_port; /* stored in network order */
  227. int socket_send_buffer_size;
  228. int socket_receive_buffer_size;
  229. char **domains;
  230. int ndomains;
  231. struct apattern *sortlist;
  232. int nsort;
  233. char *lookups;
  234. int ednspsz;
  235. /* For binding to local devices and/or IP addresses. Leave
  236. * them null/zero for no binding.
  237. */
  238. char local_dev_name[32];
  239. unsigned int local_ip4;
  240. unsigned char local_ip6[16];
  241. int optmask; /* the option bitfield passed in at init time */
  242. /* Server addresses and communications state */
  243. struct server_state *servers;
  244. int nservers;
  245. /* ID to use for next query */
  246. unsigned short next_id;
  247. /* key to use when generating new ids */
  248. rc4_key id_key;
  249. /* Generation number to use for the next TCP socket open/close */
  250. int tcp_connection_generation;
  251. /* The time at which we last called process_timeouts(). Uses integer seconds
  252. just to draw the line somewhere. */
  253. time_t last_timeout_processed;
  254. /* Last server we sent a query to. */
  255. int last_server;
  256. /* Circular, doubly-linked list of queries, bucketed various ways.... */
  257. /* All active queries in a single list: */
  258. struct list_node all_queries;
  259. /* Queries bucketed by qid, for quickly dispatching DNS responses: */
  260. #define ARES_QID_TABLE_SIZE 2048
  261. struct list_node queries_by_qid[ARES_QID_TABLE_SIZE];
  262. /* Queries bucketed by timeout, for quickly handling timeouts: */
  263. #define ARES_TIMEOUT_TABLE_SIZE 1024
  264. struct list_node queries_by_timeout[ARES_TIMEOUT_TABLE_SIZE];
  265. ares_sock_state_cb sock_state_cb;
  266. void *sock_state_cb_data;
  267. ares_sock_create_callback sock_create_cb;
  268. void *sock_create_cb_data;
  269. ares_sock_config_callback sock_config_cb;
  270. void *sock_config_cb_data;
  271. const struct ares_socket_functions * sock_funcs;
  272. void *sock_func_cb_data;
  273. /* Path for resolv.conf file, configurable via ares_options */
  274. char *resolvconf_path;
  275. };
  276. /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */
  277. int ares__is_onion_domain(const char *name);
  278. /* Memory management functions */
  279. extern void *(*ares_malloc)(size_t size);
  280. extern void *(*ares_realloc)(void *ptr, size_t size);
  281. extern void (*ares_free)(void *ptr);
  282. /* return true if now is exactly check time or later */
  283. int ares__timedout(struct timeval *now,
  284. struct timeval *check);
  285. void ares__send_query(ares_channel channel, struct query *query,
  286. struct timeval *now);
  287. void ares__close_sockets(ares_channel channel, struct server_state *server);
  288. int ares__get_hostent(FILE *fp, int family, struct hostent **host);
  289. int ares__read_line(FILE *fp, char **buf, size_t *bufsize);
  290. void ares__free_query(struct query *query);
  291. unsigned short ares__generate_new_id(rc4_key* key);
  292. struct timeval ares__tvnow(void);
  293. int ares__expand_name_for_response(const unsigned char *encoded,
  294. const unsigned char *abuf, int alen,
  295. char **s, long *enclen);
  296. void ares__init_servers_state(ares_channel channel);
  297. void ares__destroy_servers_state(ares_channel channel);
  298. int ares__parse_qtype_reply(const unsigned char* abuf, int alen, int* qtype);
  299. int ares__single_domain(ares_channel channel, const char *name, char **s);
  300. int ares__cat_domain(const char *name, const char *domain, char **s);
  301. int ares__sortaddrinfo(ares_channel channel, struct ares_addrinfo_node *ai_node);
  302. int ares__readaddrinfo(FILE *fp, const char *name, unsigned short port,
  303. const struct ares_addrinfo_hints *hints,
  304. struct ares_addrinfo *ai);
  305. struct ares_addrinfo *ares__malloc_addrinfo(void);
  306. struct ares_addrinfo_node *ares__malloc_addrinfo_node(void);
  307. void ares__freeaddrinfo_nodes(struct ares_addrinfo_node *ai_node);
  308. struct ares_addrinfo_node *ares__append_addrinfo_node(struct ares_addrinfo_node **ai_node);
  309. void ares__addrinfo_cat_nodes(struct ares_addrinfo_node **head,
  310. struct ares_addrinfo_node *tail);
  311. struct ares_addrinfo_cname *ares__malloc_addrinfo_cname(void);
  312. void ares__freeaddrinfo_cnames(struct ares_addrinfo_cname *ai_cname);
  313. struct ares_addrinfo_cname *ares__append_addrinfo_cname(struct ares_addrinfo_cname **ai_cname);
  314. void ares__addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
  315. struct ares_addrinfo_cname *tail);
  316. int ares__parse_into_addrinfo(const unsigned char *abuf,
  317. int alen,
  318. struct ares_addrinfo *ai);
  319. int ares__parse_into_addrinfo2(const unsigned char *abuf,
  320. int alen,
  321. char **question_hostname,
  322. struct ares_addrinfo *ai);
  323. #if 0 /* Not used */
  324. long ares__tvdiff(struct timeval t1, struct timeval t2);
  325. #endif
  326. ares_socket_t ares__open_socket(ares_channel channel,
  327. int af, int type, int protocol);
  328. void ares__close_socket(ares_channel, ares_socket_t);
  329. int ares__connect_socket(ares_channel channel,
  330. ares_socket_t sockfd,
  331. const struct sockaddr *addr,
  332. ares_socklen_t addrlen);
  333. #define ARES_SWAP_BYTE(a,b) \
  334. { unsigned char swapByte = *(a); *(a) = *(b); *(b) = swapByte; }
  335. #define SOCK_STATE_CALLBACK(c, s, r, w) \
  336. do { \
  337. if ((c)->sock_state_cb) \
  338. (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \
  339. } WHILE_FALSE
  340. #ifdef CURLDEBUG
  341. /* This is low-level hard-hacking memory leak tracking and similar. Using the
  342. libcurl lowlevel code from within library is ugly and only works when
  343. c-ares is built and linked with a similarly curldebug-enabled libcurl,
  344. but we do this anyway for convenience. */
  345. #define HEADER_CURL_SETUP_ONCE_H
  346. #include "../lib/memdebug.h"
  347. #endif
  348. #endif /* __ARES_PRIVATE_H */