sys_socket.in.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /* Provide a sys/socket header file for systems lacking it (read: MinGW)
  2. and for systems where it is incomplete.
  3. Copyright (C) 2005-2008 Free Software Foundation, Inc.
  4. Written by Simon Josefsson.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /* This file is supposed to be used on platforms that lack <sys/socket.h>,
  17. on platforms where <sys/socket.h> cannot be included standalone, and on
  18. platforms where <sys/socket.h> does not provide all necessary definitions.
  19. It is intended to provide definitions and prototypes needed by an
  20. application. */
  21. #ifndef _GL_SYS_SOCKET_H
  22. #if @HAVE_SYS_SOCKET_H@
  23. # if __GNUC__ >= 3
  24. @PRAGMA_SYSTEM_HEADER@
  25. # endif
  26. /* On many platforms, <sys/socket.h> assumes prior inclusion of
  27. <sys/types.h>. */
  28. # include <sys/types.h>
  29. /* The include_next requires a split double-inclusion guard. */
  30. # @INCLUDE_NEXT@ @NEXT_SYS_SOCKET_H@
  31. #endif
  32. #ifndef _GL_SYS_SOCKET_H
  33. #define _GL_SYS_SOCKET_H
  34. #if @HAVE_SYS_SOCKET_H@
  35. /* A platform that has <sys/socket.h>. */
  36. /* For shutdown(). */
  37. # if !defined SHUT_RD
  38. # define SHUT_RD 0
  39. # endif
  40. # if !defined SHUT_WR
  41. # define SHUT_WR 1
  42. # endif
  43. # if !defined SHUT_RDWR
  44. # define SHUT_RDWR 2
  45. # endif
  46. #else
  47. # ifdef __CYGWIN__
  48. # error "Cygwin does have a sys/socket.h, doesn't it?!?"
  49. # endif
  50. /* A platform that lacks <sys/socket.h>.
  51. Currently only MinGW is supported. See the gnulib manual regarding
  52. Windows sockets. MinGW has the header files winsock2.h and
  53. ws2tcpip.h that declare the sys/socket.h definitions we need. Note
  54. that you can influence which definitions you get by setting the
  55. WINVER symbol before including these two files. For example,
  56. getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
  57. symbol is set indiriectly through WINVER). You can set this by
  58. adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
  59. code may not run on older Windows releases then. My Windows 2000
  60. box was not able to run the code, for example. The situation is
  61. slightly confusing because:
  62. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
  63. suggests that getaddrinfo should be available on all Windows
  64. releases. */
  65. # if @HAVE_WINSOCK2_H@
  66. # include <winsock2.h>
  67. # endif
  68. # if @HAVE_WS2TCPIP_H@
  69. # include <ws2tcpip.h>
  70. # endif
  71. /* For shutdown(). */
  72. # if !defined SHUT_RD && defined SD_RECEIVE
  73. # define SHUT_RD SD_RECEIVE
  74. # endif
  75. # if !defined SHUT_WR && defined SD_SEND
  76. # define SHUT_WR SD_SEND
  77. # endif
  78. # if !defined SHUT_RDWR && defined SD_BOTH
  79. # define SHUT_RDWR SD_BOTH
  80. # endif
  81. /* The definition of GL_LINK_WARNING is copied here. */
  82. # if @HAVE_WINSOCK2_H@
  83. /* Include headers needed by the emulation code. */
  84. # include <sys/types.h>
  85. # include <io.h>
  86. typedef int socklen_t;
  87. # endif
  88. # ifdef __cplusplus
  89. extern "C" {
  90. # endif
  91. # if @HAVE_WINSOCK2_H@
  92. /* Re-define FD_ISSET to avoid a WSA call while we are not using
  93. network sockets. */
  94. static inline int
  95. rpl_fd_isset (SOCKET fd, fd_set * set)
  96. {
  97. u_int i;
  98. if (set == NULL)
  99. return 0;
  100. for (i = 0; i < set->fd_count; i++)
  101. if (set->fd_array[i] == fd)
  102. return 1;
  103. return 0;
  104. }
  105. # undef FD_ISSET
  106. # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
  107. # endif
  108. /* Wrap everything else to use libc file descriptors for sockets. */
  109. # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
  110. # undef close
  111. # define close close_used_without_including_unistd_h
  112. # endif
  113. # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
  114. # undef gethostname
  115. # define gethostname gethostname_used_without_including_unistd_h
  116. # endif
  117. # if @GNULIB_SOCKET@
  118. # if @HAVE_WINSOCK2_H@
  119. # undef socket
  120. # define socket rpl_socket
  121. extern int rpl_socket (int, int, int protocol);
  122. # endif
  123. # elif @HAVE_WINSOCK2_H@
  124. # undef socket
  125. # define socket socket_used_without_requesting_gnulib_module_socket
  126. # elif defined GNULIB_POSIXCHECK
  127. # undef socket
  128. # define socket(d,t,p) \
  129. (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
  130. "use gnulib module socket for portability"), \
  131. socket (d, t, p))
  132. # endif
  133. # if @GNULIB_CONNECT@
  134. # if @HAVE_WINSOCK2_H@
  135. # undef connect
  136. # define connect rpl_connect
  137. extern int rpl_connect (int, struct sockaddr *, int);
  138. # endif
  139. # elif @HAVE_WINSOCK2_H@
  140. # undef connect
  141. # define connect socket_used_without_requesting_gnulib_module_connect
  142. # elif defined GNULIB_POSIXCHECK
  143. # undef connect
  144. # define connect(s,a,l) \
  145. (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
  146. "use gnulib module connect for portability"), \
  147. connect (s, a, l))
  148. # endif
  149. # if @GNULIB_ACCEPT@
  150. # if @HAVE_WINSOCK2_H@
  151. # undef accept
  152. # define accept rpl_accept
  153. extern int rpl_accept (int, struct sockaddr *, int *);
  154. # endif
  155. # elif @HAVE_WINSOCK2_H@
  156. # undef accept
  157. # define accept accept_used_without_requesting_gnulib_module_accept
  158. # elif defined GNULIB_POSIXCHECK
  159. # undef accept
  160. # define accept(s,a,l) \
  161. (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
  162. "use gnulib module accept for portability"), \
  163. accept (s, a, l))
  164. # endif
  165. # if @GNULIB_BIND@
  166. # if @HAVE_WINSOCK2_H@
  167. # undef bind
  168. # define bind rpl_bind
  169. extern int rpl_bind (int, struct sockaddr *, int);
  170. # endif
  171. # elif @HAVE_WINSOCK2_H@
  172. # undef bind
  173. # define bind bind_used_without_requesting_gnulib_module_bind
  174. # elif defined GNULIB_POSIXCHECK
  175. # undef bind
  176. # define bind(s,a,l) \
  177. (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
  178. "use gnulib module bind for portability"), \
  179. bind (s, a, l))
  180. # endif
  181. # if @GNULIB_GETPEERNAME@
  182. # if @HAVE_WINSOCK2_H@
  183. # undef getpeername
  184. # define getpeername rpl_getpeername
  185. extern int rpl_getpeername (int, struct sockaddr *, int *);
  186. # endif
  187. # elif @HAVE_WINSOCK2_H@
  188. # undef getpeername
  189. # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
  190. # elif defined GNULIB_POSIXCHECK
  191. # undef getpeername
  192. # define getpeername(s,a,l) \
  193. (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
  194. "use gnulib module getpeername for portability"), \
  195. getpeername (s, a, l))
  196. # endif
  197. # if @GNULIB_GETSOCKNAME@
  198. # if @HAVE_WINSOCK2_H@
  199. # undef getsockname
  200. # define getsockname rpl_getsockname
  201. extern int rpl_getsockname (int, struct sockaddr *, int *);
  202. # endif
  203. # elif @HAVE_WINSOCK2_H@
  204. # undef getsockname
  205. # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
  206. # elif defined GNULIB_POSIXCHECK
  207. # undef getsockname
  208. # define getsockname(s,a,l) \
  209. (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
  210. "use gnulib module getsockname for portability"), \
  211. getsockname (s, a, l))
  212. # endif
  213. # if @GNULIB_GETSOCKOPT@
  214. # if @HAVE_WINSOCK2_H@
  215. # undef getsockopt
  216. # define getsockopt rpl_getsockopt
  217. extern int rpl_getsockopt (int, int, int, void *, int *);
  218. # endif
  219. # elif @HAVE_WINSOCK2_H@
  220. # undef getsockopt
  221. # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
  222. # elif defined GNULIB_POSIXCHECK
  223. # undef getsockopt
  224. # define getsockopt(s,lvl,o,v,l) \
  225. (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
  226. "use gnulib module getsockopt for portability"), \
  227. getsockopt (s, lvl, o, v, l))
  228. # endif
  229. # if @GNULIB_LISTEN@
  230. # if @HAVE_WINSOCK2_H@
  231. # undef listen
  232. # define listen rpl_listen
  233. extern int rpl_listen (int, int);
  234. # endif
  235. # elif @HAVE_WINSOCK2_H@
  236. # undef listen
  237. # define listen listen_used_without_requesting_gnulib_module_listen
  238. # elif defined GNULIB_POSIXCHECK
  239. # undef listen
  240. # define listen(s,b) \
  241. (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
  242. "use gnulib module listen for portability"), \
  243. listen (s, b))
  244. # endif
  245. # if @GNULIB_RECV@
  246. # if @HAVE_WINSOCK2_H@
  247. # undef recv
  248. # define recv rpl_recv
  249. extern int rpl_recv (int, void *, int, int);
  250. # endif
  251. # elif @HAVE_WINSOCK2_H@
  252. # undef recv
  253. # define recv recv_used_without_requesting_gnulib_module_recv
  254. # elif defined GNULIB_POSIXCHECK
  255. # undef recv
  256. # define recv(s,b,n,f) \
  257. (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
  258. "use gnulib module recv for portability"), \
  259. recv (s, b, n, f))
  260. # endif
  261. # if @GNULIB_SEND@
  262. # if @HAVE_WINSOCK2_H@
  263. # undef send
  264. # define send rpl_send
  265. extern int rpl_send (int, const void *, int, int);
  266. # endif
  267. # elif @HAVE_WINSOCK2_H@
  268. # undef send
  269. # define send send_used_without_requesting_gnulib_module_send
  270. # elif defined GNULIB_POSIXCHECK
  271. # undef send
  272. # define send(s,b,n,f) \
  273. (GL_LINK_WARNING ("send is not always POSIX compliant - " \
  274. "use gnulib module send for portability"), \
  275. send (s, b, n, f))
  276. # endif
  277. # if @GNULIB_RECVFROM@
  278. # if @HAVE_WINSOCK2_H@
  279. # undef recvfrom
  280. # define recvfrom rpl_recvfrom
  281. extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
  282. # endif
  283. # elif @HAVE_WINSOCK2_H@
  284. # undef recvfrom
  285. # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
  286. # elif defined GNULIB_POSIXCHECK
  287. # undef recvfrom
  288. # define recvfrom(s,b,n,f,a,l) \
  289. (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
  290. "use gnulib module recvfrom for portability"), \
  291. recvfrom (s, b, n, f, a, l))
  292. # endif
  293. # if @GNULIB_SENDTO@
  294. # if @HAVE_WINSOCK2_H@
  295. # undef sendto
  296. # define sendto rpl_sendto
  297. extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
  298. # endif
  299. # elif @HAVE_WINSOCK2_H@
  300. # undef sendto
  301. # define sendto sendto_used_without_requesting_gnulib_module_sendto
  302. # elif defined GNULIB_POSIXCHECK
  303. # undef sendto
  304. # define sendto(s,b,n,f,a,l) \
  305. (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
  306. "use gnulib module sendto for portability"), \
  307. sendto (s, b, n, f, a, l))
  308. # endif
  309. # if @GNULIB_SETSOCKOPT@
  310. # if @HAVE_WINSOCK2_H@
  311. # undef setsockopt
  312. # define setsockopt rpl_setsockopt
  313. extern int rpl_setsockopt (int, int, int, const void *, int);
  314. # endif
  315. # elif @HAVE_WINSOCK2_H@
  316. # undef setsockopt
  317. # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
  318. # elif defined GNULIB_POSIXCHECK
  319. # undef setsockopt
  320. # define setsockopt(s,lvl,o,v,l) \
  321. (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
  322. "use gnulib module setsockopt for portability"), \
  323. setsockopt (s, lvl, o, v, l))
  324. # endif
  325. # if @GNULIB_SHUTDOWN@
  326. # if @HAVE_WINSOCK2_H@
  327. # undef shutdown
  328. # define shutdown rpl_shutdown
  329. extern int rpl_shutdown (int, int);
  330. # endif
  331. # elif @HAVE_WINSOCK2_H@
  332. # undef shutdown
  333. # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
  334. # elif defined GNULIB_POSIXCHECK
  335. # undef shutdown
  336. # define shutdown(s,h) \
  337. (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
  338. "use gnulib module shutdown for portability"), \
  339. shutdown (s, h))
  340. # endif
  341. # if @HAVE_WINSOCK2_H@
  342. # undef select
  343. # define select select_used_without_including_sys_select_h
  344. # endif
  345. # ifdef __cplusplus
  346. }
  347. # endif
  348. #endif /* HAVE_SYS_SOCKET_H */
  349. #endif /* _GL_SYS_SOCKET_H */
  350. #endif /* _GL_SYS_SOCKET_H */