3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
9 * Copyright (c) 1999 PADL Software Pty Ltd.
10 * os-ip.c -- platform-specific domain socket code
20 #include <ac/stdlib.h>
23 #include <ac/socket.h>
24 #include <ac/string.h>
26 #include <ac/unistd.h>
28 #ifdef HAVE_SYS_STAT_H
37 #endif /* HAVE_IO_H */
40 #include "ldap_defaults.h"
42 /* int ldap_int_tblsize = 0; */
44 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
46 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
50 ldap_pvt_set_errno(int err)
56 ldap_pvt_ndelay_on(LDAP *ld, int fd)
58 oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
59 return ber_pvt_socket_set_nonblock( fd, 1 );
63 ldap_pvt_ndelay_off(LDAP *ld, int fd)
65 oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
66 return ber_pvt_socket_set_nonblock( fd, 0 );
70 ldap_pvt_socket(LDAP *ld)
72 ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
73 oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
78 ldap_pvt_close_socket(LDAP *ld, int s)
80 oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
87 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
94 * check the socket for errors after select returned.
97 ldap_pvt_is_socket_ready(LDAP *ld, int s)
99 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
101 #if defined( notyet ) /* && defined( SO_ERROR ) */
104 socklen_t dummy = sizeof(so_errno);
105 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
111 ldap_pvt_set_errno(so_errno);
120 struct sockaddr_un sa;
122 socklen_t dummy = sizeof(sa);
123 if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
126 /* XXX: needs to be replace with ber_stream_read() */
138 #if !defined(HAVE_GETPEEREID) && \
139 !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
140 defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
142 static const char abandonPDU[] = {LDAP_TAG_MESSAGE, 6,
143 LDAP_TAG_MSGID, 1, 0, LDAP_REQ_ABANDON, 1, 0};
147 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
150 struct timeval tv, *opt_tv=NULL;
151 fd_set wfds, *z=NULL;
153 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
154 tv.tv_usec = opt_tv->tv_usec;
155 tv.tv_sec = opt_tv->tv_sec;
158 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
159 s, opt_tv ? tv.tv_sec : -1L, async);
161 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
164 if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
167 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
171 /* Send a dummy message with access rights. Remote side will
172 * obtain our uid/gid by fstat'ing this descriptor.
177 if (pipe(fds) == 0) {
178 /* Abandon, noop, has no reply */
179 struct iovec iov = {abandonPDU, sizeof(abandonPDU)};
180 struct msghdr msg = {0};
183 msg.msg_accrights = (char *)fds;
184 msg.msg_accrightslen = sizeof(int);
185 sendmsg( s, &msg, 0 );
194 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
199 if ( async ) return ( -2 );
206 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
207 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
208 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
210 if( rc == AC_SOCKET_ERROR ) return rc;
212 if ( FD_ISSET(s, &wfds) ) {
213 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
215 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
223 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
224 ldap_pvt_set_errno( ETIMEDOUT );
229 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
231 struct sockaddr_un server;
235 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
237 s = ldap_pvt_socket( ld );
238 if ( s == AC_SOCKET_INVALID ) {
242 if ( path == NULL || path[0] == '\0' ) {
245 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
246 ldap_pvt_set_errno( ENAMETOOLONG );
251 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
253 memset( &server, '\0', sizeof(server) );
254 server.sun_family = AF_LOCAL;
255 strcpy( server.sun_path, path );
257 rc = ldap_pvt_connect(ld, s, &server, async);
260 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
262 ldap_pvt_close_socket(ld, s);
268 #endif /* LDAP_PF_LOCAL */