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.
10 * os-ip.c -- platform-specific TCP & UDP related code
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
23 #include <ac/unistd.h>
27 #endif /* HAVE_IO_H */
31 int ldap_int_tblsize = 0;
34 * nonblock connect code
35 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
37 * Copyright 1999, Lars Uffmann, All rights reserved.
38 * This software is not subject to any license of my employer
41 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
43 * Read about the rationale in ldap_connect_timeout:
44 * ftp://koobera.math.uic.edu/www/docs/connect.html.
47 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
49 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
53 ldap_pvt_set_errno(int err)
59 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
63 assert( dest != NULL );
70 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
77 SAFEMEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
84 ldap_pvt_ndelay_on(LDAP *ld, int fd)
86 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
87 return ber_pvt_socket_set_nonblock( fd, 1 );
91 ldap_pvt_ndelay_off(LDAP *ld, int fd)
93 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
94 return ber_pvt_socket_set_nonblock( fd, 0 );
98 ldap_pvt_socket(LDAP *ld)
100 ber_socket_t s = socket(AF_INET, SOCK_STREAM, 0);
101 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
106 ldap_pvt_close_socket(LDAP *ld, int s)
108 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
113 ldap_pvt_prepare_socket(LDAP *ld, int fd)
115 osip_debug(ld, "ldap_prepare_socket: %d\n",fd,0,0);
120 if ( setsockopt( fd, IPPROTO_TCP, TCP_NODELAY,
121 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
123 osip_debug(ld, "ldap_prepare_socket: "
124 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
135 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
138 sock_errstr(errno) ); \
142 * check the socket for errors after select returned.
145 ldap_pvt_is_socket_ready(LDAP *ld, int s)
147 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
149 #if defined( notyet ) /* && defined( SO_ERROR ) */
152 int dummy = sizeof(so_errno);
153 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy ) == -1 ) {
157 ldap_pvt_set_errno(so_errno);
166 struct sockaddr_in sin;
168 int dummy = sizeof(sin);
169 if ( getpeername( s, (struct sockaddr *) &sin, &dummy ) == -1 ) {
170 /* XXX: needs to be replace with ber_stream_read() */
173 ldap_pvt_set_errno( WSAGetLastError() );
186 ldap_pvt_connect(LDAP *ld, ber_socket_t s, struct sockaddr_in *sin, int async)
188 struct timeval tv, *opt_tv=NULL;
189 fd_set wfds, *z=NULL;
194 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
195 tv.tv_usec = opt_tv->tv_usec;
196 tv.tv_sec = opt_tv->tv_sec;
199 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
200 s, opt_tv ? tv.tv_sec : -1L, async);
202 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
205 if ( connect(s, (struct sockaddr *) sin, sizeof(struct sockaddr_in)) == 0 )
207 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
213 ldap_pvt_set_errno( WSAGetLastError() );
216 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
221 if ( async ) return ( -2 );
232 if ( select(ldap_int_tblsize, z, &wfds,
238 opt_tv ? &tv : NULL) == -1)
242 /* This means the connection failed */
243 if (FD_ISSET(s, &efds))
245 ldap_pvt_set_errno(WSAECONNREFUSED);
246 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
247 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
251 if ( FD_ISSET(s, &wfds) ) {
252 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
254 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
258 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
259 ldap_pvt_set_errno( ETIMEDOUT );
263 #ifndef HAVE_INET_ATON
265 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
267 unsigned long u = inet_addr( host );
268 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
278 ldap_connect_to_host(LDAP *ld, Sockbuf *sb, const char *host,
279 unsigned long address, int port, int async)
281 struct sockaddr_in sin;
283 ber_socket_t s = AC_SOCKET_INVALID;
284 int rc, i, use_hp = 0;
285 struct hostent *hp, he_buf;
287 char *ha_buf=NULL, *p, *q;
289 osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
292 if (! inet_aton( host, &in) ) {
293 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
294 &hp, &local_h_errno);
297 ; /*XXX NO MEMORY? */
299 if ( (rc < 0) || (hp == NULL) ) {
301 ldap_pvt_set_errno( WSAGetLastError() );
303 /* not exactly right, but... */
304 ldap_pvt_set_errno( EHOSTUNREACH );
306 if (ha_buf) LDAP_FREE(ha_buf);
315 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
317 if ( (s = ldap_pvt_socket( ld )) == -1 )
318 /* use_hp ? continue : break; */
321 if ( ldap_pvt_prepare_socket(ld, s) == -1 ) {
322 ldap_pvt_close_socket(ld, s);
323 /* use_hp ? continue : break; */
327 (void)memset((char *)&sin, 0, sizeof(struct sockaddr_in));
328 sin.sin_family = AF_INET;
330 p = (char *)&sin.sin_addr;
331 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
332 SAFEMEMCPY(p, q, sizeof(sin.sin_addr) );
334 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
335 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
337 rc = ldap_pvt_connect(ld, s, &sin, async);
339 if ( (rc == 0) || (rc == -2) ) {
340 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
344 ldap_pvt_close_socket(ld, s);
349 if (ha_buf) LDAP_FREE(ha_buf);
353 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || defined( HAVE_TLS ) || defined( HAVE_CYRUS_SASL )
355 ldap_host_connected_to( Sockbuf *sb )
359 struct sockaddr_in sin;
361 /* buffers for gethostbyaddr_r */
362 struct hostent he_buf;
366 #define DO_RETURN(x) if (ha_buf) LDAP_FREE(ha_buf); return (x);
368 (void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
371 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
372 if ( getpeername( sd, (struct sockaddr *)&sin, &len ) == -1 ) {
377 * do a reverse lookup on the addr to get the official hostname.
378 * this is necessary for kerberos to work right, since the official
379 * hostname is used as the kerberos instance.
381 if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
382 sizeof( sin.sin_addr ),
383 AF_INET, &he_buf, &ha_buf,
384 &hp,&local_h_errno ) ==0 ) && (hp != NULL) )
386 if ( hp->h_name != NULL ) {
387 char *host = LDAP_STRDUP( hp->h_name );
396 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND || HAVE_TLS */
403 fd_set si_use_readfds;
404 fd_set si_use_writefds;
409 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
411 struct selectinfo *sip;
414 sip = (struct selectinfo *)ld->ld_selectinfo;
416 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
417 if ( !FD_ISSET( sd, &sip->si_writefds )) {
418 FD_SET( sd, &sip->si_writefds );
424 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
426 struct selectinfo *sip;
429 sip = (struct selectinfo *)ld->ld_selectinfo;
431 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
432 if ( !FD_ISSET( sd, &sip->si_readfds )) {
433 FD_SET( sd, &sip->si_readfds );
439 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
441 struct selectinfo *sip;
444 sip = (struct selectinfo *)ld->ld_selectinfo;
446 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
447 FD_CLR( sd, &sip->si_writefds );
448 FD_CLR( sd, &sip->si_readfds );
453 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
455 struct selectinfo *sip;
458 sip = (struct selectinfo *)ld->ld_selectinfo;
460 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
461 return( FD_ISSET( sd, &sip->si_use_writefds ));
466 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
468 struct selectinfo *sip;
471 sip = (struct selectinfo *)ld->ld_selectinfo;
473 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
474 return( FD_ISSET( sd, &sip->si_use_readfds ));
479 ldap_new_select_info( void )
481 struct selectinfo *sip;
483 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
484 sizeof( struct selectinfo ))) != NULL ) {
485 FD_ZERO( &sip->si_readfds );
486 FD_ZERO( &sip->si_writefds );
489 return( (void *)sip );
494 ldap_free_select_info( void *sip )
501 ldap_int_ip_init( void )
504 #if defined( HAVE_SYSCONF )
505 tblsize = sysconf( _SC_OPEN_MAX );
506 #elif defined( HAVE_GETDTABLESIZE )
507 tblsize = getdtablesize();
509 tblsize = FD_SETSIZE;
510 #endif /* !USE_SYSCONF */
513 if( tblsize > FD_SETSIZE )
514 tblsize = FD_SETSIZE;
515 #endif /* FD_SETSIZE*/
516 ldap_int_tblsize = tblsize;
521 do_ldap_select( LDAP *ld, struct timeval *timeout )
523 struct selectinfo *sip;
525 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
527 if ( ldap_int_tblsize == 0 )
530 sip = (struct selectinfo *)ld->ld_selectinfo;
531 sip->si_use_readfds = sip->si_readfds;
532 sip->si_use_writefds = sip->si_writefds;
534 return( select( ldap_int_tblsize,
535 &sip->si_use_readfds, &sip->si_use_writefds,