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)
30 #include <sys/types.h>
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
38 #include <sys/ioctl.h>
39 #define writev(s,v,c) writev_s(s,v,c)
47 #define DEFAULT_TIMEOUT 5000 /* milliseconds */
48 #define DEFAULT_TRIES 4
50 #define INADDR_NONE 0xffffffff
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)
97 # include "ares_strdup.h"
98 # define strdup(ptr) ares_strdup(ptr)
101 #ifndef HAVE_STRCASECMP
102 # include "ares_strcasecmp.h"
103 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
106 #ifndef HAVE_STRNCASECMP
107 # include "ares_strcasecmp.h"
108 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
112 # include "ares_writev.h"
113 # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
119 struct in_addr addr4;
120 struct ares_in6_addr addr6;
123 #define addrV4 addr.addr4
124 #define addrV6 addr.addr6
128 struct send_request {
129 /* Remaining data to send */
130 const unsigned char *data;
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;
138 /* Next request in queue */
139 struct send_request *next;
142 struct server_state {
143 struct ares_addr addr;
144 ares_socket_t udp_socket;
145 ares_socket_t tcp_socket;
147 /* Mini-buffer for reading the length word */
148 unsigned char tcp_lenbuf[2];
152 /* Buffer for reading actual TCP data */
153 unsigned char *tcp_buffer;
156 /* TCP output queue */
157 struct send_request *qhead;
158 struct send_request *qtail;
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
164 int tcp_connection_generation;
166 /* Circular, doubly-linked list of outstanding queries to this server */
167 struct list_node queries_to_server;
169 /* Link back to owning channel */
170 ares_channel channel;
172 /* Is this server broken? We mark connections as broken when a
173 * request that is queued for sending times out.
178 /* State to represent a DNS query */
180 /* Query ID from qbuf, for faster lookup, and current timeout */
182 struct timeval timeout;
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
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;
195 /* Query buf with length at beginning, for TCP transmission */
196 unsigned char *tcpbuf;
199 /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
200 const unsigned char *qbuf;
202 ares_callback callback;
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 */
211 int timeouts; /* number of timeouts we saw for this request */
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? */
220 /* An IP address pattern; matches an IP address X if X & mask == addr */
221 #define PATTERN_MASK 0x1
222 #define PATTERN_CIDR 0x2
227 struct in_addr addr4;
228 struct ares_in6_addr addr6;
232 struct in_addr addr4;
233 struct ares_in6_addr addr6;
240 typedef struct rc4_key
242 unsigned char state[256];
247 struct ares_channeldata {
248 /* Configuration data */
250 int timeout; /* in milliseconds */
253 int rotate; /* if true, all servers specified are used */
256 int socket_send_buffer_size;
257 int socket_receive_buffer_size;
260 struct apattern *sortlist;
264 /* For binding to local devices and/or IP addresses. Leave
265 * them null/zero for no binding.
267 char local_dev_name[32];
268 unsigned int local_ip4;
269 unsigned char local_ip6[16];
271 int optmask; /* the option bitfield passed in at init time */
273 /* Server addresses and communications state */
274 struct server_state *servers;
277 /* ID to use for next query */
278 unsigned short next_id;
279 /* key to use when generating new ids */
282 /* Generation number to use for the next TCP socket open/close */
283 int tcp_connection_generation;
285 /* The time at which we last called process_timeouts(). Uses integer seconds
286 just to draw the line somewhere. */
287 time_t last_timeout_processed;
289 /* Last server we sent a query to. */
292 /* Circular, doubly-linked list of queries, bucketed various ways.... */
293 /* All active queries in a single list: */
294 struct list_node all_queries;
295 /* Queries bucketed by qid, for quickly dispatching DNS responses: */
296 #define ARES_QID_TABLE_SIZE 2048
297 struct list_node queries_by_qid[ARES_QID_TABLE_SIZE];
298 /* Queries bucketed by timeout, for quickly handling timeouts: */
299 #define ARES_TIMEOUT_TABLE_SIZE 1024
300 struct list_node queries_by_timeout[ARES_TIMEOUT_TABLE_SIZE];
302 ares_sock_state_cb sock_state_cb;
303 void *sock_state_cb_data;
305 ares_sock_create_callback sock_create_cb;
306 void *sock_create_cb_data;
309 /* return true if now is exactly check time or later */
310 int ares__timedout(struct timeval *now,
311 struct timeval *check);
312 /* add the specific number of milliseconds to the time in the first argument */
313 int ares__timeadd(struct timeval *now,
315 /* return time offset between now and (future) check, in milliseconds */
316 long ares__timeoffset(struct timeval *now,
317 struct timeval *check);
318 /* returns ARES_SUCCESS if library has been initialized */
319 int ares_library_initialized(void);
320 void ares__rc4(rc4_key* key,unsigned char *buffer_ptr, int buffer_len);
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);
335 long ares__tvdiff(struct timeval t1, struct timeval t2);
338 #define ARES_SWAP_BYTE(a,b) \
339 { unsigned char swapByte = *(a); *(a) = *(b); *(b) = swapByte; }
341 #define SOCK_STATE_CALLBACK(c, s, r, w) \
343 if ((c)->sock_state_cb) \
344 (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \
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 #include "../lib/memdebug.h"
355 #endif /* __ARES_PRIVATE_H */