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