1 /* os-ip.c -- platform-specific TCP & UDP related code */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 The OpenLDAP Foundation.
6 * Portions Copyright 1999 Lars Uffmann.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
17 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
18 * All rights reserved.
20 /* Significant additional contributors include:
28 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
34 #include <ac/unistd.h>
38 #endif /* HAVE_IO_H */
42 int ldap_int_tblsize = 0;
44 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
46 int ldap_int_inet4or6 = AF_UNSPEC;
48 int ldap_int_inet4or6 = AF_INET;
54 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
56 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
61 #define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
63 #endif /* LDAP_DEBUG */
66 ldap_pvt_set_errno(int err)
72 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
76 assert( dest != NULL );
83 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
90 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
97 ldap_pvt_ndelay_on(LDAP *ld, int fd)
99 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
100 return ber_pvt_socket_set_nonblock( fd, 1 );
104 ldap_pvt_ndelay_off(LDAP *ld, int fd)
106 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
107 return ber_pvt_socket_set_nonblock( fd, 0 );
111 ldap_int_socket(LDAP *ld, int family, int type )
113 ber_socket_t s = socket(family, type, 0);
114 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
119 ldap_pvt_close_socket(LDAP *ld, int s)
121 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
126 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
128 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
131 if( proto == LDAP_PROTO_TCP ) {
133 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
134 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
136 osip_debug(ld, "ldap_prepare_socket: "
137 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
151 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
154 sock_errstr(errno) ); \
158 * check the socket for errors after select returned.
161 ldap_pvt_is_socket_ready(LDAP *ld, int s)
163 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
165 #if defined( notyet ) /* && defined( SO_ERROR ) */
168 socklen_t dummy = sizeof(so_errno);
169 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
175 ldap_pvt_set_errno(so_errno);
185 struct sockaddr_storage sin;
187 struct sockaddr_in sin;
190 socklen_t dummy = sizeof(sin);
191 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
194 /* XXX: needs to be replace with ber_stream_read() */
206 #endif /* HAVE_WINSOCK */
209 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
210 struct sockaddr *sin, socklen_t addrlen,
214 struct timeval tv, *opt_tv=NULL;
215 fd_set wfds, *z=NULL;
220 #ifdef LDAP_CONNECTIONLESS
221 /* We could do a connect() but that would interfere with
222 * attempts to poll a broadcast address
224 if (LDAP_IS_UDP(ld)) {
225 if (ld->ld_options.ldo_peer)
226 ldap_memfree(ld->ld_options.ldo_peer);
227 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
228 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
232 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
233 tv.tv_usec = opt_tv->tv_usec;
234 tv.tv_sec = opt_tv->tv_sec;
237 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
238 s, opt_tv ? tv.tv_sec : -1L, async);
240 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
243 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
244 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
250 ldap_pvt_set_errno( WSAGetLastError() );
253 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
258 if ( async ) return ( -2 );
270 rc = select(ldap_int_tblsize, z, &wfds,
276 opt_tv ? &tv : NULL);
277 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
278 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
280 if( rc == AC_SOCKET_ERROR ) return rc;
283 /* This means the connection failed */
284 if ( FD_ISSET(s, &efds) ) {
286 int dummy = sizeof(so_errno);
287 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
288 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
291 so_errno = WSAGetLastError();
293 ldap_pvt_set_errno(so_errno);
294 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
295 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
299 if ( FD_ISSET(s, &wfds) ) {
301 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
304 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
308 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
309 ldap_pvt_set_errno( ETIMEDOUT );
313 #ifndef HAVE_INET_ATON
315 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
317 unsigned long u = inet_addr( host );
318 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
328 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
330 const char *host, int port,
335 ber_socket_t s = AC_SOCKET_INVALID;
337 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
340 struct addrinfo hints, *res, *sai;
344 struct hostent *hp = NULL;
345 struct hostent he_buf;
350 if( host == NULL ) host = "localhost";
353 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
355 "ldap_connect_to_host: TCP %s:%d\n",
358 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
360 "ldap_connect_to_host: UDP %s:%d\n",
364 osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
369 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
370 memset( &hints, '\0', sizeof(hints) );
372 hints.ai_flags = AI_ADDRCONFIG;
374 hints.ai_family = ldap_int_inet4or6;
375 hints.ai_socktype = socktype;
376 snprintf(serv, sizeof serv, "%d", port );
378 #ifdef LDAP_R_COMPILE
379 /* most getaddrinfo(3) use non-threadsafe resolver libraries */
380 ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
383 err = getaddrinfo( host, serv, &hints, &res );
385 #ifdef LDAP_R_COMPILE
386 ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
390 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
391 AC_GAI_STRERROR(err), 0, 0);
396 for( sai=res; sai != NULL; sai=sai->ai_next) {
397 if( sai->ai_addr == NULL ) {
398 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
399 "ai_addr is NULL?\n", 0, 0, 0);
403 /* we assume AF_x and PF_x are equal for all x */
404 s = ldap_int_socket( ld, sai->ai_family, socktype );
405 if ( s == AC_SOCKET_INVALID ) {
409 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
410 ldap_pvt_close_socket(ld, s);
414 switch (sai->ai_family) {
417 char addr[INET6_ADDRSTRLEN];
419 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
421 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
426 char addr[INET_ADDRSTRLEN];
428 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
430 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
435 rc = ldap_pvt_connect( ld, s,
436 sai->ai_addr, sai->ai_addrlen, async );
437 if ( (rc == 0) || (rc == -2) ) {
438 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
441 ldap_pvt_close_socket(ld, s);
446 if (! inet_aton( host, &in ) ) {
448 rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
449 &hp, &local_h_errno );
451 if ( (rc < 0) || (hp == NULL) ) {
453 ldap_pvt_set_errno( WSAGetLastError() );
455 /* not exactly right, but... */
456 ldap_pvt_set_errno( EHOSTUNREACH );
458 if (ha_buf) LDAP_FREE(ha_buf);
466 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
467 struct sockaddr_in sin;
469 s = ldap_int_socket( ld, PF_INET, socktype );
470 if ( s == AC_SOCKET_INVALID ) {
471 /* use_hp ? continue : break; */
475 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
476 ldap_pvt_close_socket(ld, s);
480 (void)memset((char *)&sin, '\0', sizeof sin);
481 sin.sin_family = AF_INET;
482 sin.sin_port = htons((short) port);
485 AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
486 sizeof(sin.sin_addr) );
488 AC_MEMCPY( &sin.sin_addr, &in.s_addr,
489 sizeof(sin.sin_addr) );
492 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
493 inet_ntoa(sin.sin_addr), port, 0);
495 rc = ldap_pvt_connect(ld, s,
496 (struct sockaddr *)&sin, sizeof(sin),
499 if ( (rc == 0) || (rc == -2) ) {
500 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
504 ldap_pvt_close_socket(ld, s);
508 if (ha_buf) LDAP_FREE(ha_buf);
514 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
515 defined( HAVE_CYRUS_SASL )
517 ldap_host_connected_to( Sockbuf *sb, const char *host )
521 struct sockaddr_storage sabuf;
523 struct sockaddr sabuf;
525 struct sockaddr *sa = (struct sockaddr *) &sabuf;
529 (void)memset( (char *)sa, '\0', sizeof sabuf );
532 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
533 if ( getpeername( sd, sa, &len ) == -1 ) {
538 * do a reverse lookup on the addr to get the official hostname.
539 * this is necessary for kerberos to work right, since the official
540 * hostname is used as the kerberos instance.
543 switch (sa->sa_family) {
546 return LDAP_STRDUP( ldap_int_hostname );
551 struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
552 if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
553 &localhost, sizeof(localhost)) == 0 )
555 return LDAP_STRDUP( ldap_int_hostname );
562 struct in_addr localhost;
563 localhost.s_addr = htonl( INADDR_ANY );
565 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
566 &localhost, sizeof(localhost) ) == 0 )
568 return LDAP_STRDUP( ldap_int_hostname );
571 #ifdef INADDR_LOOPBACK
572 localhost.s_addr = htonl( INADDR_LOOPBACK );
574 if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
575 &localhost, sizeof(localhost) ) == 0 )
577 return LDAP_STRDUP( ldap_int_hostname );
591 char hbuf[NI_MAXHOST];
594 if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
597 return LDAP_STRDUP( hbuf );
602 return host ? LDAP_STRDUP( host ) : NULL;
611 fd_set si_use_readfds;
612 fd_set si_use_writefds;
617 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
619 struct selectinfo *sip;
622 sip = (struct selectinfo *)ld->ld_selectinfo;
624 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
625 if ( !FD_ISSET( sd, &sip->si_writefds )) {
626 FD_SET( sd, &sip->si_writefds );
632 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
634 struct selectinfo *sip;
637 sip = (struct selectinfo *)ld->ld_selectinfo;
639 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
640 if ( !FD_ISSET( sd, &sip->si_readfds )) {
641 FD_SET( sd, &sip->si_readfds );
647 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
649 struct selectinfo *sip;
652 sip = (struct selectinfo *)ld->ld_selectinfo;
654 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
655 FD_CLR( sd, &sip->si_writefds );
656 FD_CLR( sd, &sip->si_readfds );
661 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
663 struct selectinfo *sip;
666 sip = (struct selectinfo *)ld->ld_selectinfo;
668 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
669 return( FD_ISSET( sd, &sip->si_use_writefds ));
674 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
676 struct selectinfo *sip;
679 sip = (struct selectinfo *)ld->ld_selectinfo;
681 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
682 return( FD_ISSET( sd, &sip->si_use_readfds ));
687 ldap_new_select_info( void )
689 struct selectinfo *sip;
691 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
692 sizeof( struct selectinfo ))) != NULL ) {
693 FD_ZERO( &sip->si_readfds );
694 FD_ZERO( &sip->si_writefds );
697 return( (void *)sip );
702 ldap_free_select_info( void *sip )
709 ldap_int_ip_init( void )
712 #if defined( HAVE_SYSCONF )
713 tblsize = sysconf( _SC_OPEN_MAX );
714 #elif defined( HAVE_GETDTABLESIZE )
715 tblsize = getdtablesize();
717 tblsize = FD_SETSIZE;
718 #endif /* !USE_SYSCONF */
721 if( tblsize > FD_SETSIZE )
722 tblsize = FD_SETSIZE;
723 #endif /* FD_SETSIZE*/
724 ldap_int_tblsize = tblsize;
729 ldap_int_select( LDAP *ld, struct timeval *timeout )
731 struct selectinfo *sip;
734 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
736 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
739 if ( ldap_int_tblsize == 0 )
742 sip = (struct selectinfo *)ld->ld_selectinfo;
743 sip->si_use_readfds = sip->si_readfds;
744 sip->si_use_writefds = sip->si_writefds;
746 return( select( ldap_int_tblsize,
747 &sip->si_use_readfds, &sip->si_use_writefds,