1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1998-12-29 04:53:15 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1995 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* os-ip.c -- platform-specific TCP & UDP related code
|
|
|
|
*/
|
|
|
|
|
1998-10-25 10:02:31 +08:00
|
|
|
#include "portable.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-08-21 03:42:38 +08:00
|
|
|
#include <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-10-25 10:02:31 +08:00
|
|
|
#if defined( HAVE_SYS_FILIO_H )
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <sys/filio.h>
|
1998-10-25 10:02:31 +08:00
|
|
|
#elif defined( HAVE_SYS_IOCTL_H )
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <sys/ioctl.h>
|
1998-10-25 10:02:31 +08:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_connect_to_host( Sockbuf *sb, const char *host, unsigned long address,
|
1998-08-09 08:43:13 +08:00
|
|
|
int port, int async )
|
|
|
|
/*
|
|
|
|
* if host == NULL, connect using address
|
|
|
|
* "address" and "port" must be in network byte order
|
|
|
|
* zero is returned upon success, -1 if fatal error, -2 EINPROGRESS
|
1998-11-10 03:41:09 +08:00
|
|
|
* async is only used ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS (non-0 means don't wait for connect)
|
1998-08-09 08:43:13 +08:00
|
|
|
* XXX async is not used yet!
|
|
|
|
*/
|
|
|
|
{
|
1998-08-21 03:42:38 +08:00
|
|
|
int rc, i, s = 0;
|
|
|
|
int connected, use_hp;
|
1998-08-09 08:43:13 +08:00
|
|
|
struct sockaddr_in sin;
|
1998-08-21 03:42:38 +08:00
|
|
|
struct hostent *hp = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef notyet
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
|
1999-03-29 13:28:29 +08:00
|
|
|
ioctl_t status; /* for ioctl call */
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif /* notyet */
|
1998-12-19 09:27:20 +08:00
|
|
|
|
1999-01-15 22:49:03 +08:00
|
|
|
/* buffers for ldap_pvt_gethostbyname_a */
|
1998-12-19 09:27:20 +08:00
|
|
|
struct hostent he_buf;
|
|
|
|
int local_h_errno;
|
|
|
|
char *ha_buf=NULL;
|
|
|
|
#define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
|
|
|
|
|
1998-08-09 11:04:12 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_connect_to_host: %s:%d\n",
|
1998-10-25 10:02:31 +08:00
|
|
|
( host == NULL ) ? "(by address)" : host, (int) ntohs( (short) port ), 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
connected = use_hp = 0;
|
|
|
|
|
1998-11-12 10:24:43 +08:00
|
|
|
if ( host != NULL ) {
|
|
|
|
address = inet_addr( host );
|
|
|
|
/* This was just a test for -1 until OSF1 let inet_addr return
|
|
|
|
unsigned int, which is narrower than 'unsigned long address' */
|
|
|
|
if ( address == 0xffffffff || address == (unsigned long) -1 ) {
|
1999-01-15 22:49:03 +08:00
|
|
|
if ( ( ldap_pvt_gethostbyname_a( host, &he_buf, &ha_buf,
|
1998-12-19 09:27:20 +08:00
|
|
|
&hp, &local_h_errno) < 0) || (hp==NULL))
|
|
|
|
{
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
|
|
|
errno = WSAGetLastError();
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
errno = EHOSTUNREACH; /* not exactly right, but... */
|
1998-10-25 10:02:31 +08:00
|
|
|
#endif
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( -1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
use_hp = 1;
|
1998-11-12 10:24:43 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = -1;
|
|
|
|
for ( i = 0; !use_hp || ( hp->h_addr_list[ i ] != 0 ); i++ ) {
|
|
|
|
if (( s = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) {
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( -1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
#ifdef notyet
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
|
1998-08-09 08:43:13 +08:00
|
|
|
status = 1;
|
|
|
|
if ( async && ioctl( s, FIONBIO, (caddr_t)&status ) == -1 ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
|
|
|
|
s, 0, 0 );
|
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif /* notyet */
|
|
|
|
(void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = port;
|
|
|
|
SAFEMEMCPY( (char *) &sin.sin_addr.s_addr,
|
|
|
|
( use_hp ? (char *) hp->h_addr_list[ i ] :
|
|
|
|
(char *) &address ), sizeof( sin.sin_addr.s_addr) );
|
|
|
|
|
|
|
|
if ( connect( s, (struct sockaddr *)&sin,
|
|
|
|
sizeof( struct sockaddr_in )) >= 0 ) {
|
|
|
|
connected = 1;
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
} else {
|
1998-10-25 10:02:31 +08:00
|
|
|
#ifdef HAVE_WINSOCK
|
1998-12-19 09:27:20 +08:00
|
|
|
errno = WSAGetLastError();
|
1998-10-25 10:02:31 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef notyet
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef EAGAIN
|
|
|
|
if ( errno == EINPROGRESS || errno == EAGAIN ) {
|
|
|
|
#else /* EAGAIN */
|
|
|
|
if ( errno == EINPROGRESS ) {
|
|
|
|
#endif /* EAGAIN */
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"connect would block...\n", 0, 0, 0 );
|
|
|
|
rc = -2;
|
|
|
|
break;
|
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif /* notyet */
|
|
|
|
|
|
|
|
#ifdef LDAP_DEBUG
|
|
|
|
if ( ldap_debug & LDAP_DEBUG_TRACE ) {
|
|
|
|
perror( (char *)inet_ntoa( sin.sin_addr ));
|
|
|
|
}
|
|
|
|
#endif
|
1998-10-25 10:02:31 +08:00
|
|
|
tcp_close( s );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( !use_hp ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_pvt_sb_set_desc( sb, s );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( connected ) {
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#ifdef notyet
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
|
1998-08-09 08:43:13 +08:00
|
|
|
status = 0;
|
|
|
|
if ( !async && ioctl( s, FIONBIO, (caddr_t)&on ) == -1 ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY, "FIONBIO ioctl failed on %d\n",
|
|
|
|
s, 0, 0 );
|
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif /* notyet */
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "sd %d connected to: %s\n",
|
1998-10-25 10:02:31 +08:00
|
|
|
s, (char *) inet_ntoa( sin.sin_addr ), 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( rc );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-12-19 09:27:20 +08:00
|
|
|
|
|
|
|
#undef DO_RETURN
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
void
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_close_connection( Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_pvt_sb_close( sb );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-03 04:23:14 +08:00
|
|
|
#if defined( HAVE_KERBEROS ) || defined( HAVE_TLS )
|
1998-08-09 08:43:13 +08:00
|
|
|
char *
|
1998-08-09 11:04:12 +08:00
|
|
|
ldap_host_connected_to( Sockbuf *sb )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
char *p;
|
|
|
|
int len;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
1998-12-19 09:27:20 +08:00
|
|
|
/* buffers for gethostbyaddr_r */
|
|
|
|
struct hostent he_buf;
|
|
|
|
int local_h_errno;
|
|
|
|
char *ha_buf=NULL;
|
|
|
|
#define DO_RETURN(x) if (ha_buf) free(ha_buf); return (x);
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
(void)memset( (char *)&sin, 0, sizeof( struct sockaddr_in ));
|
|
|
|
len = sizeof( sin );
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( getpeername( ber_pvt_sb_get_desc(sb), (struct sockaddr *)&sin, &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.
|
|
|
|
*/
|
1999-01-15 22:49:03 +08:00
|
|
|
if ((ldap_pvt_gethostbyaddr_a( (char *) &sin.sin_addr,
|
1998-12-19 09:27:20 +08:00
|
|
|
sizeof( sin.sin_addr ),
|
|
|
|
AF_INET, &he_buf, &ha_buf,
|
|
|
|
&hp,&local_h_errno ) ==0 ) && (hp != NULL) )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( hp->h_name != NULL ) {
|
1999-03-03 04:23:14 +08:00
|
|
|
char *host = strdup( hp->h_name );
|
|
|
|
DO_RETURN( host );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-19 09:27:20 +08:00
|
|
|
DO_RETURN( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-12-19 09:27:20 +08:00
|
|
|
#undef DO_RETURN
|
|
|
|
|
1999-03-03 04:23:14 +08:00
|
|
|
#endif /* HAVE_KERBEROS || HAVE_TLS */
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
|
1998-11-10 03:41:09 +08:00
|
|
|
#ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
|
1998-08-09 08:43:13 +08:00
|
|
|
/* for UNIX */
|
|
|
|
struct selectinfo {
|
|
|
|
fd_set si_readfds;
|
|
|
|
fd_set si_writefds;
|
|
|
|
fd_set si_use_readfds;
|
|
|
|
fd_set si_use_writefds;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_writefds )) {
|
1998-10-25 10:02:31 +08:00
|
|
|
FD_SET( (u_int) sb->sb_sd, &sip->si_writefds );
|
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;
|
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( !FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_readfds )) {
|
1998-10-25 10:02:31 +08:00
|
|
|
FD_SET( (u_int) sb->sb_sd, &sip->si_readfds );
|
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;
|
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_writefds );
|
|
|
|
FD_CLR( (u_int) ber_pvt_sb_get_desc(sb), &sip->si_readfds );
|
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;
|
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_writefds ));
|
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;
|
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
return( FD_ISSET( ber_pvt_sb_get_desc(sb), &sip->si_use_readfds ));
|
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;
|
|
|
|
|
|
|
|
if (( sip = (struct selectinfo *)calloc( 1,
|
|
|
|
sizeof( struct selectinfo ))) != NULL ) {
|
|
|
|
FD_ZERO( &sip->si_readfds );
|
|
|
|
FD_ZERO( &sip->si_writefds );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
free( sip );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
do_ldap_select( LDAP *ld, struct timeval *timeout )
|
|
|
|
{
|
|
|
|
struct selectinfo *sip;
|
|
|
|
static int tblsize;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE, "do_ldap_select\n", 0, 0, 0 );
|
|
|
|
|
|
|
|
if ( tblsize == 0 ) {
|
1998-10-25 10:02:31 +08:00
|
|
|
#if defined( HAVE_SYSCONF )
|
1998-08-09 08:43:13 +08:00
|
|
|
tblsize = sysconf( _SC_OPEN_MAX );
|
1998-10-25 10:02:31 +08:00
|
|
|
#elif defined( HAVE_GETDTABLESIZE )
|
1998-08-09 08:43:13 +08:00
|
|
|
tblsize = getdtablesize();
|
1998-11-17 07:55:37 +08:00
|
|
|
#else
|
|
|
|
tblsize = FD_SETSIZE;
|
1998-08-20 08:05:08 +08:00
|
|
|
#endif /* !USE_SYSCONF */
|
|
|
|
|
|
|
|
#ifdef FD_SETSIZE
|
|
|
|
if( tblsize > FD_SETSIZE ) {
|
|
|
|
tblsize = FD_SETSIZE;
|
|
|
|
}
|
|
|
|
#endif /* FD_SETSIZE*/
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sip = (struct selectinfo *)ld->ld_selectinfo;
|
|
|
|
sip->si_use_readfds = sip->si_readfds;
|
|
|
|
sip->si_use_writefds = sip->si_writefds;
|
|
|
|
|
|
|
|
return( select( tblsize, &sip->si_use_readfds, &sip->si_use_writefds,
|
|
|
|
NULL, timeout ));
|
|
|
|
}
|
1998-11-10 03:41:09 +08:00
|
|
|
#endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
|