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