3 * Copyright 1998-2002 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 )
34 int ldap_int_inet4or6 = AF_UNSPEC;
38 * nonblock connect code
39 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
41 * Copyright 1999, Lars Uffmann, All rights reserved.
42 * This software is not subject to any license of my employer
45 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
47 * Read about the rationale in ldap_connect_timeout:
48 * ftp://koobera.math.uic.edu/www/docs/connect.html.
51 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
53 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
57 ldap_pvt_set_errno(int err)
63 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
67 assert( dest != NULL );
74 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
81 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
88 ldap_pvt_ndelay_on(LDAP *ld, int fd)
90 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
91 return ber_pvt_socket_set_nonblock( fd, 1 );
95 ldap_pvt_ndelay_off(LDAP *ld, int fd)
97 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
98 return ber_pvt_socket_set_nonblock( fd, 0 );
102 ldap_int_socket(LDAP *ld, int family, int type )
104 ber_socket_t s = socket(family, type, 0);
105 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
110 ldap_pvt_close_socket(LDAP *ld, int s)
112 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
117 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
119 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
122 if( proto == LDAP_PROTO_TCP ) {
124 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
125 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
127 osip_debug(ld, "ldap_prepare_socket: "
128 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
142 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
145 sock_errstr(errno) ); \
149 * check the socket for errors after select returned.
152 ldap_pvt_is_socket_ready(LDAP *ld, int s)
154 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
156 #if defined( notyet ) /* && defined( SO_ERROR ) */
159 socklen_t dummy = sizeof(so_errno);
160 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
166 ldap_pvt_set_errno(so_errno);
175 struct sockaddr_in sin;
177 socklen_t dummy = sizeof(sin);
178 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
181 /* XXX: needs to be replace with ber_stream_read() */
193 #endif /* HAVE_WINSOCK */
196 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
197 struct sockaddr *sin, socklen_t addrlen,
200 struct timeval tv, *opt_tv=NULL;
201 fd_set wfds, *z=NULL;
206 #ifdef LDAP_CONNECTIONLESS
207 /* We could do a connect() but that would interfere with
208 * attempts to poll a broadcast address
210 if (LDAP_IS_UDP(ld)) {
211 if (ld->ld_options.ldo_peer)
212 ldap_memfree(ld->ld_options.ldo_peer);
213 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
214 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
218 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
219 tv.tv_usec = opt_tv->tv_usec;
220 tv.tv_sec = opt_tv->tv_sec;
223 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
224 s, opt_tv ? tv.tv_sec : -1L, async);
226 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
229 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
231 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
237 ldap_pvt_set_errno( WSAGetLastError() );
240 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
245 if ( async ) return ( -2 );
256 if ( select(ldap_int_tblsize, z, &wfds,
262 opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
268 /* This means the connection failed */
269 if ( FD_ISSET(s, &efds) ) {
271 int dummy = sizeof(so_errno);
272 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
273 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
276 so_errno = WSAGetLastError();
278 ldap_pvt_set_errno(so_errno);
279 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
280 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
284 if ( FD_ISSET(s, &wfds) ) {
286 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
289 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
293 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
294 ldap_pvt_set_errno( ETIMEDOUT );
298 #ifndef HAVE_INET_ATON
300 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
302 unsigned long u = inet_addr( host );
303 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
313 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
316 unsigned long address, int port, int async )
318 struct sockaddr_in sin;
319 ber_socket_t s = AC_SOCKET_INVALID;
320 int rc, i, use_hp = 0;
321 struct hostent *hp = NULL;
322 char *ha_buf=NULL, *p, *q;
327 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
328 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
330 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
331 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
335 osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
341 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
344 struct addrinfo hints, *res, *sai;
346 memset( &hints, '\0', sizeof(hints) );
347 hints.ai_family = ldap_int_inet4or6;
348 hints.ai_socktype = socktype;
350 snprintf(serv, sizeof serv, "%d", port );
351 if ( ( err = getaddrinfo(host, serv, &hints, &res) ) ) {
352 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
353 AC_GAI_STRERROR(err), 0, 0);
358 for( sai=res; sai != NULL; sai=sai->ai_next) {
359 if( sai->ai_addr == NULL ) {
360 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
361 "ai_addr is NULL?\n", 0, 0, 0);
365 /* we assume AF_x and PF_x are equal for all x */
366 s = ldap_int_socket( ld, sai->ai_family, socktype );
367 if ( s == AC_SOCKET_INVALID ) {
371 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
372 ldap_pvt_close_socket(ld, s);
376 switch (sai->ai_family) {
379 char addr[INET6_ADDRSTRLEN];
381 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
383 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
388 char addr[INET_ADDRSTRLEN];
390 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
392 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
397 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
398 if ( (rc == 0) || (rc == -2) ) {
399 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
402 ldap_pvt_close_socket(ld, s);
409 if (! inet_aton( host, &in) ) {
411 struct hostent he_buf;
412 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
413 &hp, &local_h_errno);
415 if ( (rc < 0) || (hp == NULL) ) {
417 ldap_pvt_set_errno( WSAGetLastError() );
419 /* not exactly right, but... */
420 ldap_pvt_set_errno( EHOSTUNREACH );
422 if (ha_buf) LDAP_FREE(ha_buf);
432 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
433 s = ldap_int_socket( ld, PF_INET, socktype );
434 if ( s == AC_SOCKET_INVALID ) {
435 /* use_hp ? continue : break; */
439 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
440 ldap_pvt_close_socket(ld, s);
444 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
445 sin.sin_family = AF_INET;
446 sin.sin_port = htons((short) port);
447 p = (char *)&sin.sin_addr;
448 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
449 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
451 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
452 inet_ntoa(sin.sin_addr),port,0);
454 rc = ldap_pvt_connect(ld, s,
455 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
458 if ( (rc == 0) || (rc == -2) ) {
459 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
463 ldap_pvt_close_socket(ld, s);
468 if (ha_buf) LDAP_FREE(ha_buf);
472 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
473 defined( HAVE_CYRUS_SASL )
475 ldap_host_connected_to( Sockbuf *sb )
483 /* buffers for gethostbyaddr_r */
484 struct hostent he_buf;
489 (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
492 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
493 if ( getpeername( sd, &sa, &len ) == -1 ) {
498 * do a reverse lookup on the addr to get the official hostname.
499 * this is necessary for kerberos to work right, since the official
500 * hostname is used as the kerberos instance.
503 switch (sa.sa_family) {
506 return LDAP_STRDUP( ldap_int_hostname );
510 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
511 len = sizeof( struct in6_addr );
515 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
516 len = sizeof( struct in_addr );
519 struct sockaddr_in localhost;
520 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
522 if( memcmp ( &localhost.sin_addr,
523 &((struct sockaddr_in *)&sa)->sin_addr,
524 sizeof(localhost.sin_addr) ) == 0 )
526 return LDAP_STRDUP( ldap_int_hostname );
529 #ifdef INADDR_LOOPBACK
530 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
532 if( memcmp ( &localhost.sin_addr,
533 &((struct sockaddr_in *)&sa)->sin_addr,
534 sizeof(localhost.sin_addr) ) == 0 )
536 return LDAP_STRDUP( ldap_int_hostname );
548 if ((ldap_pvt_gethostbyaddr_a( addr, len,
549 sa.sa_family, &he_buf, &ha_buf,
550 &hp,&local_h_errno ) == 0 ) &&
551 (hp != NULL) && ( hp->h_name != NULL ) )
553 host = LDAP_STRDUP( hp->h_name );
566 fd_set si_use_readfds;
567 fd_set si_use_writefds;
572 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
574 struct selectinfo *sip;
577 sip = (struct selectinfo *)ld->ld_selectinfo;
579 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
580 if ( !FD_ISSET( sd, &sip->si_writefds )) {
581 FD_SET( sd, &sip->si_writefds );
587 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
589 struct selectinfo *sip;
592 sip = (struct selectinfo *)ld->ld_selectinfo;
594 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
595 if ( !FD_ISSET( sd, &sip->si_readfds )) {
596 FD_SET( sd, &sip->si_readfds );
602 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
604 struct selectinfo *sip;
607 sip = (struct selectinfo *)ld->ld_selectinfo;
609 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
610 FD_CLR( sd, &sip->si_writefds );
611 FD_CLR( sd, &sip->si_readfds );
616 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
618 struct selectinfo *sip;
621 sip = (struct selectinfo *)ld->ld_selectinfo;
623 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
624 return( FD_ISSET( sd, &sip->si_use_writefds ));
629 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
631 struct selectinfo *sip;
634 sip = (struct selectinfo *)ld->ld_selectinfo;
636 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
637 return( FD_ISSET( sd, &sip->si_use_readfds ));
642 ldap_new_select_info( void )
644 struct selectinfo *sip;
646 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
647 sizeof( struct selectinfo ))) != NULL ) {
648 FD_ZERO( &sip->si_readfds );
649 FD_ZERO( &sip->si_writefds );
652 return( (void *)sip );
657 ldap_free_select_info( void *sip )
664 ldap_int_ip_init( void )
667 #if defined( HAVE_SYSCONF )
668 tblsize = sysconf( _SC_OPEN_MAX );
669 #elif defined( HAVE_GETDTABLESIZE )
670 tblsize = getdtablesize();
672 tblsize = FD_SETSIZE;
673 #endif /* !USE_SYSCONF */
676 if( tblsize > FD_SETSIZE )
677 tblsize = FD_SETSIZE;
678 #endif /* FD_SETSIZE*/
679 ldap_int_tblsize = tblsize;
684 ldap_int_select( LDAP *ld, struct timeval *timeout )
686 struct selectinfo *sip;
689 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
691 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
694 if ( ldap_int_tblsize == 0 )
697 sip = (struct selectinfo *)ld->ld_selectinfo;
698 sip->si_use_readfds = sip->si_readfds;
699 sip->si_use_writefds = sip->si_writefds;
701 return( select( ldap_int_tblsize,
702 &sip->si_use_readfds, &sip->si_use_writefds,