1 /*
2  * Copyright (c) 2001, 02  Motoyuki Kasahara
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the project nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _D_GETADDRINFO_H
30 #define _D_GETADDRINFO_H
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35 
36 #ifdef __MINGW32__
37 #  undef SIZE_MAX
38 #endif // __MINGW32__
39 
40 #ifdef HAVE_CONFIG_H
41 #  include "config.h"
42 #endif // HAVE_CONFIG_H
43 
44 #ifdef __MINGW32__
45 #  ifndef _WIN32_WINNT
46 #    define _WIN32_WINNT 0x501
47 #  endif // _WIN32_WINNT
48 #  include <winsock2.h>
49 #  undef ERROR
50 #  include <ws2tcpip.h>
51 #endif // __MINGW32__
52 
53 #ifdef HAVE_SYS_SOCKET_H
54 #  include <sys/socket.h>
55 #endif // HAVE_SYS_SOCKET_H
56 #ifdef HAVE_NETDB_H
57 #  include <netdb.h>
58 #endif // HAVE_NETDB_H
59 
60 #include <sys/types.h>
61 
62 /********************************************************************/
63 /*
64  * Undefine all the macros.
65  * <netdb.h> might defines some of them.
66  */
67 #ifdef EAI_ADDRFAMILY
68 #  undef EAI_ADDRFAMILY
69 #endif
70 #ifdef EAI_AGAIN
71 #  undef EAI_AGAIN
72 #endif
73 #ifdef EAI_BADFLAGS
74 #  undef EAI_BADFLAGS
75 #endif
76 #ifdef EAI_FAIL
77 #  undef EAI_FAIL
78 #endif
79 #ifdef EAI_FAMILY
80 #  undef EAI_FAMILY
81 #endif
82 #ifdef EAI_MEMORY
83 #  undef EAI_MEMORY
84 #endif
85 #ifdef EAI_NONAME
86 #  undef EAI_NONAME
87 #endif
88 #ifdef EAI_OVERFLOW
89 #  undef EAI_OVERFLOW
90 #endif
91 #ifdef EAI_SERVICE
92 #  undef EAI_SERVICE
93 #endif
94 #ifdef EAI_SOCKTYPE
95 #  undef EAI_SOCKTYPE
96 #endif
97 #ifdef EAI_SYSTEM
98 #  undef EAI_SYSTEM
99 #endif
100 
101 #ifdef AI_PASSIVE
102 #  undef AI_PASSIVE
103 #endif
104 #ifdef AI_CANONNAME
105 #  undef AI_CANONNAME
106 #endif
107 #ifdef AI_NUMERICHOST
108 #  undef AI_NUMERICHOST
109 #endif
110 #ifdef AI_NUMERICSERV
111 #  undef AI_NUMERICSERV
112 #endif
113 #ifdef AI_V4MAPPED
114 #  undef AI_V4MAPPED
115 #endif
116 #ifdef AI_ALL
117 #  undef AI_ALL
118 #endif
119 #ifdef AI_ADDRCONFIG
120 #  undef AI_ADDRCONFIG
121 #endif
122 #ifdef AI_DEFAULT
123 #  undef AI_DEFAULT
124 #endif
125 
126 #ifdef NI_NOFQDN
127 #  undef NI_NOFQDN
128 #endif
129 #ifdef NI_NUMERICHOST
130 #  undef NI_NUMERICHOST
131 #endif
132 #ifdef NI_NAMEREQD
133 #  undef NI_NAMEREQD
134 #endif
135 #ifdef NI_NUMERICSERV
136 #  undef NI_NUMERICSERV
137 #endif
138 #ifdef NI_NUMERICSCOPE
139 #  undef NI_NUMERICSCOPE
140 #endif
141 
142 #ifdef NI_DGRAM
143 #  undef NI_DGRAM
144 #endif
145 #ifdef NI_MAXHOST
146 #  undef NI_MAXHOST
147 #endif
148 #ifdef NI_MAXSERV
149 #  undef NI_MAXSERV
150 #endif
151 
152 /*
153  * Fake struct and function names.
154  * <netdb.h> might declares all or some of them.
155  */
156 #if defined(HAVE_GETADDRINFO) || defined(HAVE_GETNAMEINFO)
157 #  define addrinfo my_addrinfo
158 #  define gai_strerror my_gai_strerror
159 #  define freeaddrinfo my_freeaddrinfo
160 #  define getaddrinfo my_getaddrinfo
161 #  define getnameinfo my_getnameinfo
162 #endif
163 
164 /* <from linux's netdb.h> */
165 /* Possible values for `ai_flags' field in `addrinfo' structure.  */
166 #define AI_PASSIVE 0x0001     /* Socket address is intended for `bind'.  */
167 #define AI_CANONNAME 0x0002   /* Request for canonical name.  */
168 #define AI_NUMERICHOST 0x0004 /* Don't use name resolution.  */
169 #define AI_V4MAPPED 0x0008    /* IPv4 mapped addresses are acceptable.  */
170 #define AI_ALL 0x0010         /* Return IPv4 mapped and IPv6 addresses.  */
171 #define AI_ADDRCONFIG                                                          \
172   0x0020 /* Use configuration of this host to choose                           \
173             returned address type..  */
174 #ifdef __USE_GNU
175 #  define AI_IDN                                                               \
176     0x0040                   /* IDN encode input (assuming it is encoded       \
177                                 in the current locale's character set)         \
178                                 before looking it up. */
179 #  define AI_CANONIDN 0x0080 /* Translate canonical name from IDN format. */
180 #  define AI_IDN_ALLOW_UNASSIGNED                                              \
181     0x0100 /* Don't reject unassigned Unicode                                  \
182               code points.  */
183 #  define AI_IDN_USE_STD3_ASCII_RULES                                          \
184     0x0200 /* Validate strings according to                                    \
185               STD3 rules.  */
186 #endif
187 #define AI_NUMERICSERV 0x0400 /* Don't use name resolution.  */
188 
189 /* Error values for `getaddrinfo' function.  */
190 #define EAI_BADFLAGS -1   /* Invalid value for `ai_flags' field.  */
191 #define EAI_NONAME -2     /* NAME or SERVICE is unknown.  */
192 #define EAI_AGAIN -3      /* Temporary failure in name resolution.  */
193 #define EAI_FAIL -4       /* Non-recoverable failure in name res.  */
194 #define EAI_NODATA -5     /* No address associated with NAME.  */
195 #define EAI_FAMILY -6     /* `ai_family' not supported.  */
196 #define EAI_SOCKTYPE -7   /* `ai_socktype' not supported.  */
197 #define EAI_SERVICE -8    /* SERVICE not supported for `ai_socktype'.  */
198 #define EAI_ADDRFAMILY -9 /* Address family for NAME not supported.  */
199 #define EAI_MEMORY -10    /* Memory allocation failure.  */
200 #define EAI_SYSTEM -11    /* System error returned in `errno'.  */
201 #define EAI_OVERFLOW -12  /* Argument buffer overflow.  */
202 #ifdef __USE_GNU
203 #  define EAI_INPROGRESS -100  /* Processing request in progress.  */
204 #  define EAI_CANCELED -101    /* Request canceled.  */
205 #  define EAI_NOTCANCELED -102 /* Request not canceled.  */
206 #  define EAI_ALLDONE -103     /* All requests done.  */
207 #  define EAI_INTR -104        /* Interrupted by a signal.  */
208 #  define EAI_IDN_ENCODE -105  /* IDN encoding failed.  */
209 #endif
210 
211 #define NI_MAXHOST 1025
212 #define NI_MAXSERV 32
213 
214 #define NI_NUMERICHOST 1 /* Don't try to look up hostname.  */
215 #define NI_NUMERICSERV 2 /* Don't convert port number to name.  */
216 #define NI_NOFQDN 4      /* Only return nodename portion.  */
217 #define NI_NAMEREQD 8    /* Don't return numeric addresses.  */
218 #define NI_DGRAM 16      /* Look up UDP service rather than TCP.  */
219 #ifdef __USE_GNU
220 #  define NI_IDN 32 /* Convert name from IDN format.  */
221 #  define NI_IDN_ALLOW_UNASSIGNED                                              \
222     64 /* Don't reject unassigned Unicode                                      \
223           code points.  */
224 #  define NI_IDN_USE_STD3_ASCII_RULES                                          \
225     128 /* Validate strings according to                                       \
226            STD3 rules.  */
227 #endif
228 /* </from linux's netdb.h> */
229 
230 #define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG)
231 
232 /*
233  * Address families and Protocol families.
234  */
235 #ifndef AF_UNSPEC
236 #  define AF_UNSPEC AF_INET
237 #endif
238 #ifndef PF_UNSPEC
239 #  define PF_UNSPEC PF_INET
240 #endif
241 
242 /* Nexenta OS(GNU/Solaris OS) defines `struct addrinfo' in netdb.h */
243 #if !defined(__MINGW32__) && !defined(__sun)
244 
245 /*
246  * struct addrinfo.
247  */
248 struct addrinfo {
249   int ai_flags;
250   int ai_family;
251   int ai_socktype;
252   int ai_protocol;
253   socklen_t ai_addrlen;
254   char* ai_canonname;
255   struct sockaddr* ai_addr;
256   struct addrinfo* ai_next;
257 };
258 
259 #endif // !__MINGW32__ && !__sun
260 
261 /*
262  * Functions.
263  */
264 #ifdef __STDC__
265 const char* gai_strerror(int);
266 void freeaddrinfo(struct addrinfo*);
267 int getaddrinfo(const char*, const char*, const struct addrinfo*,
268                 struct addrinfo**);
269 int getnameinfo(const struct sockaddr*, socklen_t, char*, socklen_t, char*,
270                 socklen_t, int);
271 #else
272 const char* gai_strerror();
273 void freeaddrinfo();
274 int getaddrinfo();
275 int getnameinfo();
276 #endif
277 
278 #ifdef __cplusplus
279 };
280 #endif /* __cplusplus */
281 
282 #endif /* not _D_GETADDRINFO_H */
283