3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
21 #include <sys/types.h>
23 #if defined(_AIX) || defined(NETWARE)
24 /* HP-UX systems version 9, 10 and 11 lack sys/select.h and so does oldish
25 libc5-based Linux systems. Only include it on system that are known to
27 #include <sys/select.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <sys/socket.h>
46 #define ARES_SUCCESS 0
48 /* Server error codes (ARES_ENODATA indicates no relevant answer) */
49 #define ARES_ENODATA 1
50 #define ARES_EFORMERR 2
51 #define ARES_ESERVFAIL 3
52 #define ARES_ENOTFOUND 4
53 #define ARES_ENOTIMP 5
54 #define ARES_EREFUSED 6
56 /* Locally generated error codes */
57 #define ARES_EBADQUERY 7
58 #define ARES_EBADNAME 8
59 #define ARES_EBADFAMILY 9
60 #define ARES_EBADRESP 10
61 #define ARES_ECONNREFUSED 11
62 #define ARES_ETIMEOUT 12
65 #define ARES_ENOMEM 15
66 #define ARES_EDESTRUCTION 16
67 #define ARES_EBADSTR 17
69 /* ares_getnameinfo error codes */
70 #define ARES_EBADFLAGS 18
72 /* ares_getaddrinfo error codes */
73 #define ARES_ENONAME 19
74 #define ARES_EBADHINTS 20
77 #define ARES_FLAG_USEVC (1 << 0)
78 #define ARES_FLAG_PRIMARY (1 << 1)
79 #define ARES_FLAG_IGNTC (1 << 2)
80 #define ARES_FLAG_NORECURSE (1 << 3)
81 #define ARES_FLAG_STAYOPEN (1 << 4)
82 #define ARES_FLAG_NOSEARCH (1 << 5)
83 #define ARES_FLAG_NOALIASES (1 << 6)
84 #define ARES_FLAG_NOCHECKRESP (1 << 7)
86 /* Option mask values */
87 #define ARES_OPT_FLAGS (1 << 0)
88 #define ARES_OPT_TIMEOUT (1 << 1)
89 #define ARES_OPT_TRIES (1 << 2)
90 #define ARES_OPT_NDOTS (1 << 3)
91 #define ARES_OPT_UDP_PORT (1 << 4)
92 #define ARES_OPT_TCP_PORT (1 << 5)
93 #define ARES_OPT_SERVERS (1 << 6)
94 #define ARES_OPT_DOMAINS (1 << 7)
95 #define ARES_OPT_LOOKUPS (1 << 8)
96 #define ARES_OPT_SOCK_STATE_CB (1 << 9)
98 /* Nameinfo flag values */
99 #define ARES_NI_NOFQDN (1 << 0)
100 #define ARES_NI_NUMERICHOST (1 << 1)
101 #define ARES_NI_NAMEREQD (1 << 2)
102 #define ARES_NI_NUMERICSERV (1 << 3)
103 #define ARES_NI_DGRAM (1 << 4)
104 #define ARES_NI_TCP 0
105 #define ARES_NI_UDP ARES_NI_DGRAM
106 #define ARES_NI_SCTP (1 << 5)
107 #define ARES_NI_DCCP (1 << 6)
108 #define ARES_NI_NUMERICSCOPE (1 << 7)
109 #define ARES_NI_LOOKUPHOST (1 << 8)
110 #define ARES_NI_LOOKUPSERVICE (1 << 9)
111 /* Reserved for future use */
112 #define ARES_NI_IDN (1 << 10)
113 #define ARES_NI_IDN_ALLOW_UNASSIGNED (1 << 11)
114 #define ARES_NI_IDN_USE_STD3_ASCII_RULES (1 << 12)
116 /* Addrinfo flag values */
117 #define ARES_AI_CANONNAME (1 << 0)
118 #define ARES_AI_NUMERICHOST (1 << 1)
119 #define ARES_AI_PASSIVE (1 << 2)
120 #define ARES_AI_NUMERICSERV (1 << 3)
121 #define ARES_AI_V4MAPPED (1 << 4)
122 #define ARES_AI_ALL (1 << 5)
123 #define ARES_AI_ADDRCONFIG (1 << 6)
124 /* Reserved for future use */
125 #define ARES_AI_IDN (1 << 10)
126 #define ARES_AI_IDN_ALLOW_UNASSIGNED (1 << 11)
127 #define ARES_AI_IDN_USE_STD3_ASCII_RULES (1 << 12)
128 #define ARES_AI_CANONIDN (1 << 13)
130 #define ARES_AI_MASK (ARES_AI_CANONNAME|ARES_AI_NUMERICHOST|ARES_AI_PASSIVE| \
131 ARES_AI_NUMERICSERV|ARES_AI_V4MAPPED|ARES_AI_ALL| \
133 #define ARES_GETSOCK_MAXNUM 16 /* ares_getsock() can return info about this
135 #define ARES_GETSOCK_READABLE(bits,num) (bits & (1<< (num)))
136 #define ARES_GETSOCK_WRITABLE(bits,num) (bits & (1 << ((num) + \
137 ARES_GETSOCK_MAXNUM)))
141 * Typedef our socket type
144 #ifndef ares_socket_typedef
146 typedef SOCKET ares_socket_t;
147 #define ARES_SOCKET_BAD INVALID_SOCKET
149 typedef int ares_socket_t;
150 #define ARES_SOCKET_BAD -1
152 #define ares_socket_typedef
153 #endif /* ares_socket_typedef */
156 typedef void (*ares_sock_state_cb)(void *data,
161 typedef void (*ares_sock_state_cb)(void *data,
167 struct ares_options {
172 unsigned short udp_port;
173 unsigned short tcp_port;
174 struct in_addr *servers;
179 ares_sock_state_cb sock_state_cb;
180 void *sock_state_cb_data;
186 struct ares_channeldata;
187 typedef struct ares_channeldata *ares_channel;
188 typedef void (*ares_callback)(void *arg, int status, unsigned char *abuf,
190 typedef void (*ares_host_callback)(void *arg, int status,
191 struct hostent *hostent);
192 typedef void (*ares_nameinfo_callback)(void *arg, int status,
193 char *node, char *service);
195 int ares_init(ares_channel *channelptr);
196 int ares_init_options(ares_channel *channelptr, struct ares_options *options,
198 void ares_destroy(ares_channel channel);
199 void ares_cancel(ares_channel channel);
200 void ares_send(ares_channel channel, const unsigned char *qbuf, int qlen,
201 ares_callback callback, void *arg);
202 void ares_query(ares_channel channel, const char *name, int dnsclass,
203 int type, ares_callback callback, void *arg);
204 void ares_search(ares_channel channel, const char *name, int dnsclass,
205 int type, ares_callback callback, void *arg);
206 void ares_gethostbyname(ares_channel channel, const char *name, int family,
207 ares_host_callback callback, void *arg);
208 void ares_gethostbyaddr(ares_channel channel, const void *addr, int addrlen,
209 int family, ares_host_callback callback, void *arg);
210 void ares_getnameinfo(ares_channel channel, const struct sockaddr *sa,
211 socklen_t salen, int flags,
212 ares_nameinfo_callback callback,
214 int ares_fds(ares_channel channel, fd_set *read_fds, fd_set *write_fds);
215 int ares_getsock(ares_channel channel, int *socks, int numsocks);
216 struct timeval *ares_timeout(ares_channel channel, struct timeval *maxtv,
218 void ares_process(ares_channel channel, fd_set *read_fds, fd_set *write_fds);
219 void ares_process_fd(ares_channel channel, ares_socket_t read_fd,
220 ares_socket_t write_fd);
222 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
223 int rd, unsigned char **buf, int *buflen);
224 int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
225 int alen, char **s, long *enclen);
226 int ares_expand_string(const unsigned char *encoded, const unsigned char *abuf,
227 int alen, unsigned char **s, long *enclen);
228 int ares_parse_a_reply(const unsigned char *abuf, int alen,
229 struct hostent **host);
230 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
231 struct hostent **host);
232 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
233 int addrlen, int family, struct hostent **host);
234 int ares_parse_ns_reply(const unsigned char *abuf, int alen,
235 struct hostent **host);
236 void ares_free_string(void *str);
237 void ares_free_hostent(struct hostent *host);
238 const char *ares_strerror(int code);