50b2ba06e15b388f6bc4c3f44c2f0a90d1ba2e1b
[platform/upstream/c-ares.git] / src / lib / 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 /* By using a double cast, we can get rid of the bogus warning of
54  * warning: cast from 'const struct sockaddr *' to 'const struct sockaddr_in6 *' increases required alignment from 1 to 4 [-Wcast-align]
55  */
56 #define CARES_INADDR_CAST(type, var) ((type)((void *)var))
57
58 #if defined(WIN32) && !defined(WATT32)
59
60 #define WIN_NS_9X            "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
61 #define WIN_NS_NT_KEY        "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
62 #define WIN_DNSCLIENT        "Software\\Policies\\Microsoft\\System\\DNSClient"
63 #define WIN_NT_DNSCLIENT     "Software\\Policies\\Microsoft\\Windows NT\\DNSClient"
64 #define NAMESERVER           "NameServer"
65 #define DHCPNAMESERVER       "DhcpNameServer"
66 #define DATABASEPATH         "DatabasePath"
67 #define WIN_PATH_HOSTS       "\\hosts"
68 #define SEARCHLIST_KEY       "SearchList"
69 #define PRIMARYDNSSUFFIX_KEY "PrimaryDNSSuffix"
70 #define INTERFACES_KEY       "Interfaces"
71 #define DOMAIN_KEY           "Domain"
72 #define DHCPDOMAIN_KEY       "DhcpDomain"
73
74 #elif defined(WATT32)
75
76 #define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
77 W32_FUNC const char *_w32_GetHostsFile (void);
78
79 #elif defined(NETWARE)
80
81 #define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
82 #define PATH_HOSTS              "sys:/etc/hosts"
83
84 #elif defined(__riscos__)
85
86 #define PATH_HOSTS             "InetDBase:Hosts"
87
88 #else
89
90 #define PATH_RESOLV_CONF        "/etc/resolv.conf"
91 #ifdef ETC_INET
92 #define PATH_HOSTS              "/etc/inet/hosts"
93 #else
94 #define PATH_HOSTS              "/etc/hosts"
95 #endif
96
97 #endif
98
99 #define ARES_ID_KEY_LEN 31
100
101 #include "ares_ipv6.h"
102 #include "ares_llist.h"
103
104 #ifndef HAVE_GETENV
105 #  include "ares_getenv.h"
106 #  define getenv(ptr) ares_getenv(ptr)
107 #endif
108
109 #include "ares_strdup.h"
110 #include "ares_strsplit.h"
111
112 #ifndef HAVE_STRCASECMP
113 #  include "ares_strcasecmp.h"
114 #  define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
115 #endif
116
117 #ifndef HAVE_STRNCASECMP
118 #  include "ares_strcasecmp.h"
119 #  define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
120 #endif
121
122 #ifndef HAVE_WRITEV
123 #  include "ares_writev.h"
124 #  define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
125 #endif
126
127 /********* EDNS defines section ******/
128 #define EDNSPACKETSZ   1280  /* Reasonable UDP payload size, as suggested
129                                 in RFC2671 */
130 #define MAXENDSSZ      4096  /* Maximum (local) limit for edns packet size */
131 #define EDNSFIXEDSZ    11    /* Size of EDNS header */
132 /********* EDNS defines section ******/
133
134 struct ares_addr {
135   int family;
136   union {
137     struct in_addr       addr4;
138     struct ares_in6_addr addr6;
139   } addr;
140   int udp_port;  /* stored in network order */
141   int tcp_port;  /* stored in network order */
142 };
143 #define addrV4 addr.addr4
144 #define addrV6 addr.addr6
145
146 struct query;
147
148 struct send_request {
149   /* Remaining data to send */
150   const unsigned char *data;
151   size_t len;
152
153   /* The query for which we're sending this data */
154   struct query* owner_query;
155   /* The buffer we're using, if we have our own copy of the packet */
156   unsigned char *data_storage;
157
158   /* Next request in queue */
159   struct send_request *next;
160 };
161
162 struct server_state {
163   struct ares_addr addr;
164   ares_socket_t udp_socket;
165   ares_socket_t tcp_socket;
166
167   /* Mini-buffer for reading the length word */
168   unsigned char tcp_lenbuf[2];
169   int tcp_lenbuf_pos;
170   int tcp_length;
171
172   /* Buffer for reading actual TCP data */
173   unsigned char *tcp_buffer;
174   int tcp_buffer_pos;
175
176   /* TCP output queue */
177   struct send_request *qhead;
178   struct send_request *qtail;
179
180   /* Which incarnation of this connection is this? We don't want to
181    * retransmit requests into the very same socket, but if the server
182    * closes on us and we re-open the connection, then we do want to
183    * re-send. */
184   int tcp_connection_generation;
185
186   /* Circular, doubly-linked list of outstanding queries to this server */
187   struct list_node queries_to_server;
188
189   /* Link back to owning channel */
190   ares_channel channel;
191
192   /* Is this server broken? We mark connections as broken when a
193    * request that is queued for sending times out.
194    */
195   int is_broken;
196 };
197
198 /* State to represent a DNS query */
199 struct query {
200   /* Query ID from qbuf, for faster lookup, and current timeout */
201   unsigned short qid;
202   struct timeval timeout;
203
204   /*
205    * Links for the doubly-linked lists in which we insert a query.
206    * These circular, doubly-linked lists that are hash-bucketed based
207    * the attributes we care about, help making most important
208    * operations O(1).
209    */
210   struct list_node queries_by_qid;    /* hopefully in same cache line as qid */
211   struct list_node queries_by_timeout;
212   struct list_node queries_to_server;
213   struct list_node all_queries;
214
215   /* Query buf with length at beginning, for TCP transmission */
216   unsigned char *tcpbuf;
217   int tcplen;
218
219   /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
220   const unsigned char *qbuf;
221   int qlen;
222   ares_callback callback;
223   void *arg;
224
225   /* Query status */
226   int try_count; /* Number of times we tried this query already. */
227   int server; /* Server this query has last been sent to. */
228   struct query_server_info *server_info;   /* per-server state */
229   int using_tcp;
230   int error_status;
231   int timeouts; /* number of timeouts we saw for this request */
232 };
233
234 /* Per-server state for a query */
235 struct query_server_info {
236   int skip_server;  /* should we skip server, due to errors, etc? */
237   int tcp_connection_generation;  /* into which TCP connection did we send? */
238 };
239
240 /* An IP address pattern; matches an IP address X if X & mask == addr */
241 #define PATTERN_MASK 0x1
242 #define PATTERN_CIDR 0x2
243
244 struct apattern {
245   union
246   {
247     struct in_addr       addr4;
248     struct ares_in6_addr addr6;
249   } addr;
250   union
251   {
252     struct in_addr       addr4;
253     struct ares_in6_addr addr6;
254     unsigned short       bits;
255   } mask;
256   int family;
257   unsigned short type;
258 };
259
260 typedef struct rc4_key
261 {
262   unsigned char state[256];
263   unsigned char x;
264   unsigned char y;
265 } rc4_key;
266
267 struct ares_channeldata {
268   /* Configuration data */
269   int flags;
270   int timeout; /* in milliseconds */
271   int tries;
272   int ndots;
273   int rotate; /* if true, all servers specified are used */
274   int udp_port; /* stored in network order */
275   int tcp_port; /* stored in network order */
276   int socket_send_buffer_size;
277   int socket_receive_buffer_size;
278   char **domains;
279   int ndomains;
280   struct apattern *sortlist;
281   int nsort;
282   char *lookups;
283   int ednspsz;
284
285   /* For binding to local devices and/or IP addresses.  Leave
286    * them null/zero for no binding.
287    */
288   char local_dev_name[32];
289   unsigned int local_ip4;
290   unsigned char local_ip6[16];
291
292   int optmask; /* the option bitfield passed in at init time */
293
294   /* Server addresses and communications state */
295   struct server_state *servers;
296   int nservers;
297
298   /* ID to use for next query */
299   unsigned short next_id;
300   /* key to use when generating new ids */
301   rc4_key id_key;
302
303   /* Generation number to use for the next TCP socket open/close */
304   int tcp_connection_generation;
305
306   /* The time at which we last called process_timeouts(). Uses integer seconds
307      just to draw the line somewhere. */
308   time_t last_timeout_processed;
309
310   /* Last server we sent a query to. */
311   int last_server;
312
313   /* Circular, doubly-linked list of queries, bucketed various ways.... */
314   /* All active queries in a single list: */
315   struct list_node all_queries;
316   /* Queries bucketed by qid, for quickly dispatching DNS responses: */
317 #define ARES_QID_TABLE_SIZE 2048
318   struct list_node queries_by_qid[ARES_QID_TABLE_SIZE];
319   /* Queries bucketed by timeout, for quickly handling timeouts: */
320 #define ARES_TIMEOUT_TABLE_SIZE 1024
321   struct list_node queries_by_timeout[ARES_TIMEOUT_TABLE_SIZE];
322
323   ares_sock_state_cb sock_state_cb;
324   void *sock_state_cb_data;
325
326   ares_sock_create_callback sock_create_cb;
327   void *sock_create_cb_data;
328
329   ares_sock_config_callback sock_config_cb;
330   void *sock_config_cb_data;
331
332   const struct ares_socket_functions * sock_funcs;
333   void *sock_func_cb_data;
334
335   /* Path for resolv.conf file, configurable via ares_options */
336   char *resolvconf_path;
337 };
338
339 /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */
340 int ares__is_onion_domain(const char *name);
341
342 /* Memory management functions */
343 extern void *(*ares_malloc)(size_t size);
344 extern void *(*ares_realloc)(void *ptr, size_t size);
345 extern void (*ares_free)(void *ptr);
346
347 /* return true if now is exactly check time or later */
348 int ares__timedout(struct timeval *now,
349                    struct timeval *check);
350
351 void ares__send_query(ares_channel channel, struct query *query,
352                       struct timeval *now);
353 void ares__close_sockets(ares_channel channel, struct server_state *server);
354 int ares__get_hostent(FILE *fp, int family, struct hostent **host);
355 int ares__read_line(FILE *fp, char **buf, size_t *bufsize);
356 void ares__free_query(struct query *query);
357 unsigned short ares__generate_new_id(rc4_key* key);
358 struct timeval ares__tvnow(void);
359 int ares__expand_name_for_response(const unsigned char *encoded,
360                                    const unsigned char *abuf, int alen,
361                                    char **s, long *enclen);
362 void ares__init_servers_state(ares_channel channel);
363 void ares__destroy_servers_state(ares_channel channel);
364 int ares__parse_qtype_reply(const unsigned char* abuf, int alen, int* qtype);
365 int ares__single_domain(ares_channel channel, const char *name, char **s);
366 int ares__cat_domain(const char *name, const char *domain, char **s);
367 int ares__sortaddrinfo(ares_channel channel, struct ares_addrinfo_node *ai_node);
368 int ares__readaddrinfo(FILE *fp, const char *name, unsigned short port,
369                        const struct ares_addrinfo_hints *hints,
370                        struct ares_addrinfo *ai);
371
372 struct ares_addrinfo *ares__malloc_addrinfo(void);
373
374 struct ares_addrinfo_node *ares__malloc_addrinfo_node(void);
375 void ares__freeaddrinfo_nodes(struct ares_addrinfo_node *ai_node);
376
377 struct ares_addrinfo_node *ares__append_addrinfo_node(struct ares_addrinfo_node **ai_node);
378 void ares__addrinfo_cat_nodes(struct ares_addrinfo_node **head,
379                               struct ares_addrinfo_node *tail);
380
381 struct ares_addrinfo_cname *ares__malloc_addrinfo_cname(void);
382 void ares__freeaddrinfo_cnames(struct ares_addrinfo_cname *ai_cname);
383
384 struct ares_addrinfo_cname *ares__append_addrinfo_cname(struct ares_addrinfo_cname **ai_cname);
385
386 void ares__addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
387                                struct ares_addrinfo_cname *tail);
388
389 int ares__parse_into_addrinfo(const unsigned char *abuf,
390                               int alen,
391                               struct ares_addrinfo *ai);
392
393 int ares__parse_into_addrinfo2(const unsigned char *abuf,
394                                int alen,
395                                char **question_hostname,
396                                struct ares_addrinfo *ai);
397
398 #if 0 /* Not used */
399 long ares__tvdiff(struct timeval t1, struct timeval t2);
400 #endif
401
402 ares_socket_t ares__open_socket(ares_channel channel,
403                                 int af, int type, int protocol);
404 void ares__close_socket(ares_channel, ares_socket_t);
405 int ares__connect_socket(ares_channel channel,
406                          ares_socket_t sockfd,
407                          const struct sockaddr *addr,
408                          ares_socklen_t addrlen);
409
410 #define ARES_SWAP_BYTE(a,b) \
411   { unsigned char swapByte = *(a);  *(a) = *(b);  *(b) = swapByte; }
412
413 #define SOCK_STATE_CALLBACK(c, s, r, w)                                 \
414   do {                                                                  \
415     if ((c)->sock_state_cb)                                             \
416       (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w));       \
417   } WHILE_FALSE
418
419 #endif /* __ARES_PRIVATE_H */