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 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"
65 #define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
67 #elif defined(NETWARE)
69 #define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
70 #define PATH_HOSTS "sys:/etc/hosts"
72 #elif defined(__riscos__)
74 #define PATH_HOSTS "InetDBase:Hosts"
78 #define PATH_RESOLV_CONF "/etc/resolv.conf"
80 #define PATH_HOSTS "/etc/inet/hosts"
82 #define PATH_HOSTS "/etc/hosts"
87 #define ARES_ID_KEY_LEN 31
89 #include "ares_ipv6.h"
90 #include "ares_llist.h"
93 # include "ares_strdup.h"
94 # define strdup(ptr) ares_strdup(ptr)
97 #ifndef HAVE_STRCASECMP
98 # include "ares_strcasecmp.h"
99 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
102 #ifndef HAVE_STRNCASECMP
103 # include "ares_strcasecmp.h"
104 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
108 # include "ares_writev.h"
109 # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
115 struct in_addr addr4;
116 struct ares_in6_addr addr6;
119 #define addrV4 addr.addr4
120 #define addrV6 addr.addr6
124 struct send_request {
125 /* Remaining data to send */
126 const unsigned char *data;
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;
134 /* Next request in queue */
135 struct send_request *next;
138 struct server_state {
139 struct ares_addr addr;
140 ares_socket_t udp_socket;
141 ares_socket_t tcp_socket;
143 /* Mini-buffer for reading the length word */
144 unsigned char tcp_lenbuf[2];
148 /* Buffer for reading actual TCP data */
149 unsigned char *tcp_buffer;
152 /* TCP output queue */
153 struct send_request *qhead;
154 struct send_request *qtail;
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
160 int tcp_connection_generation;
162 /* Circular, doubly-linked list of outstanding queries to this server */
163 struct list_node queries_to_server;
165 /* Link back to owning channel */
166 ares_channel channel;
168 /* Is this server broken? We mark connections as broken when a
169 * request that is queued for sending times out.
174 /* State to represent a DNS query */
176 /* Query ID from qbuf, for faster lookup, and current timeout */
178 struct timeval timeout;
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
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;
191 /* Query buf with length at beginning, for TCP transmission */
192 unsigned char *tcpbuf;
195 /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
196 const unsigned char *qbuf;
198 ares_callback callback;
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 */
207 int timeouts; /* number of timeouts we saw for this request */
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? */
216 /* An IP address pattern; matches an IP address X if X & mask == addr */
217 #define PATTERN_MASK 0x1
218 #define PATTERN_CIDR 0x2
223 struct in_addr addr4;
224 struct ares_in6_addr addr6;
228 struct in_addr addr4;
229 struct ares_in6_addr addr6;
236 typedef struct rc4_key
238 unsigned char state[256];
243 struct ares_channeldata {
244 /* Configuration data */
246 int timeout; /* in milliseconds */
249 int rotate; /* if true, all servers specified are used */
252 int socket_send_buffer_size;
253 int socket_receive_buffer_size;
256 struct apattern *sortlist;
260 int optmask; /* the option bitfield passed in at init time */
262 /* Server addresses and communications state */
263 struct server_state *servers;
266 /* ID to use for next query */
267 unsigned short next_id;
268 /* key to use when generating new ids */
271 /* Generation number to use for the next TCP socket open/close */
272 int tcp_connection_generation;
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;
278 /* Last server we sent a query to. */
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];
291 ares_sock_state_cb sock_state_cb;
292 void *sock_state_cb_data;
294 ares_sock_create_callback sock_create_cb;
295 void *sock_create_cb_data;
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,
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);
324 long ares__tvdiff(struct timeval t1, struct timeval t2);
327 #define ARES_SWAP_BYTE(a,b) \
328 { unsigned char swapByte = *(a); *(a) = *(b); *(b) = swapByte; }
330 #define SOCK_STATE_CALLBACK(c, s, r, w) \
332 if ((c)->sock_state_cb) \
333 (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \
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"
344 #endif /* __ARES_PRIVATE_H */