Support Watt-32 under Win32.
[platform/upstream/c-ares.git] / ares.h
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  * Copyright (C) 2007 by Daniel Stenberg
5  *
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.
17  */
18
19 #ifndef ARES__H
20 #define ARES__H
21
22 /*
23  * Define WIN32 when build target is Win32 API
24  */
25
26 #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
27 #define WIN32
28 #endif
29
30 #include <sys/types.h>
31
32 #if defined(_AIX) || (defined(NETWARE) && defined(__NOVELL_LIBC__))
33 /* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
34    libc5-based Linux systems. Only include it on system that are known to
35    require it! */
36 #include <sys/select.h>
37 #endif
38 #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
39 #include <sys/bsdskt.h>
40 #endif
41
42 #if defined(WATT32)
43   #include <netinet/in.h>
44   #include <sys/socket.h>
45   #include <tcp.h>
46 #elif defined(WIN32)
47 #  ifndef WIN32_LEAN_AND_MEAN
48 #    define WIN32_LEAN_AND_MEAN
49 #  endif
50 #  include <windows.h>
51 #  include <winsock2.h>
52 #  include <ws2tcpip.h>
53 #else
54   #include <netinet/in.h>
55   #include <sys/socket.h>
56 #endif
57
58 #ifdef  __cplusplus
59 extern "C" {
60 #endif
61
62 #define ARES_SUCCESS            0
63
64 /* Server error codes (ARES_ENODATA indicates no relevant answer) */
65 #define ARES_ENODATA            1
66 #define ARES_EFORMERR           2
67 #define ARES_ESERVFAIL          3
68 #define ARES_ENOTFOUND          4
69 #define ARES_ENOTIMP            5
70 #define ARES_EREFUSED           6
71
72 /* Locally generated error codes */
73 #define ARES_EBADQUERY          7
74 #define ARES_EBADNAME           8
75 #define ARES_EBADFAMILY         9
76 #define ARES_EBADRESP           10
77 #define ARES_ECONNREFUSED       11
78 #define ARES_ETIMEOUT           12
79 #define ARES_EOF                13
80 #define ARES_EFILE              14
81 #define ARES_ENOMEM             15
82 #define ARES_EDESTRUCTION       16
83 #define ARES_EBADSTR            17
84
85 /* ares_getnameinfo error codes */
86 #define ARES_EBADFLAGS          18
87
88 /* ares_getaddrinfo error codes */
89 #define ARES_ENONAME            19
90 #define ARES_EBADHINTS          20
91
92 /* Flag values */
93 #define ARES_FLAG_USEVC         (1 << 0)
94 #define ARES_FLAG_PRIMARY       (1 << 1)
95 #define ARES_FLAG_IGNTC         (1 << 2)
96 #define ARES_FLAG_NORECURSE     (1 << 3)
97 #define ARES_FLAG_STAYOPEN      (1 << 4)
98 #define ARES_FLAG_NOSEARCH      (1 << 5)
99 #define ARES_FLAG_NOALIASES     (1 << 6)
100 #define ARES_FLAG_NOCHECKRESP   (1 << 7)
101
102 /* Option mask values */
103 #define ARES_OPT_FLAGS          (1 << 0)
104 #define ARES_OPT_TIMEOUT        (1 << 1)
105 #define ARES_OPT_TRIES          (1 << 2)
106 #define ARES_OPT_NDOTS          (1 << 3)
107 #define ARES_OPT_UDP_PORT       (1 << 4)
108 #define ARES_OPT_TCP_PORT       (1 << 5)
109 #define ARES_OPT_SERVERS        (1 << 6)
110 #define ARES_OPT_DOMAINS        (1 << 7)
111 #define ARES_OPT_LOOKUPS        (1 << 8)
112 #define ARES_OPT_SOCK_STATE_CB  (1 << 9)
113 #define ARES_OPT_SORTLIST       (1 << 10)
114 #define ARES_OPT_SOCK_SNDBUF    (1 << 11)
115 #define ARES_OPT_SOCK_RCVBUF    (1 << 12)
116 #define ARES_OPT_TIMEOUTMS      (1 << 13)
117
118 /* Nameinfo flag values */
119 #define ARES_NI_NOFQDN                  (1 << 0)
120 #define ARES_NI_NUMERICHOST             (1 << 1)
121 #define ARES_NI_NAMEREQD                (1 << 2)
122 #define ARES_NI_NUMERICSERV             (1 << 3)
123 #define ARES_NI_DGRAM                   (1 << 4)
124 #define ARES_NI_TCP                     0
125 #define ARES_NI_UDP                     ARES_NI_DGRAM
126 #define ARES_NI_SCTP                    (1 << 5)
127 #define ARES_NI_DCCP                    (1 << 6)
128 #define ARES_NI_NUMERICSCOPE            (1 << 7)
129 #define ARES_NI_LOOKUPHOST              (1 << 8)
130 #define ARES_NI_LOOKUPSERVICE           (1 << 9)
131 /* Reserved for future use */
132 #define ARES_NI_IDN                     (1 << 10)
133 #define ARES_NI_IDN_ALLOW_UNASSIGNED    (1 << 11)
134 #define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
135
136 /* Addrinfo flag values */
137 #define ARES_AI_CANONNAME               (1 << 0)
138 #define ARES_AI_NUMERICHOST             (1 << 1)
139 #define ARES_AI_PASSIVE                 (1 << 2)
140 #define ARES_AI_NUMERICSERV             (1 << 3)
141 #define ARES_AI_V4MAPPED                (1 << 4)
142 #define ARES_AI_ALL                     (1 << 5)
143 #define ARES_AI_ADDRCONFIG              (1 << 6)
144 /* Reserved for future use */
145 #define ARES_AI_IDN                     (1 << 10)
146 #define ARES_AI_IDN_ALLOW_UNASSIGNED    (1 << 11)
147 #define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
148 #define ARES_AI_CANONIDN                (1 << 13)
149
150 #define ARES_AI_MASK (ARES_AI_CANONNAME|ARES_AI_NUMERICHOST|ARES_AI_PASSIVE| \
151                       ARES_AI_NUMERICSERV|ARES_AI_V4MAPPED|ARES_AI_ALL| \
152                       ARES_AI_ADDRCONFIG)
153 #define ARES_GETSOCK_MAXNUM 16 /* ares_getsock() can return info about this
154                                   many sockets */
155 #define ARES_GETSOCK_READABLE(bits,num) (bits & (1<< (num)))
156 #define ARES_GETSOCK_WRITABLE(bits,num) (bits & (1 << ((num) + \
157                                          ARES_GETSOCK_MAXNUM)))
158
159
160 /*
161  * Typedef our socket type
162  */
163
164 #ifndef ares_socket_typedef
165 #ifdef WIN32
166 typedef SOCKET ares_socket_t;
167 #define ARES_SOCKET_BAD INVALID_SOCKET
168 #else
169 typedef int ares_socket_t;
170 #define ARES_SOCKET_BAD -1
171 #endif
172 #define ares_socket_typedef
173 #endif /* ares_socket_typedef */
174
175 typedef void (*ares_sock_state_cb)(void *data,
176                                    ares_socket_t socket_fd,
177                                    int readable,
178                                    int writable);
179
180 struct apattern;
181
182 struct ares_options {
183   int flags;
184   int timeout; /* in seconds or milliseconds, depending on options */
185   int tries;
186   int ndots;
187   unsigned short udp_port;
188   unsigned short tcp_port;
189   int socket_send_buffer_size;
190   int socket_receive_buffer_size;
191   struct in_addr *servers;
192   int nservers;
193   char **domains;
194   int ndomains;
195   char *lookups;
196   ares_sock_state_cb sock_state_cb;
197   void *sock_state_cb_data;
198   struct apattern *sortlist;
199   int nsort;
200 };
201
202 struct hostent;
203 struct timeval;
204 struct sockaddr;
205 struct ares_channeldata;
206 typedef struct ares_channeldata *ares_channel;
207 typedef void (*ares_callback)(void *arg, int status, int timeouts,
208                               unsigned char *abuf, int alen);
209 typedef void (*ares_host_callback)(void *arg, int status, int timeouts,
210                                    struct hostent *hostent);
211 typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
212                                        char *node, char *service);
213
214 int ares_init(ares_channel *channelptr);
215 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
216                       int optmask);
217 int ares_save_options(ares_channel channel, struct ares_options *options, int *optmask);
218 void ares_destroy_options(struct ares_options *options);
219 void ares_destroy(ares_channel channel);
220 void ares_cancel(ares_channel channel);
221 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
222                ares_callback callback, void *arg);
223 void ares_query(ares_channel channel, const char *name, int dnsclass,
224                 int type, ares_callback callback, void *arg);
225 void ares_search(ares_channel channel, const char *name, int dnsclass,
226                  int type, ares_callback callback, void *arg);
227 void ares_gethostbyname(ares_channel channel, const char *name, int family,
228                         ares_host_callback callback, void *arg);
229 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
230                         int family, ares_host_callback callback, void *arg);
231 void ares_getnameinfo(ares_channel channel, const struct sockaddr *sa,
232                       socklen_t salen, int flags,
233                       ares_nameinfo_callback callback,
234                       void *arg);
235 int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds);
236 int ares_getsock(ares_channel channel, int *socks, int numsocks);
237 struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv,
238                              struct timeval *tv);
239 void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds);
240 void ares_process_fd(ares_channel channel, ares_socket_t read_fd,
241                      ares_socket_t write_fd);
242
243 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
244                  int rd, unsigned char **buf, int *buflen);
245 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
246                      int alen, char **s, long *enclen);
247 int ares_expand_string(const unsigned char *encoded, const unsigned char *abuf,
248                      int alen, unsigned char **s, long *enclen);
249
250 #if !defined(HAVE_STRUCT_IN6_ADDR) && !defined(s6_addr)
251 struct in6_addr {
252   union {
253     unsigned char _S6_u8[16];
254   } _S6_un;
255 };
256 #define s6_addr _S6_un._S6_u8
257 #endif
258
259 struct addrttl {
260   struct in_addr ipaddr;
261   int            ttl;
262 };
263 struct addr6ttl {
264   struct in6_addr ip6addr;
265   int             ttl;
266 };
267
268 /*
269 ** Parse the buffer, starting at *abuf and of length alen bytes, previously
270 ** obtained from an ares_search call.  Put the results in *host, if nonnull.
271 ** Also, if addrttls is nonnull, put up to *naddrttls IPv4 addresses along with
272 ** their TTLs in that array, and set *naddrttls to the number of addresses
273 ** so written.
274 */
275 int ares_parse_a_reply(const unsigned char *abuf, int alen,
276                        struct hostent **host,
277                        struct addrttl *addrttls, int *naddrttls);
278 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
279                        struct hostent **host,
280                        struct addr6ttl *addrttls, int *naddrttls);
281 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
282                          int addrlen, int family, struct hostent **host);
283 int ares_parse_ns_reply(const unsigned char *abuf, int alen,
284                        struct hostent **host);
285 void ares_free_string(void *str);
286 void ares_free_hostent(struct hostent *host);
287 const char *ares_strerror(int code);
288
289 #ifdef  __cplusplus
290 }
291 #endif
292
293 #endif /* ARES__H */