sys_socket.in.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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-2009 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_STRUCT_SOCKADDR_STORAGE@
  35. # include <alignof.h>
  36. /* Code taken from glibc sysdeps/unix/sysv/linux/bits/socket.h on
  37. 2009-05-08, licensed under LGPLv2.1+, plus portability fixes. */
  38. # define __ss_aligntype unsigned long int
  39. # define _SS_SIZE 256
  40. # define _SS_PADSIZE \
  41. (_SS_SIZE - ((sizeof (sa_family_t) >= alignof (__ss_aligntype) \
  42. ? sizeof (sa_family_t) \
  43. : alignof (__ss_aligntype)) \
  44. + sizeof (__ss_aligntype)))
  45. struct sockaddr_storage
  46. {
  47. sa_family_t ss_family; /* Address family, etc. */
  48. __ss_aligntype __ss_align; /* Force desired alignment. */
  49. char __ss_padding[_SS_PADSIZE];
  50. };
  51. #endif
  52. #if @HAVE_SYS_SOCKET_H@
  53. /* A platform that has <sys/socket.h>. */
  54. /* For shutdown(). */
  55. # if !defined SHUT_RD
  56. # define SHUT_RD 0
  57. # endif
  58. # if !defined SHUT_WR
  59. # define SHUT_WR 1
  60. # endif
  61. # if !defined SHUT_RDWR
  62. # define SHUT_RDWR 2
  63. # endif
  64. #else
  65. # ifdef __CYGWIN__
  66. # error "Cygwin does have a sys/socket.h, doesn't it?!?"
  67. # endif
  68. /* A platform that lacks <sys/socket.h>.
  69. Currently only MinGW is supported. See the gnulib manual regarding
  70. Windows sockets. MinGW has the header files winsock2.h and
  71. ws2tcpip.h that declare the sys/socket.h definitions we need. Note
  72. that you can influence which definitions you get by setting the
  73. WINVER symbol before including these two files. For example,
  74. getaddrinfo is only available if _WIN32_WINNT >= 0x0501 (that
  75. symbol is set indiriectly through WINVER). You can set this by
  76. adding AC_DEFINE(WINVER, 0x0501) to configure.ac. Note that your
  77. code may not run on older Windows releases then. My Windows 2000
  78. box was not able to run the code, for example. The situation is
  79. slightly confusing because:
  80. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/getaddrinfo_2.asp
  81. suggests that getaddrinfo should be available on all Windows
  82. releases. */
  83. # if @HAVE_WINSOCK2_H@
  84. # include <winsock2.h>
  85. # endif
  86. # if @HAVE_WS2TCPIP_H@
  87. # include <ws2tcpip.h>
  88. # endif
  89. /* For shutdown(). */
  90. # if !defined SHUT_RD && defined SD_RECEIVE
  91. # define SHUT_RD SD_RECEIVE
  92. # endif
  93. # if !defined SHUT_WR && defined SD_SEND
  94. # define SHUT_WR SD_SEND
  95. # endif
  96. # if !defined SHUT_RDWR && defined SD_BOTH
  97. # define SHUT_RDWR SD_BOTH
  98. # endif
  99. /* The definition of GL_LINK_WARNING is copied here. */
  100. # if @HAVE_WINSOCK2_H@
  101. /* Include headers needed by the emulation code. */
  102. # include <sys/types.h>
  103. # include <io.h>
  104. typedef int socklen_t;
  105. # endif
  106. # ifdef __cplusplus
  107. extern "C" {
  108. # endif
  109. # if @HAVE_WINSOCK2_H@
  110. /* Re-define FD_ISSET to avoid a WSA call while we are not using
  111. network sockets. */
  112. static inline int
  113. rpl_fd_isset (SOCKET fd, fd_set * set)
  114. {
  115. u_int i;
  116. if (set == NULL)
  117. return 0;
  118. for (i = 0; i < set->fd_count; i++)
  119. if (set->fd_array[i] == fd)
  120. return 1;
  121. return 0;
  122. }
  123. # undef FD_ISSET
  124. # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
  125. # endif
  126. /* Wrap everything else to use libc file descriptors for sockets. */
  127. # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
  128. # undef close
  129. # define close close_used_without_including_unistd_h
  130. # endif
  131. # if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
  132. # undef gethostname
  133. # define gethostname gethostname_used_without_including_unistd_h
  134. # endif
  135. # if @GNULIB_SOCKET@
  136. # if @HAVE_WINSOCK2_H@
  137. # undef socket
  138. # define socket rpl_socket
  139. extern int rpl_socket (int, int, int protocol);
  140. # endif
  141. # elif @HAVE_WINSOCK2_H@
  142. # undef socket
  143. # define socket socket_used_without_requesting_gnulib_module_socket
  144. # elif defined GNULIB_POSIXCHECK
  145. # undef socket
  146. # define socket(d,t,p) \
  147. (GL_LINK_WARNING ("socket is not always POSIX compliant - " \
  148. "use gnulib module socket for portability"), \
  149. socket (d, t, p))
  150. # endif
  151. # if @GNULIB_CONNECT@
  152. # if @HAVE_WINSOCK2_H@
  153. # undef connect
  154. # define connect rpl_connect
  155. extern int rpl_connect (int, struct sockaddr *, int);
  156. # endif
  157. # elif @HAVE_WINSOCK2_H@
  158. # undef connect
  159. # define connect socket_used_without_requesting_gnulib_module_connect
  160. # elif defined GNULIB_POSIXCHECK
  161. # undef connect
  162. # define connect(s,a,l) \
  163. (GL_LINK_WARNING ("connect is not always POSIX compliant - " \
  164. "use gnulib module connect for portability"), \
  165. connect (s, a, l))
  166. # endif
  167. # if @GNULIB_ACCEPT@
  168. # if @HAVE_WINSOCK2_H@
  169. # undef accept
  170. # define accept rpl_accept
  171. extern int rpl_accept (int, struct sockaddr *, int *);
  172. # endif
  173. # elif @HAVE_WINSOCK2_H@
  174. # undef accept
  175. # define accept accept_used_without_requesting_gnulib_module_accept
  176. # elif defined GNULIB_POSIXCHECK
  177. # undef accept
  178. # define accept(s,a,l) \
  179. (GL_LINK_WARNING ("accept is not always POSIX compliant - " \
  180. "use gnulib module accept for portability"), \
  181. accept (s, a, l))
  182. # endif
  183. # if @GNULIB_BIND@
  184. # if @HAVE_WINSOCK2_H@
  185. # undef bind
  186. # define bind rpl_bind
  187. extern int rpl_bind (int, struct sockaddr *, int);
  188. # endif
  189. # elif @HAVE_WINSOCK2_H@
  190. # undef bind
  191. # define bind bind_used_without_requesting_gnulib_module_bind
  192. # elif defined GNULIB_POSIXCHECK
  193. # undef bind
  194. # define bind(s,a,l) \
  195. (GL_LINK_WARNING ("bind is not always POSIX compliant - " \
  196. "use gnulib module bind for portability"), \
  197. bind (s, a, l))
  198. # endif
  199. # if @GNULIB_GETPEERNAME@
  200. # if @HAVE_WINSOCK2_H@
  201. # undef getpeername
  202. # define getpeername rpl_getpeername
  203. extern int rpl_getpeername (int, struct sockaddr *, int *);
  204. # endif
  205. # elif @HAVE_WINSOCK2_H@
  206. # undef getpeername
  207. # define getpeername getpeername_used_without_requesting_gnulib_module_getpeername
  208. # elif defined GNULIB_POSIXCHECK
  209. # undef getpeername
  210. # define getpeername(s,a,l) \
  211. (GL_LINK_WARNING ("getpeername is not always POSIX compliant - " \
  212. "use gnulib module getpeername for portability"), \
  213. getpeername (s, a, l))
  214. # endif
  215. # if @GNULIB_GETSOCKNAME@
  216. # if @HAVE_WINSOCK2_H@
  217. # undef getsockname
  218. # define getsockname rpl_getsockname
  219. extern int rpl_getsockname (int, struct sockaddr *, int *);
  220. # endif
  221. # elif @HAVE_WINSOCK2_H@
  222. # undef getsockname
  223. # define getsockname getsockname_used_without_requesting_gnulib_module_getsockname
  224. # elif defined GNULIB_POSIXCHECK
  225. # undef getsockname
  226. # define getsockname(s,a,l) \
  227. (GL_LINK_WARNING ("getsockname is not always POSIX compliant - " \
  228. "use gnulib module getsockname for portability"), \
  229. getsockname (s, a, l))
  230. # endif
  231. # if @GNULIB_GETSOCKOPT@
  232. # if @HAVE_WINSOCK2_H@
  233. # undef getsockopt
  234. # define getsockopt rpl_getsockopt
  235. extern int rpl_getsockopt (int, int, int, void *, socklen_t *);
  236. # endif
  237. # elif @HAVE_WINSOCK2_H@
  238. # undef getsockopt
  239. # define getsockopt getsockopt_used_without_requesting_gnulib_module_getsockopt
  240. # elif defined GNULIB_POSIXCHECK
  241. # undef getsockopt
  242. # define getsockopt(s,lvl,o,v,l) \
  243. (GL_LINK_WARNING ("getsockopt is not always POSIX compliant - " \
  244. "use gnulib module getsockopt for portability"), \
  245. getsockopt (s, lvl, o, v, l))
  246. # endif
  247. # if @GNULIB_LISTEN@
  248. # if @HAVE_WINSOCK2_H@
  249. # undef listen
  250. # define listen rpl_listen
  251. extern int rpl_listen (int, int);
  252. # endif
  253. # elif @HAVE_WINSOCK2_H@
  254. # undef listen
  255. # define listen listen_used_without_requesting_gnulib_module_listen
  256. # elif defined GNULIB_POSIXCHECK
  257. # undef listen
  258. # define listen(s,b) \
  259. (GL_LINK_WARNING ("listen is not always POSIX compliant - " \
  260. "use gnulib module listen for portability"), \
  261. listen (s, b))
  262. # endif
  263. # if @GNULIB_RECV@
  264. # if @HAVE_WINSOCK2_H@
  265. # undef recv
  266. # define recv rpl_recv
  267. extern int rpl_recv (int, void *, int, int);
  268. # endif
  269. # elif @HAVE_WINSOCK2_H@
  270. # undef recv
  271. # define recv recv_used_without_requesting_gnulib_module_recv
  272. # elif defined GNULIB_POSIXCHECK
  273. # undef recv
  274. # define recv(s,b,n,f) \
  275. (GL_LINK_WARNING ("recv is not always POSIX compliant - " \
  276. "use gnulib module recv for portability"), \
  277. recv (s, b, n, f))
  278. # endif
  279. # if @GNULIB_SEND@
  280. # if @HAVE_WINSOCK2_H@
  281. # undef send
  282. # define send rpl_send
  283. extern int rpl_send (int, const void *, int, int);
  284. # endif
  285. # elif @HAVE_WINSOCK2_H@
  286. # undef send
  287. # define send send_used_without_requesting_gnulib_module_send
  288. # elif defined GNULIB_POSIXCHECK
  289. # undef send
  290. # define send(s,b,n,f) \
  291. (GL_LINK_WARNING ("send is not always POSIX compliant - " \
  292. "use gnulib module send for portability"), \
  293. send (s, b, n, f))
  294. # endif
  295. # if @GNULIB_RECVFROM@
  296. # if @HAVE_WINSOCK2_H@
  297. # undef recvfrom
  298. # define recvfrom rpl_recvfrom
  299. extern int rpl_recvfrom (int, void *, int, int, struct sockaddr *, int *);
  300. # endif
  301. # elif @HAVE_WINSOCK2_H@
  302. # undef recvfrom
  303. # define recvfrom recvfrom_used_without_requesting_gnulib_module_recvfrom
  304. # elif defined GNULIB_POSIXCHECK
  305. # undef recvfrom
  306. # define recvfrom(s,b,n,f,a,l) \
  307. (GL_LINK_WARNING ("recvfrom is not always POSIX compliant - " \
  308. "use gnulib module recvfrom for portability"), \
  309. recvfrom (s, b, n, f, a, l))
  310. # endif
  311. # if @GNULIB_SENDTO@
  312. # if @HAVE_WINSOCK2_H@
  313. # undef sendto
  314. # define sendto rpl_sendto
  315. extern int rpl_sendto (int, const void *, int, int, struct sockaddr *, int);
  316. # endif
  317. # elif @HAVE_WINSOCK2_H@
  318. # undef sendto
  319. # define sendto sendto_used_without_requesting_gnulib_module_sendto
  320. # elif defined GNULIB_POSIXCHECK
  321. # undef sendto
  322. # define sendto(s,b,n,f,a,l) \
  323. (GL_LINK_WARNING ("sendto is not always POSIX compliant - " \
  324. "use gnulib module sendto for portability"), \
  325. sendto (s, b, n, f, a, l))
  326. # endif
  327. # if @GNULIB_SETSOCKOPT@
  328. # if @HAVE_WINSOCK2_H@
  329. # undef setsockopt
  330. # define setsockopt rpl_setsockopt
  331. extern int rpl_setsockopt (int, int, int, const void *, socklen_t);
  332. # endif
  333. # elif @HAVE_WINSOCK2_H@
  334. # undef setsockopt
  335. # define setsockopt setsockopt_used_without_requesting_gnulib_module_setsockopt
  336. # elif defined GNULIB_POSIXCHECK
  337. # undef setsockopt
  338. # define setsockopt(s,lvl,o,v,l) \
  339. (GL_LINK_WARNING ("setsockopt is not always POSIX compliant - " \
  340. "use gnulib module setsockopt for portability"), \
  341. setsockopt (s, lvl, o, v, l))
  342. # endif
  343. # if @GNULIB_SHUTDOWN@
  344. # if @HAVE_WINSOCK2_H@
  345. # undef shutdown
  346. # define shutdown rpl_shutdown
  347. extern int rpl_shutdown (int, int);
  348. # endif
  349. # elif @HAVE_WINSOCK2_H@
  350. # undef shutdown
  351. # define shutdown shutdown_used_without_requesting_gnulib_module_shutdown
  352. # elif defined GNULIB_POSIXCHECK
  353. # undef shutdown
  354. # define shutdown(s,h) \
  355. (GL_LINK_WARNING ("shutdown is not always POSIX compliant - " \
  356. "use gnulib module shutdown for portability"), \
  357. shutdown (s, h))
  358. # endif
  359. # if @HAVE_WINSOCK2_H@
  360. # undef select
  361. # define select select_used_without_including_sys_select_h
  362. # endif
  363. # ifdef __cplusplus
  364. }
  365. # endif
  366. #endif /* HAVE_SYS_SOCKET_H */
  367. #endif /* _GL_SYS_SOCKET_H */
  368. #endif /* _GL_SYS_SOCKET_H */