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 )
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);
179 struct sockaddr_in sin;
181 socklen_t dummy = sizeof(sin);
182 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
185 /* XXX: needs to be replace with ber_stream_read() */
197 #endif /* HAVE_WINSOCK */
200 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
201 struct sockaddr *sin, socklen_t addrlen,
204 struct timeval tv, *opt_tv=NULL;
205 fd_set wfds, *z=NULL;
210 #ifdef LDAP_CONNECTIONLESS
211 /* We could do a connect() but that would interfere with
212 * attempts to poll a broadcast address
214 if (LDAP_IS_UDP(ld)) {
215 if (ld->ld_options.ldo_peer)
216 ldap_memfree(ld->ld_options.ldo_peer);
217 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
218 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
222 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
223 tv.tv_usec = opt_tv->tv_usec;
224 tv.tv_sec = opt_tv->tv_sec;
227 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
228 s, opt_tv ? tv.tv_sec : -1L, async);
230 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
233 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
235 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
241 ldap_pvt_set_errno( WSAGetLastError() );
244 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
249 if ( async ) return ( -2 );
260 if ( select(ldap_int_tblsize, z, &wfds,
266 opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
272 /* This means the connection failed */
273 if ( FD_ISSET(s, &efds) ) {
275 int dummy = sizeof(so_errno);
276 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
277 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
280 so_errno = WSAGetLastError();
282 ldap_pvt_set_errno(so_errno);
283 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
284 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
288 if ( FD_ISSET(s, &wfds) ) {
290 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
293 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
297 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
298 ldap_pvt_set_errno( ETIMEDOUT );
302 #ifndef HAVE_INET_ATON
304 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
306 unsigned long u = inet_addr( host );
307 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
317 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
320 unsigned long address, int port, int async )
322 struct sockaddr_in sin;
323 ber_socket_t s = AC_SOCKET_INVALID;
324 int rc, i, use_hp = 0;
325 struct hostent *hp = NULL;
326 char *ha_buf=NULL, *p, *q;
331 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
332 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
334 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
335 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
339 osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
345 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
348 struct addrinfo hints, *res, *sai;
350 memset( &hints, '\0', sizeof(hints) );
351 hints.ai_family = ldap_int_inet4or6;
352 hints.ai_socktype = socktype;
354 snprintf(serv, sizeof serv, "%d", port );
355 if ( ( err = getaddrinfo(host, serv, &hints, &res) ) ) {
356 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
357 AC_GAI_STRERROR(err), 0, 0);
362 for( sai=res; sai != NULL; sai=sai->ai_next) {
363 if( sai->ai_addr == NULL ) {
364 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
365 "ai_addr is NULL?\n", 0, 0, 0);
369 /* we assume AF_x and PF_x are equal for all x */
370 s = ldap_int_socket( ld, sai->ai_family, socktype );
371 if ( s == AC_SOCKET_INVALID ) {
375 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
376 ldap_pvt_close_socket(ld, s);
380 switch (sai->ai_family) {
383 char addr[INET6_ADDRSTRLEN];
385 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
387 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
392 char addr[INET_ADDRSTRLEN];
394 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
396 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
401 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
402 if ( (rc == 0) || (rc == -2) ) {
403 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
406 ldap_pvt_close_socket(ld, s);
413 if (! inet_aton( host, &in) ) {
415 struct hostent he_buf;
416 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
417 &hp, &local_h_errno);
419 if ( (rc < 0) || (hp == NULL) ) {
421 ldap_pvt_set_errno( WSAGetLastError() );
423 /* not exactly right, but... */
424 ldap_pvt_set_errno( EHOSTUNREACH );
426 if (ha_buf) LDAP_FREE(ha_buf);
436 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
437 s = ldap_int_socket( ld, PF_INET, socktype );
438 if ( s == AC_SOCKET_INVALID ) {
439 /* use_hp ? continue : break; */
443 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
444 ldap_pvt_close_socket(ld, s);
448 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
449 sin.sin_family = AF_INET;
450 sin.sin_port = htons((short) port);
451 p = (char *)&sin.sin_addr;
452 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
453 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
455 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
456 inet_ntoa(sin.sin_addr),port,0);
458 rc = ldap_pvt_connect(ld, s,
459 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
462 if ( (rc == 0) || (rc == -2) ) {
463 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
467 ldap_pvt_close_socket(ld, s);
472 if (ha_buf) LDAP_FREE(ha_buf);
476 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
477 defined( HAVE_CYRUS_SASL )
479 ldap_host_connected_to( Sockbuf *sb )
487 /* buffers for gethostbyaddr_r */
488 struct hostent he_buf;
493 (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
496 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
497 if ( getpeername( sd, &sa, &len ) == -1 ) {
502 * do a reverse lookup on the addr to get the official hostname.
503 * this is necessary for kerberos to work right, since the official
504 * hostname is used as the kerberos instance.
507 switch (sa.sa_family) {
510 return LDAP_STRDUP( ldap_int_hostname );
514 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
515 len = sizeof( struct in6_addr );
519 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
520 len = sizeof( struct in_addr );
523 struct sockaddr_in localhost;
524 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
526 if( memcmp ( &localhost.sin_addr,
527 &((struct sockaddr_in *)&sa)->sin_addr,
528 sizeof(localhost.sin_addr) ) == 0 )
530 return LDAP_STRDUP( ldap_int_hostname );
533 #ifdef INADDR_LOOPBACK
534 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
536 if( memcmp ( &localhost.sin_addr,
537 &((struct sockaddr_in *)&sa)->sin_addr,
538 sizeof(localhost.sin_addr) ) == 0 )
540 return LDAP_STRDUP( ldap_int_hostname );
552 if ((ldap_pvt_gethostbyaddr_a( addr, len,
553 sa.sa_family, &he_buf, &ha_buf,
554 &hp,&local_h_errno ) == 0 ) &&
555 (hp != NULL) && ( hp->h_name != NULL ) )
557 host = LDAP_STRDUP( hp->h_name );
570 fd_set si_use_readfds;
571 fd_set si_use_writefds;
576 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
578 struct selectinfo *sip;
581 sip = (struct selectinfo *)ld->ld_selectinfo;
583 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
584 if ( !FD_ISSET( sd, &sip->si_writefds )) {
585 FD_SET( sd, &sip->si_writefds );
591 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
593 struct selectinfo *sip;
596 sip = (struct selectinfo *)ld->ld_selectinfo;
598 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
599 if ( !FD_ISSET( sd, &sip->si_readfds )) {
600 FD_SET( sd, &sip->si_readfds );
606 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
608 struct selectinfo *sip;
611 sip = (struct selectinfo *)ld->ld_selectinfo;
613 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
614 FD_CLR( sd, &sip->si_writefds );
615 FD_CLR( sd, &sip->si_readfds );
620 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
622 struct selectinfo *sip;
625 sip = (struct selectinfo *)ld->ld_selectinfo;
627 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
628 return( FD_ISSET( sd, &sip->si_use_writefds ));
633 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
635 struct selectinfo *sip;
638 sip = (struct selectinfo *)ld->ld_selectinfo;
640 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
641 return( FD_ISSET( sd, &sip->si_use_readfds ));
646 ldap_new_select_info( void )
648 struct selectinfo *sip;
650 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
651 sizeof( struct selectinfo ))) != NULL ) {
652 FD_ZERO( &sip->si_readfds );
653 FD_ZERO( &sip->si_writefds );
656 return( (void *)sip );
661 ldap_free_select_info( void *sip )
668 ldap_int_ip_init( void )
671 #if defined( HAVE_SYSCONF )
672 tblsize = sysconf( _SC_OPEN_MAX );
673 #elif defined( HAVE_GETDTABLESIZE )
674 tblsize = getdtablesize();
676 tblsize = FD_SETSIZE;
677 #endif /* !USE_SYSCONF */
680 if( tblsize > FD_SETSIZE )
681 tblsize = FD_SETSIZE;
682 #endif /* FD_SETSIZE*/
683 ldap_int_tblsize = tblsize;
688 ldap_int_select( LDAP *ld, struct timeval *timeout )
690 struct selectinfo *sip;
693 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
695 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
698 if ( ldap_int_tblsize == 0 )
701 sip = (struct selectinfo *)ld->ld_selectinfo;
702 sip->si_use_readfds = sip->si_readfds;
703 sip->si_use_writefds = sip->si_writefds;
705 return( select( ldap_int_tblsize,
706 &sip->si_use_readfds, &sip->si_use_writefds,