socket.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Generic socket.h */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. #ifndef _AC_SOCKET_H_
  17. #define _AC_SOCKET_H_
  18. #ifdef HAVE_SYS_TYPES_H
  19. #include <sys/types.h>
  20. #endif
  21. #ifdef HAVE_POLL_H
  22. #include <poll.h>
  23. #elif defined(HAVE_SYS_POLL_H)
  24. #include <sys/poll.h>
  25. #endif
  26. #ifdef HAVE_SYS_SOCKET_H
  27. #include <sys/socket.h>
  28. #ifdef HAVE_SYS_UN_H
  29. #include <sys/un.h>
  30. #endif
  31. #ifdef HAVE_SYS_SELECT_H
  32. #include <sys/select.h>
  33. #endif
  34. #include <netinet/in.h>
  35. #ifdef HAVE_NETINET_TCP_H
  36. #include <netinet/tcp.h>
  37. #endif
  38. #ifdef HAVE_ARPA_INET_H
  39. #include <arpa/inet.h>
  40. #endif
  41. #ifdef HAVE_ARPA_NAMESER_H
  42. #include <arpa/nameser.h>
  43. #endif
  44. #include <netdb.h>
  45. #ifdef HAVE_RESOLV_H
  46. #include <resolv.h>
  47. #endif
  48. #endif /* HAVE_SYS_SOCKET_H */
  49. #ifdef HAVE_WINSOCK2
  50. #include <winsock2.h>
  51. #include <ws2tcpip.h>
  52. #elif defined(HAVE_WINSOCK)
  53. #include <winsock.h>
  54. #endif
  55. #ifdef HAVE_PCNFS
  56. #error #include <tklib.h>
  57. #endif /* HAVE_PCNFS */
  58. #ifndef INADDR_LOOPBACK
  59. #define INADDR_LOOPBACK (0x7f000001UL)
  60. #endif
  61. #ifndef MAXHOSTNAMELEN
  62. #define MAXHOSTNAMELEN 64
  63. #endif
  64. #undef sock_errno
  65. #undef sock_errstr
  66. #define sock_errno() errno
  67. #define sock_errstr(e, b, l) AC_STRERROR_R(e, b, l)
  68. #define sock_errset(e) ((void) (errno = (e)))
  69. #ifdef HAVE_WINSOCK
  70. # define tcp_read( s, buf, len ) recv( s, buf, len, 0 )
  71. # define tcp_write( s, buf, len ) send( s, buf, len, 0 )
  72. # define ioctl( s, c, a ) ioctlsocket( (s), (c), (a) )
  73. # define ioctl_t u_long
  74. # define AC_SOCKET_INVALID ((unsigned int) -1)
  75. # ifdef SD_BOTH
  76. # define tcp_close( s ) (shutdown( s, SD_BOTH ), closesocket( s ))
  77. # else
  78. # define tcp_close( s ) closesocket( s )
  79. # endif
  80. #define EWOULDBLOCK WSAEWOULDBLOCK
  81. #define EINPROGRESS WSAEINPROGRESS
  82. #define ETIMEDOUT WSAETIMEDOUT
  83. #undef sock_errno
  84. #undef sock_errstr
  85. #undef sock_errset
  86. #define sock_errno() WSAGetLastError()
  87. #define sock_errstr(e, b, l) ber_pvt_wsa_err2string(e)
  88. #define sock_errset(e) WSASetLastError(e)
  89. LBER_F( char * ) ber_pvt_wsa_err2string LDAP_P((int));
  90. #elif defined(MACOS)
  91. # define tcp_close( s ) tcpclose( s )
  92. # define tcp_read( s, buf, len ) tcpread( s, buf, len )
  93. # define tcp_write( s, buf, len ) tcpwrite( s, buf, len )
  94. #elif defined(HAVE_PCNFS)
  95. # define tcp_close( s ) close( s )
  96. # define tcp_read( s, buf, len ) recv( s, buf, len, 0 )
  97. # define tcp_write( s, buf, len ) send( s, buf, len, 0 )
  98. #elif defined(HAVE_NCSA)
  99. # define tcp_close( s ) do { netclose( s ); netshut() } while(0)
  100. # define tcp_read( s, buf, len ) nread( s, buf, len )
  101. # define tcp_write( s, buf, len ) netwrite( s, buf, len )
  102. #elif defined(HAVE_CLOSESOCKET)
  103. # define tcp_close( s ) closesocket( s )
  104. # ifdef __BEOS__
  105. # define tcp_read( s, buf, len ) recv( s, buf, len, 0 )
  106. # define tcp_write( s, buf, len ) send( s, buf, len, 0 )
  107. # endif
  108. #else
  109. # define tcp_read( s, buf, len) read( s, buf, len )
  110. # define tcp_write( s, buf, len) write( s, buf, len )
  111. # ifdef SHUT_RDWR
  112. # define tcp_close( s ) (shutdown( s, SHUT_RDWR ), close( s ))
  113. # else
  114. # define tcp_close( s ) close( s )
  115. # endif
  116. #ifdef HAVE_PIPE
  117. /*
  118. * Only use pipe() on systems where file and socket descriptors
  119. * are interchangeable
  120. */
  121. # define USE_PIPE HAVE_PIPE
  122. #endif
  123. #endif /* MACOS */
  124. #ifndef ioctl_t
  125. # define ioctl_t int
  126. #endif
  127. #ifndef AC_SOCKET_INVALID
  128. # define AC_SOCKET_INVALID (-1)
  129. #endif
  130. #ifndef AC_SOCKET_ERROR
  131. # define AC_SOCKET_ERROR (-1)
  132. #endif
  133. #if !defined( HAVE_INET_ATON ) && !defined( inet_aton )
  134. # define inet_aton ldap_pvt_inet_aton
  135. struct in_addr;
  136. LDAP_F (int) ldap_pvt_inet_aton LDAP_P(( const char *, struct in_addr * ));
  137. #endif
  138. #if defined(__WIN32) && defined(_ALPHA)
  139. /* NT on Alpha is hosed. */
  140. # define AC_HTONL( l ) \
  141. ((((l)&0xffU)<<24) + (((l)&0xff00U)<<8) + \
  142. (((l)&0xff0000U)>>8) + (((l)&0xff000000U)>>24))
  143. # define AC_NTOHL(l) AC_HTONL(l)
  144. #else
  145. # define AC_HTONL( l ) htonl( l )
  146. # define AC_NTOHL( l ) ntohl( l )
  147. #endif
  148. /* htons()/ntohs() may be broken much like htonl()/ntohl() */
  149. #define AC_HTONS( s ) htons( s )
  150. #define AC_NTOHS( s ) ntohs( s )
  151. #ifdef LDAP_PF_LOCAL
  152. # if !defined( AF_LOCAL ) && defined( AF_UNIX )
  153. # define AF_LOCAL AF_UNIX
  154. # endif
  155. # if !defined( PF_LOCAL ) && defined( PF_UNIX )
  156. # define PF_LOCAL PF_UNIX
  157. # endif
  158. #endif
  159. #ifndef INET_ADDRSTRLEN
  160. # define INET_ADDRSTRLEN 16
  161. #endif
  162. #ifndef INET6_ADDRSTRLEN
  163. # define INET6_ADDRSTRLEN 46
  164. #endif
  165. #if defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO )
  166. # ifdef HAVE_GAI_STRERROR
  167. # define AC_GAI_STRERROR(x) (gai_strerror((x)))
  168. # else
  169. # define AC_GAI_STRERROR(x) (ldap_pvt_gai_strerror((x)))
  170. LDAP_F (char *) ldap_pvt_gai_strerror( int );
  171. # endif
  172. #endif
  173. #if defined(LDAP_PF_LOCAL) && \
  174. !defined(HAVE_GETPEEREID) && \
  175. !defined(HAVE_GETPEERUCRED) && \
  176. !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
  177. defined(HAVE_SENDMSG) && (defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTSLEN) || \
  178. defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL))
  179. # define LDAP_PF_LOCAL_SENDMSG 1
  180. #endif
  181. #ifdef HAVE_GETPEEREID
  182. #define LUTIL_GETPEEREID( s, uid, gid, bv ) getpeereid( s, uid, gid )
  183. #elif defined(LDAP_PF_LOCAL_SENDMSG)
  184. struct berval;
  185. LDAP_LUTIL_F( int ) lutil_getpeereid( int s, uid_t *, gid_t *, struct berval *bv );
  186. #define LUTIL_GETPEEREID( s, uid, gid, bv ) lutil_getpeereid( s, uid, gid, bv )
  187. #else
  188. LDAP_LUTIL_F( int ) lutil_getpeereid( int s, uid_t *, gid_t * );
  189. #define LUTIL_GETPEEREID( s, uid, gid, bv ) lutil_getpeereid( s, uid, gid )
  190. #endif
  191. typedef union Sockaddr {
  192. struct sockaddr sa_addr;
  193. struct sockaddr_in sa_in_addr;
  194. #ifdef LDAP_PF_INET6
  195. struct sockaddr_storage sa_storage;
  196. struct sockaddr_in6 sa_in6_addr;
  197. #endif
  198. #ifdef LDAP_PF_LOCAL
  199. struct sockaddr_un sa_un_addr;
  200. #endif
  201. } Sockaddr;
  202. /* DNS RFC defines max host name as 255. New systems seem to use 1024 */
  203. #ifndef NI_MAXHOST
  204. #define NI_MAXHOST 256
  205. #endif
  206. #ifdef HAVE_POLL
  207. # ifndef INFTIM
  208. # define INFTIM (-1)
  209. # endif
  210. #undef POLL_OTHER
  211. #define POLL_OTHER (POLLERR|POLLHUP)
  212. #undef POLL_READ
  213. #define POLL_READ (POLLIN|POLLPRI|POLL_OTHER)
  214. #undef POLL_WRITE
  215. #define POLL_WRITE (POLLOUT|POLL_OTHER)
  216. #endif
  217. #endif /* _AC_SOCKET_H_ */