Upgrade to 1.17.1
[platform/upstream/c-ares.git] / ares.h
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  * Copyright (C) 2007-2013 by Daniel Stenberg
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 #include "ares_version.h"  /* c-ares version defines   */
22 #include "ares_build.h"    /* c-ares build definitions */
23 #include "ares_rules.h"    /* c-ares rules enforcement */
24
25 /*
26  * Define WIN32 when build target is Win32 API
27  */
28
29 #if (defined(_WIN32) || defined(__WIN32__)) && \
30    !defined(WIN32) && !defined(__SYMBIAN32__)
31 #  define WIN32
32 #endif
33
34 #include <sys/types.h>
35
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
38    require it! */
39 #if defined(_AIX) || defined(__NOVELL_LIBC__) || defined(__NetBSD__) || \
40     defined(__minix) || defined(__SYMBIAN32__) || defined(__INTEGRITY) || \
41     defined(ANDROID) || defined(__ANDROID__)
42 #include <sys/select.h>
43 #endif
44 #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
45 #include <sys/bsdskt.h>
46 #endif
47
48 #if defined(WATT32)
49 #  include <netinet/in.h>
50 #  include <sys/socket.h>
51 #  include <tcp.h>
52 #elif defined(_WIN32_WCE)
53 #  ifndef WIN32_LEAN_AND_MEAN
54 #    define WIN32_LEAN_AND_MEAN
55 #  endif
56 #  include <windows.h>
57 #  include <winsock.h>
58 #elif defined(WIN32)
59 #  ifndef WIN32_LEAN_AND_MEAN
60 #    define WIN32_LEAN_AND_MEAN
61 #  endif
62 #  include <windows.h>
63 #  include <winsock2.h>
64 #  include <ws2tcpip.h>
65 #else
66 #  include <sys/socket.h>
67 #  include <netinet/in.h>
68 #endif
69
70 #ifdef  __cplusplus
71 extern "C" {
72 #endif
73
74 /*
75 ** c-ares external API function linkage decorations.
76 */
77
78 #ifdef CARES_STATICLIB
79 #  define CARES_EXTERN
80 #elif defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__)
81 #  if defined(CARES_BUILDING_LIBRARY)
82 #    define CARES_EXTERN  __declspec(dllexport)
83 #  else
84 #    define CARES_EXTERN  __declspec(dllimport)
85 #  endif
86 #elif defined(CARES_BUILDING_LIBRARY) && defined(CARES_SYMBOL_HIDING)
87 #  define CARES_EXTERN CARES_SYMBOL_SCOPE_EXTERN
88 #else
89 #  define CARES_EXTERN
90 #endif
91
92
93 #define ARES_SUCCESS            0
94
95 /* Server error codes (ARES_ENODATA indicates no relevant answer) */
96 #define ARES_ENODATA            1
97 #define ARES_EFORMERR           2
98 #define ARES_ESERVFAIL          3
99 #define ARES_ENOTFOUND          4
100 #define ARES_ENOTIMP            5
101 #define ARES_EREFUSED           6
102
103 /* Locally generated error codes */
104 #define ARES_EBADQUERY          7
105 #define ARES_EBADNAME           8
106 #define ARES_EBADFAMILY         9
107 #define ARES_EBADRESP           10
108 #define ARES_ECONNREFUSED       11
109 #define ARES_ETIMEOUT           12
110 #define ARES_EOF                13
111 #define ARES_EFILE              14
112 #define ARES_ENOMEM             15
113 #define ARES_EDESTRUCTION       16
114 #define ARES_EBADSTR            17
115
116 /* ares_getnameinfo error codes */
117 #define ARES_EBADFLAGS          18
118
119 /* ares_getaddrinfo error codes */
120 #define ARES_ENONAME            19
121 #define ARES_EBADHINTS          20
122
123 /* Uninitialized library error code */
124 #define ARES_ENOTINITIALIZED    21          /* introduced in 1.7.0 */
125
126 /* ares_library_init error codes */
127 #define ARES_ELOADIPHLPAPI           22     /* introduced in 1.7.0 */
128 #define ARES_EADDRGETNETWORKPARAMS   23     /* introduced in 1.7.0 */
129
130 /* More error codes */
131 #define ARES_ECANCELLED         24          /* introduced in 1.7.0 */
132
133 /* Flag values */
134 #define ARES_FLAG_USEVC         (1 << 0)
135 #define ARES_FLAG_PRIMARY       (1 << 1)
136 #define ARES_FLAG_IGNTC         (1 << 2)
137 #define ARES_FLAG_NORECURSE     (1 << 3)
138 #define ARES_FLAG_STAYOPEN      (1 << 4)
139 #define ARES_FLAG_NOSEARCH      (1 << 5)
140 #define ARES_FLAG_NOALIASES     (1 << 6)
141 #define ARES_FLAG_NOCHECKRESP   (1 << 7)
142 #define ARES_FLAG_EDNS          (1 << 8)
143
144 /* Option mask values */
145 #define ARES_OPT_FLAGS          (1 << 0)
146 #define ARES_OPT_TIMEOUT        (1 << 1)
147 #define ARES_OPT_TRIES          (1 << 2)
148 #define ARES_OPT_NDOTS          (1 << 3)
149 #define ARES_OPT_UDP_PORT       (1 << 4)
150 #define ARES_OPT_TCP_PORT       (1 << 5)
151 #define ARES_OPT_SERVERS        (1 << 6)
152 #define ARES_OPT_DOMAINS        (1 << 7)
153 #define ARES_OPT_LOOKUPS        (1 << 8)
154 #define ARES_OPT_SOCK_STATE_CB  (1 << 9)
155 #define ARES_OPT_SORTLIST       (1 << 10)
156 #define ARES_OPT_SOCK_SNDBUF    (1 << 11)
157 #define ARES_OPT_SOCK_RCVBUF    (1 << 12)
158 #define ARES_OPT_TIMEOUTMS      (1 << 13)
159 #define ARES_OPT_ROTATE         (1 << 14)
160 #define ARES_OPT_EDNSPSZ        (1 << 15)
161 #define ARES_OPT_NOROTATE       (1 << 16)
162
163 /* Nameinfo flag values */
164 #define ARES_NI_NOFQDN                  (1 << 0)
165 #define ARES_NI_NUMERICHOST             (1 << 1)
166 #define ARES_NI_NAMEREQD                (1 << 2)
167 #define ARES_NI_NUMERICSERV             (1 << 3)
168 #define ARES_NI_DGRAM                   (1 << 4)
169 #define ARES_NI_TCP                     0
170 #define ARES_NI_UDP                     ARES_NI_DGRAM
171 #define ARES_NI_SCTP                    (1 << 5)
172 #define ARES_NI_DCCP                    (1 << 6)
173 #define ARES_NI_NUMERICSCOPE            (1 << 7)
174 #define ARES_NI_LOOKUPHOST              (1 << 8)
175 #define ARES_NI_LOOKUPSERVICE           (1 << 9)
176 /* Reserved for future use */
177 #define ARES_NI_IDN                     (1 << 10)
178 #define ARES_NI_IDN_ALLOW_UNASSIGNED    (1 << 11)
179 #define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
180
181 /* Addrinfo flag values */
182 #define ARES_AI_CANONNAME               (1 << 0)
183 #define ARES_AI_NUMERICHOST             (1 << 1)
184 #define ARES_AI_PASSIVE                 (1 << 2)
185 #define ARES_AI_NUMERICSERV             (1 << 3)
186 #define ARES_AI_V4MAPPED                (1 << 4)
187 #define ARES_AI_ALL                     (1 << 5)
188 #define ARES_AI_ADDRCONFIG              (1 << 6)
189 /* Reserved for future use */
190 #define ARES_AI_IDN                     (1 << 10)
191 #define ARES_AI_IDN_ALLOW_UNASSIGNED    (1 << 11)
192 #define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
193 #define ARES_AI_CANONIDN                (1 << 13)
194
195 #define ARES_AI_MASK (ARES_AI_CANONNAME|ARES_AI_NUMERICHOST|ARES_AI_PASSIVE| \
196                       ARES_AI_NUMERICSERV|ARES_AI_V4MAPPED|ARES_AI_ALL| \
197                       ARES_AI_ADDRCONFIG)
198 #define ARES_GETSOCK_MAXNUM 16 /* ares_getsock() can return info about this
199                                   many sockets */
200 #define ARES_GETSOCK_READABLE(bits,num) (bits & (1<< (num)))
201 #define ARES_GETSOCK_WRITABLE(bits,num) (bits & (1 << ((num) + \
202                                          ARES_GETSOCK_MAXNUM)))
203
204 /* c-ares library initialization flag values */
205 #define ARES_LIB_INIT_NONE   (0)
206 #define ARES_LIB_INIT_WIN32  (1 << 0)
207 #define ARES_LIB_INIT_ALL    (ARES_LIB_INIT_WIN32)
208
209
210 /*
211  * Typedef our socket type
212  */
213
214 #ifndef ares_socket_typedef
215 #ifdef WIN32
216 typedef SOCKET ares_socket_t;
217 #define ARES_SOCKET_BAD INVALID_SOCKET
218 #else
219 typedef int ares_socket_t;
220 #define ARES_SOCKET_BAD -1
221 #endif
222 #define ares_socket_typedef
223 #endif /* ares_socket_typedef */
224
225 typedef void (*ares_sock_state_cb)(void *data,
226                                    ares_socket_t socket_fd,
227                                    int readable,
228                                    int writable);
229
230 struct apattern;
231
232 /* NOTE about the ares_options struct to users and developers.
233
234    This struct will remain looking like this. It will not be extended nor
235    shrunk in future releases, but all new options will be set by ares_set_*()
236    options instead of with the ares_init_options() function.
237
238    Eventually (in a galaxy far far away), all options will be settable by
239    ares_set_*() options and the ares_init_options() function will become
240    deprecated.
241
242    When new options are added to c-ares, they are not added to this
243    struct. And they are not "saved" with the ares_save_options() function but
244    instead we encourage the use of the ares_dup() function. Needless to say,
245    if you add config options to c-ares you need to make sure ares_dup()
246    duplicates this new option.
247
248  */
249 struct ares_options {
250   int flags;
251   int timeout; /* in seconds or milliseconds, depending on options */
252   int tries;
253   int ndots;
254   unsigned short udp_port;
255   unsigned short tcp_port;
256   int socket_send_buffer_size;
257   int socket_receive_buffer_size;
258   struct in_addr *servers;
259   int nservers;
260   char **domains;
261   int ndomains;
262   char *lookups;
263   ares_sock_state_cb sock_state_cb;
264   void *sock_state_cb_data;
265   struct apattern *sortlist;
266   int nsort;
267   int ednspsz;
268 };
269
270 struct hostent;
271 struct timeval;
272 struct sockaddr;
273 struct ares_channeldata;
274
275 typedef struct ares_channeldata *ares_channel;
276
277 typedef void (*ares_callback)(void *arg,
278                               int status,
279                               int timeouts,
280                               unsigned char *abuf,
281                               int alen);
282
283 typedef void (*ares_host_callback)(void *arg,
284                                    int status,
285                                    int timeouts,
286                                    struct hostent *hostent);
287
288 typedef void (*ares_nameinfo_callback)(void *arg,
289                                        int status,
290                                        int timeouts,
291                                        char *node,
292                                        char *service);
293
294 typedef int  (*ares_sock_create_callback)(ares_socket_t socket_fd,
295                                           int type,
296                                           void *data);
297
298 typedef int  (*ares_sock_config_callback)(ares_socket_t socket_fd,
299                                           int type,
300                                           void *data);
301
302 CARES_EXTERN int ares_library_init(int flags);
303
304 CARES_EXTERN int ares_library_init_mem(int flags,
305                                        void *(*amalloc)(size_t size),
306                                        void (*afree)(void *ptr),
307                                        void *(*arealloc)(void *ptr, size_t size));
308
309 CARES_EXTERN int ares_library_initialized(void);
310
311 CARES_EXTERN void ares_library_cleanup(void);
312
313 CARES_EXTERN const char *ares_version(int *version);
314
315 CARES_EXTERN int ares_init(ares_channel *channelptr);
316
317 CARES_EXTERN int ares_init_options(ares_channel *channelptr,
318                                    struct ares_options *options,
319                                    int optmask);
320
321 CARES_EXTERN int ares_save_options(ares_channel channel,
322                                    struct ares_options *options,
323                                    int *optmask);
324
325 CARES_EXTERN void ares_destroy_options(struct ares_options *options);
326
327 CARES_EXTERN int ares_dup(ares_channel *dest,
328                           ares_channel src);
329
330 CARES_EXTERN void ares_destroy(ares_channel channel);
331
332 CARES_EXTERN void ares_cancel(ares_channel channel);
333
334 /* These next 3 configure local binding for the out-going socket
335  * connection.  Use these to specify source IP and/or network device
336  * on multi-homed systems.
337  */
338 CARES_EXTERN void ares_set_local_ip4(ares_channel channel, unsigned int local_ip);
339
340 /* local_ip6 should be 16 bytes in length */
341 CARES_EXTERN void ares_set_local_ip6(ares_channel channel,
342                                      const unsigned char* local_ip6);
343
344 /* local_dev_name should be null terminated. */
345 CARES_EXTERN void ares_set_local_dev(ares_channel channel,
346                                      const char* local_dev_name);
347
348 CARES_EXTERN void ares_set_socket_callback(ares_channel channel,
349                                            ares_sock_create_callback callback,
350                                            void *user_data);
351
352 CARES_EXTERN void ares_set_socket_configure_callback(ares_channel channel,
353                                                      ares_sock_config_callback callback,
354                                                      void *user_data);
355
356 CARES_EXTERN int ares_set_sortlist(ares_channel channel,
357                                    const char *sortstr);
358
359 CARES_EXTERN void ares_send(ares_channel channel,
360                             const unsigned char *qbuf,
361                             int qlen,
362                             ares_callback callback,
363                             void *arg);
364
365 CARES_EXTERN void ares_query(ares_channel channel,
366                              const char *name,
367                              int dnsclass,
368                              int type,
369                              ares_callback callback,
370                              void *arg);
371
372 CARES_EXTERN void ares_search(ares_channel channel,
373                               const char *name,
374                               int dnsclass,
375                               int type,
376                               ares_callback callback,
377                               void *arg);
378
379 CARES_EXTERN void ares_gethostbyname(ares_channel channel,
380                                      const char *name,
381                                      int family,
382                                      ares_host_callback callback,
383                                      void *arg);
384
385 CARES_EXTERN int ares_gethostbyname_file(ares_channel channel,
386                                          const char *name,
387                                          int family,
388                                          struct hostent **host);
389
390 CARES_EXTERN void ares_gethostbyaddr(ares_channel channel,
391                                      const void *addr,
392                                      int addrlen,
393                                      int family,
394                                      ares_host_callback callback,
395                                      void *arg);
396
397 CARES_EXTERN void ares_getnameinfo(ares_channel channel,
398                                    const struct sockaddr *sa,
399                                    ares_socklen_t salen,
400                                    int flags,
401                                    ares_nameinfo_callback callback,
402                                    void *arg);
403
404 CARES_EXTERN int ares_fds(ares_channel channel,
405                           fd_set *read_fds,
406                           fd_set *write_fds);
407
408 CARES_EXTERN int ares_getsock(ares_channel channel,
409                               ares_socket_t *socks,
410                               int numsocks);
411
412 CARES_EXTERN struct timeval *ares_timeout(ares_channel channel,
413                                           struct timeval *maxtv,
414                                           struct timeval *tv);
415
416 CARES_EXTERN void ares_process(ares_channel channel,
417                                fd_set *read_fds,
418                                fd_set *write_fds);
419
420 CARES_EXTERN void ares_process_fd(ares_channel channel,
421                                   ares_socket_t read_fd,
422                                   ares_socket_t write_fd);
423
424 CARES_EXTERN int ares_create_query(const char *name,
425                                    int dnsclass,
426                                    int type,
427                                    unsigned short id,
428                                    int rd,
429                                    unsigned char **buf,
430                                    int *buflen,
431                                    int max_udp_size);
432
433 CARES_EXTERN int ares_mkquery(const char *name,
434                               int dnsclass,
435                               int type,
436                               unsigned short id,
437                               int rd,
438                               unsigned char **buf,
439                               int *buflen);
440
441 CARES_EXTERN int ares_expand_name(const unsigned char *encoded,
442                                   const unsigned char *abuf,
443                                   int alen,
444                                   char **s,
445                                   long *enclen);
446
447 CARES_EXTERN int ares_expand_string(const unsigned char *encoded,
448                                     const unsigned char *abuf,
449                                     int alen,
450                                     unsigned char **s,
451                                     long *enclen);
452
453 /*
454  * NOTE: before c-ares 1.7.0 we would most often use the system in6_addr
455  * struct below when ares itself was built, but many apps would use this
456  * private version since the header checked a HAVE_* define for it. Starting
457  * with 1.7.0 we always declare and use our own to stop relying on the
458  * system's one.
459  */
460 struct ares_in6_addr {
461   union {
462     unsigned char _S6_u8[16];
463   } _S6_un;
464 };
465
466 struct ares_addrttl {
467   struct in_addr ipaddr;
468   int            ttl;
469 };
470
471 struct ares_addr6ttl {
472   struct ares_in6_addr ip6addr;
473   int             ttl;
474 };
475
476 struct ares_srv_reply {
477   struct ares_srv_reply  *next;
478   char                   *host;
479   unsigned short          priority;
480   unsigned short          weight;
481   unsigned short          port;
482 };
483
484 struct ares_mx_reply {
485   struct ares_mx_reply   *next;
486   char                   *host;
487   unsigned short          priority;
488 };
489
490 struct ares_txt_reply {
491   struct ares_txt_reply  *next;
492   unsigned char          *txt;
493   size_t                  length;  /* length excludes null termination */
494 };
495
496 /* NOTE: This structure is a superset of ares_txt_reply
497  */
498 struct ares_txt_ext {
499   struct ares_txt_ext      *next;
500   unsigned char            *txt;
501   size_t                   length;
502   /* 1 - if start of new record
503    * 0 - if a chunk in the same record */
504   unsigned char            record_start;
505 };
506
507 struct ares_naptr_reply {
508   struct ares_naptr_reply *next;
509   unsigned char           *flags;
510   unsigned char           *service;
511   unsigned char           *regexp;
512   char                    *replacement;
513   unsigned short           order;
514   unsigned short           preference;
515 };
516
517 struct ares_soa_reply {
518   char        *nsname;
519   char        *hostmaster;
520   unsigned int serial;
521   unsigned int refresh;
522   unsigned int retry;
523   unsigned int expire;
524   unsigned int minttl;
525 };
526
527 /*
528 ** Parse the buffer, starting at *abuf and of length alen bytes, previously
529 ** obtained from an ares_search call.  Put the results in *host, if nonnull.
530 ** Also, if addrttls is nonnull, put up to *naddrttls IPv4 addresses along with
531 ** their TTLs in that array, and set *naddrttls to the number of addresses
532 ** so written.
533 */
534
535 CARES_EXTERN int ares_parse_a_reply(const unsigned char *abuf,
536                                     int alen,
537                                     struct hostent **host,
538                                     struct ares_addrttl *addrttls,
539                                     int *naddrttls);
540
541 CARES_EXTERN int ares_parse_aaaa_reply(const unsigned char *abuf,
542                                        int alen,
543                                        struct hostent **host,
544                                        struct ares_addr6ttl *addrttls,
545                                        int *naddrttls);
546
547 CARES_EXTERN int ares_parse_ptr_reply(const unsigned char *abuf,
548                                       int alen,
549                                       const void *addr,
550                                       int addrlen,
551                                       int family,
552                                       struct hostent **host);
553
554 CARES_EXTERN int ares_parse_ns_reply(const unsigned char *abuf,
555                                      int alen,
556                                      struct hostent **host);
557
558 CARES_EXTERN int ares_parse_srv_reply(const unsigned char* abuf,
559                                       int alen,
560                                       struct ares_srv_reply** srv_out);
561
562 CARES_EXTERN int ares_parse_mx_reply(const unsigned char* abuf,
563                                       int alen,
564                                       struct ares_mx_reply** mx_out);
565
566 CARES_EXTERN int ares_parse_txt_reply(const unsigned char* abuf,
567                                       int alen,
568                                       struct ares_txt_reply** txt_out);
569
570 CARES_EXTERN int ares_parse_txt_reply_ext(const unsigned char* abuf,
571                                           int alen,
572                                           struct ares_txt_ext** txt_out);
573
574 CARES_EXTERN int ares_parse_naptr_reply(const unsigned char* abuf,
575                                         int alen,
576                                         struct ares_naptr_reply** naptr_out);
577
578 CARES_EXTERN int ares_parse_soa_reply(const unsigned char* abuf,
579                                       int alen,
580                                       struct ares_soa_reply** soa_out);
581
582 CARES_EXTERN void ares_free_string(void *str);
583
584 CARES_EXTERN void ares_free_hostent(struct hostent *host);
585
586 CARES_EXTERN void ares_free_data(void *dataptr);
587
588 CARES_EXTERN const char *ares_strerror(int code);
589
590 struct ares_addr_node {
591   struct ares_addr_node *next;
592   int family;
593   union {
594     struct in_addr       addr4;
595     struct ares_in6_addr addr6;
596   } addr;
597 };
598
599 struct ares_addr_port_node {
600   struct ares_addr_port_node *next;
601   int family;
602   union {
603     struct in_addr       addr4;
604     struct ares_in6_addr addr6;
605   } addr;
606   int udp_port;
607   int tcp_port;
608 };
609
610 CARES_EXTERN int ares_set_servers(ares_channel channel,
611                                   struct ares_addr_node *servers);
612 CARES_EXTERN int ares_set_servers_ports(ares_channel channel,
613                                         struct ares_addr_port_node *servers);
614
615 /* Incomming string format: host[:port][,host[:port]]... */
616 CARES_EXTERN int ares_set_servers_csv(ares_channel channel,
617                                       const char* servers);
618 CARES_EXTERN int ares_set_servers_ports_csv(ares_channel channel,
619                                             const char* servers);
620
621 CARES_EXTERN int ares_get_servers(ares_channel channel,
622                                   struct ares_addr_node **servers);
623 CARES_EXTERN int ares_get_servers_ports(ares_channel channel,
624                                         struct ares_addr_port_node **servers);
625
626 CARES_EXTERN const char *ares_inet_ntop(int af, const void *src, char *dst,
627                                         ares_socklen_t size);
628
629 CARES_EXTERN int ares_inet_pton(int af, const char *src, void *dst);
630
631
632 #ifdef  __cplusplus
633 }
634 #endif
635
636 #endif /* ARES__H */