2003-11-26 15:16:36 +08:00
|
|
|
/* os-ip.c -- platform-specific TCP & UDP related code */
|
1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 15:16:36 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2005-01-02 04:49:32 +08:00
|
|
|
* Copyright 1998-2005 The OpenLDAP Foundation.
|
2003-11-30 09:07:47 +08:00
|
|
|
* Portions Copyright 1999 Lars Uffmann.
|
2003-11-26 15:16:36 +08:00
|
|
|
* All rights reserved.
|
1998-08-09 08:43:13 +08:00
|
|
|
*
|
2003-11-26 15:16:36 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
|
|
|
*/
|
|
|
|
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
2003-11-30 09:07:47 +08:00
|
|
|
/* Significant additional contributors include:
|
|
|
|
* Lars Uffman
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
#include <ac/unistd.h>
|
1998-08-21 03:42:38 +08:00
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif /* HAVE_IO_H */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
#include "ldap-int.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-06-19 14:44:16 +08:00
|
|
|
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
|
2002-07-12 23:36:33 +08:00
|
|
|
# ifdef LDAP_PF_INET6
|
2002-06-19 14:44:16 +08:00
|
|
|
int ldap_int_inet4or6 = AF_UNSPEC;
|
2002-07-12 23:36:33 +08:00
|
|
|
# else
|
|
|
|
int ldap_int_inet4or6 = AF_INET;
|
|
|
|
# endif
|
2002-06-19 14:44:16 +08:00
|
|
|
#endif
|
|
|
|
|
2003-06-06 21:56:18 +08:00
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
#define osip_debug(ld,fmt,arg1,arg2,arg3) \
|
|
|
|
do { \
|
2000-05-11 17:47:55 +08:00
|
|
|
ldap_log_printf(NULL, LDAP_DEBUG_TRACE, fmt, arg1, arg2, arg3); \
|
1999-08-04 03:27:22 +08:00
|
|
|
} while(0)
|
|
|
|
|
2003-06-06 21:56:18 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
#define osip_debug(ld,fmt,arg1,arg2,arg3) ((void)0)
|
|
|
|
|
|
|
|
#endif /* LDAP_DEBUG */
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
static void
|
|
|
|
ldap_pvt_set_errno(int err)
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-08-04 03:27:22 +08:00
|
|
|
errno = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_int_timeval_dup( struct timeval **dest, const struct timeval *src )
|
|
|
|
{
|
|
|
|
struct timeval *new;
|
|
|
|
|
|
|
|
assert( dest != NULL );
|
|
|
|
|
|
|
|
if (src == NULL) {
|
|
|
|
*dest = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-06-07 13:17:29 +08:00
|
|
|
new = (struct timeval *) LDAP_MALLOC(sizeof(struct timeval));
|
1999-08-04 03:27:22 +08:00
|
|
|
|
|
|
|
if( new == NULL ) {
|
|
|
|
*dest = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( (char *) new, (const char *) src, sizeof(struct timeval));
|
1999-08-04 03:27:22 +08:00
|
|
|
|
|
|
|
*dest = new;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldap_pvt_ndelay_on(LDAP *ld, int fd)
|
|
|
|
{
|
|
|
|
osip_debug(ld, "ldap_ndelay_on: %d\n",fd,0,0);
|
1999-08-05 02:32:38 +08:00
|
|
|
return ber_pvt_socket_set_nonblock( fd, 1 );
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
1998-12-19 09:27:20 +08:00
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
static int
|
|
|
|
ldap_pvt_ndelay_off(LDAP *ld, int fd)
|
|
|
|
{
|
|
|
|
osip_debug(ld, "ldap_ndelay_off: %d\n",fd,0,0);
|
1999-08-05 02:32:38 +08:00
|
|
|
return ber_pvt_socket_set_nonblock( fd, 0 );
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ber_socket_t
|
2000-07-14 06:54:38 +08:00
|
|
|
ldap_int_socket(LDAP *ld, int family, int type )
|
1999-08-04 03:27:22 +08:00
|
|
|
{
|
2000-07-14 06:54:38 +08:00
|
|
|
ber_socket_t s = socket(family, type, 0);
|
1999-08-04 03:27:22 +08:00
|
|
|
osip_debug(ld, "ldap_new_socket: %d\n",s,0,0);
|
|
|
|
return ( s );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldap_pvt_close_socket(LDAP *ld, int s)
|
|
|
|
{
|
|
|
|
osip_debug(ld, "ldap_close_socket: %d\n",s,0,0);
|
|
|
|
return tcp_close(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-14 06:54:38 +08:00
|
|
|
ldap_int_prepare_socket(LDAP *ld, int s, int proto )
|
1999-08-04 03:27:22 +08:00
|
|
|
{
|
2000-07-14 06:54:38 +08:00
|
|
|
osip_debug(ld, "ldap_prepare_socket: %d\n", s,0,0);
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-07-23 05:49:35 +08:00
|
|
|
#ifdef TCP_NODELAY
|
2000-09-14 05:47:12 +08:00
|
|
|
if( proto == LDAP_PROTO_TCP ) {
|
2000-07-14 06:54:38 +08:00
|
|
|
int dummy = 1;
|
|
|
|
if ( setsockopt( s, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(char*) &dummy, sizeof(dummy) ) == AC_SOCKET_ERROR )
|
|
|
|
{
|
|
|
|
osip_debug(ld, "ldap_prepare_socket: "
|
|
|
|
"setsockopt(%d, TCP_NODELAY) failed (ignored).\n",
|
|
|
|
s, 0, 0);
|
|
|
|
}
|
1999-10-20 03:07:24 +08:00
|
|
|
}
|
1999-07-23 05:49:35 +08:00
|
|
|
#endif
|
2000-07-14 06:54:38 +08:00
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-01-10 13:12:11 +08:00
|
|
|
#ifndef HAVE_WINSOCK
|
|
|
|
|
1999-08-05 02:32:38 +08:00
|
|
|
#undef TRACE
|
|
|
|
#define TRACE do { \
|
|
|
|
osip_debug(ld, \
|
2000-05-11 17:47:55 +08:00
|
|
|
"ldap_is_socket_ready: error on socket %d: errno: %d (%s)\n", \
|
1999-08-05 02:32:38 +08:00
|
|
|
s, \
|
|
|
|
errno, \
|
2000-05-11 17:47:55 +08:00
|
|
|
sock_errstr(errno) ); \
|
1999-08-05 02:32:38 +08:00
|
|
|
} while( 0 )
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
/*
|
|
|
|
* check the socket for errors after select returned.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ldap_pvt_is_socket_ready(LDAP *ld, int s)
|
|
|
|
{
|
|
|
|
osip_debug(ld, "ldap_is_sock_ready: %d\n",s,0,0);
|
|
|
|
|
1999-08-05 02:32:38 +08:00
|
|
|
#if defined( notyet ) /* && defined( SO_ERROR ) */
|
1999-08-04 03:27:22 +08:00
|
|
|
{
|
|
|
|
int so_errno;
|
2002-04-08 17:43:22 +08:00
|
|
|
socklen_t dummy = sizeof(so_errno);
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( getsockopt( s, SOL_SOCKET, SO_ERROR, &so_errno, &dummy )
|
2000-10-18 11:24:50 +08:00
|
|
|
== AC_SOCKET_ERROR )
|
2000-10-18 08:27:31 +08:00
|
|
|
{
|
1999-08-04 03:27:22 +08:00
|
|
|
return -1;
|
1999-08-05 02:32:38 +08:00
|
|
|
}
|
1999-08-04 03:27:22 +08:00
|
|
|
if ( so_errno ) {
|
|
|
|
ldap_pvt_set_errno(so_errno);
|
|
|
|
TRACE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
/* error slippery */
|
2002-08-10 11:48:09 +08:00
|
|
|
#ifdef LDAP_PF_INET6
|
|
|
|
struct sockaddr_storage sin;
|
|
|
|
#else
|
1999-08-04 03:27:22 +08:00
|
|
|
struct sockaddr_in sin;
|
2002-08-10 11:48:09 +08:00
|
|
|
#endif
|
1999-08-04 03:27:22 +08:00
|
|
|
char ch;
|
2002-04-08 17:43:22 +08:00
|
|
|
socklen_t dummy = sizeof(sin);
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( getpeername( s, (struct sockaddr *) &sin, &dummy )
|
2000-10-18 11:24:50 +08:00
|
|
|
== AC_SOCKET_ERROR )
|
2000-10-18 08:27:31 +08:00
|
|
|
{
|
1999-08-05 02:32:38 +08:00
|
|
|
/* XXX: needs to be replace with ber_stream_read() */
|
1999-08-04 03:27:22 +08:00
|
|
|
read(s, &ch, 1);
|
|
|
|
TRACE;
|
|
|
|
return -1;
|
1999-08-05 02:32:38 +08:00
|
|
|
}
|
1999-08-04 03:27:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
1999-08-05 02:32:38 +08:00
|
|
|
#undef TRACE
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2002-01-10 13:12:11 +08:00
|
|
|
#endif /* HAVE_WINSOCK */
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
static int
|
2000-07-14 06:54:38 +08:00
|
|
|
ldap_pvt_connect(LDAP *ld, ber_socket_t s,
|
|
|
|
struct sockaddr *sin, socklen_t addrlen,
|
|
|
|
int async)
|
1999-08-04 03:27:22 +08:00
|
|
|
{
|
2002-07-28 13:30:39 +08:00
|
|
|
int rc;
|
2005-09-11 02:59:35 +08:00
|
|
|
struct timeval tv = { 0 },
|
|
|
|
*opt_tv = NULL;
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2001-09-28 08:18:40 +08:00
|
|
|
#ifdef LDAP_CONNECTIONLESS
|
|
|
|
/* We could do a connect() but that would interfere with
|
|
|
|
* attempts to poll a broadcast address
|
|
|
|
*/
|
|
|
|
if (LDAP_IS_UDP(ld)) {
|
|
|
|
if (ld->ld_options.ldo_peer)
|
|
|
|
ldap_memfree(ld->ld_options.ldo_peer);
|
|
|
|
ld->ld_options.ldo_peer=ldap_memalloc(sizeof(struct sockaddr));
|
|
|
|
AC_MEMCPY(ld->ld_options.ldo_peer,sin,sizeof(struct sockaddr));
|
|
|
|
return ( 0 );
|
|
|
|
}
|
|
|
|
#endif
|
2005-09-11 02:59:35 +08:00
|
|
|
opt_tv = ld->ld_options.ldo_tm_net;
|
|
|
|
if ( opt_tv != NULL ) {
|
|
|
|
tv = *opt_tv;
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
|
|
|
|
1999-09-02 16:05:17 +08:00
|
|
|
osip_debug(ld, "ldap_connect_timeout: fd: %d tm: %ld async: %d\n",
|
|
|
|
s, opt_tv ? tv.tv_sec : -1L, async);
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2005-10-09 02:19:50 +08:00
|
|
|
if ( opt_tv && ldap_pvt_ndelay_on(ld, s) == -1 )
|
1999-08-04 03:27:22 +08:00
|
|
|
return ( -1 );
|
|
|
|
|
2002-07-28 13:30:39 +08:00
|
|
|
if ( connect(s, sin, addrlen) != AC_SOCKET_ERROR ) {
|
2005-10-09 02:19:50 +08:00
|
|
|
if ( opt_tv && ldap_pvt_ndelay_off(ld, s) == -1 )
|
1999-08-04 03:27:22 +08:00
|
|
|
return ( -1 );
|
|
|
|
return ( 0 );
|
|
|
|
}
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
1999-08-04 03:27:22 +08:00
|
|
|
ldap_pvt_set_errno( WSAGetLastError() );
|
1998-10-25 10:02:31 +08:00
|
|
|
#endif
|
1999-08-04 03:27:22 +08:00
|
|
|
|
1999-08-04 08:40:03 +08:00
|
|
|
if ( errno != EINPROGRESS && errno != EWOULDBLOCK ) {
|
1999-08-04 03:27:22 +08:00
|
|
|
return ( -1 );
|
1999-08-04 08:40:03 +08:00
|
|
|
}
|
1999-08-04 03:27:22 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef notyet
|
1999-08-04 03:27:22 +08:00
|
|
|
if ( async ) return ( -2 );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
#ifdef HAVE_POLL
|
2004-07-10 03:29:46 +08:00
|
|
|
{
|
|
|
|
struct pollfd fd;
|
|
|
|
int timeout = INFTIM;
|
|
|
|
|
|
|
|
if( opt_tv != NULL ) timeout = TV2MILLISEC( &tv );
|
|
|
|
|
|
|
|
fd.fd = s;
|
2004-10-19 06:08:37 +08:00
|
|
|
fd.events = POLL_WRITE;
|
2004-07-10 03:29:46 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
fd.revents = 0;
|
|
|
|
rc = poll( &fd, 1, timeout );
|
|
|
|
} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
|
|
|
|
LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
|
|
|
|
|
|
|
|
if( rc == AC_SOCKET_ERROR ) return rc;
|
|
|
|
|
2004-10-19 06:08:37 +08:00
|
|
|
if( fd.revents & POLL_WRITE ) {
|
2004-07-10 03:29:46 +08:00
|
|
|
if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
|
|
|
|
if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
|
|
|
|
return ( 0 );
|
|
|
|
}
|
|
|
|
}
|
2004-07-10 01:50:22 +08:00
|
|
|
#else
|
2004-07-10 03:29:46 +08:00
|
|
|
{
|
|
|
|
fd_set wfds, *z=NULL;
|
|
|
|
#ifdef HAVE_WINSOCK
|
|
|
|
fd_set efds;
|
|
|
|
#endif
|
2004-11-18 04:04:18 +08:00
|
|
|
|
2005-04-13 15:27:36 +08:00
|
|
|
#if defined( FD_SETSIZE ) && !defined( HAVE_WINSOCK )
|
2004-11-18 04:04:18 +08:00
|
|
|
if ( s >= FD_SETSIZE ) {
|
|
|
|
rc = AC_SOCKET_ERROR;
|
|
|
|
tcp_close( s );
|
|
|
|
ldap_pvt_set_errno( EMFILE );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
2005-01-29 03:03:15 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
FD_SET(s, &wfds );
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2000-05-11 17:47:55 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
2005-01-29 03:03:15 +08:00
|
|
|
FD_ZERO(&efds);
|
|
|
|
FD_SET(s, &efds );
|
2000-05-11 17:47:55 +08:00
|
|
|
#endif
|
|
|
|
|
2004-07-10 03:29:46 +08:00
|
|
|
rc = select(ldap_int_tblsize, z, &wfds,
|
2000-05-11 17:47:55 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
2004-07-10 03:29:46 +08:00
|
|
|
&efds,
|
2000-05-11 17:47:55 +08:00
|
|
|
#else
|
2004-07-10 03:29:46 +08:00
|
|
|
z,
|
2000-05-11 17:47:55 +08:00
|
|
|
#endif
|
2004-07-10 03:29:46 +08:00
|
|
|
opt_tv ? &tv : NULL);
|
|
|
|
} while( rc == AC_SOCKET_ERROR && errno == EINTR &&
|
|
|
|
LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART ));
|
2002-07-28 13:30:39 +08:00
|
|
|
|
2004-07-10 03:29:46 +08:00
|
|
|
if( rc == AC_SOCKET_ERROR ) return rc;
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2000-05-11 17:47:55 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
2004-07-10 03:29:46 +08:00
|
|
|
/* This means the connection failed */
|
|
|
|
if ( FD_ISSET(s, &efds) ) {
|
|
|
|
int so_errno;
|
|
|
|
int dummy = sizeof(so_errno);
|
|
|
|
if ( getsockopt( s, SOL_SOCKET, SO_ERROR,
|
|
|
|
(char *) &so_errno, &dummy ) == AC_SOCKET_ERROR || !so_errno )
|
|
|
|
{
|
|
|
|
/* impossible */
|
|
|
|
so_errno = WSAGetLastError();
|
|
|
|
}
|
|
|
|
ldap_pvt_set_errno(so_errno);
|
|
|
|
osip_debug(ld, "ldap_pvt_connect: error on socket %d: "
|
|
|
|
"errno: %d (%s)\n", s, errno, sock_errstr(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-05-11 17:47:55 +08:00
|
|
|
#endif
|
2004-07-10 03:29:46 +08:00
|
|
|
if ( FD_ISSET(s, &wfds) ) {
|
2002-01-10 13:12:11 +08:00
|
|
|
#ifndef HAVE_WINSOCK
|
2004-07-10 03:29:46 +08:00
|
|
|
if ( ldap_pvt_is_socket_ready(ld, s) == -1 ) return -1;
|
2002-01-10 13:12:11 +08:00
|
|
|
#endif
|
2004-07-10 03:29:46 +08:00
|
|
|
if ( ldap_pvt_ndelay_off(ld, s) == -1 ) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
2004-07-10 01:50:22 +08:00
|
|
|
#endif
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
osip_debug(ld, "ldap_connect_timeout: timed out\n",0,0,0);
|
|
|
|
ldap_pvt_set_errno( ETIMEDOUT );
|
2004-07-10 03:29:46 +08:00
|
|
|
return -1;
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
|
|
|
|
1999-08-04 04:02:44 +08:00
|
|
|
#ifndef HAVE_INET_ATON
|
|
|
|
int
|
|
|
|
ldap_pvt_inet_aton( const char *host, struct in_addr *in)
|
1999-08-04 03:27:22 +08:00
|
|
|
{
|
|
|
|
unsigned long u = inet_addr( host );
|
2004-12-31 20:02:23 +08:00
|
|
|
|
|
|
|
#ifdef INADDR_NONE
|
|
|
|
if ( u == INADDR_NONE ) return 0;
|
|
|
|
#endif
|
|
|
|
if ( u == 0xffffffffUL || u == (unsigned long) -1L ) return 0;
|
|
|
|
|
|
|
|
in->s_addr = u;
|
|
|
|
return 1;
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
1999-08-04 04:02:44 +08:00
|
|
|
#endif
|
1999-08-04 03:27:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2000-07-14 06:54:38 +08:00
|
|
|
ldap_connect_to_host(LDAP *ld, Sockbuf *sb,
|
|
|
|
int proto,
|
2003-04-22 01:06:37 +08:00
|
|
|
const char *host, int port,
|
|
|
|
int async )
|
1999-08-04 03:27:22 +08:00
|
|
|
{
|
2003-04-22 02:28:08 +08:00
|
|
|
int rc;
|
|
|
|
int socktype;
|
1999-08-04 03:27:22 +08:00
|
|
|
ber_socket_t s = AC_SOCKET_INVALID;
|
2003-04-22 02:28:08 +08:00
|
|
|
|
2003-04-22 01:06:37 +08:00
|
|
|
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
|
|
|
|
char serv[7];
|
|
|
|
int err;
|
|
|
|
struct addrinfo hints, *res, *sai;
|
|
|
|
#else
|
2003-04-22 02:28:08 +08:00
|
|
|
int i;
|
|
|
|
int use_hp = 0;
|
2003-04-22 02:45:46 +08:00
|
|
|
struct hostent *hp = NULL;
|
2003-02-11 03:59:32 +08:00
|
|
|
struct hostent he_buf;
|
2003-04-22 01:06:37 +08:00
|
|
|
struct in_addr in;
|
2003-04-22 02:45:46 +08:00
|
|
|
char *ha_buf=NULL;
|
2003-04-22 02:28:08 +08:00
|
|
|
#endif
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2003-04-22 01:06:37 +08:00
|
|
|
if( host == NULL ) host = "localhost";
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2001-09-28 08:18:40 +08:00
|
|
|
switch(proto) {
|
2002-02-21 23:39:35 +08:00
|
|
|
case LDAP_PROTO_TCP: socktype = SOCK_STREAM;
|
2003-04-22 01:06:37 +08:00
|
|
|
osip_debug( ld,
|
|
|
|
"ldap_connect_to_host: TCP %s:%d\n",
|
|
|
|
host, port, 0);
|
2002-02-21 23:39:35 +08:00
|
|
|
break;
|
|
|
|
case LDAP_PROTO_UDP: socktype = SOCK_DGRAM;
|
2003-04-22 01:06:37 +08:00
|
|
|
osip_debug( ld,
|
|
|
|
"ldap_connect_to_host: UDP %s:%d\n",
|
|
|
|
host, port, 0);
|
2002-02-21 23:39:35 +08:00
|
|
|
break;
|
|
|
|
default:
|
2003-04-22 01:06:37 +08:00
|
|
|
osip_debug( ld, "ldap_connect_to_host: unknown proto: %d\n",
|
|
|
|
proto, 0, 0 );
|
2001-09-28 08:18:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-05-10 07:41:38 +08:00
|
|
|
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
|
2003-04-22 01:06:37 +08:00
|
|
|
memset( &hints, '\0', sizeof(hints) );
|
2004-08-28 02:59:34 +08:00
|
|
|
#ifdef USE_AI_ATTRCONFIG /* FIXME: configure test needed */
|
|
|
|
/* Use AI_ATTRCONFIG only on systems where its known to be needed. */
|
|
|
|
hints.ai_flags = AI_ATTRCONFIG;
|
|
|
|
#endif
|
2003-04-22 01:06:37 +08:00
|
|
|
hints.ai_family = ldap_int_inet4or6;
|
|
|
|
hints.ai_socktype = socktype;
|
|
|
|
snprintf(serv, sizeof serv, "%d", port );
|
2002-12-03 09:15:03 +08:00
|
|
|
|
|
|
|
#ifdef LDAP_R_COMPILE
|
2003-04-22 01:06:37 +08:00
|
|
|
/* most getaddrinfo(3) use non-threadsafe resolver libraries */
|
|
|
|
ldap_pvt_thread_mutex_lock(&ldap_int_resolv_mutex);
|
2002-12-03 09:15:03 +08:00
|
|
|
#endif
|
|
|
|
|
2003-04-22 01:06:37 +08:00
|
|
|
err = getaddrinfo( host, serv, &hints, &res );
|
2002-12-03 09:15:03 +08:00
|
|
|
|
|
|
|
#ifdef LDAP_R_COMPILE
|
2003-04-22 01:06:37 +08:00
|
|
|
ldap_pvt_thread_mutex_unlock(&ldap_int_resolv_mutex);
|
2002-12-03 09:15:03 +08:00
|
|
|
#endif
|
|
|
|
|
2003-04-22 01:06:37 +08:00
|
|
|
if ( err != 0 ) {
|
|
|
|
osip_debug(ld, "ldap_connect_to_host: getaddrinfo failed: %s\n",
|
|
|
|
AC_GAI_STRERROR(err), 0, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rc = -1;
|
|
|
|
|
|
|
|
for( sai=res; sai != NULL; sai=sai->ai_next) {
|
|
|
|
if( sai->ai_addr == NULL ) {
|
|
|
|
osip_debug(ld, "ldap_connect_to_host: getaddrinfo "
|
|
|
|
"ai_addr is NULL?\n", 0, 0, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we assume AF_x and PF_x are equal for all x */
|
|
|
|
s = ldap_int_socket( ld, sai->ai_family, socktype );
|
|
|
|
if ( s == AC_SOCKET_INVALID ) {
|
|
|
|
continue;
|
2000-06-17 03:15:08 +08:00
|
|
|
}
|
2003-04-22 01:06:37 +08:00
|
|
|
|
|
|
|
if ( ldap_int_prepare_socket(ld, s, proto ) == -1 ) {
|
|
|
|
ldap_pvt_close_socket(ld, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sai->ai_family) {
|
2000-06-17 03:15:08 +08:00
|
|
|
#ifdef LDAP_PF_INET6
|
|
|
|
case AF_INET6: {
|
|
|
|
char addr[INET6_ADDRSTRLEN];
|
|
|
|
inet_ntop( AF_INET6,
|
|
|
|
&((struct sockaddr_in6 *)sai->ai_addr)->sin6_addr,
|
|
|
|
addr, sizeof addr);
|
|
|
|
osip_debug(ld, "ldap_connect_to_host: Trying %s %s\n",
|
|
|
|
addr, serv, 0);
|
|
|
|
} break;
|
|
|
|
#endif
|
|
|
|
case AF_INET: {
|
|
|
|
char addr[INET_ADDRSTRLEN];
|
|
|
|
inet_ntop( AF_INET,
|
|
|
|
&((struct sockaddr_in *)sai->ai_addr)->sin_addr,
|
|
|
|
addr, sizeof addr);
|
|
|
|
osip_debug(ld, "ldap_connect_to_host: Trying %s:%s\n",
|
|
|
|
addr, serv, 0);
|
|
|
|
} break;
|
2003-04-22 01:06:37 +08:00
|
|
|
}
|
2000-07-14 06:54:38 +08:00
|
|
|
|
2003-04-22 01:06:37 +08:00
|
|
|
rc = ldap_pvt_connect( ld, s,
|
|
|
|
sai->ai_addr, sai->ai_addrlen, async );
|
|
|
|
if ( (rc == 0) || (rc == -2) ) {
|
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
|
|
|
|
break;
|
2001-09-29 14:32:58 +08:00
|
|
|
}
|
2003-04-22 01:06:37 +08:00
|
|
|
ldap_pvt_close_socket(ld, s);
|
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
2001-05-10 07:41:38 +08:00
|
|
|
|
2000-06-17 03:15:08 +08:00
|
|
|
#else
|
2003-04-22 01:06:37 +08:00
|
|
|
if (! inet_aton( host, &in ) ) {
|
|
|
|
int local_h_errno;
|
|
|
|
rc = ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
|
|
|
|
&hp, &local_h_errno );
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2003-04-22 01:06:37 +08:00
|
|
|
if ( (rc < 0) || (hp == NULL) ) {
|
1999-08-04 03:27:22 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
2003-04-22 01:06:37 +08:00
|
|
|
ldap_pvt_set_errno( WSAGetLastError() );
|
1999-08-04 03:27:22 +08:00
|
|
|
#else
|
2003-04-22 01:06:37 +08:00
|
|
|
/* not exactly right, but... */
|
|
|
|
ldap_pvt_set_errno( EHOSTUNREACH );
|
1999-08-04 03:27:22 +08:00
|
|
|
#endif
|
2003-04-22 01:06:37 +08:00
|
|
|
if (ha_buf) LDAP_FREE(ha_buf);
|
|
|
|
return -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2003-04-22 01:06:37 +08:00
|
|
|
|
|
|
|
use_hp = 1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
rc = s = -1;
|
|
|
|
for ( i = 0; !use_hp || (hp->h_addr_list[i] != 0); ++i, rc = -1 ) {
|
2002-08-10 11:48:09 +08:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
2001-09-28 08:18:40 +08:00
|
|
|
s = ldap_int_socket( ld, PF_INET, socktype );
|
2000-10-18 08:27:31 +08:00
|
|
|
if ( s == AC_SOCKET_INVALID ) {
|
1999-08-04 03:27:22 +08:00
|
|
|
/* use_hp ? continue : break; */
|
|
|
|
break;
|
2000-06-17 03:15:08 +08:00
|
|
|
}
|
1999-03-03 04:23:14 +08:00
|
|
|
|
2000-07-14 06:54:38 +08:00
|
|
|
if ( ldap_int_prepare_socket( ld, s, proto ) == -1 ) {
|
1999-08-04 03:27:22 +08:00
|
|
|
ldap_pvt_close_socket(ld, s);
|
|
|
|
break;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2002-08-10 11:48:09 +08:00
|
|
|
(void)memset((char *)&sin, '\0', sizeof sin);
|
1999-08-04 03:27:22 +08:00
|
|
|
sin.sin_family = AF_INET;
|
2001-10-07 10:40:16 +08:00
|
|
|
sin.sin_port = htons((short) port);
|
2003-04-22 02:45:46 +08:00
|
|
|
|
|
|
|
if( use_hp ) {
|
|
|
|
AC_MEMCPY( &sin.sin_addr, hp->h_addr_list[i],
|
|
|
|
sizeof(sin.sin_addr) );
|
|
|
|
} else {
|
|
|
|
AC_MEMCPY( &sin.sin_addr, &in.s_addr,
|
|
|
|
sizeof(sin.sin_addr) );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-10-14 11:40:03 +08:00
|
|
|
#ifdef HAVE_INET_NTOA_B
|
|
|
|
{
|
|
|
|
/* for VxWorks */
|
|
|
|
char address[INET_ADDR_LEN];
|
|
|
|
inet_ntoa_b(sin.sin_address, address);
|
|
|
|
osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
|
|
|
|
address, port, 0);
|
|
|
|
}
|
|
|
|
#else
|
1999-08-04 03:27:22 +08:00
|
|
|
osip_debug(ld, "ldap_connect_to_host: Trying %s:%d\n",
|
2003-04-22 02:45:46 +08:00
|
|
|
inet_ntoa(sin.sin_addr), port, 0);
|
2005-10-14 11:40:03 +08:00
|
|
|
#endif
|
1999-08-04 03:27:22 +08:00
|
|
|
|
2000-07-14 06:54:38 +08:00
|
|
|
rc = ldap_pvt_connect(ld, s,
|
2003-04-22 02:45:46 +08:00
|
|
|
(struct sockaddr *)&sin, sizeof(sin),
|
2000-07-14 06:54:38 +08:00
|
|
|
async);
|
1998-12-19 09:27:20 +08:00
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
if ( (rc == 0) || (rc == -2) ) {
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_SET_FD, &s );
|
1999-08-04 03:27:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_pvt_close_socket(ld, s);
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2003-04-22 02:45:46 +08:00
|
|
|
if (!use_hp) break;
|
1999-08-04 03:27:22 +08:00
|
|
|
}
|
|
|
|
if (ha_buf) LDAP_FREE(ha_buf);
|
2003-04-22 01:06:37 +08:00
|
|
|
#endif
|
|
|
|
|
1999-08-04 03:27:22 +08:00
|
|
|
return rc;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2001-06-26 05:44:41 +08:00
|
|
|
#if defined( LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND ) || \
|
|
|
|
defined( HAVE_CYRUS_SASL )
|
1998-08-09 08:43:13 +08:00
|
|
|
char *
|
2003-04-22 09:17:23 +08:00
|
|
|
ldap_host_connected_to( Sockbuf *sb, const char *host )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2000-07-14 06:54:38 +08:00
|
|
|
socklen_t len;
|
2002-08-10 06:14:43 +08:00
|
|
|
#ifdef LDAP_PF_INET6
|
|
|
|
struct sockaddr_storage sabuf;
|
|
|
|
#else
|
|
|
|
struct sockaddr sabuf;
|
|
|
|
#endif
|
|
|
|
struct sockaddr *sa = (struct sockaddr *) &sabuf;
|
2000-07-14 06:54:38 +08:00
|
|
|
ber_socket_t sd;
|
|
|
|
|
2002-08-10 06:14:43 +08:00
|
|
|
(void)memset( (char *)sa, '\0', sizeof sabuf );
|
|
|
|
len = sizeof sabuf;
|
1999-03-03 04:23:14 +08:00
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
|
2002-08-10 06:14:43 +08:00
|
|
|
if ( getpeername( sd, sa, &len ) == -1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do a reverse lookup on the addr to get the official hostname.
|
|
|
|
* this is necessary for kerberos to work right, since the official
|
|
|
|
* hostname is used as the kerberos instance.
|
|
|
|
*/
|
2000-06-17 03:15:08 +08:00
|
|
|
|
2002-08-10 06:14:43 +08:00
|
|
|
switch (sa->sa_family) {
|
2000-07-14 06:54:38 +08:00
|
|
|
#ifdef LDAP_PF_LOCAL
|
|
|
|
case AF_LOCAL:
|
|
|
|
return LDAP_STRDUP( ldap_int_hostname );
|
|
|
|
#endif
|
2000-06-17 03:15:08 +08:00
|
|
|
#ifdef LDAP_PF_INET6
|
|
|
|
case AF_INET6:
|
2003-03-05 09:35:39 +08:00
|
|
|
{
|
|
|
|
struct in6_addr localhost = IN6ADDR_LOOPBACK_INIT;
|
|
|
|
if( memcmp ( &((struct sockaddr_in6 *)sa)->sin6_addr,
|
|
|
|
&localhost, sizeof(localhost)) == 0 )
|
|
|
|
{
|
|
|
|
return LDAP_STRDUP( ldap_int_hostname );
|
|
|
|
}
|
|
|
|
}
|
2000-06-17 03:15:08 +08:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case AF_INET:
|
2000-07-14 06:54:38 +08:00
|
|
|
{
|
2003-03-05 09:35:39 +08:00
|
|
|
struct in_addr localhost;
|
|
|
|
localhost.s_addr = htonl( INADDR_ANY );
|
2000-07-14 06:54:38 +08:00
|
|
|
|
2003-03-05 09:35:39 +08:00
|
|
|
if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
|
|
|
|
&localhost, sizeof(localhost) ) == 0 )
|
2000-07-14 06:54:38 +08:00
|
|
|
{
|
|
|
|
return LDAP_STRDUP( ldap_int_hostname );
|
|
|
|
}
|
2000-08-15 10:13:00 +08:00
|
|
|
|
|
|
|
#ifdef INADDR_LOOPBACK
|
2003-03-05 09:35:39 +08:00
|
|
|
localhost.s_addr = htonl( INADDR_LOOPBACK );
|
2000-08-15 10:13:00 +08:00
|
|
|
|
2003-03-05 09:35:39 +08:00
|
|
|
if( memcmp ( &((struct sockaddr_in *)sa)->sin_addr,
|
|
|
|
&localhost, sizeof(localhost) ) == 0 )
|
2000-08-15 10:13:00 +08:00
|
|
|
{
|
|
|
|
return LDAP_STRDUP( ldap_int_hostname );
|
|
|
|
}
|
|
|
|
#endif
|
2000-07-14 06:54:38 +08:00
|
|
|
}
|
2000-06-17 03:15:08 +08:00
|
|
|
break;
|
2000-07-14 06:54:38 +08:00
|
|
|
|
2000-06-17 03:15:08 +08:00
|
|
|
default:
|
|
|
|
return( NULL );
|
|
|
|
break;
|
|
|
|
}
|
2000-07-14 06:54:38 +08:00
|
|
|
|
1998-12-19 09:27:20 +08:00
|
|
|
{
|
2003-04-22 09:17:23 +08:00
|
|
|
char *herr;
|
2003-12-21 16:25:55 +08:00
|
|
|
#ifdef NI_MAXHOST
|
2003-04-22 09:17:23 +08:00
|
|
|
char hbuf[NI_MAXHOST];
|
2003-12-21 16:25:55 +08:00
|
|
|
#elif defined( MAXHOSTNAMELEN
|
|
|
|
char hbuf[MAXHOSTNAMELEN];
|
|
|
|
#else
|
|
|
|
char hbuf[256];
|
|
|
|
#endif
|
2003-04-22 09:17:23 +08:00
|
|
|
hbuf[0] = 0;
|
|
|
|
|
|
|
|
if (ldap_pvt_get_hname( sa, len, hbuf, sizeof(hbuf), &herr ) == 0
|
|
|
|
&& hbuf[0] )
|
|
|
|
{
|
|
|
|
return LDAP_STRDUP( hbuf );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2003-04-22 09:17:23 +08:00
|
|
|
return host ? LDAP_STRDUP( host ) : NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2001-06-26 05:44:41 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
|
2004-10-19 05:09:17 +08:00
|
|
|
struct selectinfo {
|
2004-07-10 01:50:22 +08:00
|
|
|
#ifdef HAVE_POLL
|
2004-10-19 05:09:17 +08:00
|
|
|
/* for UNIX poll(2) */
|
|
|
|
int si_maxfd;
|
|
|
|
struct pollfd si_fds[FD_SETSIZE];
|
2004-07-10 01:50:22 +08:00
|
|
|
#else
|
2004-10-19 05:09:17 +08:00
|
|
|
/* for UNIX select(2) */
|
1998-08-09 08:43:13 +08:00
|
|
|
fd_set si_readfds;
|
|
|
|
fd_set si_writefds;
|
|
|
|
fd_set si_use_readfds;
|
|
|
|
fd_set si_use_writefds;
|
2004-07-10 01:50:22 +08:00
|
|
|
#endif
|
2004-10-19 05:09:17 +08:00
|
|
|
};
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_mark_select_write( LDAP *ld, Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_socket_t sd;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
1999-03-03 04:23:14 +08:00
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
/* for UNIX poll(2) */
|
|
|
|
{
|
|
|
|
int empty=-1;
|
|
|
|
int i;
|
|
|
|
for(i=0; i < sip->si_maxfd; i++) {
|
|
|
|
if( sip->si_fds[i].fd == sd ) {
|
2004-10-19 06:08:37 +08:00
|
|
|
sip->si_fds[i].events |= POLL_WRITE;
|
2004-10-19 05:09:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( empty==-1 && sip->si_fds[i].fd == -1 ) {
|
|
|
|
empty=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( empty == -1 ) {
|
|
|
|
if( sip->si_maxfd >= FD_SETSIZE ) {
|
|
|
|
/* FIXME */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
empty = sip->si_maxfd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sip->si_fds[empty].fd = sd;
|
2004-10-19 06:08:37 +08:00
|
|
|
sip->si_fds[empty].events = POLL_WRITE;
|
2004-10-19 05:09:17 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* for UNIX select(2) */
|
2000-06-02 04:59:21 +08:00
|
|
|
if ( !FD_ISSET( sd, &sip->si_writefds )) {
|
|
|
|
FD_SET( sd, &sip->si_writefds );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2004-10-19 05:09:17 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_mark_select_read( LDAP *ld, Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_socket_t sd;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
/* for UNIX poll(2) */
|
|
|
|
{
|
|
|
|
int empty=-1;
|
|
|
|
int i;
|
|
|
|
for(i=0; i < sip->si_maxfd; i++) {
|
|
|
|
if( sip->si_fds[i].fd == sd ) {
|
2004-10-19 06:08:37 +08:00
|
|
|
sip->si_fds[i].events |= POLL_READ;
|
2004-10-19 05:09:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( empty==-1 && sip->si_fds[i].fd == -1 ) {
|
|
|
|
empty=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( empty == -1 ) {
|
|
|
|
if( sip->si_maxfd >= FD_SETSIZE ) {
|
|
|
|
/* FIXME */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
empty = sip->si_maxfd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sip->si_fds[empty].fd = sd;
|
2004-10-19 06:08:37 +08:00
|
|
|
sip->si_fds[empty].events = POLL_READ;
|
2004-10-19 05:09:17 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* for UNIX select(2) */
|
2000-06-02 04:59:21 +08:00
|
|
|
if ( !FD_ISSET( sd, &sip->si_readfds )) {
|
|
|
|
FD_SET( sd, &sip->si_readfds );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2004-10-19 05:09:17 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_mark_select_clear( LDAP *ld, Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_socket_t sd;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
/* for UNIX poll(2) */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i < sip->si_maxfd; i++) {
|
|
|
|
if( sip->si_fds[i].fd == sd ) {
|
|
|
|
sip->si_fds[i].fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* for UNIX select(2) */
|
2000-06-02 04:59:21 +08:00
|
|
|
FD_CLR( sd, &sip->si_writefds );
|
|
|
|
FD_CLR( sd, &sip->si_readfds );
|
2004-10-19 05:09:17 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_is_write_ready( LDAP *ld, Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_socket_t sd;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
/* for UNIX poll(2) */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i < sip->si_maxfd; i++) {
|
|
|
|
if( sip->si_fds[i].fd == sd ) {
|
2004-10-19 06:08:37 +08:00
|
|
|
return sip->si_fds[i].revents & POLL_WRITE;
|
2004-10-19 05:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* for UNIX select(2) */
|
2000-06-02 04:59:21 +08:00
|
|
|
return( FD_ISSET( sd, &sip->si_use_writefds ));
|
2004-10-19 05:09:17 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_is_read_ready( LDAP *ld, Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_socket_t sd;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
2000-06-02 04:59:21 +08:00
|
|
|
ber_sockbuf_ctrl( sb, LBER_SB_OPT_GET_FD, &sd );
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
/* for UNIX poll(2) */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i < sip->si_maxfd; i++) {
|
|
|
|
if( sip->si_fds[i].fd == sd ) {
|
2004-10-19 06:08:37 +08:00
|
|
|
return sip->si_fds[i].revents & POLL_READ;
|
2004-10-19 05:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* for UNIX select(2) */
|
2000-06-02 04:59:21 +08:00
|
|
|
return( FD_ISSET( sd, &sip->si_use_readfds ));
|
2004-10-19 05:09:17 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
ldap_new_select_info( void )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
|
|
|
|
2004-10-19 05:09:17 +08:00
|
|
|
sip = (struct selectinfo *)LDAP_CALLOC( 1, sizeof( struct selectinfo ));
|
|
|
|
|
|
|
|
if ( sip == NULL ) return NULL;
|
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
/* for UNIX poll(2) */
|
|
|
|
/* sip->si_maxfd=0 */
|
|
|
|
#else
|
|
|
|
/* for UNIX select(2) */
|
|
|
|
FD_ZERO( &sip->si_readfds );
|
|
|
|
FD_ZERO( &sip->si_writefds );
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( (void *)sip );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_free_select_info( void *sip )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-29 09:19:14 +08:00
|
|
|
LDAP_FREE( sip );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
#ifndef HAVE_POLL
|
|
|
|
int ldap_int_tblsize = 0;
|
|
|
|
|
1999-07-13 14:21:14 +08:00
|
|
|
void
|
|
|
|
ldap_int_ip_init( void )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1998-10-25 10:02:31 +08:00
|
|
|
#if defined( HAVE_SYSCONF )
|
2005-03-31 05:53:01 +08:00
|
|
|
long tblsize = sysconf( _SC_OPEN_MAX );
|
|
|
|
if( tblsize > INT_MAX ) tblsize = INT_MAX;
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#elif defined( HAVE_GETDTABLESIZE )
|
2005-03-31 05:53:01 +08:00
|
|
|
int tblsize = getdtablesize();
|
1998-11-17 07:55:37 +08:00
|
|
|
#else
|
2005-03-31 05:53:01 +08:00
|
|
|
int tblsize = FD_SETSIZE;
|
1998-08-20 08:05:08 +08:00
|
|
|
#endif /* !USE_SYSCONF */
|
|
|
|
|
|
|
|
#ifdef FD_SETSIZE
|
2004-07-10 01:50:22 +08:00
|
|
|
if( tblsize > FD_SETSIZE ) tblsize = FD_SETSIZE;
|
2004-10-19 05:09:17 +08:00
|
|
|
#endif /* FD_SETSIZE */
|
2004-07-10 01:50:22 +08:00
|
|
|
|
1999-07-13 14:21:14 +08:00
|
|
|
ldap_int_tblsize = tblsize;
|
|
|
|
}
|
2004-07-10 01:50:22 +08:00
|
|
|
#endif
|
1999-07-13 14:21:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
int
|
2001-12-24 14:36:44 +08:00
|
|
|
ldap_int_select( LDAP *ld, struct timeval *timeout )
|
1999-07-13 14:21:14 +08:00
|
|
|
{
|
2004-10-19 05:09:17 +08:00
|
|
|
int rc;
|
1999-07-13 14:21:14 +08:00
|
|
|
struct selectinfo *sip;
|
|
|
|
|
2001-12-24 14:36:44 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_int_select\n", 0, 0, 0 );
|
1999-07-13 14:21:14 +08:00
|
|
|
|
2004-07-10 01:50:22 +08:00
|
|
|
#ifndef HAVE_POLL
|
|
|
|
if ( ldap_int_tblsize == 0 ) ldap_int_ip_init();
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
2005-06-29 20:28:40 +08:00
|
|
|
assert( sip != NULL );
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_POLL
|
|
|
|
{
|
|
|
|
int to = timeout ? TV2MILLISEC( timeout ) : INFTIM;
|
|
|
|
rc = poll( sip->si_fds, sip->si_maxfd, to );
|
|
|
|
}
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
sip->si_use_readfds = sip->si_readfds;
|
|
|
|
sip->si_use_writefds = sip->si_writefds;
|
|
|
|
|
2004-10-19 05:09:17 +08:00
|
|
|
rc = select( ldap_int_tblsize,
|
2004-07-10 01:50:22 +08:00
|
|
|
&sip->si_use_readfds, &sip->si_use_writefds,
|
2004-10-19 11:18:22 +08:00
|
|
|
NULL, timeout );
|
2004-07-10 01:50:22 +08:00
|
|
|
#endif
|
2004-10-19 05:09:17 +08:00
|
|
|
|
|
|
|
return rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|