3 * Copyright 1998-2002 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 /* XXX non-portable */
33 #endif /* HAVE_IO_H */
36 #include "ldap_defaults.h"
38 /* int ldap_int_tblsize = 0; */
40 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
42 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
46 ldap_pvt_set_errno(int err)
52 ldap_pvt_ndelay_on(LDAP *ld, int fd)
54 oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
55 return ber_pvt_socket_set_nonblock( fd, 1 );
59 ldap_pvt_ndelay_off(LDAP *ld, int fd)
61 oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
62 return ber_pvt_socket_set_nonblock( fd, 0 );
66 ldap_pvt_socket(LDAP *ld)
68 ber_socket_t s = socket(PF_LOCAL, SOCK_STREAM, 0);
69 oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
74 ldap_pvt_close_socket(LDAP *ld, int s)
76 oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
83 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
90 * check the socket for errors after select returned.
93 ldap_pvt_is_socket_ready(LDAP *ld, int s)
95 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
97 #if defined( notyet ) /* && defined( SO_ERROR ) */
100 socklen_t dummy = sizeof(so_errno);
101 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
107 ldap_pvt_set_errno(so_errno);
116 struct sockaddr_un sa;
118 socklen_t dummy = sizeof(sa);
119 if ( getpeername( s, (struct sockaddr *) &sa, &dummy )
122 /* XXX: needs to be replace with ber_stream_read() */
135 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sa, int async)
138 struct timeval tv, *opt_tv=NULL;
139 fd_set wfds, *z=NULL;
141 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
142 tv.tv_usec = opt_tv->tv_usec;
143 tv.tv_sec = opt_tv->tv_sec;
146 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
147 s, opt_tv ? tv.tv_sec : -1L, async);
149 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
152 if ( connect(s, (struct sockaddr *) sa, sizeof(struct sockaddr_un))
155 if ( ldap_pvt_ndelay_off(ld, s) == -1 ) {
161 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
166 if ( async ) return ( -2 );
173 rc = select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL);
174 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
175 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
177 if( rc == AC_SOCKET_ERROR ) return rc;
179 if ( FD_ISSET(s, &wfds) ) {
180 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
182 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
186 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
187 ldap_pvt_set_errno( ETIMEDOUT );
192 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
194 struct sockaddr_un server;
198 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
200 s = ldap_pvt_socket( ld );
201 if ( s == AC_SOCKET_INVALID ) {
205 if ( path == NULL || path[0] == '\0' ) {
208 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
209 ldap_pvt_set_errno( ENAMETOOLONG );
214 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
216 memset( &server, '\0', sizeof(server) );
217 server.sun_family = AF_LOCAL;
218 strcpy( server.sun_path, path );
220 rc = ldap_pvt_connect(ld, s, &server, async);
223 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
225 ldap_pvt_close_socket(ld, s);
231 #endif /* LDAP_PF_LOCAL */