3 * Copyright 1998-2003 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;
33 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
35 int ldap_int_inet4or6 = AF_UNSPEC;
37 int ldap_int_inet4or6 = AF_INET;
42 * nonblock connect code
43 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
45 * Copyright 1999, Lars Uffmann, All rights reserved.
46 * This software is not subject to any license of my employer
49 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
51 * Read about the rationale in ldap_connect_timeout:
52 * ftp://koobera.math.uic.edu/www/docs/connect.html.
55 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
57 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
61 ldap_pvt_set_errno(int err)
67 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
71 assert( dest != NULL );
78 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
85 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
92 ldap_pvt_ndelay_on(LDAP *ld, int fd)
94 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
95 return ber_pvt_socket_set_nonblock( fd, 1 );
99 ldap_pvt_ndelay_off(LDAP *ld, int fd)
101 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
102 return ber_pvt_socket_set_nonblock( fd, 0 );
106 ldap_int_socket(LDAP *ld, int family, int type )
108 ber_socket_t s = socket(family, type, 0);
109 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
114 ldap_pvt_close_socket(LDAP *ld, int s)
116 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
121 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
123 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
126 if( proto == LDAP_PROTO_TCP ) {
128 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
129 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
131 osip_debug(ld, "ldap_prepare_socket: "
132 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
146 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
149 sock_errstr(errno) ); \
153 * check the socket for errors after select returned.
156 ldap_pvt_is_socket_ready(LDAP *ld, int s)
158 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
160 #if defined( notyet ) /* && defined( SO_ERROR ) */
163 socklen_t dummy = sizeof(so_errno);
164 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
170 ldap_pvt_set_errno(so_errno);
180 struct sockaddr_storage sin;
182 struct sockaddr_in sin;
185 socklen_t dummy = sizeof(sin);
186 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
189 /* XXX: needs to be replace with ber_stream_read() */
201 #endif /* HAVE_WINSOCK */
204 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
205 struct sockaddr *sin, socklen_t addrlen,
209 struct timeval tv, *opt_tv=NULL;
210 fd_set wfds, *z=NULL;
215 #ifdef LDAP_CONNECTIONLESS
216 /* We could do a connect() but that would interfere with
217 * attempts to poll a broadcast address
219 if (LDAP_IS_UDP(ld)) {
220 if (ld->ld_options.ldo_peer)
221 ldap_memfree(ld->ld_options.ldo_peer);
222 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
223 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
227 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
228 tv.tv_usec = opt_tv->tv_usec;
229 tv.tv_sec = opt_tv->tv_sec;
232 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
233 s, opt_tv ? tv.tv_sec : -1L, async);
235 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
238 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
239 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
245 ldap_pvt_set_errno( WSAGetLastError() );
248 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
253 if ( async ) return ( -2 );
265 rc = select(ldap_int_tblsize, z, &wfds,
271 opt_tv ? &tv : NULL);
272 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
273 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
275 if( rc == AC_SOCKET_ERROR ) return rc;
278 /* This means the connection failed */
279 if ( FD_ISSET(s, &efds) ) {
281 int dummy = sizeof(so_errno);
282 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
283 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
286 so_errno = WSAGetLastError();
288 ldap_pvt_set_errno(so_errno);
289 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
290 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
294 if ( FD_ISSET(s, &wfds) ) {
296 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
299 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
303 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
304 ldap_pvt_set_errno( ETIMEDOUT );
308 #ifndef HAVE_INET_ATON
310 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
312 unsigned long u = inet_addr( host );
313 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
323 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
325 const char *host, int port,
330 ber_socket_t s = AC_SOCKET_INVALID;
332 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
335 struct addrinfo hints, *res, *sai;
339 struct hostent *hp = NULL;
340 struct hostent he_buf;
342 char *ha_buf=NULL, *p, *q;
345 if( host == NULL ) host = "localhost";
348 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
350 "ldap_connect_to_host: TCP %s:%d\n",
353 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
355 "ldap_connect_to_host: UDP %s:%d\n",
359 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
364 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
365 memset( &hints, '\0', sizeof(hints) );
366 hints.ai_family = ldap_int_inet4or6;
367 hints.ai_socktype = socktype;
368 snprintf(serv, sizeof serv, "%d", port );
370 #ifdef LDAP_R_COMPILE
371 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
372 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
375 err = getaddrinfo( host, serv, &hints, &res );
377 #ifdef LDAP_R_COMPILE
378 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
382 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
383 AC_GAI_STRERROR(err), 0, 0);
388 for( sai=res; sai != NULL; sai=sai->ai_next) {
389 if( sai->ai_addr == NULL ) {
390 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
391 "ai_addr is NULL?\n", 0, 0, 0);
395 /* we assume AF_x and PF_x are equal for all x */
396 s = ldap_int_socket( ld, sai->ai_family, socktype );
397 if ( s == AC_SOCKET_INVALID ) {
401 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
402 ldap_pvt_close_socket(ld, s);
406 switch (sai->ai_family) {
409 char addr[INET6_ADDRSTRLEN];
411 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
413 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
418 char addr[INET_ADDRSTRLEN];
420 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
422 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
427 rc = ldap_pvt_connect( ld, s,
428 sai->ai_addr, sai->ai_addrlen, async );
429 if ( (rc == 0) || (rc == -2) ) {
430 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
433 ldap_pvt_close_socket(ld, s);
438 if (! inet_aton( host, &in ) ) {
440 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
441 &hp, &local_h_errno );
443 if ( (rc < 0) || (hp == NULL) ) {
445 ldap_pvt_set_errno( WSAGetLastError() );
447 /* not exactly right, but... */
448 ldap_pvt_set_errno( EHOSTUNREACH );
450 if (ha_buf) LDAP_FREE(ha_buf);
458 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
459 struct sockaddr_in sin;
461 s = ldap_int_socket( ld, PF_INET, socktype );
462 if ( s == AC_SOCKET_INVALID ) {
463 /* use_hp ? continue : break; */
467 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
468 ldap_pvt_close_socket(ld, s);
472 (void)memset((char *)&sin, '\0', sizeof sin);
473 sin.sin_family = AF_INET;
474 sin.sin_port = htons((short) port);
475 p = (char *)&sin.sin_addr;
476 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&in.s_addr;
477 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
479 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
480 inet_ntoa(sin.sin_addr),port,0);
482 rc = ldap_pvt_connect(ld, s,
483 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
486 if ( (rc == 0) || (rc == -2) ) {
487 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
491 ldap_pvt_close_socket(ld, s);
496 if (ha_buf) LDAP_FREE(ha_buf);
502 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
503 defined( HAVE_CYRUS_SASL )
505 ldap_host_connected_to( Sockbuf *sb )
509 struct sockaddr_storage sabuf;
511 struct sockaddr sabuf;
513 struct sockaddr *sa = (struct sockaddr *) &sabuf;
514 char *host = NULL, *herr;
515 char hbuf[NI_MAXHOST];
519 (void)memset( (char *)sa, '\0', sizeof sabuf );
522 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
523 if ( getpeername( sd, sa, &len ) == -1 ) {
528 * do a reverse lookup on the addr to get the official hostname.
529 * this is necessary for kerberos to work right, since the official
530 * hostname is used as the kerberos instance.
533 switch (sa->sa_family) {
536 return LDAP_STRDUP( ldap_int_hostname );
541 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
542 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
543 &localhost, sizeof(localhost)) == 0 )
545 return LDAP_STRDUP( ldap_int_hostname );
552 struct in_addr localhost;
553 localhost.s_addr = htonl( INADDR_ANY );
555 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
556 &localhost, sizeof(localhost) ) == 0 )
558 return LDAP_STRDUP( ldap_int_hostname );
561 #ifdef INADDR_LOOPBACK
562 localhost.s_addr = htonl( INADDR_LOOPBACK );
564 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
565 &localhost, sizeof(localhost) ) == 0 )
567 return LDAP_STRDUP( ldap_int_hostname );
579 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0 &&
582 host = LDAP_STRDUP( hbuf );
594 fd_set si_use_readfds;
595 fd_set si_use_writefds;
600 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
602 struct selectinfo *sip;
605 sip = (struct selectinfo *)ld->ld_selectinfo;
607 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
608 if ( !FD_ISSET( sd, &sip->si_writefds )) {
609 FD_SET( sd, &sip->si_writefds );
615 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
617 struct selectinfo *sip;
620 sip = (struct selectinfo *)ld->ld_selectinfo;
622 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
623 if ( !FD_ISSET( sd, &sip->si_readfds )) {
624 FD_SET( sd, &sip->si_readfds );
630 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
632 struct selectinfo *sip;
635 sip = (struct selectinfo *)ld->ld_selectinfo;
637 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
638 FD_CLR( sd, &sip->si_writefds );
639 FD_CLR( sd, &sip->si_readfds );
644 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
646 struct selectinfo *sip;
649 sip = (struct selectinfo *)ld->ld_selectinfo;
651 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
652 return( FD_ISSET( sd, &sip->si_use_writefds ));
657 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
659 struct selectinfo *sip;
662 sip = (struct selectinfo *)ld->ld_selectinfo;
664 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
665 return( FD_ISSET( sd, &sip->si_use_readfds ));
670 ldap_new_select_info( void )
672 struct selectinfo *sip;
674 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
675 sizeof( struct selectinfo ))) != NULL ) {
676 FD_ZERO( &sip->si_readfds );
677 FD_ZERO( &sip->si_writefds );
680 return( (void *)sip );
685 ldap_free_select_info( void *sip )
692 ldap_int_ip_init( void )
695 #if defined( HAVE_SYSCONF )
696 tblsize = sysconf( _SC_OPEN_MAX );
697 #elif defined( HAVE_GETDTABLESIZE )
698 tblsize = getdtablesize();
700 tblsize = FD_SETSIZE;
701 #endif /* !USE_SYSCONF */
704 if( tblsize > FD_SETSIZE )
705 tblsize = FD_SETSIZE;
706 #endif /* FD_SETSIZE*/
707 ldap_int_tblsize = tblsize;
712 ldap_int_select( LDAP *ld, struct timeval *timeout )
714 struct selectinfo *sip;
717 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
719 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
722 if ( ldap_int_tblsize == 0 )
725 sip = (struct selectinfo *)ld->ld_selectinfo;
726 sip->si_use_readfds = sip->si_readfds;
727 sip->si_use_writefds = sip->si_writefds;
729 return( select( ldap_int_tblsize,
730 &sip->si_use_readfds, &sip->si_use_writefds,