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,
205 struct timeval tv, *opt_tv=NULL;
206 fd_set wfds, *z=NULL;
211 #ifdef LDAP_CONNECTIONLESS
212 /* We could do a connect() but that would interfere with
213 * attempts to poll a broadcast address
215 if (LDAP_IS_UDP(ld)) {
216 if (ld->ld_options.ldo_peer)
217 ldap_memfree(ld->ld_options.ldo_peer);
218 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
219 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
223 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
224 tv.tv_usec = opt_tv->tv_usec;
225 tv.tv_sec = opt_tv->tv_sec;
228 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
229 s, opt_tv ? tv.tv_sec : -1L, async);
231 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
234 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 );
261 rc = select(ldap_int_tblsize, z, &wfds,
267 opt_tv ? &tv : NULL);
268 } while( rc == AC_SOCKET_ERROR && errno == EINTR &&
269 LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
271 if( rc == AC_SOCKET_ERROR ) return rc;
274 /* This means the connection failed */
275 if ( FD_ISSET(s, &efds) ) {
277 int dummy = sizeof(so_errno);
278 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
279 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
282 so_errno = WSAGetLastError();
284 ldap_pvt_set_errno(so_errno);
285 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
286 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
290 if ( FD_ISSET(s, &wfds) ) {
292 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
295 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
299 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
300 ldap_pvt_set_errno( ETIMEDOUT );
304 #ifndef HAVE_INET_ATON
306 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
308 unsigned long u = inet_addr( host );
309 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
319 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
322 unsigned long address, int port, int async )
324 struct sockaddr_in sin;
325 ber_socket_t s = AC_SOCKET_INVALID;
326 int rc, i, use_hp = 0;
327 struct hostent *hp = NULL;
328 char *ha_buf=NULL, *p, *q;
333 case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
334 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
336 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
337 osip_debug(ld, "ldap_connect_to_host: TCP %s:%d\n",host,port,0);
341 osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
347 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
350 struct addrinfo hints, *res, *sai;
352 memset( &hints, '\0', sizeof(hints) );
353 hints.ai_family = ldap_int_inet4or6;
354 hints.ai_socktype = socktype;
356 snprintf(serv, sizeof serv, "%d", port );
357 if ( ( err = getaddrinfo(host, serv, &hints, &res) ) ) {
358 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
359 AC_GAI_STRERROR(err), 0, 0);
364 for( sai=res; sai != NULL; sai=sai->ai_next) {
365 if( sai->ai_addr == NULL ) {
366 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
367 "ai_addr is NULL?\n", 0, 0, 0);
371 /* we assume AF_x and PF_x are equal for all x */
372 s = ldap_int_socket( ld, sai->ai_family, socktype );
373 if ( s == AC_SOCKET_INVALID ) {
377 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
378 ldap_pvt_close_socket(ld, s);
382 switch (sai->ai_family) {
385 char addr[INET6_ADDRSTRLEN];
387 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
389 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
394 char addr[INET_ADDRSTRLEN];
396 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
398 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
403 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
404 if ( (rc == 0) || (rc == -2) ) {
405 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
408 ldap_pvt_close_socket(ld, s);
415 if (! inet_aton( host, &in) ) {
417 struct hostent he_buf;
418 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
419 &hp, &local_h_errno);
421 if ( (rc < 0) || (hp == NULL) ) {
423 ldap_pvt_set_errno( WSAGetLastError() );
425 /* not exactly right, but... */
426 ldap_pvt_set_errno( EHOSTUNREACH );
428 if (ha_buf) LDAP_FREE(ha_buf);
438 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
439 s = ldap_int_socket( ld, PF_INET, socktype );
440 if ( s == AC_SOCKET_INVALID ) {
441 /* use_hp ? continue : break; */
445 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
446 ldap_pvt_close_socket(ld, s);
450 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
451 sin.sin_family = AF_INET;
452 sin.sin_port = htons((short) port);
453 p = (char *)&sin.sin_addr;
454 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
455 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
457 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
458 inet_ntoa(sin.sin_addr),port,0);
460 rc = ldap_pvt_connect(ld, s,
461 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
464 if ( (rc == 0) || (rc == -2) ) {
465 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
469 ldap_pvt_close_socket(ld, s);
474 if (ha_buf) LDAP_FREE(ha_buf);
478 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
479 defined( HAVE_CYRUS_SASL )
481 ldap_host_connected_to( Sockbuf *sb )
486 struct sockaddr_storage sabuf;
488 struct sockaddr sabuf;
490 struct sockaddr *sa = (struct sockaddr *) &sabuf;
494 /* buffers for gethostbyaddr_r */
495 struct hostent he_buf;
500 (void)memset( (char *)sa, '\0', sizeof sabuf );
503 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
504 if ( getpeername( sd, sa, &len ) == -1 ) {
509 * do a reverse lookup on the addr to get the official hostname.
510 * this is necessary for kerberos to work right, since the official
511 * hostname is used as the kerberos instance.
514 switch (sa->sa_family) {
517 return LDAP_STRDUP( ldap_int_hostname );
521 addr = (char *) &((struct sockaddr_in6 *)sa)->sin6_addr;
522 len = sizeof( struct in6_addr );
526 addr = (char *) &((struct sockaddr_in *)sa)->sin_addr;
527 len = sizeof( struct in_addr );
530 struct sockaddr_in localhost;
531 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
533 if( memcmp ( &localhost.sin_addr,
534 &((struct sockaddr_in *)sa)->sin_addr,
535 sizeof(localhost.sin_addr) ) == 0 )
537 return LDAP_STRDUP( ldap_int_hostname );
540 #ifdef INADDR_LOOPBACK
541 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
543 if( memcmp ( &localhost.sin_addr,
544 &((struct sockaddr_in *)sa)->sin_addr,
545 sizeof(localhost.sin_addr) ) == 0 )
547 return LDAP_STRDUP( ldap_int_hostname );
559 if ((ldap_pvt_gethostbyaddr_a( addr, len, sa->sa_family,
560 &he_buf, &ha_buf, &hp, &local_h_errno ) == 0 ) &&
561 (hp != NULL) && ( hp->h_name != NULL ) )
563 host = LDAP_STRDUP( hp->h_name );
576 fd_set si_use_readfds;
577 fd_set si_use_writefds;
582 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
584 struct selectinfo *sip;
587 sip = (struct selectinfo *)ld->ld_selectinfo;
589 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
590 if ( !FD_ISSET( sd, &sip->si_writefds )) {
591 FD_SET( sd, &sip->si_writefds );
597 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
599 struct selectinfo *sip;
602 sip = (struct selectinfo *)ld->ld_selectinfo;
604 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
605 if ( !FD_ISSET( sd, &sip->si_readfds )) {
606 FD_SET( sd, &sip->si_readfds );
612 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
614 struct selectinfo *sip;
617 sip = (struct selectinfo *)ld->ld_selectinfo;
619 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
620 FD_CLR( sd, &sip->si_writefds );
621 FD_CLR( sd, &sip->si_readfds );
626 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
628 struct selectinfo *sip;
631 sip = (struct selectinfo *)ld->ld_selectinfo;
633 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
634 return( FD_ISSET( sd, &sip->si_use_writefds ));
639 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
641 struct selectinfo *sip;
644 sip = (struct selectinfo *)ld->ld_selectinfo;
646 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
647 return( FD_ISSET( sd, &sip->si_use_readfds ));
652 ldap_new_select_info( void )
654 struct selectinfo *sip;
656 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
657 sizeof( struct selectinfo ))) != NULL ) {
658 FD_ZERO( &sip->si_readfds );
659 FD_ZERO( &sip->si_writefds );
662 return( (void *)sip );
667 ldap_free_select_info( void *sip )
674 ldap_int_ip_init( void )
677 #if defined( HAVE_SYSCONF )
678 tblsize = sysconf( _SC_OPEN_MAX );
679 #elif defined( HAVE_GETDTABLESIZE )
680 tblsize = getdtablesize();
682 tblsize = FD_SETSIZE;
683 #endif /* !USE_SYSCONF */
686 if( tblsize > FD_SETSIZE )
687 tblsize = FD_SETSIZE;
688 #endif /* FD_SETSIZE*/
689 ldap_int_tblsize = tblsize;
694 ldap_int_select( LDAP *ld, struct timeval *timeout )
696 struct selectinfo *sip;
699 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_select\n", 0, 0, 0 );
701 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
704 if ( ldap_int_tblsize == 0 )
707 sip = (struct selectinfo *)ld->ld_selectinfo;
708 sip->si_use_readfds = sip->si_readfds;
709 sip->si_use_writefds = sip->si_writefds;
711 return( select( ldap_int_tblsize,
712 &sip->si_use_readfds, &sip->si_use_writefds,