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 /* 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]
56 #define CARES_INADDR_CAST(type, var) ((type)((void *)var))
58 #if defined(WIN32) && !defined(WATT32)
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"
76 #define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
77 W32_FUNC const char *_w32_GetHostsFile (void);
79 #elif defined(NETWARE)
81 #define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
82 #define PATH_HOSTS "sys:/etc/hosts"
84 #elif defined(__riscos__)
86 #define PATH_HOSTS "InetDBase:Hosts"
90 #define PATH_RESOLV_CONF "/etc/resolv.conf"
92 #define PATH_HOSTS "/etc/inet/hosts"
94 #define PATH_HOSTS "/etc/hosts"
99 #define ARES_ID_KEY_LEN 31
101 #include "ares_ipv6.h"
102 #include "ares_llist.h"
105 # include "ares_getenv.h"
106 # define getenv(ptr) ares_getenv(ptr)
109 #include "ares_strdup.h"
110 #include "ares_strsplit.h"
112 #ifndef HAVE_STRCASECMP
113 # include "ares_strcasecmp.h"
114 # define strcasecmp(p1,p2) ares_strcasecmp(p1,p2)
117 #ifndef HAVE_STRNCASECMP
118 # include "ares_strcasecmp.h"
119 # define strncasecmp(p1,p2,n) ares_strncasecmp(p1,p2,n)
123 # include "ares_writev.h"
124 # define writev(s,ptr,cnt) ares_writev(s,ptr,cnt)
127 /********* EDNS defines section ******/
128 #define EDNSPACKETSZ 1280 /* Reasonable UDP payload size, as suggested
130 #define MAXENDSSZ 4096 /* Maximum (local) limit for edns packet size */
131 #define EDNSFIXEDSZ 11 /* Size of EDNS header */
132 /********* EDNS defines section ******/
137 struct in_addr addr4;
138 struct ares_in6_addr addr6;
140 int udp_port; /* stored in network order */
141 int tcp_port; /* stored in network order */
143 #define addrV4 addr.addr4
144 #define addrV6 addr.addr6
148 struct send_request {
149 /* Remaining data to send */
150 const unsigned char *data;
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;
158 /* Next request in queue */
159 struct send_request *next;
162 struct server_state {
163 struct ares_addr addr;
164 ares_socket_t udp_socket;
165 ares_socket_t tcp_socket;
167 /* Mini-buffer for reading the length word */
168 unsigned char tcp_lenbuf[2];
172 /* Buffer for reading actual TCP data */
173 unsigned char *tcp_buffer;
176 /* TCP output queue */
177 struct send_request *qhead;
178 struct send_request *qtail;
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
184 int tcp_connection_generation;
186 /* Circular, doubly-linked list of outstanding queries to this server */
187 struct list_node queries_to_server;
189 /* Link back to owning channel */
190 ares_channel channel;
192 /* Is this server broken? We mark connections as broken when a
193 * request that is queued for sending times out.
198 /* State to represent a DNS query */
200 /* Query ID from qbuf, for faster lookup, and current timeout */
202 struct timeval timeout;
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
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;
215 /* Query buf with length at beginning, for TCP transmission */
216 unsigned char *tcpbuf;
219 /* Arguments passed to ares_send() (qbuf points into tcpbuf) */
220 const unsigned char *qbuf;
222 ares_callback callback;
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 */
231 int timeouts; /* number of timeouts we saw for this request */
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? */
240 /* An IP address pattern; matches an IP address X if X & mask == addr */
241 #define PATTERN_MASK 0x1
242 #define PATTERN_CIDR 0x2
247 struct in_addr addr4;
248 struct ares_in6_addr addr6;
252 struct in_addr addr4;
253 struct ares_in6_addr addr6;
260 typedef struct rc4_key
262 unsigned char state[256];
267 struct ares_channeldata {
268 /* Configuration data */
270 int timeout; /* in milliseconds */
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;
280 struct apattern *sortlist;
285 /* For binding to local devices and/or IP addresses. Leave
286 * them null/zero for no binding.
288 char local_dev_name[32];
289 unsigned int local_ip4;
290 unsigned char local_ip6[16];
292 int optmask; /* the option bitfield passed in at init time */
294 /* Server addresses and communications state */
295 struct server_state *servers;
298 /* ID to use for next query */
299 unsigned short next_id;
300 /* key to use when generating new ids */
303 /* Generation number to use for the next TCP socket open/close */
304 int tcp_connection_generation;
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;
310 /* Last server we sent a query to. */
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];
323 ares_sock_state_cb sock_state_cb;
324 void *sock_state_cb_data;
326 ares_sock_create_callback sock_create_cb;
327 void *sock_create_cb_data;
329 ares_sock_config_callback sock_config_cb;
330 void *sock_config_cb_data;
332 const struct ares_socket_functions * sock_funcs;
333 void *sock_func_cb_data;
335 /* Path for resolv.conf file, configurable via ares_options */
336 char *resolvconf_path;
339 /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */
340 int ares__is_onion_domain(const char *name);
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);
347 /* return true if now is exactly check time or later */
348 int ares__timedout(struct timeval *now,
349 struct timeval *check);
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);
372 struct ares_addrinfo *ares__malloc_addrinfo(void);
374 struct ares_addrinfo_node *ares__malloc_addrinfo_node(void);
375 void ares__freeaddrinfo_nodes(struct ares_addrinfo_node *ai_node);
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);
381 struct ares_addrinfo_cname *ares__malloc_addrinfo_cname(void);
382 void ares__freeaddrinfo_cnames(struct ares_addrinfo_cname *ai_cname);
384 struct ares_addrinfo_cname *ares__append_addrinfo_cname(struct ares_addrinfo_cname **ai_cname);
386 void ares__addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
387 struct ares_addrinfo_cname *tail);
389 int ares__parse_into_addrinfo(const unsigned char *abuf,
391 struct ares_addrinfo *ai);
393 int ares__parse_into_addrinfo2(const unsigned char *abuf,
395 char **question_hostname,
396 struct ares_addrinfo *ai);
399 long ares__tvdiff(struct timeval t1, struct timeval t2);
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);
410 #define ARES_SWAP_BYTE(a,b) \
411 { unsigned char swapByte = *(a); *(a) = *(b); *(b) = swapByte; }
413 #define SOCK_STATE_CALLBACK(c, s, r, w) \
415 if ((c)->sock_state_cb) \
416 (c)->sock_state_cb((c)->sock_state_cb_data, (s), (r), (w)); \
419 #endif /* __ARES_PRIVATE_H */