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