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