ares.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* $Id$ */
  2. /* Copyright 1998 by the Massachusetts Institute of Technology.
  3. * Copyright (C) 2007-2009 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. #ifndef ARES__H
  18. #define ARES__H
  19. #include "ares_build.h" /* c-ares build definitions */
  20. #include "ares_rules.h" /* c-ares rules enforcement */
  21. /*
  22. * Define WIN32 when build target is Win32 API
  23. */
  24. #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  25. #define WIN32
  26. #endif
  27. #include <sys/types.h>
  28. /* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
  29. libc5-based Linux systems. Only include it on system that are known to
  30. require it! */
  31. #if defined(_AIX) || defined(__NOVELL_LIBC__) || defined(__NetBSD__) || \
  32. defined(__minix) || defined(__SYMBIAN32__) || defined(__INTEGRITY)
  33. #include <sys/select.h>
  34. #endif
  35. #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
  36. #include <sys/bsdskt.h>
  37. #endif
  38. #if defined(WATT32)
  39. #include <netinet/in.h>
  40. #include <sys/socket.h>
  41. #include <tcp.h>
  42. #elif defined(WIN32)
  43. # ifndef WIN32_LEAN_AND_MEAN
  44. # define WIN32_LEAN_AND_MEAN
  45. # endif
  46. # include <windows.h>
  47. # include <winsock2.h>
  48. # include <ws2tcpip.h>
  49. #else
  50. #include <sys/socket.h>
  51. #include <netinet/in.h>
  52. #endif
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. #define ARES_SUCCESS 0
  57. /* Server error codes (ARES_ENODATA indicates no relevant answer) */
  58. #define ARES_ENODATA 1
  59. #define ARES_EFORMERR 2
  60. #define ARES_ESERVFAIL 3
  61. #define ARES_ENOTFOUND 4
  62. #define ARES_ENOTIMP 5
  63. #define ARES_EREFUSED 6
  64. /* Locally generated error codes */
  65. #define ARES_EBADQUERY 7
  66. #define ARES_EBADNAME 8
  67. #define ARES_EBADFAMILY 9
  68. #define ARES_EBADRESP 10
  69. #define ARES_ECONNREFUSED 11
  70. #define ARES_ETIMEOUT 12
  71. #define ARES_EOF 13
  72. #define ARES_EFILE 14
  73. #define ARES_ENOMEM 15
  74. #define ARES_EDESTRUCTION 16
  75. #define ARES_EBADSTR 17
  76. /* ares_getnameinfo error codes */
  77. #define ARES_EBADFLAGS 18
  78. /* ares_getaddrinfo error codes */
  79. #define ARES_ENONAME 19
  80. #define ARES_EBADHINTS 20
  81. /* Flag values */
  82. #define ARES_FLAG_USEVC (1 << 0)
  83. #define ARES_FLAG_PRIMARY (1 << 1)
  84. #define ARES_FLAG_IGNTC (1 << 2)
  85. #define ARES_FLAG_NORECURSE (1 << 3)
  86. #define ARES_FLAG_STAYOPEN (1 << 4)
  87. #define ARES_FLAG_NOSEARCH (1 << 5)
  88. #define ARES_FLAG_NOALIASES (1 << 6)
  89. #define ARES_FLAG_NOCHECKRESP (1 << 7)
  90. /* Option mask values */
  91. #define ARES_OPT_FLAGS (1 << 0)
  92. #define ARES_OPT_TIMEOUT (1 << 1)
  93. #define ARES_OPT_TRIES (1 << 2)
  94. #define ARES_OPT_NDOTS (1 << 3)
  95. #define ARES_OPT_UDP_PORT (1 << 4)
  96. #define ARES_OPT_TCP_PORT (1 << 5)
  97. #define ARES_OPT_SERVERS (1 << 6)
  98. #define ARES_OPT_DOMAINS (1 << 7)
  99. #define ARES_OPT_LOOKUPS (1 << 8)
  100. #define ARES_OPT_SOCK_STATE_CB (1 << 9)
  101. #define ARES_OPT_SORTLIST (1 << 10)
  102. #define ARES_OPT_SOCK_SNDBUF (1 << 11)
  103. #define ARES_OPT_SOCK_RCVBUF (1 << 12)
  104. #define ARES_OPT_TIMEOUTMS (1 << 13)
  105. #define ARES_OPT_ROTATE (1 << 14)
  106. /* Nameinfo flag values */
  107. #define ARES_NI_NOFQDN (1 << 0)
  108. #define ARES_NI_NUMERICHOST (1 << 1)
  109. #define ARES_NI_NAMEREQD (1 << 2)
  110. #define ARES_NI_NUMERICSERV (1 << 3)
  111. #define ARES_NI_DGRAM (1 << 4)
  112. #define ARES_NI_TCP 0
  113. #define ARES_NI_UDP ARES_NI_DGRAM
  114. #define ARES_NI_SCTP (1 << 5)
  115. #define ARES_NI_DCCP (1 << 6)
  116. #define ARES_NI_NUMERICSCOPE (1 << 7)
  117. #define ARES_NI_LOOKUPHOST (1 << 8)
  118. #define ARES_NI_LOOKUPSERVICE (1 << 9)
  119. /* Reserved for future use */
  120. #define ARES_NI_IDN (1 << 10)
  121. #define ARES_NI_IDN_ALLOW_UNASSIGNED (1 << 11)
  122. #define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
  123. /* Addrinfo flag values */
  124. #define ARES_AI_CANONNAME (1 << 0)
  125. #define ARES_AI_NUMERICHOST (1 << 1)
  126. #define ARES_AI_PASSIVE (1 << 2)
  127. #define ARES_AI_NUMERICSERV (1 << 3)
  128. #define ARES_AI_V4MAPPED (1 << 4)
  129. #define ARES_AI_ALL (1 << 5)
  130. #define ARES_AI_ADDRCONFIG (1 << 6)
  131. /* Reserved for future use */
  132. #define ARES_AI_IDN (1 << 10)
  133. #define ARES_AI_IDN_ALLOW_UNASSIGNED (1 << 11)
  134. #define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
  135. #define ARES_AI_CANONIDN (1 << 13)
  136. #define ARES_AI_MASK (ARES_AI_CANONNAME|ARES_AI_NUMERICHOST|ARES_AI_PASSIVE| \
  137. ARES_AI_NUMERICSERV|ARES_AI_V4MAPPED|ARES_AI_ALL| \
  138. ARES_AI_ADDRCONFIG)
  139. #define ARES_GETSOCK_MAXNUM 16 /* ares_getsock() can return info about this
  140. many sockets */
  141. #define ARES_GETSOCK_READABLE(bits,num) (bits & (1<< (num)))
  142. #define ARES_GETSOCK_WRITABLE(bits,num) (bits & (1 << ((num) + \
  143. ARES_GETSOCK_MAXNUM)))
  144. /*
  145. * Typedef our socket type
  146. */
  147. #ifndef ares_socket_typedef
  148. #ifdef WIN32
  149. typedef SOCKET ares_socket_t;
  150. #define ARES_SOCKET_BAD INVALID_SOCKET
  151. #else
  152. typedef int ares_socket_t;
  153. #define ARES_SOCKET_BAD -1
  154. #endif
  155. #define ares_socket_typedef
  156. #endif /* ares_socket_typedef */
  157. typedef void (*ares_sock_state_cb)(void *data,
  158. ares_socket_t socket_fd,
  159. int readable,
  160. int writable);
  161. struct apattern;
  162. /* NOTE about the ares_options struct to users and developers.
  163. This struct will remain looking like this. It will not be extended nor
  164. shrunk in future releases, but all new options will be set by ares_set_*()
  165. options instead of with the ares_init_options() function.
  166. Eventually (in a galaxy far far away), all options will be settable by
  167. ares_set_*() options and the ares_init_options() function will become
  168. deprecated.
  169. When new options are added to c-ares, they are not added to this
  170. struct. And they are not "saved" with the ares_save_options() function but
  171. instead we encourage the use of the ares_dup() function. Needless to say,
  172. if you add config options to c-ares you need to make sure ares_dup()
  173. duplicates this new option.
  174. */
  175. struct ares_options {
  176. int flags;
  177. int timeout; /* in seconds or milliseconds, depending on options */
  178. int tries;
  179. int ndots;
  180. unsigned short udp_port;
  181. unsigned short tcp_port;
  182. int socket_send_buffer_size;
  183. int socket_receive_buffer_size;
  184. struct in_addr *servers;
  185. int nservers;
  186. char **domains;
  187. int ndomains;
  188. char *lookups;
  189. ares_sock_state_cb sock_state_cb;
  190. void *sock_state_cb_data;
  191. struct apattern *sortlist;
  192. int nsort;
  193. };
  194. struct hostent;
  195. struct timeval;
  196. struct sockaddr;
  197. struct ares_channeldata;
  198. typedef struct ares_channeldata *ares_channel;
  199. typedef void (*ares_callback)(void *arg, int status, int timeouts,
  200. unsigned char *abuf, int alen);
  201. typedef void (*ares_host_callback)(void *arg, int status, int timeouts,
  202. struct hostent *hostent);
  203. typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
  204. char *node, char *service);
  205. typedef int (*ares_sock_create_callback)(ares_socket_t socket_fd,
  206. int type, void *data);
  207. int ares_init(ares_channel *channelptr);
  208. int ares_init_options(ares_channel *channelptr, struct ares_options *options,
  209. int optmask);
  210. int ares_save_options(ares_channel channel, struct ares_options *options,
  211. int *optmask);
  212. void ares_destroy_options(struct ares_options *options);
  213. int ares_dup(ares_channel *dest, ares_channel src);
  214. void ares_destroy(ares_channel channel);
  215. void ares_cancel(ares_channel channel);
  216. void ares_set_socket_callback(ares_channel channel,
  217. ares_sock_create_callback callback,
  218. void *user_data);
  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. int ares_gethostbyname_file(ares_channel channel, const char *name,
  228. int family, struct hostent **host);
  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. ares_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. int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
  243. int rd, unsigned char **buf, int *buflen);
  244. int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
  245. int alen, char **s, long *enclen);
  246. int ares_expand_string(const unsigned char *encoded, const unsigned char *abuf,
  247. int alen, unsigned char **s, long *enclen);
  248. /*
  249. * NOTE: before c-ares 1.6.1 we would most often use the system in6_addr
  250. * struct below when ares itself was built, but many apps would use this
  251. * private version since the header checked a HAVE_* define for it. Starting
  252. * with 1.6.1 we always declare and use our own to stop relying on the
  253. * system's one.
  254. */
  255. struct ares_in6_addr {
  256. union {
  257. unsigned char _S6_u8[16];
  258. } _S6_un;
  259. };
  260. /*
  261. * TODO: the structs 'addrttl' and 'addr6ttl' really should get their names
  262. * prefixed with ares_ to keep them in our own "name space".
  263. */
  264. struct addrttl {
  265. struct in_addr ipaddr;
  266. int ttl;
  267. };
  268. struct addr6ttl {
  269. struct ares_in6_addr ip6addr;
  270. int ttl;
  271. };
  272. /*
  273. ** Parse the buffer, starting at *abuf and of length alen bytes, previously
  274. ** obtained from an ares_search call. Put the results in *host, if nonnull.
  275. ** Also, if addrttls is nonnull, put up to *naddrttls IPv4 addresses along with
  276. ** their TTLs in that array, and set *naddrttls to the number of addresses
  277. ** so written.
  278. */
  279. int ares_parse_a_reply(const unsigned char *abuf, int alen,
  280. struct hostent **host,
  281. struct addrttl *addrttls, int *naddrttls);
  282. int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
  283. struct hostent **host,
  284. struct addr6ttl *addrttls, int *naddrttls);
  285. int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
  286. int addrlen, int family, struct hostent **host);
  287. int ares_parse_ns_reply(const unsigned char *abuf, int alen,
  288. struct hostent **host);
  289. void ares_free_string(void *str);
  290. void ares_free_hostent(struct hostent *host);
  291. const char *ares_strerror(int code);
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif /* ARES__H */