ares.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* MIT License
  2. *
  3. * Copyright (c) Massachusetts Institute of Technology
  4. * Copyright (c) Daniel Stenberg
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * SPDX-License-Identifier: MIT
  26. */
  27. #ifndef ARES__H
  28. #define ARES__H
  29. #include "ares_version.h" /* c-ares version defines */
  30. #include "ares_build.h" /* c-ares build definitions */
  31. #if defined(_WIN32)
  32. # ifndef WIN32_LEAN_AND_MEAN
  33. # define WIN32_LEAN_AND_MEAN
  34. # endif
  35. #endif
  36. #ifdef CARES_HAVE_SYS_TYPES_H
  37. # include <sys/types.h>
  38. #endif
  39. #ifdef CARES_HAVE_SYS_SOCKET_H
  40. # include <sys/socket.h>
  41. #endif
  42. #ifdef CARES_HAVE_SYS_SELECT_H
  43. # include <sys/select.h>
  44. #endif
  45. #ifdef CARES_HAVE_WINSOCK2_H
  46. # include <winsock2.h>
  47. /* To aid with linking against a static c-ares build, lets tell the microsoft
  48. * compiler to pull in needed dependencies */
  49. # ifdef _MSC_VER
  50. # pragma comment(lib, "ws2_32")
  51. # pragma comment(lib, "advapi32")
  52. # pragma comment(lib, "iphlpapi")
  53. # endif
  54. #endif
  55. #ifdef CARES_HAVE_WS2TCPIP_H
  56. # include <ws2tcpip.h>
  57. #endif
  58. #ifdef CARES_HAVE_WINDOWS_H
  59. # include <windows.h>
  60. #endif
  61. /* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
  62. libc5-based Linux systems. Only include it on system that are known to
  63. require it! */
  64. #if defined(_AIX) || defined(__NOVELL_LIBC__) || defined(__NetBSD__) || \
  65. defined(__minix) || defined(__SYMBIAN32__) || defined(__INTEGRITY) || \
  66. defined(ANDROID) || defined(__ANDROID__) || defined(__OpenBSD__) || \
  67. defined(__QNXNTO__) || defined(__MVS__) || defined(__HAIKU__)
  68. # include <sys/select.h>
  69. #endif
  70. #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
  71. # include <sys/bsdskt.h>
  72. #endif
  73. #if !defined(_WIN32)
  74. # include <netinet/in.h>
  75. #endif
  76. #ifdef WATT32
  77. # include <tcp.h>
  78. #endif
  79. #if defined(ANDROID) || defined(__ANDROID__)
  80. # include <jni.h>
  81. #endif
  82. typedef CARES_TYPEOF_ARES_SOCKLEN_T ares_socklen_t;
  83. typedef CARES_TYPEOF_ARES_SSIZE_T ares_ssize_t;
  84. #ifdef __cplusplus
  85. extern "C" {
  86. #endif
  87. /*
  88. ** c-ares external API function linkage decorations.
  89. */
  90. #if defined(_WIN32) || defined(__CYGWIN__) || defined(__SYMBIAN32__)
  91. # ifdef CARES_STATICLIB
  92. # define CARES_EXTERN
  93. # else
  94. # ifdef CARES_BUILDING_LIBRARY
  95. # define CARES_EXTERN __declspec(dllexport)
  96. # else
  97. # define CARES_EXTERN __declspec(dllimport)
  98. # endif
  99. # endif
  100. #else
  101. # if defined(__GNUC__) && __GNUC__ >= 4
  102. # define CARES_EXTERN __attribute__((visibility("default")))
  103. # elif defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 900
  104. # define CARES_EXTERN __attribute__((visibility("default")))
  105. # elif defined(__SUNPRO_C)
  106. # define CARES_EXTERN _global
  107. # else
  108. # define CARES_EXTERN
  109. # endif
  110. #endif
  111. #ifdef __GNUC__
  112. # define CARES_GCC_VERSION \
  113. (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
  114. #else
  115. # define CARES_GCC_VERSION 0
  116. #endif
  117. #ifndef __has_attribute
  118. # define __has_attribute(x) 0
  119. #endif
  120. #if 1
  121. # define CARES_DEPRECATED
  122. # define CARES_DEPRECATED_FOR(f)
  123. #else
  124. # if CARES_GCC_VERSION >= 30200 || __has_attribute(__deprecated__)
  125. # define CARES_DEPRECATED __attribute__((__deprecated__))
  126. # else
  127. # define CARES_DEPRECATED
  128. # endif
  129. # if CARES_GCC_VERSION >= 40500 || defined(__clang__)
  130. # define CARES_DEPRECATED_FOR(f) \
  131. __attribute__((deprecated("Use " #f " instead")))
  132. # elif defined(_MSC_VER)
  133. # define CARES_DEPRECATED_FOR(f) __declspec(deprecated("Use " #f " instead"))
  134. # else
  135. # define CARES_DEPRECATED_FOR(f) CARES_DEPRECATED
  136. # endif
  137. #endif
  138. typedef enum {
  139. ARES_SUCCESS = 0,
  140. /* Server error codes (ARES_ENODATA indicates no relevant answer) */
  141. ARES_ENODATA = 1,
  142. ARES_EFORMERR = 2,
  143. ARES_ESERVFAIL = 3,
  144. ARES_ENOTFOUND = 4,
  145. ARES_ENOTIMP = 5,
  146. ARES_EREFUSED = 6,
  147. /* Locally generated error codes */
  148. ARES_EBADQUERY = 7,
  149. ARES_EBADNAME = 8,
  150. ARES_EBADFAMILY = 9,
  151. ARES_EBADRESP = 10,
  152. ARES_ECONNREFUSED = 11,
  153. ARES_ETIMEOUT = 12,
  154. ARES_EOF = 13,
  155. ARES_EFILE = 14,
  156. ARES_ENOMEM = 15,
  157. ARES_EDESTRUCTION = 16,
  158. ARES_EBADSTR = 17,
  159. /* ares_getnameinfo error codes */
  160. ARES_EBADFLAGS = 18,
  161. /* ares_getaddrinfo error codes */
  162. ARES_ENONAME = 19,
  163. ARES_EBADHINTS = 20,
  164. /* Uninitialized library error code */
  165. ARES_ENOTINITIALIZED = 21, /* introduced in 1.7.0 */
  166. /* ares_library_init error codes */
  167. ARES_ELOADIPHLPAPI = 22, /* introduced in 1.7.0 */
  168. ARES_EADDRGETNETWORKPARAMS = 23, /* introduced in 1.7.0 */
  169. /* More error codes */
  170. ARES_ECANCELLED = 24, /* introduced in 1.7.0 */
  171. /* More ares_getaddrinfo error codes */
  172. ARES_ESERVICE = 25, /* ares_getaddrinfo() was passed a text service name that
  173. * is not recognized. introduced in 1.16.0 */
  174. ARES_ENOSERVER = 26 /* No DNS servers were configured */
  175. } ares_status_t;
  176. typedef enum {
  177. ARES_FALSE = 0,
  178. ARES_TRUE = 1
  179. } ares_bool_t;
  180. /*! Values for ARES_OPT_EVENT_THREAD */
  181. typedef enum {
  182. /*! Default (best choice) event system */
  183. ARES_EVSYS_DEFAULT = 0,
  184. /*! Win32 IOCP/AFD_POLL event system */
  185. ARES_EVSYS_WIN32 = 1,
  186. /*! Linux epoll */
  187. ARES_EVSYS_EPOLL = 2,
  188. /*! BSD/MacOS kqueue */
  189. ARES_EVSYS_KQUEUE = 3,
  190. /*! POSIX poll() */
  191. ARES_EVSYS_POLL = 4,
  192. /*! last fallback on Unix-like systems, select() */
  193. ARES_EVSYS_SELECT = 5
  194. } ares_evsys_t;
  195. /* Flag values */
  196. #define ARES_FLAG_USEVC (1 << 0)
  197. #define ARES_FLAG_PRIMARY (1 << 1)
  198. #define ARES_FLAG_IGNTC (1 << 2)
  199. #define ARES_FLAG_NORECURSE (1 << 3)
  200. #define ARES_FLAG_STAYOPEN (1 << 4)
  201. #define ARES_FLAG_NOSEARCH (1 << 5)
  202. #define ARES_FLAG_NOALIASES (1 << 6)
  203. #define ARES_FLAG_NOCHECKRESP (1 << 7)
  204. #define ARES_FLAG_EDNS (1 << 8)
  205. #define ARES_FLAG_NO_DFLT_SVR (1 << 9)
  206. #define ARES_FLAG_DNS0x20 (1 << 10)
  207. /* Option mask values */
  208. #define ARES_OPT_FLAGS (1 << 0)
  209. #define ARES_OPT_TIMEOUT (1 << 1)
  210. #define ARES_OPT_TRIES (1 << 2)
  211. #define ARES_OPT_NDOTS (1 << 3)
  212. #define ARES_OPT_UDP_PORT (1 << 4)
  213. #define ARES_OPT_TCP_PORT (1 << 5)
  214. #define ARES_OPT_SERVERS (1 << 6)
  215. #define ARES_OPT_DOMAINS (1 << 7)
  216. #define ARES_OPT_LOOKUPS (1 << 8)
  217. #define ARES_OPT_SOCK_STATE_CB (1 << 9)
  218. #define ARES_OPT_SORTLIST (1 << 10)
  219. #define ARES_OPT_SOCK_SNDBUF (1 << 11)
  220. #define ARES_OPT_SOCK_RCVBUF (1 << 12)
  221. #define ARES_OPT_TIMEOUTMS (1 << 13)
  222. #define ARES_OPT_ROTATE (1 << 14)
  223. #define ARES_OPT_EDNSPSZ (1 << 15)
  224. #define ARES_OPT_NOROTATE (1 << 16)
  225. #define ARES_OPT_RESOLVCONF (1 << 17)
  226. #define ARES_OPT_HOSTS_FILE (1 << 18)
  227. #define ARES_OPT_UDP_MAX_QUERIES (1 << 19)
  228. #define ARES_OPT_MAXTIMEOUTMS (1 << 20)
  229. #define ARES_OPT_QUERY_CACHE (1 << 21)
  230. #define ARES_OPT_EVENT_THREAD (1 << 22)
  231. #define ARES_OPT_SERVER_FAILOVER (1 << 23)
  232. /* Nameinfo flag values */
  233. #define ARES_NI_NOFQDN (1 << 0)
  234. #define ARES_NI_NUMERICHOST (1 << 1)
  235. #define ARES_NI_NAMEREQD (1 << 2)
  236. #define ARES_NI_NUMERICSERV (1 << 3)
  237. #define ARES_NI_DGRAM (1 << 4)
  238. #define ARES_NI_TCP 0
  239. #define ARES_NI_UDP ARES_NI_DGRAM
  240. #define ARES_NI_SCTP (1 << 5)
  241. #define ARES_NI_DCCP (1 << 6)
  242. #define ARES_NI_NUMERICSCOPE (1 << 7)
  243. #define ARES_NI_LOOKUPHOST (1 << 8)
  244. #define ARES_NI_LOOKUPSERVICE (1 << 9)
  245. /* Reserved for future use */
  246. #define ARES_NI_IDN (1 << 10)
  247. #define ARES_NI_IDN_ALLOW_UNASSIGNED (1 << 11)
  248. #define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
  249. /* Addrinfo flag values */
  250. #define ARES_AI_CANONNAME (1 << 0)
  251. #define ARES_AI_NUMERICHOST (1 << 1)
  252. #define ARES_AI_PASSIVE (1 << 2)
  253. #define ARES_AI_NUMERICSERV (1 << 3)
  254. #define ARES_AI_V4MAPPED (1 << 4)
  255. #define ARES_AI_ALL (1 << 5)
  256. #define ARES_AI_ADDRCONFIG (1 << 6)
  257. #define ARES_AI_NOSORT (1 << 7)
  258. #define ARES_AI_ENVHOSTS (1 << 8)
  259. /* Reserved for future use */
  260. #define ARES_AI_IDN (1 << 10)
  261. #define ARES_AI_IDN_ALLOW_UNASSIGNED (1 << 11)
  262. #define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
  263. #define ARES_AI_CANONIDN (1 << 13)
  264. #define ARES_AI_MASK \
  265. (ARES_AI_CANONNAME | ARES_AI_NUMERICHOST | ARES_AI_PASSIVE | \
  266. ARES_AI_NUMERICSERV | ARES_AI_V4MAPPED | ARES_AI_ALL | ARES_AI_ADDRCONFIG)
  267. #define ARES_GETSOCK_MAXNUM \
  268. 16 /* ares_getsock() can return info about this \
  269. many sockets */
  270. #define ARES_GETSOCK_READABLE(bits, num) (bits & (1 << (num)))
  271. #define ARES_GETSOCK_WRITABLE(bits, num) \
  272. (bits & (1 << ((num) + ARES_GETSOCK_MAXNUM)))
  273. /* c-ares library initialization flag values */
  274. #define ARES_LIB_INIT_NONE (0)
  275. #define ARES_LIB_INIT_WIN32 (1 << 0)
  276. #define ARES_LIB_INIT_ALL (ARES_LIB_INIT_WIN32)
  277. /* Server state callback flag values */
  278. #define ARES_SERV_STATE_UDP (1 << 0) /* Query used UDP */
  279. #define ARES_SERV_STATE_TCP (1 << 1) /* Query used TCP */
  280. /*
  281. * Typedef our socket type
  282. */
  283. #ifndef ares_socket_typedef
  284. # if defined(_WIN32) && !defined(WATT32)
  285. typedef SOCKET ares_socket_t;
  286. # define ARES_SOCKET_BAD INVALID_SOCKET
  287. # else
  288. typedef int ares_socket_t;
  289. # define ARES_SOCKET_BAD -1
  290. # endif
  291. # define ares_socket_typedef
  292. #endif /* ares_socket_typedef */
  293. typedef void (*ares_sock_state_cb)(void *data, ares_socket_t socket_fd,
  294. int readable, int writable);
  295. struct apattern;
  296. /* Options controlling server failover behavior.
  297. * The retry chance is the probability (1/N) by which we will retry a failed
  298. * server instead of the best server when selecting a server to send queries
  299. * to.
  300. * The retry delay is the minimum time in milliseconds to wait between doing
  301. * such retries (applied per-server).
  302. */
  303. struct ares_server_failover_options {
  304. unsigned short retry_chance;
  305. size_t retry_delay;
  306. };
  307. /* NOTE about the ares_options struct to users and developers.
  308. This struct will remain looking like this. It will not be extended nor
  309. shrunk in future releases, but all new options will be set by ares_set_*()
  310. options instead of with the ares_init_options() function.
  311. Eventually (in a galaxy far far away), all options will be settable by
  312. ares_set_*() options and the ares_init_options() function will become
  313. deprecated.
  314. When new options are added to c-ares, they are not added to this
  315. struct. And they are not "saved" with the ares_save_options() function but
  316. instead we encourage the use of the ares_dup() function. Needless to say,
  317. if you add config options to c-ares you need to make sure ares_dup()
  318. duplicates this new option.
  319. */
  320. struct ares_options {
  321. int flags;
  322. int timeout; /* in seconds or milliseconds, depending on options */
  323. int tries;
  324. int ndots;
  325. unsigned short udp_port; /* host byte order */
  326. unsigned short tcp_port; /* host byte order */
  327. int socket_send_buffer_size;
  328. int socket_receive_buffer_size;
  329. struct in_addr *servers;
  330. int nservers;
  331. char **domains;
  332. int ndomains;
  333. char *lookups;
  334. ares_sock_state_cb sock_state_cb;
  335. void *sock_state_cb_data;
  336. struct apattern *sortlist;
  337. int nsort;
  338. int ednspsz;
  339. char *resolvconf_path;
  340. char *hosts_path;
  341. int udp_max_queries;
  342. int maxtimeout; /* in milliseconds */
  343. unsigned int qcache_max_ttl; /* Maximum TTL for query cache, 0=disabled */
  344. ares_evsys_t evsys;
  345. struct ares_server_failover_options server_failover_opts;
  346. };
  347. struct hostent;
  348. struct timeval;
  349. struct sockaddr;
  350. struct ares_channeldata;
  351. struct ares_addrinfo;
  352. struct ares_addrinfo_hints;
  353. /* Legacy typedef, don't use, you can't specify "const" */
  354. typedef struct ares_channeldata *ares_channel;
  355. /* Current main channel typedef */
  356. typedef struct ares_channeldata ares_channel_t;
  357. /*
  358. * NOTE: before c-ares 1.7.0 we would most often use the system in6_addr
  359. * struct below when ares itself was built, but many apps would use this
  360. * private version since the header checked a HAVE_* define for it. Starting
  361. * with 1.7.0 we always declare and use our own to stop relying on the
  362. * system's one.
  363. */
  364. struct ares_in6_addr {
  365. union {
  366. unsigned char _S6_u8[16];
  367. } _S6_un;
  368. };
  369. struct ares_addr {
  370. int family;
  371. union {
  372. struct in_addr addr4;
  373. struct ares_in6_addr addr6;
  374. } addr;
  375. };
  376. /* DNS record parser, writer, and helpers */
  377. #include "ares_dns_record.h"
  378. typedef void (*ares_callback)(void *arg, int status, int timeouts,
  379. unsigned char *abuf, int alen);
  380. typedef void (*ares_callback_dnsrec)(void *arg, ares_status_t status,
  381. size_t timeouts,
  382. const ares_dns_record_t *dnsrec);
  383. typedef void (*ares_host_callback)(void *arg, int status, int timeouts,
  384. struct hostent *hostent);
  385. typedef void (*ares_nameinfo_callback)(void *arg, int status, int timeouts,
  386. char *node, char *service);
  387. typedef int (*ares_sock_create_callback)(ares_socket_t socket_fd, int type,
  388. void *data);
  389. typedef int (*ares_sock_config_callback)(ares_socket_t socket_fd, int type,
  390. void *data);
  391. typedef void (*ares_addrinfo_callback)(void *arg, int status, int timeouts,
  392. struct ares_addrinfo *res);
  393. typedef void (*ares_server_state_callback)(const char *server_string,
  394. ares_bool_t success, int flags,
  395. void *data);
  396. CARES_EXTERN int ares_library_init(int flags);
  397. CARES_EXTERN int ares_library_init_mem(int flags, void *(*amalloc)(size_t size),
  398. void (*afree)(void *ptr),
  399. void *(*arealloc)(void *ptr,
  400. size_t size));
  401. #if defined(ANDROID) || defined(__ANDROID__)
  402. CARES_EXTERN void ares_library_init_jvm(JavaVM *jvm);
  403. CARES_EXTERN int ares_library_init_android(jobject connectivity_manager);
  404. CARES_EXTERN int ares_library_android_initialized(void);
  405. #endif
  406. CARES_EXTERN int ares_library_initialized(void);
  407. CARES_EXTERN void ares_library_cleanup(void);
  408. CARES_EXTERN const char *ares_version(int *version);
  409. CARES_EXTERN CARES_DEPRECATED_FOR(ares_init_options) int ares_init(
  410. ares_channel_t **channelptr);
  411. CARES_EXTERN int ares_init_options(ares_channel_t **channelptr,
  412. const struct ares_options *options,
  413. int optmask);
  414. CARES_EXTERN int ares_save_options(const ares_channel_t *channel,
  415. struct ares_options *options, int *optmask);
  416. CARES_EXTERN void ares_destroy_options(struct ares_options *options);
  417. CARES_EXTERN int ares_dup(ares_channel_t **dest, const ares_channel_t *src);
  418. CARES_EXTERN ares_status_t ares_reinit(ares_channel_t *channel);
  419. CARES_EXTERN void ares_destroy(ares_channel_t *channel);
  420. CARES_EXTERN void ares_cancel(ares_channel_t *channel);
  421. /* These next 3 configure local binding for the out-going socket
  422. * connection. Use these to specify source IP and/or network device
  423. * on multi-homed systems.
  424. */
  425. CARES_EXTERN void ares_set_local_ip4(ares_channel_t *channel,
  426. unsigned int local_ip);
  427. /* local_ip6 should be 16 bytes in length */
  428. CARES_EXTERN void ares_set_local_ip6(ares_channel_t *channel,
  429. const unsigned char *local_ip6);
  430. /* local_dev_name should be null terminated. */
  431. CARES_EXTERN void ares_set_local_dev(ares_channel_t *channel,
  432. const char *local_dev_name);
  433. CARES_EXTERN void ares_set_socket_callback(ares_channel_t *channel,
  434. ares_sock_create_callback callback,
  435. void *user_data);
  436. CARES_EXTERN void ares_set_socket_configure_callback(
  437. ares_channel_t *channel, ares_sock_config_callback callback, void *user_data);
  438. CARES_EXTERN void
  439. ares_set_server_state_callback(ares_channel_t *channel,
  440. ares_server_state_callback callback,
  441. void *user_data);
  442. CARES_EXTERN int ares_set_sortlist(ares_channel_t *channel,
  443. const char *sortstr);
  444. CARES_EXTERN void ares_getaddrinfo(ares_channel_t *channel, const char *node,
  445. const char *service,
  446. const struct ares_addrinfo_hints *hints,
  447. ares_addrinfo_callback callback, void *arg);
  448. CARES_EXTERN void ares_freeaddrinfo(struct ares_addrinfo *ai);
  449. /*
  450. * Virtual function set to have user-managed socket IO.
  451. * Note that all functions need to be defined, and when
  452. * set, the library will not do any bind nor set any
  453. * socket options, assuming the client handles these
  454. * through either socket creation or the
  455. * ares_sock_config_callback call.
  456. */
  457. struct iovec;
  458. struct ares_socket_functions {
  459. ares_socket_t (*asocket)(int, int, int, void *);
  460. int (*aclose)(ares_socket_t, void *);
  461. int (*aconnect)(ares_socket_t, const struct sockaddr *, ares_socklen_t,
  462. void *);
  463. ares_ssize_t (*arecvfrom)(ares_socket_t, void *, size_t, int,
  464. struct sockaddr *, ares_socklen_t *, void *);
  465. ares_ssize_t (*asendv)(ares_socket_t, const struct iovec *, int, void *);
  466. };
  467. CARES_EXTERN void
  468. ares_set_socket_functions(ares_channel_t *channel,
  469. const struct ares_socket_functions *funcs,
  470. void *user_data);
  471. CARES_EXTERN CARES_DEPRECATED_FOR(ares_send_dnsrec) void ares_send(
  472. ares_channel_t *channel, const unsigned char *qbuf, int qlen,
  473. ares_callback callback, void *arg);
  474. /*! Send a DNS query as an ares_dns_record_t with a callback containing the
  475. * parsed DNS record.
  476. *
  477. * \param[in] channel Pointer to channel on which queries will be sent.
  478. * \param[in] dnsrec DNS Record to send
  479. * \param[in] callback Callback function invoked on completion or failure of
  480. * the query sequence.
  481. * \param[in] arg Additional argument passed to the callback function.
  482. * \param[out] qid Query ID
  483. * \return One of the c-ares status codes.
  484. */
  485. CARES_EXTERN ares_status_t ares_send_dnsrec(ares_channel_t *channel,
  486. const ares_dns_record_t *dnsrec,
  487. ares_callback_dnsrec callback,
  488. void *arg, unsigned short *qid);
  489. CARES_EXTERN CARES_DEPRECATED_FOR(ares_query_dnsrec) void ares_query(
  490. ares_channel_t *channel, const char *name, int dnsclass, int type,
  491. ares_callback callback, void *arg);
  492. /*! Perform a DNS query with a callback containing the parsed DNS record.
  493. *
  494. * \param[in] channel Pointer to channel on which queries will be sent.
  495. * \param[in] name Query name
  496. * \param[in] dnsclass DNS Class
  497. * \param[in] type DNS Record Type
  498. * \param[in] callback Callback function invoked on completion or failure of
  499. * the query sequence.
  500. * \param[in] arg Additional argument passed to the callback function.
  501. * \param[out] qid Query ID
  502. * \return One of the c-ares status codes.
  503. */
  504. CARES_EXTERN ares_status_t ares_query_dnsrec(ares_channel_t *channel,
  505. const char *name,
  506. ares_dns_class_t dnsclass,
  507. ares_dns_rec_type_t type,
  508. ares_callback_dnsrec callback,
  509. void *arg, unsigned short *qid);
  510. CARES_EXTERN CARES_DEPRECATED_FOR(ares_search_dnsrec) void ares_search(
  511. ares_channel_t *channel, const char *name, int dnsclass, int type,
  512. ares_callback callback, void *arg);
  513. /*! Search for a complete DNS message.
  514. *
  515. * \param[in] channel Pointer to channel on which queries will be sent.
  516. * \param[in] dnsrec Pointer to initialized and filled DNS record object.
  517. * \param[in] callback Callback function invoked on completion or failure of
  518. * the query sequence.
  519. * \param[in] arg Additional argument passed to the callback function.
  520. * \return One of the c-ares status codes. In all cases, except
  521. * ARES_EFORMERR due to misuse, this error code will also be sent
  522. * to the provided callback.
  523. */
  524. CARES_EXTERN ares_status_t ares_search_dnsrec(ares_channel_t *channel,
  525. const ares_dns_record_t *dnsrec,
  526. ares_callback_dnsrec callback,
  527. void *arg);
  528. CARES_EXTERN CARES_DEPRECATED_FOR(ares_getaddrinfo) void ares_gethostbyname(
  529. ares_channel_t *channel, const char *name, int family,
  530. ares_host_callback callback, void *arg);
  531. CARES_EXTERN int ares_gethostbyname_file(ares_channel_t *channel,
  532. const char *name, int family,
  533. struct hostent **host);
  534. CARES_EXTERN void ares_gethostbyaddr(ares_channel_t *channel, const void *addr,
  535. int addrlen, int family,
  536. ares_host_callback callback, void *arg);
  537. CARES_EXTERN void ares_getnameinfo(ares_channel_t *channel,
  538. const struct sockaddr *sa,
  539. ares_socklen_t salen, int flags,
  540. ares_nameinfo_callback callback, void *arg);
  541. CARES_EXTERN CARES_DEPRECATED_FOR(
  542. ARES_OPT_EVENT_THREAD or
  543. ARES_OPT_SOCK_STATE_CB) int ares_fds(const ares_channel_t *channel,
  544. fd_set *read_fds, fd_set *write_fds);
  545. CARES_EXTERN CARES_DEPRECATED_FOR(
  546. ARES_OPT_EVENT_THREAD or
  547. ARES_OPT_SOCK_STATE_CB) int ares_getsock(const ares_channel_t *channel,
  548. ares_socket_t *socks, int numsocks);
  549. CARES_EXTERN struct timeval *ares_timeout(const ares_channel_t *channel,
  550. struct timeval *maxtv,
  551. struct timeval *tv);
  552. CARES_EXTERN CARES_DEPRECATED_FOR(ares_process_fd) void ares_process(
  553. ares_channel_t *channel, fd_set *read_fds, fd_set *write_fds);
  554. CARES_EXTERN void ares_process_fd(ares_channel_t *channel,
  555. ares_socket_t read_fd,
  556. ares_socket_t write_fd);
  557. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_record_create) int ares_create_query(
  558. const char *name, int dnsclass, int type, unsigned short id, int rd,
  559. unsigned char **buf, int *buflen, int max_udp_size);
  560. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_record_create) int ares_mkquery(
  561. const char *name, int dnsclass, int type, unsigned short id, int rd,
  562. unsigned char **buf, int *buflen);
  563. CARES_EXTERN int ares_expand_name(const unsigned char *encoded,
  564. const unsigned char *abuf, int alen, char **s,
  565. long *enclen);
  566. CARES_EXTERN int ares_expand_string(const unsigned char *encoded,
  567. const unsigned char *abuf, int alen,
  568. unsigned char **s, long *enclen);
  569. struct ares_addrttl {
  570. struct in_addr ipaddr;
  571. int ttl;
  572. };
  573. struct ares_addr6ttl {
  574. struct ares_in6_addr ip6addr;
  575. int ttl;
  576. };
  577. struct ares_caa_reply {
  578. struct ares_caa_reply *next;
  579. int critical;
  580. unsigned char *property;
  581. size_t plength; /* plength excludes null termination */
  582. unsigned char *value;
  583. size_t length; /* length excludes null termination */
  584. };
  585. struct ares_srv_reply {
  586. struct ares_srv_reply *next;
  587. char *host;
  588. unsigned short priority;
  589. unsigned short weight;
  590. unsigned short port;
  591. };
  592. struct ares_mx_reply {
  593. struct ares_mx_reply *next;
  594. char *host;
  595. unsigned short priority;
  596. };
  597. struct ares_txt_reply {
  598. struct ares_txt_reply *next;
  599. unsigned char *txt;
  600. size_t length; /* length excludes null termination */
  601. };
  602. /* NOTE: This structure is a superset of ares_txt_reply
  603. */
  604. struct ares_txt_ext {
  605. struct ares_txt_ext *next;
  606. unsigned char *txt;
  607. size_t length;
  608. /* 1 - if start of new record
  609. * 0 - if a chunk in the same record */
  610. unsigned char record_start;
  611. };
  612. struct ares_naptr_reply {
  613. struct ares_naptr_reply *next;
  614. unsigned char *flags;
  615. unsigned char *service;
  616. unsigned char *regexp;
  617. char *replacement;
  618. unsigned short order;
  619. unsigned short preference;
  620. };
  621. struct ares_soa_reply {
  622. char *nsname;
  623. char *hostmaster;
  624. unsigned int serial;
  625. unsigned int refresh;
  626. unsigned int retry;
  627. unsigned int expire;
  628. unsigned int minttl;
  629. };
  630. struct ares_uri_reply {
  631. struct ares_uri_reply *next;
  632. unsigned short priority;
  633. unsigned short weight;
  634. char *uri;
  635. int ttl;
  636. };
  637. /*
  638. * Similar to addrinfo, but with extra ttl and missing canonname.
  639. */
  640. struct ares_addrinfo_node {
  641. int ai_ttl;
  642. int ai_flags;
  643. int ai_family;
  644. int ai_socktype;
  645. int ai_protocol;
  646. ares_socklen_t ai_addrlen;
  647. struct sockaddr *ai_addr;
  648. struct ares_addrinfo_node *ai_next;
  649. };
  650. /*
  651. * alias - label of the resource record.
  652. * name - value (canonical name) of the resource record.
  653. * See RFC2181 10.1.1. CNAME terminology.
  654. */
  655. struct ares_addrinfo_cname {
  656. int ttl;
  657. char *alias;
  658. char *name;
  659. struct ares_addrinfo_cname *next;
  660. };
  661. struct ares_addrinfo {
  662. struct ares_addrinfo_cname *cnames;
  663. struct ares_addrinfo_node *nodes;
  664. char *name;
  665. };
  666. struct ares_addrinfo_hints {
  667. int ai_flags;
  668. int ai_family;
  669. int ai_socktype;
  670. int ai_protocol;
  671. };
  672. /*
  673. ** Parse the buffer, starting at *abuf and of length alen bytes, previously
  674. ** obtained from an ares_search call. Put the results in *host, if nonnull.
  675. ** Also, if addrttls is nonnull, put up to *naddrttls IPv4 addresses along with
  676. ** their TTLs in that array, and set *naddrttls to the number of addresses
  677. ** so written.
  678. */
  679. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_a_reply(
  680. const unsigned char *abuf, int alen, struct hostent **host,
  681. struct ares_addrttl *addrttls, int *naddrttls);
  682. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_aaaa_reply(
  683. const unsigned char *abuf, int alen, struct hostent **host,
  684. struct ares_addr6ttl *addrttls, int *naddrttls);
  685. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_caa_reply(
  686. const unsigned char *abuf, int alen, struct ares_caa_reply **caa_out);
  687. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_ptr_reply(
  688. const unsigned char *abuf, int alen, const void *addr, int addrlen,
  689. int family, struct hostent **host);
  690. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_ns_reply(
  691. const unsigned char *abuf, int alen, struct hostent **host);
  692. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_srv_reply(
  693. const unsigned char *abuf, int alen, struct ares_srv_reply **srv_out);
  694. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_mx_reply(
  695. const unsigned char *abuf, int alen, struct ares_mx_reply **mx_out);
  696. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_txt_reply(
  697. const unsigned char *abuf, int alen, struct ares_txt_reply **txt_out);
  698. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_txt_reply_ext(
  699. const unsigned char *abuf, int alen, struct ares_txt_ext **txt_out);
  700. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_naptr_reply(
  701. const unsigned char *abuf, int alen, struct ares_naptr_reply **naptr_out);
  702. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_soa_reply(
  703. const unsigned char *abuf, int alen, struct ares_soa_reply **soa_out);
  704. CARES_EXTERN CARES_DEPRECATED_FOR(ares_dns_parse) int ares_parse_uri_reply(
  705. const unsigned char *abuf, int alen, struct ares_uri_reply **uri_out);
  706. CARES_EXTERN void ares_free_string(void *str);
  707. CARES_EXTERN void ares_free_hostent(struct hostent *host);
  708. CARES_EXTERN void ares_free_data(void *dataptr);
  709. CARES_EXTERN const char *ares_strerror(int code);
  710. struct ares_addr_node {
  711. struct ares_addr_node *next;
  712. int family;
  713. union {
  714. struct in_addr addr4;
  715. struct ares_in6_addr addr6;
  716. } addr;
  717. };
  718. struct ares_addr_port_node {
  719. struct ares_addr_port_node *next;
  720. int family;
  721. union {
  722. struct in_addr addr4;
  723. struct ares_in6_addr addr6;
  724. } addr;
  725. int udp_port;
  726. int tcp_port;
  727. };
  728. CARES_EXTERN CARES_DEPRECATED_FOR(ares_set_servers_csv) int ares_set_servers(
  729. ares_channel_t *channel, const struct ares_addr_node *servers);
  730. CARES_EXTERN
  731. CARES_DEPRECATED_FOR(ares_set_servers_ports_csv)
  732. int ares_set_servers_ports(ares_channel_t *channel,
  733. const struct ares_addr_port_node *servers);
  734. /* Incoming string format: host[:port][,host[:port]]... */
  735. CARES_EXTERN int ares_set_servers_csv(ares_channel_t *channel,
  736. const char *servers);
  737. CARES_EXTERN int ares_set_servers_ports_csv(ares_channel_t *channel,
  738. const char *servers);
  739. CARES_EXTERN char *ares_get_servers_csv(const ares_channel_t *channel);
  740. CARES_EXTERN CARES_DEPRECATED_FOR(ares_get_servers_csv) int ares_get_servers(
  741. const ares_channel_t *channel, struct ares_addr_node **servers);
  742. CARES_EXTERN
  743. CARES_DEPRECATED_FOR(ares_get_servers_csv)
  744. int ares_get_servers_ports(const ares_channel_t *channel,
  745. struct ares_addr_port_node **servers);
  746. CARES_EXTERN const char *ares_inet_ntop(int af, const void *src, char *dst,
  747. ares_socklen_t size);
  748. CARES_EXTERN int ares_inet_pton(int af, const char *src, void *dst);
  749. /*! Whether or not the c-ares library was built with threadsafety
  750. *
  751. * \return ARES_TRUE if built with threadsafety, ARES_FALSE if not
  752. */
  753. CARES_EXTERN ares_bool_t ares_threadsafety(void);
  754. /*! Block until notified that there are no longer any queries in queue, or
  755. * the specified timeout has expired.
  756. *
  757. * \param[in] channel Initialized ares channel
  758. * \param[in] timeout_ms Number of milliseconds to wait for the queue to be
  759. * empty. -1 for Infinite.
  760. * \return ARES_ENOTIMP if not built with threading support, ARES_ETIMEOUT
  761. * if requested timeout expires, ARES_SUCCESS when queue is empty.
  762. */
  763. CARES_EXTERN ares_status_t ares_queue_wait_empty(ares_channel_t *channel,
  764. int timeout_ms);
  765. /*! Retrieve the total number of active queries pending answers from servers.
  766. * Some c-ares requests may spawn multiple queries, such as ares_getaddrinfo()
  767. * when using AF_UNSPEC, which will be reflected in this number.
  768. *
  769. * \param[in] channel Initialized ares channel
  770. * \return Number of active queries to servers
  771. */
  772. CARES_EXTERN size_t ares_queue_active_queries(const ares_channel_t *channel);
  773. #ifdef __cplusplus
  774. }
  775. #endif
  776. #endif /* ARES__H */