1 #ifndef __ARES_PRIVATE_H
2 #define __ARES_PRIVATE_H
5 /* Copyright 1998 by the Massachusetts Institute of Technology.
6 * Copyright (C) 2004-2010 by Daniel Stenberg
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.
22 * Define WIN32 when build target is Win32 API
25 #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
35 #include <sys/ioctl.h>
36 #define writev(s,v,c) writev_s(s,v,c)
40 #define DEFAULT_TIMEOUT 5000 /* milliseconds */
41 #define DEFAULT_TRIES 4
43 #define INADDR_NONE 0xffffffff
46 #ifdef CARES_EXPOSE_STATICS
47 /* Make some internal functions visible for testing */
48 #define STATIC_TESTABLE
50 #define STATIC_TESTABLE static
53 #if defined(WIN32) && !defined(WATT32)
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"
64 #define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
66 #elif defined(NETWARE)
68 #define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
69 #define PATH_HOSTS "sys:/etc/hosts"
71 #elif defined(__riscos__)
73 #define PATH_HOSTS "InetDBase:Hosts"
77 #define PATH_RESOLV_CONF "/etc/resolv.conf"
79 #define PATH_HOSTS "/etc/inet/hosts"
81 #define PATH_HOSTS "/etc/hosts"
86 #define ARES_ID_KEY_LEN 31
88 #include "ares_ipv6.h"
89 #include "ares_llist.h"
92 # include "ares_getenv.h"
93 # define getenv(ptr) ares_getenv(ptr)
96 #include "ares_strdup.h"
98 #ifndef HAVE_STRCASECMP
99 # include "ares_strcasecmp.h"
100 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
103 #ifndef HAVE_STRNCASECMP
104 # include "ares_strcasecmp.h"
105 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
109 # include "ares_writev.h"
110 # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
113 /********* EDNS defines section ******/
114 #define EDNSPACKETSZ 1280 /* Reasonable UDP payload size, as suggested
116 #define MAXENDSSZ 4096 /* Maximum (local) limit for edns packet size */
117 #define EDNSFIXEDSZ 11 /* Size of EDNS header */
118 /********* EDNS defines section ******/
123 struct in_addr addr4;
124 struct ares_in6_addr addr6;
126 int udp_port; /* stored in network order */
127 int tcp_port; /* stored in network order */
129 #define addrV4 addr.addr4
130 #define addrV6 addr.addr6
134 struct send_request {
135 /* Remaining data to send */
136 const unsigned char *data;
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;
144 /* Next request in queue */
145 struct send_request *next;
148 struct server_state {
149 struct ares_addr addr;
150 ares_socket_t udp_socket;
151 ares_socket_t tcp_socket;
153 /* Mini-buffer for reading the length word */
154 unsigned char tcp_lenbuf[2];
158 /* Buffer for reading actual TCP data */
159 unsigned char *tcp_buffer;
162 /* TCP output queue */
163 struct send_request *qhead;
164 struct send_request *qtail;
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
170 int tcp_connection_generation;
172 /* Circular, doubly-linked list of outstanding queries to this server */
173 struct list_node queries_to_server;
175 /* Link back to owning channel */
176 ares_channel channel;
178 /* Is this server broken? We mark connections as broken when a
179 * request that is queued for sending times out.
184 /* State to represent a DNS query */
186 /* Query ID from qbuf, for faster lookup, and current timeout */
188 struct timeval timeout;
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
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;
201 /* Query buf with length at beginning, for TCP transmission */
202 unsigned char *tcpbuf;
205 /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
206 const unsigned char *qbuf;
208 ares_callback callback;
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 */
217 int timeouts; /* number of timeouts we saw for this request */
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? */
226 /* An IP address pattern; matches an IP address X if X & mask == addr */
227 #define PATTERN_MASK 0x1
228 #define PATTERN_CIDR 0x2
233 struct in_addr addr4;
234 struct ares_in6_addr addr6;
238 struct in_addr addr4;
239 struct ares_in6_addr addr6;
246 typedef struct rc4_key
248 unsigned char state[256];
253 struct ares_channeldata {
254 /* Configuration data */
256 int timeout; /* in milliseconds */
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;
266 struct apattern *sortlist;
271 /* For binding to local devices and/or IP addresses. Leave
272 * them null/zero for no binding.
274 char local_dev_name[32];
275 unsigned int local_ip4;
276 unsigned char local_ip6[16];
278 int optmask; /* the option bitfield passed in at init time */
280 /* Server addresses and communications state */
281 struct server_state *servers;
284 /* ID to use for next query */
285 unsigned short next_id;
286 /* key to use when generating new ids */
289 /* Generation number to use for the next TCP socket open/close */
290 int tcp_connection_generation;
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;
296 /* Last server we sent a query to. */
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];
309 ares_sock_state_cb sock_state_cb;
310 void *sock_state_cb_data;
312 ares_sock_create_callback sock_create_cb;
313 void *sock_create_cb_data;
315 ares_sock_config_callback sock_config_cb;
316 void *sock_config_cb_data;
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);
324 /* return true if now is exactly check time or later */
325 int ares__timedout(struct timeval *now,
326 struct timeval *check);
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);
342 long ares__tvdiff(struct timeval t1, struct timeval t2);
345 #define ARES_SWAP_BYTE(a,b) \
346 { unsigned char swapByte = *(a); *(a) = *(b); *(b) = swapByte; }
348 #define SOCK_STATE_CALLBACK(c, s, r, w) \
350 if ((c)->sock_state_cb) \
351 (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \
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"
363 #endif /* __ARES_PRIVATE_H */