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 ) == -1 ) {
158 ldap_pvt_set_errno(so_errno);
167 struct sockaddr_in sin;
169 int dummy = sizeof(sin);
170 if ( getpeername( s, (struct sockaddr *) &sin, &dummy ) == -1 ) {
171 /* XXX: needs to be replace with ber_stream_read() */
174 ldap_pvt_set_errno( WSAGetLastError() );
187 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
188 struct sockaddr *sin, socklen_t addrlen,
191 struct timeval tv, *opt_tv=NULL;
192 fd_set wfds, *z=NULL;
197 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
198 tv.tv_usec = opt_tv->tv_usec;
199 tv.tv_sec = opt_tv->tv_sec;
202 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
203 s, opt_tv ? tv.tv_sec : -1L, async);
205 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
208 if ( connect(s, sin, addrlen) == 0 )
210 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
216 ldap_pvt_set_errno( WSAGetLastError() );
219 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
224 if ( async ) return ( -2 );
235 if ( select(ldap_int_tblsize, z, &wfds,
241 opt_tv ? &tv : NULL) == -1)
245 /* This means the connection failed */
246 if (FD_ISSET(s, &efds))
248 ldap_pvt_set_errno(WSAECONNREFUSED);
249 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
250 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
254 if ( FD_ISSET(s, &wfds) ) {
255 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
257 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
261 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
262 ldap_pvt_set_errno( ETIMEDOUT );
266 #ifndef HAVE_INET_ATON
268 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
270 unsigned long u = inet_addr( host );
271 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
281 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
284 unsigned long address, int port, int async)
286 struct sockaddr_in sin;
288 ber_socket_t s = AC_SOCKET_INVALID;
289 int rc, i, use_hp = 0;
290 struct hostent *hp, he_buf;
292 char *ha_buf=NULL, *p, *q;
294 osip_debug(ld, "ldap_connect_to_host\n",0,0,0);
297 #ifdef HAVE_GETADDRINFO
299 struct addrinfo hints, *res, *sai;
301 memset( &hints, '\0', sizeof(hints) );
302 hints.ai_family = AF_UNSPEC;
303 hints.ai_socktype = proto == LDAP_PROTO_UDP
304 ? SOCK_DGRAM : SOCK_STREAM;
306 snprintf(serv, sizeof serv, "%d", ntohs(port));
307 if ( getaddrinfo(host, serv, &hints, &res) ) {
308 osip_debug(ld, "ldap_connect_to_host:getaddrinfo failed\n",0,0,0);
314 /* we assume AF_x and PF_x are equal for all x */
315 s = ldap_int_socket( ld, sai->ai_family,
316 proto == LDAP_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM );
321 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
322 ldap_pvt_close_socket(ld, s);
326 switch (sai->ai_family) {
329 char addr[INET6_ADDRSTRLEN];
331 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
333 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
338 char addr[INET_ADDRSTRLEN];
340 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
342 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
347 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
348 if ( (rc == 0) || (rc == -2) ) {
349 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
352 ldap_pvt_close_socket(ld, s);
353 } while ((sai = sai->ai_next) != NULL);
357 if (! inet_aton( host, &in) ) {
358 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
359 &hp, &local_h_errno);
361 if ( (rc < 0) || (hp == NULL) ) {
363 ldap_pvt_set_errno( WSAGetLastError() );
365 /* not exactly right, but... */
366 ldap_pvt_set_errno( EHOSTUNREACH );
368 if (ha_buf) LDAP_FREE(ha_buf);
378 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
380 s = ldap_int_socket( ld, PF_INET,
381 proto == LDAP_PROTO_UDP ? SOCK_DGRAM : SOCK_STREAM );
383 /* use_hp ? continue : break; */
387 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
388 ldap_pvt_close_socket(ld, s);
392 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
393 sin.sin_family = AF_INET;
395 p = (char *)&sin.sin_addr;
396 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
397 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
399 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
400 inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),0);
402 rc = ldap_pvt_connect(ld, s,
403 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
406 if ( (rc == 0) || (rc == -2) ) {
407 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
411 ldap_pvt_close_socket(ld, s);
416 if (ha_buf) LDAP_FREE(ha_buf);
420 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) \
421 || defined( HAVE_TLS ) || defined( HAVE_CYRUS_SASL )
423 ldap_host_connected_to( Sockbuf *sb )
431 /* buffers for gethostbyaddr_r */
432 struct hostent he_buf;
437 (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
440 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
441 if ( getpeername( sd, &sa, &len ) == -1 ) {
446 * do a reverse lookup on the addr to get the official hostname.
447 * this is necessary for kerberos to work right, since the official
448 * hostname is used as the kerberos instance.
451 switch (sa.sa_family) {
454 return LDAP_STRDUP( ldap_int_hostname );
458 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
459 len = sizeof( struct in6_addr );
463 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
464 len = sizeof( struct in_addr );
467 struct sockaddr_in localhost;
468 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
470 if( memcmp ( &localhost.sin_addr,
471 &((struct sockaddr_in *)&sa)->sin_addr,
472 sizeof(localhost.sin_addr) ) == 0 )
474 return LDAP_STRDUP( ldap_int_hostname );
477 #ifdef INADDR_LOOPBACK
478 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
480 if( memcmp ( &localhost.sin_addr,
481 &((struct sockaddr_in *)&sa)->sin_addr,
482 sizeof(localhost.sin_addr) ) == 0 )
484 return LDAP_STRDUP( ldap_int_hostname );
496 if ((ldap_pvt_gethostbyaddr_a( addr, len,
497 sa.sa_family, &he_buf, &ha_buf,
498 &hp,&local_h_errno ) == 0 ) &&
499 (hp != NULL) && ( hp->h_name != NULL ) )
501 host = LDAP_STRDUP( hp->h_name );
507 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND || HAVE_TLS */
514 fd_set si_use_readfds;
515 fd_set si_use_writefds;
520 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
522 struct selectinfo *sip;
525 sip = (struct selectinfo *)ld->ld_selectinfo;
527 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
528 if ( !FD_ISSET( sd, &sip->si_writefds )) {
529 FD_SET( sd, &sip->si_writefds );
535 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
537 struct selectinfo *sip;
540 sip = (struct selectinfo *)ld->ld_selectinfo;
542 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
543 if ( !FD_ISSET( sd, &sip->si_readfds )) {
544 FD_SET( sd, &sip->si_readfds );
550 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
552 struct selectinfo *sip;
555 sip = (struct selectinfo *)ld->ld_selectinfo;
557 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
558 FD_CLR( sd, &sip->si_writefds );
559 FD_CLR( sd, &sip->si_readfds );
564 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
566 struct selectinfo *sip;
569 sip = (struct selectinfo *)ld->ld_selectinfo;
571 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
572 return( FD_ISSET( sd, &sip->si_use_writefds ));
577 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
579 struct selectinfo *sip;
582 sip = (struct selectinfo *)ld->ld_selectinfo;
584 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
585 return( FD_ISSET( sd, &sip->si_use_readfds ));
590 ldap_new_select_info( void )
592 struct selectinfo *sip;
594 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
595 sizeof( struct selectinfo ))) != NULL ) {
596 FD_ZERO( &sip->si_readfds );
597 FD_ZERO( &sip->si_writefds );
600 return( (void *)sip );
605 ldap_free_select_info( void *sip )
612 ldap_int_ip_init( void )
615 #if defined( HAVE_SYSCONF )
616 tblsize = sysconf( _SC_OPEN_MAX );
617 #elif defined( HAVE_GETDTABLESIZE )
618 tblsize = getdtablesize();
620 tblsize = FD_SETSIZE;
621 #endif /* !USE_SYSCONF */
624 if( tblsize > FD_SETSIZE )
625 tblsize = FD_SETSIZE;
626 #endif /* FD_SETSIZE*/
627 ldap_int_tblsize = tblsize;
632 do_ldap_select( LDAP *ld, struct timeval *timeout )
634 struct selectinfo *sip;
636 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
638 if ( ldap_int_tblsize == 0 )
641 sip = (struct selectinfo *)ld->ld_selectinfo;
642 sip->si_use_readfds = sip->si_readfds;
643 sip->si_use_writefds = sip->si_writefds;
645 return( select( ldap_int_tblsize,
646 &sip->si_use_readfds, &sip->si_use_writefds,