1 /* Generic socket.h */ 2 /* $OpenLDAP: pkg/ldap/include/ac/socket.h,v 1.67.2.4 2008/02/11 23:26:40 kurt Exp $ */ 3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 4 * 5 * Copyright 1998-2008 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 17 #ifndef _AC_SOCKET_H_ 18 #define _AC_SOCKET_H_ 19 20 #ifdef HAVE_SYS_TYPES_H 21 #include <sys/types.h> 22 #endif 23 24 #ifdef HAVE_POLL_H 25 #include <poll.h> 26 #elif defined(HAVE_SYS_POLL_H) 27 #include <sys/poll.h> 28 #endif 29 30 #ifdef HAVE_SYS_SOCKET_H 31 #include <sys/socket.h> 32 33 #ifdef HAVE_SYS_UN_H 34 #include <sys/un.h> 35 #endif 36 37 #ifdef HAVE_SYS_SELECT_H 38 #include <sys/select.h> 39 #endif 40 41 #include <netinet/in.h> 42 43 #ifdef HAVE_NETINET_TCP_H 44 #include <netinet/tcp.h> 45 #endif 46 47 #ifdef HAVE_ARPA_INET_H 48 #include <arpa/inet.h> 49 #endif 50 51 #ifdef HAVE_ARPA_NAMESER_H 52 #include <arpa/nameser.h> 53 #endif 54 55 #include <netdb.h> 56 57 #ifdef HAVE_RESOLV_H 58 #include <resolv.h> 59 #endif 60 61 #endif /* HAVE_SYS_SOCKET_H */ 62 63 #ifdef HAVE_WINSOCK2 64 #include <winsock2.h> 65 #elif HAVE_WINSOCK 66 #include <winsock.h> 67 #endif 68 69 #ifdef HAVE_PCNFS 70 #include <tklib.h> 71 #endif /* HAVE_PCNFS */ 72 73 #ifndef INADDR_LOOPBACK 74 #define INADDR_LOOPBACK (0x7f000001UL) 75 #endif 76 77 #ifndef MAXHOSTNAMELEN 78 #define MAXHOSTNAMELEN 64 79 #endif 80 81 #undef sock_errno 82 #undef sock_errstr 83 #define sock_errno() errno 84 #define sock_errstr(e) STRERROR(e) 85 #define sock_errset(e) errno = (e) 86 87 #ifdef HAVE_WINSOCK 88 # define tcp_read( s, buf, len ) recv( s, buf, len, 0 ) 89 # define tcp_write( s, buf, len ) send( s, buf, len, 0 ) 90 # define ioctl( s, c, a ) ioctlsocket( (s), (c), (a) ) 91 # define ioctl_t u_long 92 # define AC_SOCKET_INVALID ((unsigned int) ~0) 93 94 # ifdef SD_BOTH 95 # define tcp_close( s ) (shutdown( s, SD_BOTH ), closesocket( s )) 96 # else 97 # define tcp_close( s ) closesocket( s ) 98 # endif 99 100 #define EWOULDBLOCK WSAEWOULDBLOCK 101 #define EINPROGRESS WSAEINPROGRESS 102 #define ETIMEDOUT WSAETIMEDOUT 103 104 #undef sock_errno 105 #undef sock_errstr 106 #undef sock_errset 107 #define sock_errno() WSAGetLastError() 108 #define sock_errstr(e) ber_pvt_wsa_err2string(e) 109 #define sock_errset(e) WSASetLastError(e) 110 111 LBER_F( char * ) ber_pvt_wsa_err2string LDAP_P((int)); 112 113 #elif MACOS 114 # define tcp_close( s ) tcpclose( s ) 115 # define tcp_read( s, buf, len ) tcpread( s, buf, len ) 116 # define tcp_write( s, buf, len ) tcpwrite( s, buf, len ) 117 118 #elif DOS 119 # ifdef PCNFS 120 # define tcp_close( s ) close( s ) 121 # define tcp_read( s, buf, len ) recv( s, buf, len, 0 ) 122 # define tcp_write( s, buf, len ) send( s, buf, len, 0 ) 123 # endif /* PCNFS */ 124 # ifdef NCSA 125 # define tcp_close( s ) do { netclose( s ); netshut() } while(0) 126 # define tcp_read( s, buf, len ) nread( s, buf, len ) 127 # define tcp_write( s, buf, len ) netwrite( s, buf, len ) 128 # endif /* NCSA */ 129 130 #elif defined(HAVE_CLOSESOCKET) 131 # define tcp_close( s ) closesocket( s ) 132 133 # ifdef __BEOS__ 134 # define tcp_read( s, buf, len ) recv( s, buf, len, 0 ) 135 # define tcp_write( s, buf, len ) send( s, buf, len, 0 ) 136 # endif 137 138 #else 139 # define tcp_read( s, buf, len) read( s, buf, len ) 140 # define tcp_write( s, buf, len) write( s, buf, len ) 141 142 # ifdef SHUT_RDWR 143 # define tcp_close( s ) (shutdown( s, SHUT_RDWR ), close( s )) 144 # else 145 # define tcp_close( s ) close( s ) 146 # endif 147 148 #ifdef HAVE_PIPE 149 /* 150 * Only use pipe() on systems where file and socket descriptors 151 * are interchangable 152 */ 153 # define USE_PIPE HAVE_PIPE 154 #endif 155 156 #endif /* MACOS */ 157 158 #ifndef ioctl_t 159 # define ioctl_t int 160 #endif 161 162 #ifndef AC_SOCKET_INVALID 163 # define AC_SOCKET_INVALID (-1) 164 #endif 165 #ifndef AC_SOCKET_ERROR 166 # define AC_SOCKET_ERROR (-1) 167 #endif 168 169 #if !defined( HAVE_INET_ATON ) && !defined( inet_aton ) 170 # define inet_aton ldap_pvt_inet_aton 171 struct in_addr; 172 LDAP_F (int) ldap_pvt_inet_aton LDAP_P(( const char *, struct in_addr * )); 173 #endif 174 175 #if defined(__WIN32) && defined(_ALPHA) 176 /* NT on Alpha is hosed. */ 177 # define AC_HTONL( l ) \ 178 ((((l)&0xffU)<<24) + (((l)&0xff00U)<<8) + \ 179 (((l)&0xff0000U)>>8) + (((l)&0xff000000U)>>24)) 180 # define AC_NTOHL(l) AC_HTONL(l) 181 182 #else 183 # define AC_HTONL( l ) htonl( l ) 184 # define AC_NTOHL( l ) ntohl( l ) 185 #endif 186 187 /* htons()/ntohs() may be broken much like htonl()/ntohl() */ 188 #define AC_HTONS( s ) htons( s ) 189 #define AC_NTOHS( s ) ntohs( s ) 190 191 #ifdef LDAP_PF_LOCAL 192 # if !defined( AF_LOCAL ) && defined( AF_UNIX ) 193 # define AF_LOCAL AF_UNIX 194 # endif 195 # if !defined( PF_LOCAL ) && defined( PF_UNIX ) 196 # define PF_LOCAL PF_UNIX 197 # endif 198 #endif 199 200 #ifndef INET_ADDRSTRLEN 201 # define INET_ADDRSTRLEN 16 202 #endif 203 #ifndef INET6_ADDRSTRLEN 204 # define INET6_ADDRSTRLEN 46 205 #endif 206 207 #if defined( HAVE_GETADDRINFO ) || defined( HAVE_GETNAMEINFO ) 208 # ifdef HAVE_GAI_STRERROR 209 # define AC_GAI_STRERROR(x) (gai_strerror((x))) 210 # else 211 # define AC_GAI_STRERROR(x) (ldap_pvt_gai_strerror((x))) 212 LDAP_F (char *) ldap_pvt_gai_strerror( int ); 213 # endif 214 #endif 215 216 #if defined(LDAP_PF_LOCAL) && \ 217 !defined(HAVE_GETPEEREID) && \ 218 !defined(HAVE_GETPEERUCRED) && \ 219 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \ 220 defined(HAVE_SENDMSG) && (defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTSLEN) || \ 221 defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)) 222 # define LDAP_PF_LOCAL_SENDMSG 1 223 #endif 224 225 #ifdef HAVE_GETPEEREID 226 #define LUTIL_GETPEEREID( s, uid, gid, bv ) getpeereid( s, uid, gid ) 227 #elif defined(LDAP_PF_LOCAL_SENDMSG) 228 struct berval; 229 LDAP_LUTIL_F( int ) lutil_getpeereid( int s, uid_t *, gid_t *, struct berval *bv ); 230 #define LUTIL_GETPEEREID( s, uid, gid, bv ) lutil_getpeereid( s, uid, gid, bv ) 231 #else 232 LDAP_LUTIL_F( int ) lutil_getpeereid( int s, uid_t *, gid_t * ); 233 #define LUTIL_GETPEEREID( s, uid, gid, bv ) lutil_getpeereid( s, uid, gid ) 234 #endif 235 236 /* DNS RFC defines max host name as 255. New systems seem to use 1024 */ 237 #ifndef NI_MAXHOST 238 #define NI_MAXHOST 256 239 #endif 240 241 #ifdef HAVE_POLL 242 # ifndef INFTIM 243 # define INFTIM (-1) 244 # endif 245 #undef POLL_OTHER 246 #define POLL_OTHER (POLLERR|POLLHUP) 247 #undef POLL_READ 248 #define POLL_READ (POLLIN|POLLPRI|POLL_OTHER) 249 #undef POLL_WRITE 250 #define POLL_WRITE (POLLOUT|POLL_OTHER) 251 #endif 252 253 #endif /* _AC_SOCKET_H_ */ 254