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