3 * Copyright 1998-2000 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;
34 * nonblock connect code
35 * written by Lars Uffmann, <lars.uffmann@mediaway.net>.
37 * Copyright 1999, Lars Uffmann, All rights reserved.
38 * This software is not subject to any license of my employer
41 * OpenLDAP COPYING RESTRICTIONS APPLY, see COPYRIGHT file
43 * Read about the rationale in ldap_connect_timeout:
44 * ftp://koobera.math.uic.edu/www/docs/connect.html.
47 #define osip_debug(ld,fmt,arg1,arg2,arg3) \
49 ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
53 ldap_pvt_set_errno(int err)
59 ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
63 assert( dest != NULL );
70 new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
77 AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
84 ldap_pvt_ndelay_on(LDAP *ld, int fd)
86 osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
87 return ber_pvt_socket_set_nonblock( fd, 1 );
91 ldap_pvt_ndelay_off(LDAP *ld, int fd)
93 osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
94 return ber_pvt_socket_set_nonblock( fd, 0 );
98 ldap_int_socket(LDAP *ld, int family, int type )
100 ber_socket_t s = socket(family, type, 0);
101 osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
106 ldap_pvt_close_socket(LDAP *ld, int s)
108 osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
113 ldap_int_prepare_socket(LDAP *ld, int s, int proto )
115 osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
118 if( proto == LDAP_PROTO_TCP ) {
120 if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
121 (char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
123 osip_debug(ld, "ldap_prepare_socket: "
124 "setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
136 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
139 sock_errstr(errno) ); \
143 * check the socket for errors after select returned.
146 ldap_pvt_is_socket_ready(LDAP *ld, int s)
148 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
150 #if defined( notyet ) /* && defined( SO_ERROR ) */
153 int dummy = sizeof(so_errno);
154 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
160 ldap_pvt_set_errno(so_errno);
169 struct sockaddr_in sin;
171 int dummy = sizeof(sin);
172 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
175 /* XXX: needs to be replace with ber_stream_read() */
178 ldap_pvt_set_errno( WSAGetLastError() );
191 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
192 struct sockaddr *sin, socklen_t addrlen,
195 struct timeval tv, *opt_tv=NULL;
196 fd_set wfds, *z=NULL;
201 #ifdef LDAP_CONNECTIONLESS
202 /* We could do a connect() but that would interfere with
203 * attempts to poll a broadcast address
205 if (LDAP_IS_UDP(ld)) {
206 if (ld->ld_options.ldo_peer)
207 ldap_memfree(ld->ld_options.ldo_peer);
208 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
209 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
213 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
214 tv.tv_usec = opt_tv->tv_usec;
215 tv.tv_sec = opt_tv->tv_sec;
218 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
219 s, opt_tv ? tv.tv_sec : -1L, async);
221 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
224 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
226 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
232 ldap_pvt_set_errno( WSAGetLastError() );
235 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
240 if ( async ) return ( -2 );
251 if ( select(ldap_int_tblsize, z, &wfds,
257 opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
263 /* This means the connection failed */
264 if ( FD_ISSET(s, &efds) ) {
265 ldap_pvt_set_errno(WSAECONNREFUSED);
266 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
267 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
271 if ( FD_ISSET(s, &wfds) ) {
272 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
274 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
278 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
279 ldap_pvt_set_errno( ETIMEDOUT );
283 #ifndef HAVE_INET_ATON
285 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
287 unsigned long u = inet_addr( host );
288 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
298 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
301 unsigned long address, int port, int async )
303 struct sockaddr_in sin;
304 ber_socket_t s = AC_SOCKET_INVALID;
305 int rc, i, use_hp = 0;
306 struct hostent *hp = NULL;
307 char *ha_buf=NULL, *p, *q;
310 osip_debug(ld, "ldap_connect_to_host: %s\n",host,0,0);
313 case LDAP_PROTO_TCP: socktype = SOCK_STREAM; break;
314 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM; break;
315 default: osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
321 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
324 struct addrinfo hints, *res, *sai;
326 memset( &hints, '\0', sizeof(hints) );
327 hints.ai_family = AF_UNSPEC;
328 hints.ai_socktype = socktype;
330 snprintf(serv, sizeof serv, "%d", port );
331 if ( err = getaddrinfo(host, serv, &hints, &res) ) {
332 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
333 AC_GAI_STRERROR(err), 0, 0);
338 for( sai=res; sai != NULL; sai=sai->ai_next) {
339 if( sai->ai_addr == NULL ) {
340 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
341 "ai_addr is NULL?\n", 0, 0, 0);
345 /* we assume AF_x and PF_x are equal for all x */
346 s = ldap_int_socket( ld, sai->ai_family, socktype );
347 if ( s == AC_SOCKET_INVALID ) {
351 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
352 ldap_pvt_close_socket(ld, s);
356 switch (sai->ai_family) {
359 char addr[INET6_ADDRSTRLEN];
361 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
363 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
368 char addr[INET_ADDRSTRLEN];
370 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
372 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
377 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
378 if ( (rc == 0) || (rc == -2) ) {
379 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
382 ldap_pvt_close_socket(ld, s);
389 if (! inet_aton( host, &in) ) {
391 struct hostent he_buf;
392 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
393 &hp, &local_h_errno);
395 if ( (rc < 0) || (hp == NULL) ) {
397 ldap_pvt_set_errno( WSAGetLastError() );
399 /* not exactly right, but... */
400 ldap_pvt_set_errno( EHOSTUNREACH );
402 if (ha_buf) LDAP_FREE(ha_buf);
412 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
413 s = ldap_int_socket( ld, PF_INET, socktype );
414 if ( s == AC_SOCKET_INVALID ) {
415 /* use_hp ? continue : break; */
419 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
420 ldap_pvt_close_socket(ld, s);
424 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
425 sin.sin_family = AF_INET;
426 sin.sin_port = htons((short) port);
427 p = (char *)&sin.sin_addr;
428 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
429 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
431 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
432 inet_ntoa(sin.sin_addr),port,0);
434 rc = ldap_pvt_connect(ld, s,
435 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
438 if ( (rc == 0) || (rc == -2) ) {
439 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
443 ldap_pvt_close_socket(ld, s);
448 if (ha_buf) LDAP_FREE(ha_buf);
452 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
453 defined( HAVE_CYRUS_SASL )
455 ldap_host_connected_to( Sockbuf *sb )
463 /* buffers for gethostbyaddr_r */
464 struct hostent he_buf;
469 (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
472 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
473 if ( getpeername( sd, &sa, &len ) == -1 ) {
478 * do a reverse lookup on the addr to get the official hostname.
479 * this is necessary for kerberos to work right, since the official
480 * hostname is used as the kerberos instance.
483 switch (sa.sa_family) {
486 return LDAP_STRDUP( ldap_int_hostname );
490 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
491 len = sizeof( struct in6_addr );
495 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
496 len = sizeof( struct in_addr );
499 struct sockaddr_in localhost;
500 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
502 if( memcmp ( &localhost.sin_addr,
503 &((struct sockaddr_in *)&sa)->sin_addr,
504 sizeof(localhost.sin_addr) ) == 0 )
506 return LDAP_STRDUP( ldap_int_hostname );
509 #ifdef INADDR_LOOPBACK
510 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
512 if( memcmp ( &localhost.sin_addr,
513 &((struct sockaddr_in *)&sa)->sin_addr,
514 sizeof(localhost.sin_addr) ) == 0 )
516 return LDAP_STRDUP( ldap_int_hostname );
528 if ((ldap_pvt_gethostbyaddr_a( addr, len,
529 sa.sa_family, &he_buf, &ha_buf,
530 &hp,&local_h_errno ) == 0 ) &&
531 (hp != NULL) && ( hp->h_name != NULL ) )
533 host = LDAP_STRDUP( hp->h_name );
546 fd_set si_use_readfds;
547 fd_set si_use_writefds;
552 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
554 struct selectinfo *sip;
557 sip = (struct selectinfo *)ld->ld_selectinfo;
559 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
560 if ( !FD_ISSET( sd, &sip->si_writefds )) {
561 FD_SET( sd, &sip->si_writefds );
567 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
569 struct selectinfo *sip;
572 sip = (struct selectinfo *)ld->ld_selectinfo;
574 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
575 if ( !FD_ISSET( sd, &sip->si_readfds )) {
576 FD_SET( sd, &sip->si_readfds );
582 ldap_mark_select_clear( 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 FD_CLR( sd, &sip->si_writefds );
591 FD_CLR( sd, &sip->si_readfds );
596 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
598 struct selectinfo *sip;
601 sip = (struct selectinfo *)ld->ld_selectinfo;
603 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
604 return( FD_ISSET( sd, &sip->si_use_writefds ));
609 ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
611 struct selectinfo *sip;
614 sip = (struct selectinfo *)ld->ld_selectinfo;
616 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
617 return( FD_ISSET( sd, &sip->si_use_readfds ));
622 ldap_new_select_info( void )
624 struct selectinfo *sip;
626 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
627 sizeof( struct selectinfo ))) != NULL ) {
628 FD_ZERO( &sip->si_readfds );
629 FD_ZERO( &sip->si_writefds );
632 return( (void *)sip );
637 ldap_free_select_info( void *sip )
644 ldap_int_ip_init( void )
647 #if defined( HAVE_SYSCONF )
648 tblsize = sysconf( _SC_OPEN_MAX );
649 #elif defined( HAVE_GETDTABLESIZE )
650 tblsize = getdtablesize();
652 tblsize = FD_SETSIZE;
653 #endif /* !USE_SYSCONF */
656 if( tblsize > FD_SETSIZE )
657 tblsize = FD_SETSIZE;
658 #endif /* FD_SETSIZE*/
659 ldap_int_tblsize = tblsize;
664 do_ldap_select( LDAP *ld, struct timeval *timeout )
666 struct selectinfo *sip;
668 Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
670 if ( ldap_int_tblsize == 0 )
673 sip = (struct selectinfo *)ld->ld_selectinfo;
674 sip->si_use_readfds = sip->si_readfds;
675 sip->si_use_writefds = sip->si_writefds;
677 return( select( ldap_int_tblsize,
678 &sip->si_use_readfds, &sip->si_use_writefds,