3 * Copyright 1998-2000 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 */
37 /* int ldap_int_tblsize = 0; */
39 #define oslocal_debug(ld,fmt,arg1,arg2,arg3) \
41 ldap_log_printf(ld, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
45 ldap_pvt_set_errno(int err)
51 ldap_pvt_ndelay_on(LDAP *ld, int fd)
53 oslocal_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
54 return ber_pvt_socket_set_nonblock( fd, 1 );
58 ldap_pvt_ndelay_off(LDAP *ld, int fd)
60 oslocal_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
61 return ber_pvt_socket_set_nonblock( fd, 0 );
65 ldap_pvt_socket(LDAP *ld)
67 ber_socket_t s = socket(AF_UNIX, SOCK_STREAM, 0);
68 oslocal_debug(ld, "ldap_new_socket: %d\n",s,0,0);
73 ldap_pvt_close_socket(LDAP *ld, int s)
75 oslocal_debug(ld, "ldap_close_socket: %d\n",s,0,0);
82 "ldap_is_socket_ready: errror on socket %d: errno: %d (%s)\n", \
89 * check the socket for errors after select returned.
92 ldap_pvt_is_socket_ready(LDAP *ld, int s)
94 oslocal_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
96 #if defined( notyet ) /* && defined( SO_ERROR ) */
99 int dummy = sizeof(so_errno);
100 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy ) == -1 ) {
104 ldap_pvt_set_errno(so_errno);
113 struct sockaddr_un sun;
115 int dummy = sizeof(sun);
116 if ( getpeername( s, (struct sockaddr *) &sun, &dummy ) == -1 ) {
117 /* XXX: needs to be replace with ber_stream_read() */
130 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_un *sun, int async)
132 struct timeval tv, *opt_tv=NULL;
133 fd_set wfds, *z=NULL;
135 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
136 tv.tv_usec = opt_tv->tv_usec;
137 tv.tv_sec = opt_tv->tv_sec;
140 oslocal_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
141 s, opt_tv ? tv.tv_sec : -1L, async);
143 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
146 if ( connect(s, (struct sockaddr *) sun, sizeof(struct sockaddr_un)) == 0 )
148 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
153 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
158 if ( async ) return ( -2 );
164 if ( select(ldap_int_tblsize, z, &wfds, z, opt_tv ? &tv : NULL) == -1)
167 if ( FD_ISSET(s, &wfds) ) {
168 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
170 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
174 oslocal_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
175 ldap_pvt_set_errno( ETIMEDOUT );
180 ldap_connect_to_path(LDAP *ld, Sockbuf *sb, const char *path, int async)
182 struct sockaddr_un server;
183 ber_socket_t s = AC_SOCKET_INVALID;
185 char *ha_buf=NULL, *p, *q;
187 oslocal_debug(ld, "ldap_connect_to_path\n",0,0,0);
189 if ( (s = ldap_pvt_socket( ld )) == -1 ) {
193 if ( path == NULL || path[0] == '\0' ) {
194 path = "/tmp/.ldap-sock";
196 if ( strlen(path) > (sizeof( server.sun_path ) - 1) ) {
197 ldap_pvt_set_errno( ENAMETOOLONG );
202 oslocal_debug(ld, "ldap_connect_to_path: Trying %s\n", path, 0, 0);
204 memset( &server, 0, sizeof(server) );
205 server.sun_family = AF_UNIX;
206 strcpy( server.sun_path, path );
208 rc = ldap_pvt_connect(ld, s, &server, async);
211 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, (void *)&s );
213 ldap_pvt_close_socket(ld, s);
219 #endif /* LDAP_PF_LOCAL */