3 /* Copyright 1998, 2009 by the Massachusetts Institute of Technology.
4 * Copyright (C) 2007-2009 by Daniel Stenberg
6 * Permission to use, copy, modify, and distribute this
7 * software and its documentation for any purpose and without
8 * fee is hereby granted, provided that the above copyright
9 * notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting
11 * documentation, and that the name of M.I.T. not be used in
12 * advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 * M.I.T. makes no representations about the suitability of
15 * this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
22 #include "ares_version.h" /* c-ares version defines */
23 #include "ares_build.h" /* c-ares build definitions */
24 #include "ares_rules.h" /* c-ares rules enforcement */
27 * Define WIN32 when build target is Win32 API
30 #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
34 #include <sys/types.h>
36 /* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
37 libc5-based Linux systems. Only include it on system that are known to
39 #if defined(_AIX) || defined(__NOVELL_LIBC__) || defined(__NetBSD__) || \
40 defined(__minix) || defined(__SYMBIAN32__) || defined(__INTEGRITY)
41 #include <sys/select.h>
43 #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
44 #include <sys/bsdskt.h>
48 #include <netinet/in.h>
49 #include <sys/socket.h>
52 # ifndef WIN32_LEAN_AND_MEAN
53 # define WIN32_LEAN_AND_MEAN
56 # include <winsock2.h>
57 # include <ws2tcpip.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
67 #define ARES_SUCCESS 0
69 /* Server error codes (ARES_ENODATA indicates no relevant answer) */
70 #define ARES_ENODATA 1
71 #define ARES_EFORMERR 2
72 #define ARES_ESERVFAIL 3
73 #define ARES_ENOTFOUND 4
74 #define ARES_ENOTIMP 5
75 #define ARES_EREFUSED 6
77 /* Locally generated error codes */
78 #define ARES_EBADQUERY 7
79 #define ARES_EBADNAME 8
80 #define ARES_EBADFAMILY 9
81 #define ARES_EBADRESP 10
82 #define ARES_ECONNREFUSED 11
83 #define ARES_ETIMEOUT 12
86 #define ARES_ENOMEM 15
87 #define ARES_EDESTRUCTION 16
88 #define ARES_EBADSTR 17
89 #define ARES_ECANCELLED 21
91 /* ares_getnameinfo error codes */
92 #define ARES_EBADFLAGS 18
94 /* ares_getaddrinfo error codes */
95 #define ARES_ENONAME 19
96 #define ARES_EBADHINTS 20
98 /* Uninitialized library error code */
99 #define ARES_ENOTINITIALIZED 21
101 /* ares_library_init error codes */
102 #define ARES_ELOADIPHLPAPI 22
103 #define ARES_EADDRGETNETWORKPARAMS 23
106 #define ARES_FLAG_USEVC (1 << 0)
107 #define ARES_FLAG_PRIMARY (1 << 1)
108 #define ARES_FLAG_IGNTC (1 << 2)
109 #define ARES_FLAG_NORECURSE (1 << 3)
110 #define ARES_FLAG_STAYOPEN (1 << 4)
111 #define ARES_FLAG_NOSEARCH (1 << 5)
112 #define ARES_FLAG_NOALIASES (1 << 6)
113 #define ARES_FLAG_NOCHECKRESP (1 << 7)
115 /* Option mask values */
116 #define ARES_OPT_FLAGS (1 << 0)
117 #define ARES_OPT_TIMEOUT (1 << 1)
118 #define ARES_OPT_TRIES (1 << 2)
119 #define ARES_OPT_NDOTS (1 << 3)
120 #define ARES_OPT_UDP_PORT (1 << 4)
121 #define ARES_OPT_TCP_PORT (1 << 5)
122 #define ARES_OPT_SERVERS (1 << 6)
123 #define ARES_OPT_DOMAINS (1 << 7)
124 #define ARES_OPT_LOOKUPS (1 << 8)
125 #define ARES_OPT_SOCK_STATE_CB (1 << 9)
126 #define ARES_OPT_SORTLIST (1 << 10)
127 #define ARES_OPT_SOCK_SNDBUF (1 << 11)
128 #define ARES_OPT_SOCK_RCVBUF (1 << 12)
129 #define ARES_OPT_TIMEOUTMS (1 << 13)
130 #define ARES_OPT_ROTATE (1 << 14)
132 /* Nameinfo flag values */
133 #define ARES_NI_NOFQDN (1 << 0)
134 #define ARES_NI_NUMERICHOST (1 << 1)
135 #define ARES_NI_NAMEREQD (1 << 2)
136 #define ARES_NI_NUMERICSERV (1 << 3)
137 #define ARES_NI_DGRAM (1 << 4)
138 #define ARES_NI_TCP 0
139 #define ARES_NI_UDP ARES_NI_DGRAM
140 #define ARES_NI_SCTP (1 << 5)
141 #define ARES_NI_DCCP (1 << 6)
142 #define ARES_NI_NUMERICSCOPE (1 << 7)
143 #define ARES_NI_LOOKUPHOST (1 << 8)
144 #define ARES_NI_LOOKUPSERVICE (1 << 9)
145 /* Reserved for future use */
146 #define ARES_NI_IDN (1 << 10)
147 #define ARES_NI_IDN_ALLOW_UNASSIGNED (1 << 11)
148 #define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
150 /* Addrinfo flag values */
151 #define ARES_AI_CANONNAME (1 << 0)
152 #define ARES_AI_NUMERICHOST (1 << 1)
153 #define ARES_AI_PASSIVE (1 << 2)
154 #define ARES_AI_NUMERICSERV (1 << 3)
155 #define ARES_AI_V4MAPPED (1 << 4)
156 #define ARES_AI_ALL (1 << 5)
157 #define ARES_AI_ADDRCONFIG (1 << 6)
158 /* Reserved for future use */
159 #define ARES_AI_IDN (1 << 10)
160 #define ARES_AI_IDN_ALLOW_UNASSIGNED (1 << 11)
161 #define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
162 #define ARES_AI_CANONIDN (1 << 13)
164 #define ARES_AI_MASK (ARES_AI_CANONNAME|ARES_AI_NUMERICHOST|ARES_AI_PASSIVE| \
165 ARES_AI_NUMERICSERV|ARES_AI_V4MAPPED|ARES_AI_ALL| \
167 #define ARES_GETSOCK_MAXNUM 16 /* ares_getsock() can return info about this
169 #define ARES_GETSOCK_READABLE(bits,num) (bits & (1<< (num)))
170 #define ARES_GETSOCK_WRITABLE(bits,num) (bits & (1 << ((num) + \
171 ARES_GETSOCK_MAXNUM)))
173 /* c-ares library initialization flag values */
174 #define ARES_LIB_INIT_NONE (0)
175 #define ARES_LIB_INIT_WIN32 (1 << 0)
176 #define ARES_LIB_INIT_ALL (ARES_LIB_INIT_WIN32)
180 * Typedef our socket type
183 #ifndef ares_socket_typedef
185 typedef SOCKET ares_socket_t;
186 #define ARES_SOCKET_BAD INVALID_SOCKET
188 typedef int ares_socket_t;
189 #define ARES_SOCKET_BAD -1
191 #define ares_socket_typedef
192 #endif /* ares_socket_typedef */
194 typedef void (*ares_sock_state_cb)(void *data,
195 ares_socket_t socket_fd,
201 /* NOTE about the ares_options struct to users and developers.
203 This struct will remain looking like this. It will not be extended nor
204 shrunk in future releases, but all new options will be set by ares_set_*()
205 options instead of with the ares_init_options() function.
207 Eventually (in a galaxy far far away), all options will be settable by
208 ares_set_*() options and the ares_init_options() function will become
211 When new options are added to c-ares, they are not added to this
212 struct. And they are not "saved" with the ares_save_options() function but
213 instead we encourage the use of the ares_dup() function. Needless to say,
214 if you add config options to c-ares you need to make sure ares_dup()
215 duplicates this new option.
218 struct ares_options {
220 int timeout; /* in seconds or milliseconds, depending on options */
223 unsigned short udp_port;
224 unsigned short tcp_port;
225 int socket_send_buffer_size;
226 int socket_receive_buffer_size;
227 struct in_addr *servers;
232 ares_sock_state_cb sock_state_cb;
233 void *sock_state_cb_data;
234 struct apattern *sortlist;
241 struct ares_channeldata;
242 typedef struct ares_channeldata *ares_channel;
243 typedef void (*ares_callback)(void *arg, int status, int timeouts,
244 unsigned char *abuf, int alen);
245 typedef void (*ares_host_callback)(void *arg, int status, int timeouts,
246 struct hostent *hostent);
247 typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
248 char *node, char *service);
249 typedef int (*ares_sock_create_callback)(ares_socket_t socket_fd,
250 int type, void *data);
252 int ares_library_init(int flags);
253 void ares_library_cleanup(void);
254 const char *ares_version(int *version);
256 int ares_init(ares_channel *channelptr);
257 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
259 int ares_save_options(ares_channel channel, struct ares_options *options,
261 void ares_destroy_options(struct ares_options *options);
262 int ares_dup(ares_channel *dest, ares_channel src);
263 void ares_destroy(ares_channel channel);
264 void ares_cancel(ares_channel channel);
265 void ares_set_socket_callback(ares_channel channel,
266 ares_sock_create_callback callback,
268 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
269 ares_callback callback, void *arg);
270 void ares_query(ares_channel channel, const char *name, int dnsclass,
271 int type, ares_callback callback, void *arg);
272 void ares_search(ares_channel channel, const char *name, int dnsclass,
273 int type, ares_callback callback, void *arg);
274 void ares_gethostbyname(ares_channel channel, const char *name, int family,
275 ares_host_callback callback, void *arg);
276 int ares_gethostbyname_file(ares_channel channel, const char *name,
277 int family, struct hostent **host);
278 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
279 int family, ares_host_callback callback, void *arg);
280 void ares_getnameinfo(ares_channel channel, const struct sockaddr *sa,
281 ares_socklen_t salen, int flags,
282 ares_nameinfo_callback callback,
284 int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds);
285 int ares_getsock(ares_channel channel, int *socks, int numsocks);
286 struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv,
288 void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds);
289 void ares_process_fd(ares_channel channel, ares_socket_t read_fd,
290 ares_socket_t write_fd);
292 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
293 int rd, unsigned char **buf, int *buflen);
294 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
295 int alen, char **s, long *enclen);
296 int ares_expand_string(const unsigned char *encoded, const unsigned char *abuf,
297 int alen, unsigned char **s, long *enclen);
300 * NOTE: before c-ares 1.6.1 we would most often use the system in6_addr
301 * struct below when ares itself was built, but many apps would use this
302 * private version since the header checked a HAVE_* define for it. Starting
303 * with 1.6.1 we always declare and use our own to stop relying on the
306 struct ares_in6_addr {
308 unsigned char _S6_u8[16];
313 * TODO: the structs 'addrttl' and 'addr6ttl' really should get their names
314 * prefixed with ares_ to keep them in our own "name space".
317 struct in_addr ipaddr;
321 struct ares_in6_addr ip6addr;
326 unsigned short weight;
327 unsigned short priority;
333 ** Parse the buffer, starting at *abuf and of length alen bytes, previously
334 ** obtained from an ares_search call. Put the results in *host, if nonnull.
335 ** Also, if addrttls is nonnull, put up to *naddrttls IPv4 addresses along with
336 ** their TTLs in that array, and set *naddrttls to the number of addresses
339 int ares_parse_a_reply(const unsigned char *abuf, int alen,
340 struct hostent **host,
341 struct addrttl *addrttls, int *naddrttls);
342 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
343 struct hostent **host,
344 struct addr6ttl *addrttls, int *naddrttls);
345 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
346 int addrlen, int family, struct hostent **host);
347 int ares_parse_ns_reply(const unsigned char *abuf, int alen,
348 struct hostent **host);
349 int ares_parse_srv_reply(const unsigned char* abuf, int alen,
350 struct srv_reply** srv_out, int *nsrvreply);
351 void ares_free_string(void *str);
352 void ares_free_hostent(struct hostent *host);
353 const char *ares_strerror(int code);