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 AC_MEMCPY( (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_int_socket(LDAP *ld, int family, int type )
100 ber_socket_t s = socket(family, type, 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_int_prepare_socket(LDAP *ld, int s, int proto )
115 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
118 if( proto == LDAP_PROTO_TCP ) {
120 if ( setsockopt( s, 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",
136 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
139 sock_errstr(errno) ); \
143 * check the socket for errors after select returned.
146 ldap_pvt_is_socket_ready(LDAP *ld, int s)
148 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
150 #if defined( notyet ) /* && defined( SO_ERROR ) */
153 int dummy = sizeof(so_errno);
154 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
160 ldap_pvt_set_errno(so_errno);
169 struct sockaddr_in sin;
171 int dummy = sizeof(sin);
172 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
175 /* XXX: needs to be replace with ber_stream_read() */
178 ldap_pvt_set_errno( WSAGetLastError() );
191 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
192 struct sockaddr *sin, socklen_t addrlen,
195 struct timeval tv, *opt_tv=NULL;
196 fd_set wfds, *z=NULL;
201 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
202 tv.tv_usec = opt_tv->tv_usec;
203 tv.tv_sec = opt_tv->tv_sec;
206 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
207 s, opt_tv ? tv.tv_sec : -1L, async);
209 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
212 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
214 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
220 ldap_pvt_set_errno( WSAGetLastError() );
223 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
228 if ( async ) return ( -2 );
239 if ( select(ldap_int_tblsize, z, &wfds,
245 opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
251 /* This means the connection failed */
252 if ( FD_ISSET(s, &efds) ) {
253 ldap_pvt_set_errno(WSAECONNREFUSED);
254 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
255 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
259 if ( FD_ISSET(s, &wfds) ) {
260 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
262 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
266 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
267 ldap_pvt_set_errno( ETIMEDOUT );
271 #ifndef HAVE_INET_ATON
273 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
275 unsigned long u = inet_addr( host );
276 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
286 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
289 unsigned long address, int port, int async)
291 struct sockaddr_in sin;
292 ber_socket_t s = AC_SOCKET_INVALID;
293 int rc, i, use_hp = 0;
294 struct hostent *hp = NULL;
295 char *ha_buf=NULL, *p, *q;
297 osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
300 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
303 struct addrinfo hints, *res, *sai;
305 memset( &hints, '\0', sizeof(hints) );
306 hints.ai_family = AF_UNSPEC;
307 hints.ai_socktype = SOCK_STREAM;
309 snprintf(serv, sizeof serv, "%d", ntohs(port));
310 if ( err = getaddrinfo(host, serv, &hints, &res) ) {
311 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
312 AC_GAI_STRERROR(err), 0, 0);
318 /* we assume AF_x and PF_x are equal for all x */
319 s = ldap_int_socket( ld, sai->ai_family, SOCK_STREAM );
320 if ( s == AC_SOCKET_INVALID ) {
324 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
325 ldap_pvt_close_socket(ld, s);
329 switch (sai->ai_family) {
332 char addr[INET6_ADDRSTRLEN];
334 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
336 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
341 char addr[INET_ADDRSTRLEN];
343 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
345 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
350 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
351 if ( (rc == 0) || (rc == -2) ) {
352 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
355 ldap_pvt_close_socket(ld, s);
356 } while ((sai = sai->ai_next) != NULL);
362 if (! inet_aton( host, &in) ) {
364 struct hostent he_buf;
365 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
366 &hp, &local_h_errno);
368 if ( (rc < 0) || (hp == NULL) ) {
370 ldap_pvt_set_errno( WSAGetLastError() );
372 /* not exactly right, but... */
373 ldap_pvt_set_errno( EHOSTUNREACH );
375 if (ha_buf) LDAP_FREE(ha_buf);
385 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
387 s = ldap_int_socket( ld, PF_INET, SOCK_STREAM );
388 if ( s == AC_SOCKET_INVALID ) {
389 /* use_hp ? continue : break; */
393 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
394 ldap_pvt_close_socket(ld, s);
398 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
399 sin.sin_family = AF_INET;
401 p = (char *)&sin.sin_addr;
402 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
403 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
405 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
406 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
408 rc = ldap_pvt_connect(ld, s,
409 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
412 if ( (rc == 0) || (rc == -2) ) {
413 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
417 ldap_pvt_close_socket(ld, s);
422 if (ha_buf) LDAP_FREE(ha_buf);
426 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) \
427 || defined( HAVE_TLS ) || defined( HAVE_CYRUS_SASL )
429 ldap_host_connected_to( Sockbuf *sb )
437 /* buffers for gethostbyaddr_r */
438 struct hostent he_buf;
443 (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
446 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
447 if ( getpeername( sd, &sa, &len ) == -1 ) {
452 * do a reverse lookup on the addr to get the official hostname.
453 * this is necessary for kerberos to work right, since the official
454 * hostname is used as the kerberos instance.
457 switch (sa.sa_family) {
460 return LDAP_STRDUP( ldap_int_hostname );
464 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
465 len = sizeof( struct in6_addr );
469 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
470 len = sizeof( struct in_addr );
473 struct sockaddr_in localhost;
474 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
476 if( memcmp ( &localhost.sin_addr,
477 &((struct sockaddr_in *)&sa)->sin_addr,
478 sizeof(localhost.sin_addr) ) == 0 )
480 return LDAP_STRDUP( ldap_int_hostname );
483 #ifdef INADDR_LOOPBACK
484 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
486 if( memcmp ( &localhost.sin_addr,
487 &((struct sockaddr_in *)&sa)->sin_addr,
488 sizeof(localhost.sin_addr) ) == 0 )
490 return LDAP_STRDUP( ldap_int_hostname );
502 if ((ldap_pvt_gethostbyaddr_a( addr, len,
503 sa.sa_family, &he_buf, &ha_buf,
504 &hp,&local_h_errno ) == 0 ) &&
505 (hp != NULL) && ( hp->h_name != NULL ) )
507 host = LDAP_STRDUP( hp->h_name );
513 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND || HAVE_TLS */
520 fd_set si_use_readfds;
521 fd_set si_use_writefds;
526 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
528 struct selectinfo *sip;
531 sip = (struct selectinfo *)ld->ld_selectinfo;
533 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
534 if ( !FD_ISSET( sd, &sip->si_writefds )) {
535 FD_SET( sd, &sip->si_writefds );
541 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
543 struct selectinfo *sip;
546 sip = (struct selectinfo *)ld->ld_selectinfo;
548 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
549 if ( !FD_ISSET( sd, &sip->si_readfds )) {
550 FD_SET( sd, &sip->si_readfds );
556 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
558 struct selectinfo *sip;
561 sip = (struct selectinfo *)ld->ld_selectinfo;
563 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
564 FD_CLR( sd, &sip->si_writefds );
565 FD_CLR( sd, &sip->si_readfds );
570 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
572 struct selectinfo *sip;
575 sip = (struct selectinfo *)ld->ld_selectinfo;
577 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
578 return( FD_ISSET( sd, &sip->si_use_writefds ));
583 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
585 struct selectinfo *sip;
588 sip = (struct selectinfo *)ld->ld_selectinfo;
590 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
591 return( FD_ISSET( sd, &sip->si_use_readfds ));
596 ldap_new_select_info( void )
598 struct selectinfo *sip;
600 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
601 sizeof( struct selectinfo ))) != NULL ) {
602 FD_ZERO( &sip->si_readfds );
603 FD_ZERO( &sip->si_writefds );
606 return( (void *)sip );
611 ldap_free_select_info( void *sip )
618 ldap_int_ip_init( void )
621 #if defined( HAVE_SYSCONF )
622 tblsize = sysconf( _SC_OPEN_MAX );
623 #elif defined( HAVE_GETDTABLESIZE )
624 tblsize = getdtablesize();
626 tblsize = FD_SETSIZE;
627 #endif /* !USE_SYSCONF */
630 if( tblsize > FD_SETSIZE )
631 tblsize = FD_SETSIZE;
632 #endif /* FD_SETSIZE*/
633 ldap_int_tblsize = tblsize;
638 do_ldap_select( LDAP *ld, struct timeval *timeout )
640 struct selectinfo *sip;
642 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
644 if ( ldap_int_tblsize == 0 )
647 sip = (struct selectinfo *)ld->ld_selectinfo;
648 sip->si_use_readfds = sip->si_readfds;
649 sip->si_use_writefds = sip->si_writefds;
651 return( select( ldap_int_tblsize,
652 &sip->si_use_readfds, &sip->si_use_writefds,