PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/getaddrinfo.h

https://github.com/tatsuhiro-t/aria2
C Header | 282 lines | 201 code | 24 blank | 57 comment | 2 complexity | 1a60b9887ebbf22ca0056ebe2e58b188 MD5 | raw file
  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. #ifndef _D_GETADDRINFO_H
  29. #define _D_GETADDRINFO_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif /* __cplusplus */
  33. #ifdef __MINGW32__
  34. # undef SIZE_MAX
  35. #endif // __MINGW32__
  36. #ifdef HAVE_CONFIG_H
  37. # include "config.h"
  38. #endif // HAVE_CONFIG_H
  39. #ifdef __MINGW32__
  40. # ifndef _WIN32_WINNT
  41. # define _WIN32_WINNT 0x501
  42. # endif // _WIN32_WINNT
  43. # include <winsock2.h>
  44. # undef ERROR
  45. # include <ws2tcpip.h>
  46. #endif // __MINGW32__
  47. #ifdef HAVE_SYS_SOCKET_H
  48. # include <sys/socket.h>
  49. #endif // HAVE_SYS_SOCKET_H
  50. #ifdef HAVE_NETDB_H
  51. # include <netdb.h>
  52. #endif // HAVE_NETDB_H
  53. #include <sys/types.h>
  54. /********************************************************************/
  55. /*
  56. * Undefine all the macros.
  57. * <netdb.h> might defines some of them.
  58. */
  59. #ifdef EAI_ADDRFAMILY
  60. # undef EAI_ADDRFAMILY
  61. #endif
  62. #ifdef EAI_AGAIN
  63. # undef EAI_AGAIN
  64. #endif
  65. #ifdef EAI_BADFLAGS
  66. # undef EAI_BADFLAGS
  67. #endif
  68. #ifdef EAI_FAIL
  69. # undef EAI_FAIL
  70. #endif
  71. #ifdef EAI_FAMILY
  72. # undef EAI_FAMILY
  73. #endif
  74. #ifdef EAI_MEMORY
  75. # undef EAI_MEMORY
  76. #endif
  77. #ifdef EAI_NONAME
  78. # undef EAI_NONAME
  79. #endif
  80. #ifdef EAI_OVERFLOW
  81. # undef EAI_OVERFLOW
  82. #endif
  83. #ifdef EAI_SERVICE
  84. # undef EAI_SERVICE
  85. #endif
  86. #ifdef EAI_SOCKTYPE
  87. # undef EAI_SOCKTYPE
  88. #endif
  89. #ifdef EAI_SYSTEM
  90. # undef EAI_SYSTEM
  91. #endif
  92. #ifdef AI_PASSIVE
  93. # undef AI_PASSIVE
  94. #endif
  95. #ifdef AI_CANONNAME
  96. # undef AI_CANONNAME
  97. #endif
  98. #ifdef AI_NUMERICHOST
  99. # undef AI_NUMERICHOST
  100. #endif
  101. #ifdef AI_NUMERICSERV
  102. # undef AI_NUMERICSERV
  103. #endif
  104. #ifdef AI_V4MAPPED
  105. # undef AI_V4MAPPED
  106. #endif
  107. #ifdef AI_ALL
  108. # undef AI_ALL
  109. #endif
  110. #ifdef AI_ADDRCONFIG
  111. # undef AI_ADDRCONFIG
  112. #endif
  113. #ifdef AI_DEFAULT
  114. # undef AI_DEFAULT
  115. #endif
  116. #ifdef NI_NOFQDN
  117. # undef NI_NOFQDN
  118. #endif
  119. #ifdef NI_NUMERICHOST
  120. # undef NI_NUMERICHOST
  121. #endif
  122. #ifdef NI_NAMEREQD
  123. # undef NI_NAMEREQD
  124. #endif
  125. #ifdef NI_NUMERICSERV
  126. # undef NI_NUMERICSERV
  127. #endif
  128. #ifdef NI_NUMERICSCOPE
  129. # undef NI_NUMERICSCOPE
  130. #endif
  131. #ifdef NI_DGRAM
  132. # undef NI_DGRAM
  133. #endif
  134. #ifdef NI_MAXHOST
  135. # undef NI_MAXHOST
  136. #endif
  137. #ifdef NI_MAXSERV
  138. # undef NI_MAXSERV
  139. #endif
  140. /*
  141. * Fake struct and function names.
  142. * <netdb.h> might declares all or some of them.
  143. */
  144. #if defined(HAVE_GETADDRINFO) || defined(HAVE_GETNAMEINFO)
  145. # define addrinfo my_addrinfo
  146. # define gai_strerror my_gai_strerror
  147. # define freeaddrinfo my_freeaddrinfo
  148. # define getaddrinfo my_getaddrinfo
  149. # define getnameinfo my_getnameinfo
  150. #endif
  151. /* <from linux's netdb.h> */
  152. /* Possible values for `ai_flags' field in `addrinfo' structure. */
  153. #define AI_PASSIVE 0x0001 /* Socket address is intended for `bind'. */
  154. #define AI_CANONNAME 0x0002 /* Request for canonical name. */
  155. #define AI_NUMERICHOST 0x0004 /* Don't use name resolution. */
  156. #define AI_V4MAPPED 0x0008 /* IPv4 mapped addresses are acceptable. */
  157. #define AI_ALL 0x0010 /* Return IPv4 mapped and IPv6 addresses. */
  158. #define AI_ADDRCONFIG \
  159. 0x0020 /* Use configuration of this host to choose \
  160. returned address type.. */
  161. #ifdef __USE_GNU
  162. # define AI_IDN \
  163. 0x0040 /* IDN encode input (assuming it is encoded \
  164. in the current locale's character set) \
  165. before looking it up. */
  166. # define AI_CANONIDN 0x0080 /* Translate canonical name from IDN format. */
  167. # define AI_IDN_ALLOW_UNASSIGNED \
  168. 0x0100 /* Don't reject unassigned Unicode \
  169. code points. */
  170. # define AI_IDN_USE_STD3_ASCII_RULES \
  171. 0x0200 /* Validate strings according to \
  172. STD3 rules. */
  173. #endif
  174. #define AI_NUMERICSERV 0x0400 /* Don't use name resolution. */
  175. /* Error values for `getaddrinfo' function. */
  176. #define EAI_BADFLAGS -1 /* Invalid value for `ai_flags' field. */
  177. #define EAI_NONAME -2 /* NAME or SERVICE is unknown. */
  178. #define EAI_AGAIN -3 /* Temporary failure in name resolution. */
  179. #define EAI_FAIL -4 /* Non-recoverable failure in name res. */
  180. #define EAI_NODATA -5 /* No address associated with NAME. */
  181. #define EAI_FAMILY -6 /* `ai_family' not supported. */
  182. #define EAI_SOCKTYPE -7 /* `ai_socktype' not supported. */
  183. #define EAI_SERVICE -8 /* SERVICE not supported for `ai_socktype'. */
  184. #define EAI_ADDRFAMILY -9 /* Address family for NAME not supported. */
  185. #define EAI_MEMORY -10 /* Memory allocation failure. */
  186. #define EAI_SYSTEM -11 /* System error returned in `errno'. */
  187. #define EAI_OVERFLOW -12 /* Argument buffer overflow. */
  188. #ifdef __USE_GNU
  189. # define EAI_INPROGRESS -100 /* Processing request in progress. */
  190. # define EAI_CANCELED -101 /* Request canceled. */
  191. # define EAI_NOTCANCELED -102 /* Request not canceled. */
  192. # define EAI_ALLDONE -103 /* All requests done. */
  193. # define EAI_INTR -104 /* Interrupted by a signal. */
  194. # define EAI_IDN_ENCODE -105 /* IDN encoding failed. */
  195. #endif
  196. #define NI_MAXHOST 1025
  197. #define NI_MAXSERV 32
  198. #define NI_NUMERICHOST 1 /* Don't try to look up hostname. */
  199. #define NI_NUMERICSERV 2 /* Don't convert port number to name. */
  200. #define NI_NOFQDN 4 /* Only return nodename portion. */
  201. #define NI_NAMEREQD 8 /* Don't return numeric addresses. */
  202. #define NI_DGRAM 16 /* Look up UDP service rather than TCP. */
  203. #ifdef __USE_GNU
  204. # define NI_IDN 32 /* Convert name from IDN format. */
  205. # define NI_IDN_ALLOW_UNASSIGNED \
  206. 64 /* Don't reject unassigned Unicode \
  207. code points. */
  208. # define NI_IDN_USE_STD3_ASCII_RULES \
  209. 128 /* Validate strings according to \
  210. STD3 rules. */
  211. #endif
  212. /* </from linux's netdb.h> */
  213. #define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG)
  214. /*
  215. * Address families and Protocol families.
  216. */
  217. #ifndef AF_UNSPEC
  218. # define AF_UNSPEC AF_INET
  219. #endif
  220. #ifndef PF_UNSPEC
  221. # define PF_UNSPEC PF_INET
  222. #endif
  223. /* Nexenta OS(GNU/Solaris OS) defines `struct addrinfo' in netdb.h */
  224. #if !defined(__MINGW32__) && !defined(__sun)
  225. /*
  226. * struct addrinfo.
  227. */
  228. struct addrinfo {
  229. int ai_flags;
  230. int ai_family;
  231. int ai_socktype;
  232. int ai_protocol;
  233. socklen_t ai_addrlen;
  234. char* ai_canonname;
  235. struct sockaddr* ai_addr;
  236. struct addrinfo* ai_next;
  237. };
  238. #endif // !__MINGW32__ && !__sun
  239. /*
  240. * Functions.
  241. */
  242. #ifdef __STDC__
  243. const char* gai_strerror(int);
  244. void freeaddrinfo(struct addrinfo*);
  245. int getaddrinfo(const char*, const char*, const struct addrinfo*,
  246. struct addrinfo**);
  247. int getnameinfo(const struct sockaddr*, socklen_t, char*, socklen_t, char*,
  248. socklen_t, int);
  249. #else
  250. const char* gai_strerror();
  251. void freeaddrinfo();
  252. int getaddrinfo();
  253. int getnameinfo();
  254. #endif
  255. #ifdef __cplusplus
  256. };
  257. #endif /* __cplusplus */
  258. #endif /* not _D_GETADDRINFO_H */