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;
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",
138 "ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
141 sock_errstr(errno) ); \
145 * check the socket for errors after select returned.
148 ldap_pvt_is_socket_ready(LDAP *ld, int s)
150 osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
152 #if defined( notyet ) /* && defined( SO_ERROR ) */
155 int dummy = sizeof(so_errno);
156 if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
162 ldap_pvt_set_errno(so_errno);
171 struct sockaddr_in sin;
173 int dummy = sizeof(sin);
174 if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
177 /* XXX: needs to be replace with ber_stream_read() */
189 #endif /* HAVE_WINSOCK */
192 ldap_pvt_connect(LDAP *ld, ber_socket_t s,
193 struct sockaddr *sin, socklen_t addrlen,
196 struct timeval tv, *opt_tv=NULL;
197 fd_set wfds, *z=NULL;
202 #ifdef LDAP_CONNECTIONLESS
203 /* We could do a connect() but that would interfere with
204 * attempts to poll a broadcast address
206 if (LDAP_IS_UDP(ld)) {
207 if (ld->ld_options.ldo_peer)
208 ldap_memfree(ld->ld_options.ldo_peer);
209 ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
210 AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
214 if ( (opt_tv = ld->ld_options.ldo_tm_net) != NULL ) {
215 tv.tv_usec = opt_tv->tv_usec;
216 tv.tv_sec = opt_tv->tv_sec;
219 osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
220 s, opt_tv ? tv.tv_sec : -1L, async);
222 if ( ldap_pvt_ndelay_on(ld, s) == -1 )
225 if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR )
227 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
233 ldap_pvt_set_errno( WSAGetLastError() );
236 if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
241 if ( async ) return ( -2 );
252 if ( select(ldap_int_tblsize, z, &wfds,
258 opt_tv ? &tv : NULL) == AC_SOCKET_ERROR )
264 /* This means the connection failed */
265 if ( FD_ISSET(s, &efds) ) {
267 int dummy = sizeof(so_errno);
268 if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
269 (char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
272 so_errno = WSAGetLastError();
274 ldap_pvt_set_errno(so_errno);
275 osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
276 "errno: %d (%s)\n", s, errno, sock_errstr(errno));
280 if ( FD_ISSET(s, &wfds) ) {
282 if ( ldap_pvt_is_socket_ready(ld, s) == -1 )
285 if ( ldap_pvt_ndelay_off(ld, s) == -1 )
289 osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
290 ldap_pvt_set_errno( ETIMEDOUT );
294 #ifndef HAVE_INET_ATON
296 ldap_pvt_inet_aton( const char *host, struct in_addr *in)
298 unsigned long u = inet_addr( host );
299 if ( u != 0xffffffff || u != (unsigned long) -1 ) {
309 ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
312 unsigned long address, int port, int async )
314 struct sockaddr_in sin;
315 ber_socket_t s = AC_SOCKET_INVALID;
316 int rc, i, use_hp = 0;
317 struct hostent *hp = NULL;
318 char *ha_buf=NULL, *p, *q;
321 osip_debug(ld, "ldap_connect_to_host: %s\n",host,0,0);
324 case LDAP_PROTO_TCP: socktype = SOCK_STREAM; break;
325 case LDAP_PROTO_UDP: socktype = SOCK_DGRAM; break;
326 default: osip_debug(ld, "ldap_connect_to_host: unknown proto: %d\n",
332 #if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
335 struct addrinfo hints, *res, *sai;
337 memset( &hints, '\0', sizeof(hints) );
338 hints.ai_family = AF_UNSPEC;
339 hints.ai_socktype = socktype;
341 snprintf(serv, sizeof serv, "%d", port );
342 if ( err = getaddrinfo(host, serv, &hints, &res) ) {
343 osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
344 AC_GAI_STRERROR(err), 0, 0);
349 for( sai=res; sai != NULL; sai=sai->ai_next) {
350 if( sai->ai_addr == NULL ) {
351 osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
352 "ai_addr is NULL?\n", 0, 0, 0);
356 /* we assume AF_x and PF_x are equal for all x */
357 s = ldap_int_socket( ld, sai->ai_family, socktype );
358 if ( s == AC_SOCKET_INVALID ) {
362 if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
363 ldap_pvt_close_socket(ld, s);
367 switch (sai->ai_family) {
370 char addr[INET6_ADDRSTRLEN];
372 &((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
374 osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
379 char addr[INET_ADDRSTRLEN];
381 &((struct sockaddr_in *)sai->ai_addr)->sin_addr,
383 osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
388 rc = ldap_pvt_connect(ld, s, sai->ai_addr, sai->ai_addrlen, async);
389 if ( (rc == 0) || (rc == -2) ) {
390 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
393 ldap_pvt_close_socket(ld, s);
400 if (! inet_aton( host, &in) ) {
402 struct hostent he_buf;
403 rc = ldap_pvt_gethostbyname_a(host, &he_buf, &ha_buf,
404 &hp, &local_h_errno);
406 if ( (rc < 0) || (hp == NULL) ) {
408 ldap_pvt_set_errno( WSAGetLastError() );
410 /* not exactly right, but... */
411 ldap_pvt_set_errno( EHOSTUNREACH );
413 if (ha_buf) LDAP_FREE(ha_buf);
423 for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
424 s = ldap_int_socket( ld, PF_INET, socktype );
425 if ( s == AC_SOCKET_INVALID ) {
426 /* use_hp ? continue : break; */
430 if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
431 ldap_pvt_close_socket(ld, s);
435 (void)memset((char *)&sin, '\0', sizeof(struct sockaddr_in));
436 sin.sin_family = AF_INET;
437 sin.sin_port = htons((short) port);
438 p = (char *)&sin.sin_addr;
439 q = use_hp ? (char *)hp->h_addr_list[i] : (char *)&address;
440 AC_MEMCPY(p, q, sizeof(sin.sin_addr) );
442 osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
443 inet_ntoa(sin.sin_addr),port,0);
445 rc = ldap_pvt_connect(ld, s,
446 (struct sockaddr *)&sin, sizeof(struct sockaddr_in),
449 if ( (rc == 0) || (rc == -2) ) {
450 ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
454 ldap_pvt_close_socket(ld, s);
459 if (ha_buf) LDAP_FREE(ha_buf);
463 #if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
464 defined( HAVE_CYRUS_SASL )
466 ldap_host_connected_to( Sockbuf *sb )
474 /* buffers for gethostbyaddr_r */
475 struct hostent he_buf;
480 (void)memset( (char *)&sa, '\0', sizeof( struct sockaddr ));
483 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
484 if ( getpeername( sd, &sa, &len ) == -1 ) {
489 * do a reverse lookup on the addr to get the official hostname.
490 * this is necessary for kerberos to work right, since the official
491 * hostname is used as the kerberos instance.
494 switch (sa.sa_family) {
497 return LDAP_STRDUP( ldap_int_hostname );
501 addr = (char *) &((struct sockaddr_in6 *)&sa)->sin6_addr;
502 len = sizeof( struct in6_addr );
506 addr = (char *) &((struct sockaddr_in *)&sa)->sin_addr;
507 len = sizeof( struct in_addr );
510 struct sockaddr_in localhost;
511 localhost.sin_addr.s_addr = htonl( INADDR_ANY );
513 if( memcmp ( &localhost.sin_addr,
514 &((struct sockaddr_in *)&sa)->sin_addr,
515 sizeof(localhost.sin_addr) ) == 0 )
517 return LDAP_STRDUP( ldap_int_hostname );
520 #ifdef INADDR_LOOPBACK
521 localhost.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
523 if( memcmp ( &localhost.sin_addr,
524 &((struct sockaddr_in *)&sa)->sin_addr,
525 sizeof(localhost.sin_addr) ) == 0 )
527 return LDAP_STRDUP( ldap_int_hostname );
539 if ((ldap_pvt_gethostbyaddr_a( addr, len,
540 sa.sa_family, &he_buf, &ha_buf,
541 &hp,&local_h_errno ) == 0 ) &&
542 (hp != NULL) && ( hp->h_name != NULL ) )
544 host = LDAP_STRDUP( hp->h_name );
557 fd_set si_use_readfds;
558 fd_set si_use_writefds;
563 ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
565 struct selectinfo *sip;
568 sip = (struct selectinfo *)ld->ld_selectinfo;
570 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
571 if ( !FD_ISSET( sd, &sip->si_writefds )) {
572 FD_SET( sd, &sip->si_writefds );
578 ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
580 struct selectinfo *sip;
583 sip = (struct selectinfo *)ld->ld_selectinfo;
585 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
586 if ( !FD_ISSET( sd, &sip->si_readfds )) {
587 FD_SET( sd, &sip->si_readfds );
593 ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
595 struct selectinfo *sip;
598 sip = (struct selectinfo *)ld->ld_selectinfo;
600 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
601 FD_CLR( sd, &sip->si_writefds );
602 FD_CLR( sd, &sip->si_readfds );
607 ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
609 struct selectinfo *sip;
612 sip = (struct selectinfo *)ld->ld_selectinfo;
614 ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
615 return( FD_ISSET( sd, &sip->si_use_writefds ));
620 ldap_is_read_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_readfds ));
633 ldap_new_select_info( void )
635 struct selectinfo *sip;
637 if (( sip = (struct selectinfo *)LDAP_CALLOC( 1,
638 sizeof( struct selectinfo ))) != NULL ) {
639 FD_ZERO( &sip->si_readfds );
640 FD_ZERO( &sip->si_writefds );
643 return( (void *)sip );
648 ldap_free_select_info( void *sip )
655 ldap_int_ip_init( void )
658 #if defined( HAVE_SYSCONF )
659 tblsize = sysconf( _SC_OPEN_MAX );
660 #elif defined( HAVE_GETDTABLESIZE )
661 tblsize = getdtablesize();
663 tblsize = FD_SETSIZE;
664 #endif /* !USE_SYSCONF */
667 if( tblsize > FD_SETSIZE )
668 tblsize = FD_SETSIZE;
669 #endif /* FD_SETSIZE*/
670 ldap_int_tblsize = tblsize;
675 ldap_int_select( LDAP *ld, struct timeval *timeout )
677 struct selectinfo *sip;
679 Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
681 if ( ldap_int_tblsize == 0 )
684 sip = (struct selectinfo *)ld->ld_selectinfo;
685 sip->si_use_readfds = sip->si_readfds;
686 sip->si_use_writefds = sip->si_writefds;
688 return( select( ldap_int_tblsize,
689 &sip->si_use_readfds, &sip->si_use_writefds,