ares_private.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #ifndef __ARES_PRIVATE_H
  2. #define __ARES_PRIVATE_H
  3. /* $Id$ */
  4. /* Copyright 1998 by the Massachusetts Institute of Technology.
  5. * Copyright (C) 2004-2008 by Daniel Stenberg
  6. *
  7. * Permission to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose and without
  9. * fee is hereby granted, provided that the above copyright
  10. * notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting
  12. * documentation, and that the name of M.I.T. not be used in
  13. * advertising or publicity pertaining to distribution of the
  14. * software without specific, written prior permission.
  15. * M.I.T. makes no representations about the suitability of
  16. * this software for any purpose. It is provided "as is"
  17. * without express or implied warranty.
  18. */
  19. /*
  20. * Define WIN32 when build target is Win32 API
  21. */
  22. #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  23. #define WIN32
  24. #endif
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #if !defined(WIN32) || defined(WATT32)
  28. #include <netinet/in.h>
  29. /* We define closesocket() here so that we can use this function all over
  30. the source code for closing sockets. */
  31. #define closesocket(x) close(x)
  32. #endif
  33. #ifdef WATT32
  34. #include <tcp.h>
  35. #include <sys/ioctl.h>
  36. #undef closesocket
  37. #define closesocket(s) close_s(s)
  38. #define writev(s,v,c) writev_s(s,v,c)
  39. #define HAVE_WRITEV 1
  40. #endif
  41. #ifdef NETWARE
  42. #include <time.h>
  43. #endif
  44. #define DEFAULT_TIMEOUT 5000 /* milliseconds */
  45. #define DEFAULT_TRIES 4
  46. #ifndef INADDR_NONE
  47. #define INADDR_NONE 0xffffffff
  48. #endif
  49. #if defined(WIN32) && !defined(WATT32)
  50. #define IS_NT() ((int)GetVersion() > 0)
  51. #define WIN_NS_9X "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
  52. #define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
  53. #define NAMESERVER "NameServer"
  54. #define DHCPNAMESERVER "DhcpNameServer"
  55. #define DATABASEPATH "DatabasePath"
  56. #define WIN_PATH_HOSTS "\\hosts"
  57. #elif defined(WATT32)
  58. #define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
  59. #elif defined(NETWARE)
  60. #define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
  61. #define PATH_HOSTS "sys:/etc/hosts"
  62. #elif defined(__riscos__)
  63. #define PATH_HOSTS "InetDBase:Hosts"
  64. #else
  65. #define PATH_RESOLV_CONF "/etc/resolv.conf"
  66. #ifdef ETC_INET
  67. #define PATH_HOSTS "/etc/inet/hosts"
  68. #else
  69. #define PATH_HOSTS "/etc/hosts"
  70. #endif
  71. #endif
  72. #define ARES_ID_KEY_LEN 31
  73. #include "ares_ipv6.h"
  74. #include "ares_llist.h"
  75. #ifndef HAVE_STRDUP
  76. # include "ares_strdup.h"
  77. # define strdup(ptr) ares_strdup(ptr)
  78. #endif
  79. #ifndef HAVE_STRCASECMP
  80. # include "ares_strcasecmp.h"
  81. # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
  82. #endif
  83. #ifndef HAVE_STRNCASECMP
  84. # include "ares_strcasecmp.h"
  85. # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
  86. #endif
  87. #ifndef HAVE_WRITEV
  88. # include "ares_writev.h"
  89. # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
  90. #endif
  91. struct ares_addr {
  92. int family;
  93. union {
  94. struct in_addr addr4;
  95. struct in6_addr addr6;
  96. } addr;
  97. };
  98. #define addrV4 addr.addr4
  99. #define addrV6 addr.addr6
  100. struct query;
  101. struct send_request {
  102. /* Remaining data to send */
  103. const unsigned char *data;
  104. size_t len;
  105. /* The query for which we're sending this data */
  106. struct query* owner_query;
  107. /* The buffer we're using, if we have our own copy of the packet */
  108. unsigned char *data_storage;
  109. /* Next request in queue */
  110. struct send_request *next;
  111. };
  112. struct server_state {
  113. struct in_addr addr;
  114. ares_socket_t udp_socket;
  115. ares_socket_t tcp_socket;
  116. /* Mini-buffer for reading the length word */
  117. unsigned char tcp_lenbuf[2];
  118. int tcp_lenbuf_pos;
  119. int tcp_length;
  120. /* Buffer for reading actual TCP data */
  121. unsigned char *tcp_buffer;
  122. int tcp_buffer_pos;
  123. /* TCP output queue */
  124. struct send_request *qhead;
  125. struct send_request *qtail;
  126. /* Which incarnation of this connection is this? We don't want to
  127. * retransmit requests into the very same socket, but if the server
  128. * closes on us and we re-open the connection, then we do want to
  129. * re-send. */
  130. int tcp_connection_generation;
  131. /* Circular, doubly-linked list of outstanding queries to this server */
  132. struct list_node queries_to_server;
  133. /* Link back to owning channel */
  134. ares_channel channel;
  135. /* Is this server broken? We mark connections as broken when a
  136. * request that is queued for sending times out.
  137. */
  138. int is_broken;
  139. };
  140. /* State to represent a DNS query */
  141. struct query {
  142. /* Query ID from qbuf, for faster lookup, and current timeout */
  143. unsigned short qid;
  144. struct timeval timeout;
  145. /*
  146. * Links for the doubly-linked lists in which we insert a query.
  147. * These circular, doubly-linked lists that are hash-bucketed based
  148. * the attributes we care about, help making most important
  149. * operations O(1).
  150. */
  151. struct list_node queries_by_qid; /* hopefully in same cache line as qid */
  152. struct list_node queries_by_timeout;
  153. struct list_node queries_to_server;
  154. struct list_node all_queries;
  155. /* Query buf with length at beginning, for TCP transmission */
  156. unsigned char *tcpbuf;
  157. int tcplen;
  158. /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
  159. const unsigned char *qbuf;
  160. int qlen;
  161. ares_callback callback;
  162. void *arg;
  163. /* Query status */
  164. int try; /* Number of times we tried this query already. */
  165. int server; /* Server this query has last been sent to. */
  166. struct query_server_info *server_info; /* per-server state */
  167. int using_tcp;
  168. int error_status;
  169. int timeouts; /* number of timeouts we saw for this request */
  170. };
  171. /* Per-server state for a query */
  172. struct query_server_info {
  173. int skip_server; /* should we skip server, due to errors, etc? */
  174. int tcp_connection_generation; /* into which TCP connection did we send? */
  175. };
  176. /* An IP address pattern; matches an IP address X if X & mask == addr */
  177. #define PATTERN_MASK 0x1
  178. #define PATTERN_CIDR 0x2
  179. struct apattern {
  180. union
  181. {
  182. struct in_addr addr4;
  183. struct in6_addr addr6;
  184. } addr;
  185. union
  186. {
  187. struct in_addr addr4;
  188. struct in6_addr addr6;
  189. unsigned short bits;
  190. } mask;
  191. int family;
  192. unsigned short type;
  193. };
  194. typedef struct rc4_key
  195. {
  196. unsigned char state[256];
  197. unsigned char x;
  198. unsigned char y;
  199. } rc4_key;
  200. struct ares_channeldata {
  201. /* Configuration data */
  202. int flags;
  203. int timeout; /* in milliseconds */
  204. int tries;
  205. int ndots;
  206. int rotate; /* if true, all servers specified are used */
  207. int udp_port;
  208. int tcp_port;
  209. int socket_send_buffer_size;
  210. int socket_receive_buffer_size;
  211. char **domains;
  212. int ndomains;
  213. struct apattern *sortlist;
  214. int nsort;
  215. char *lookups;
  216. int optmask; /* the option bitfield passed in at init time */
  217. /* Server addresses and communications state */
  218. struct server_state *servers;
  219. int nservers;
  220. /* ID to use for next query */
  221. unsigned short next_id;
  222. /* key to use when generating new ids */
  223. rc4_key id_key;
  224. /* Generation number to use for the next TCP socket open/close */
  225. int tcp_connection_generation;
  226. /* The time at which we last called process_timeouts(). Uses integer seconds
  227. just to draw the line somewhere. */
  228. time_t last_timeout_processed;
  229. /* Last server we sent a query to. */
  230. int last_server;
  231. /* Circular, doubly-linked list of queries, bucketed various ways.... */
  232. /* All active queries in a single list: */
  233. struct list_node all_queries;
  234. /* Queries bucketed by qid, for quickly dispatching DNS responses: */
  235. #define ARES_QID_TABLE_SIZE 2048
  236. struct list_node queries_by_qid[ARES_QID_TABLE_SIZE];
  237. /* Queries bucketed by timeout, for quickly handling timeouts: */
  238. #define ARES_TIMEOUT_TABLE_SIZE 1024
  239. struct list_node queries_by_timeout[ARES_TIMEOUT_TABLE_SIZE];
  240. ares_sock_state_cb sock_state_cb;
  241. void *sock_state_cb_data;
  242. ares_sock_create_callback sock_create_cb;
  243. void *sock_create_cb_data;
  244. };
  245. /* return true if now is exactly check time or later */
  246. int ares__timedout(struct timeval *now,
  247. struct timeval *check);
  248. /* add the specific number of milliseconds to the time in the first argument */
  249. int ares__timeadd(struct timeval *now,
  250. int millisecs);
  251. /* return time offset between now and (future) check, in milliseconds */
  252. long ares__timeoffset(struct timeval *now,
  253. struct timeval *check);
  254. void ares__rc4(rc4_key* key,unsigned char *buffer_ptr, int buffer_len);
  255. void ares__send_query(ares_channel channel, struct query *query,
  256. struct timeval *now);
  257. void ares__close_sockets(ares_channel channel, struct server_state *server);
  258. int ares__get_hostent(FILE *fp, int family, struct hostent **host);
  259. int ares__read_line(FILE *fp, char **buf, int *bufsize);
  260. void ares__free_query(struct query *query);
  261. unsigned short ares__generate_new_id(rc4_key* key);
  262. struct timeval ares__tvnow(void);
  263. int ares__expand_name_for_response(const unsigned char *encoded,
  264. const unsigned char *abuf, int alen,
  265. char **s, long *enclen);
  266. #if 0 /* Not used */
  267. long ares__tvdiff(struct timeval t1, struct timeval t2);
  268. #endif
  269. #define ARES_SWAP_BYTE(a,b) \
  270. { unsigned char swapByte = *(a); *(a) = *(b); *(b) = swapByte; }
  271. #define SOCK_STATE_CALLBACK(c, s, r, w) \
  272. do { \
  273. if ((c)->sock_state_cb) \
  274. (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \
  275. } while (0)
  276. #ifdef CURLDEBUG
  277. /* This is low-level hard-hacking memory leak tracking and similar. Using the
  278. libcurl lowlevel code from within library is ugly and only works when
  279. c-ares is built and linked with a similarly debug-build libcurl, but we do
  280. this anyway for convenience. */
  281. #include "../lib/memdebug.h"
  282. #endif
  283. #endif /* __ARES_PRIVATE_H */