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