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