1N/A/*
1N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A
1N/A/*
1N/A * Extended I/O callback functions for libldap that use
1N/A * NSPR (Netscape Portable Runtime) I/O.
1N/A *
1N/A * High level strategy: we use the socket-specific arg to hold our own data
1N/A * structure that includes the NSPR file handle (PRFileDesc *), among other
1N/A * useful information. We use the default argument to hold an LDAP session
1N/A * handle specific data structure.
1N/A */
1N/A
1N/A#include "ldappr-int.h"
1N/A#include <string.h>
1N/A
1N/A#define PRLDAP_POLL_ARRAY_GROWTH 5 /* grow arrays 5 elements at a time */
1N/A
1N/A/*
1N/A * Local function prototypes:
1N/A */
1N/Astatic PRIntervalTime prldap_timeout2it( int ms_timeout, int ms_maxtimeout );
1N/Astatic int LDAP_CALLBACK prldap_read( int s, void *buf, int bufsize,
1N/A struct lextiof_socket_private *socketarg );
1N/Astatic int LDAP_CALLBACK prldap_write( int s, const void *buf, int len,
1N/A struct lextiof_socket_private *socketarg );
1N/Astatic int LDAP_CALLBACK prldap_poll( LDAP_X_PollFD fds[], int nfds,
1N/A int timeout, struct lextiof_session_private *sessionarg );
1N/Astatic int LDAP_CALLBACK prldap_connect( const char *hostlist, int defport,
1N/A int timeout, unsigned long options,
1N/A struct lextiof_session_private *sessionarg,
1N/A struct lextiof_socket_private **socketargp
1N/A#ifdef _SOLARIS_SDK
1N/A , void **dhost );
1N/A#else
1N/A );
1N/A#endif /* _SOLARIS_SDK */
1N/Astatic int LDAP_CALLBACK prldap_close( int s,
1N/A struct lextiof_socket_private *socketarg );
1N/Astatic int LDAP_CALLBACK prldap_newhandle( LDAP *ld,
1N/A struct lextiof_session_private *sessionarg );
1N/Astatic void LDAP_CALLBACK prldap_disposehandle( LDAP *ld,
1N/A struct lextiof_session_private *sessionarg );
1N/Astatic int LDAP_CALLBACK prldap_shared_newhandle( LDAP *ld,
1N/A struct lextiof_session_private *sessionarg );
1N/Astatic void LDAP_CALLBACK prldap_shared_disposehandle( LDAP *ld,
1N/A struct lextiof_session_private *sessionarg );
1N/Astatic PRLDAPIOSessionArg *prldap_session_arg_alloc( void );
1N/Astatic void prldap_session_arg_free( PRLDAPIOSessionArg **prsesspp );
1N/Astatic PRLDAPIOSocketArg *prldap_socket_arg_alloc( PRLDAPIOSessionArg *sessionarg );
1N/Astatic void prldap_socket_arg_free( PRLDAPIOSocketArg **prsockpp );
1N/Astatic void *prldap_safe_realloc( void *ptr, PRUint32 size );
1N/A
1N/A
1N/A
1N/A/*
1N/A * Local macros:
1N/A */
1N/A/* given a socket-specific arg, return the corresponding PRFileDesc * */
1N/A#define PRLDAP_GET_PRFD( socketarg ) \
1N/A (((PRLDAPIOSocketArg *)(socketarg))->prsock_prfd)
1N/A
1N/A/*
1N/A * Static variables.
1N/A */
1N/Astatic int prldap_default_io_max_timeout = LDAP_X_IO_TIMEOUT_NO_TIMEOUT;
1N/A
1N/A/*
1N/A * Install NSPR I/O functions into ld (if ld is NULL, they are installed
1N/A * as the default functions for new LDAP * handles).
1N/A *
1N/A * Returns 0 if all goes well and -1 if not.
1N/A */
1N/Aint
1N/Aprldap_install_io_functions( LDAP *ld, int shared )
1N/A{
1N/A struct ldap_x_ext_io_fns iofns;
1N/A
1N/A memset( &iofns, 0, sizeof(iofns));
1N/A iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
1N/A iofns.lextiof_read = prldap_read;
1N/A iofns.lextiof_write = prldap_write;
1N/A iofns.lextiof_poll = prldap_poll;
1N/A iofns.lextiof_connect = prldap_connect;
1N/A iofns.lextiof_close = prldap_close;
1N/A if ( shared ) {
1N/A iofns.lextiof_newhandle = prldap_shared_newhandle;
1N/A iofns.lextiof_disposehandle = prldap_shared_disposehandle;
1N/A } else {
1N/A iofns.lextiof_newhandle = prldap_newhandle;
1N/A iofns.lextiof_disposehandle = prldap_disposehandle;
1N/A }
1N/A if ( NULL != ld ) {
1N/A /*
1N/A * If we are dealing with a real ld, we allocate the session specific
1N/A * data structure now. If not allocated here, it will be allocated
1N/A * inside prldap_newhandle() or prldap_shared_newhandle().
1N/A */
1N/A if ( NULL ==
1N/A ( iofns.lextiof_session_arg = prldap_session_arg_alloc())) {
1N/A ldap_set_lderrno( ld, LDAP_NO_MEMORY, NULL, NULL );
1N/A return( -1 );
1N/A }
1N/A } else {
1N/A iofns.lextiof_session_arg = NULL;
1N/A }
1N/A
1N/A if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, &iofns ) != 0 ) {
1N/A prldap_session_arg_free(
1N/A (PRLDAPIOSessionArg **) &iofns.lextiof_session_arg );
1N/A return( -1 );
1N/A }
1N/A
1N/A return( 0 );
1N/A}
1N/A
1N/A
1N/Astatic PRIntervalTime
1N/Aprldap_timeout2it( int ms_timeout, int ms_maxtimeout )
1N/A{
1N/A PRIntervalTime prit;
1N/A
1N/A if ( LDAP_X_IO_TIMEOUT_NO_WAIT == ms_timeout ) {
1N/A prit = PR_INTERVAL_NO_WAIT;
1N/A } else if ( LDAP_X_IO_TIMEOUT_NO_TIMEOUT == ms_timeout ) {
1N/A prit = PR_INTERVAL_NO_TIMEOUT;
1N/A } else {
1N/A prit = PR_MillisecondsToInterval( ms_timeout );
1N/A }
1N/A
1N/A /* cap at maximum I/O timeout */
1N/A if ( LDAP_X_IO_TIMEOUT_NO_WAIT == ms_maxtimeout ) {
1N/A prit = LDAP_X_IO_TIMEOUT_NO_WAIT;
1N/A } else if ( LDAP_X_IO_TIMEOUT_NO_TIMEOUT != ms_maxtimeout ) {
1N/A if ( LDAP_X_IO_TIMEOUT_NO_TIMEOUT == ms_timeout ||
1N/A ms_timeout > ms_maxtimeout ) {
1N/A prit = PR_MillisecondsToInterval( ms_maxtimeout );
1N/A }
1N/A }
1N/A
1N/A#ifdef PRLDAP_DEBUG
1N/A if ( PR_INTERVAL_NO_WAIT == prit ) {
1N/A fprintf( stderr, "prldap_timeout2it: NO_WAIT\n" );
1N/A } else if ( PR_INTERVAL_NO_TIMEOUT == prit ) {
1N/A fprintf( stderr, "prldap_timeout2it: NO_TIMEOUT\n" );
1N/A } else {
1N/A fprintf( stderr, "prldap_timeout2it: %dms\n",
1N/A PR_IntervalToMilliseconds(prit));
1N/A }
1N/A#endif /* PRLDAP_DEBUG */
1N/A
1N/A return( prit );
1N/A}
1N/A
1N/A
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_read( int s, void *buf, int bufsize,
1N/A struct lextiof_socket_private *socketarg )
1N/A{
1N/A PRIntervalTime prit;
1N/A
1N/A prit = prldap_timeout2it( LDAP_X_IO_TIMEOUT_NO_TIMEOUT,
1N/A socketarg->prsock_io_max_timeout );
1N/A return( PR_Recv( PRLDAP_GET_PRFD(socketarg), buf, bufsize, 0, prit ));
1N/A}
1N/A
1N/A
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_write( int s, const void *buf, int len,
1N/A struct lextiof_socket_private *socketarg )
1N/A{
1N/A PRIntervalTime prit;
1N/A
1N/A prit = prldap_timeout2it( LDAP_X_IO_TIMEOUT_NO_TIMEOUT,
1N/A socketarg->prsock_io_max_timeout );
1N/A
1N/A /*
1N/A * Note the 4th parameter (flags) to PR_Send() has been obsoleted and
1N/A * must always be 0
1N/A */
1N/A return( PR_Send( PRLDAP_GET_PRFD(socketarg), buf, len, 0, prit ));
1N/A}
1N/A
1N/A
1N/Astruct prldap_eventmap_entry {
1N/A PRInt16 evm_nspr; /* corresponding NSPR PR_Poll() event */
1N/A int evm_ldap; /* LDAP poll event */
1N/A};
1N/A
1N/Astatic struct prldap_eventmap_entry prldap_eventmap[] = {
1N/A { PR_POLL_READ, LDAP_X_POLLIN },
1N/A { PR_POLL_EXCEPT, LDAP_X_POLLPRI },
1N/A { PR_POLL_WRITE, LDAP_X_POLLOUT },
1N/A { PR_POLL_ERR, LDAP_X_POLLERR },
1N/A { PR_POLL_HUP, LDAP_X_POLLHUP },
1N/A { PR_POLL_NVAL, LDAP_X_POLLNVAL },
1N/A};
1N/A
1N/A#define PRLDAP_EVENTMAP_ENTRIES \
1N/A sizeof(prldap_eventmap)/sizeof(struct prldap_eventmap_entry )
1N/A
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_poll( LDAP_X_PollFD fds[], int nfds, int timeout,
1N/A struct lextiof_session_private *sessionarg )
1N/A{
1N/A PRLDAPIOSessionArg *prsessp = sessionarg;
1N/A PRPollDesc *pds;
1N/A int i, j, rc;
1N/A
1N/A if ( NULL == prsessp ) {
1N/A prldap_set_system_errno( EINVAL );
1N/A return( -1 );
1N/A }
1N/A
1N/A /* allocate or resize NSPR poll descriptor array */
1N/A if ( prsessp->prsess_pollds_count < nfds ) {
1N/A pds = prldap_safe_realloc( prsessp->prsess_pollds,
1N/A ( nfds + PRLDAP_POLL_ARRAY_GROWTH )
1N/A * sizeof( PRPollDesc ));
1N/A if ( NULL == pds ) {
1N/A prldap_set_system_errno( prldap_prerr2errno());
1N/A return( -1 );
1N/A }
1N/A prsessp->prsess_pollds = pds;
1N/A prsessp->prsess_pollds_count = nfds + PRLDAP_POLL_ARRAY_GROWTH;
1N/A } else {
1N/A pds = prsessp->prsess_pollds;
1N/A }
1N/A
1N/A /* populate NSPR poll info. based on LDAP info. */
1N/A for ( i = 0; i < nfds; ++i ) {
1N/A if ( NULL == fds[i].lpoll_socketarg ) {
1N/A pds[i].fd = NULL;
1N/A } else {
1N/A pds[i].fd = PRLDAP_GET_PRFD( fds[i].lpoll_socketarg );
1N/A }
1N/A pds[i].in_flags = pds[i].out_flags = 0;
1N/A if ( fds[i].lpoll_fd >= 0 ) {
1N/A for ( j = 0; j < PRLDAP_EVENTMAP_ENTRIES; ++j ) {
1N/A if (( fds[i].lpoll_events & prldap_eventmap[j].evm_ldap )
1N/A != 0 ) {
1N/A pds[i].in_flags |= prldap_eventmap[j].evm_nspr;
1N/A }
1N/A }
1N/A }
1N/A fds[i].lpoll_revents = 0; /* clear revents */
1N/A }
1N/A
1N/A /* call PR_Poll() to do the real work */
1N/A rc = PR_Poll( pds, nfds,
1N/A prldap_timeout2it( timeout, prsessp->prsess_io_max_timeout ));
1N/A
1N/A /* populate LDAP info. based on NSPR results */
1N/A for ( i = 0; i < nfds; ++i ) {
1N/A if ( pds[i].fd != NULL ) {
1N/A for ( j = 0; j < PRLDAP_EVENTMAP_ENTRIES; ++j ) {
1N/A if (( pds[i].out_flags & prldap_eventmap[j].evm_nspr )
1N/A != 0 ) {
1N/A fds[i].lpoll_revents |= prldap_eventmap[j].evm_ldap;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Utility function to try one TCP connect()
1N/A * Returns 1 if successful and -1 if not. Sets the NSPR fd inside prsockp.
1N/A */
1N/Astatic int
1N/Aprldap_try_one_address( struct lextiof_socket_private *prsockp,
1N/A PRNetAddr *addrp, int port, int timeout, unsigned long options )
1N/A{
1N/A /*
1N/A * Set up address and open a TCP socket:
1N/A */
1N/A if ( PR_SUCCESS != PR_SetNetAddr( PR_IpAddrNull, /* don't touch IP addr. */
1N/A PRLDAP_DEFAULT_ADDRESS_FAMILY, (PRUint16)port, addrp )) {
1N/A return( -1 );
1N/A }
1N/A
1N/A if (( prsockp->prsock_prfd = PR_OpenTCPSocket(
1N/A PRLDAP_DEFAULT_ADDRESS_FAMILY )) == NULL ) {
1N/A return( -1 );
1N/A }
1N/A
1N/A /*
1N/A * Set nonblocking option if requested:
1N/A */
1N/A if ( 0 != ( options & LDAP_X_EXTIOF_OPT_NONBLOCKING )) {
1N/A PRSocketOptionData optdata;
1N/A
1N/A optdata.option = PR_SockOpt_Nonblocking;
1N/A optdata.value.non_blocking = PR_TRUE;
1N/A if ( PR_SetSocketOption( prsockp->prsock_prfd, &optdata )
1N/A != PR_SUCCESS ) {
1N/A prldap_set_system_errno( prldap_prerr2errno());
1N/A PR_Close( prsockp->prsock_prfd );
1N/A return( -1 );
1N/A }
1N/A }
1N/A
1N/A#ifdef PRLDAP_DEBUG
1N/A {
1N/A char buf[ 256 ], *p, *fmtstr;
1N/A
1N/A if ( PR_SUCCESS != PR_NetAddrToString( addrp, buf, sizeof(buf ))) {
1N/A strcpy( buf, "conversion failed!" );
1N/A }
1N/A if ( strncmp( buf, "::ffff:", 7 ) == 0 ) {
1N/A /* IPv4 address mapped into IPv6 address space */
1N/A p = buf + 7;
1N/A fmtstr = "prldap_try_one_address(): Trying %s:%d...\n";
1N/A } else {
1N/A p = buf;
1N/A fmtstr = "prldap_try_one_address(): Trying [%s]:%d...\n";
1N/A }
1N/A fprintf( stderr, fmtstr, p, PR_ntohs( addrp->ipv6.port ));
1N/A }
1N/A#endif /* PRLDAP_DEBUG */
1N/A
1N/A /*
1N/A * Try to open the TCP connection itself:
1N/A */
1N/A if ( PR_SUCCESS != PR_Connect( prsockp->prsock_prfd, addrp,
1N/A prldap_timeout2it( timeout, prsockp->prsock_io_max_timeout ))) {
1N/A PR_Close( prsockp->prsock_prfd );
1N/A prsockp->prsock_prfd = NULL;
1N/A return( -1 );
1N/A }
1N/A
1N/A#ifdef PRLDAP_DEBUG
1N/A fputs( "prldap_try_one_address(): Connected.\n", stderr );
1N/A#endif /* PRLDAP_DEBUG */
1N/A
1N/A /*
1N/A * Success. Return a valid file descriptor (1 is always valid)
1N/A */
1N/A return( 1 );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * XXXmcs: At present, this code ignores the timeout when doing DNS lookups.
1N/A */
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_connect( const char *hostlist, int defport, int timeout,
1N/A unsigned long options, struct lextiof_session_private *sessionarg,
1N/A struct lextiof_socket_private **socketargp
1N/A#ifdef _SOLARIS_SDK
1N/A , void **dhost )
1N/A#else
1N/A )
1N/A#endif /* _SOLARIS_SDK */
1N/A{
1N/A int rc, parse_err, port;
1N/A char *host, hbuf[ PR_NETDB_BUF_SIZE ];
1N/A struct ldap_x_hostlist_status *status;
1N/A struct lextiof_socket_private *prsockp;
1N/A PRNetAddr addr;
1N/A PRHostEnt hent;
1N/A#ifdef _SOLARIS_SDK
1N/A char *hostname = NULL;
1N/A char *nsldapi_strdup(char *);
1N/A#endif /* _SOLARIS_SDK */
1N/A
1N/A if ( 0 != ( options & LDAP_X_EXTIOF_OPT_SECURE )) {
1N/A prldap_set_system_errno( EINVAL );
1N/A return( -1 );
1N/A }
1N/A
1N/A if ( NULL == ( prsockp = prldap_socket_arg_alloc( sessionarg ))) {
1N/A prldap_set_system_errno( prldap_prerr2errno());
1N/A return( -1 );
1N/A }
1N/A
1N/A rc = -1; /* pessimistic */
1N/A for ( parse_err = ldap_x_hostlist_first( hostlist, defport, &host, &port,
1N/A &status );
1N/A rc < 0 && LDAP_SUCCESS == parse_err && NULL != host;
1N/A parse_err = ldap_x_hostlist_next( &host, &port, status )) {
1N/A
1N/A if ( PR_SUCCESS == PR_StringToNetAddr( host, &addr )) {
1N/A
1N/A if ( PRLDAP_DEFAULT_ADDRESS_FAMILY == PR_AF_INET6 &&
1N/A PR_AF_INET == PR_NetAddrFamily( &addr )) {
1N/A PRUint32 ipv4ip = addr.inet.ip;
1N/A memset( &addr, 0, sizeof(addr));
1N/A PR_ConvertIPv4AddrToIPv6( ipv4ip, &addr.ipv6.ip );
1N/A addr.ipv6.family = PR_AF_INET6;
1N/A
1N/A }
1N/A rc = prldap_try_one_address( prsockp, &addr, port,
1N/A timeout, options );
1N/A } else {
1N/A if ( PR_SUCCESS == PR_GetIPNodeByName( host,
1N/A PRLDAP_DEFAULT_ADDRESS_FAMILY, PR_AI_DEFAULT | PR_AI_ALL, hbuf,
1N/A sizeof( hbuf ), &hent )) {
1N/A PRIntn enumIndex = 0;
1N/A
1N/A while ( rc < 0 && ( enumIndex = PR_EnumerateHostEnt(
1N/A enumIndex, &hent, (PRUint16)port, &addr )) > 0 ) {
1N/A rc = prldap_try_one_address( prsockp, &addr, port,
1N/A timeout, options );
1N/A }
1N/A }
1N/A }
1N/A
1N/A#ifdef _SOLARIS_SDK
1N/A if ( NULL != hostname ) {
1N/A ldap_memfree(hostname);
1N/A hostname = NULL;
1N/A }
1N/A if ( rc >= 0 ) {
1N/A hostname = nsldapi_strdup(host);
1N/A }
1N/A#endif /* _SOLARIS_SDK */
1N/A ldap_memfree( host );
1N/A }
1N/A
1N/A ldap_x_hostlist_statusfree( status );
1N/A
1N/A if ( rc < 0 ) {
1N/A prldap_set_system_errno( prldap_prerr2errno());
1N/A prldap_socket_arg_free( &prsockp );
1N/A } else {
1N/A *socketargp = prsockp;
1N/A }
1N/A
1N/A#ifdef _SOLARIS_SDK
1N/A if ( NULL != hostname && NULL != dhost ) *dhost = hostname;
1N/A else if ( NULL != hostname ) ldap_memfree(hostname);
1N/A#endif /* _SOLARIS_SDK */
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_close( int s, struct lextiof_socket_private *socketarg )
1N/A{
1N/A int rc;
1N/A
1N/A rc = 0;
1N/A if ( PR_Close( PRLDAP_GET_PRFD(socketarg)) != PR_SUCCESS ) {
1N/A rc = -1;
1N/A prldap_set_system_errno( prldap_prerr2errno());
1N/A }
1N/A prldap_socket_arg_free( &socketarg );
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * LDAP session handle creation callback.
1N/A *
1N/A * Allocate a session argument if not already done, and then call the
1N/A * thread's new handle function.
1N/A */
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_newhandle( LDAP *ld, struct lextiof_session_private *sessionarg )
1N/A{
1N/A
1N/A if ( NULL == sessionarg ) {
1N/A struct ldap_x_ext_io_fns iofns;
1N/A
1N/A memset( &iofns, 0, sizeof(iofns));
1N/A iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
1N/A if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS,
1N/A (void *)&iofns ) < 0 ) {
1N/A return( ldap_get_lderrno( ld, NULL, NULL ));
1N/A }
1N/A if ( NULL ==
1N/A ( iofns.lextiof_session_arg = prldap_session_arg_alloc())) {
1N/A return( LDAP_NO_MEMORY );
1N/A }
1N/A if ( ldap_set_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS,
1N/A (void *)&iofns ) < 0 ) {
1N/A return( ldap_get_lderrno( ld, NULL, NULL ));
1N/A }
1N/A }
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/* only called/installed if shared is non-zero. */
1N/Astatic int LDAP_CALLBACK
1N/Aprldap_shared_newhandle( LDAP *ld, struct lextiof_session_private *sessionarg )
1N/A{
1N/A int rc;
1N/A
1N/A if (( rc = prldap_newhandle( ld, sessionarg )) == LDAP_SUCCESS ) {
1N/A rc = prldap_thread_new_handle( ld, sessionarg );
1N/A }
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/Astatic void LDAP_CALLBACK
1N/Aprldap_disposehandle( LDAP *ld, struct lextiof_session_private *sessionarg )
1N/A{
1N/A prldap_session_arg_free( &sessionarg );
1N/A}
1N/A
1N/A
1N/A/* only called/installed if shared is non-zero */
1N/Astatic void LDAP_CALLBACK
1N/Aprldap_shared_disposehandle( LDAP *ld,
1N/A struct lextiof_session_private *sessionarg )
1N/A{
1N/A prldap_thread_dispose_handle( ld, sessionarg );
1N/A prldap_disposehandle( ld, sessionarg );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Allocate a session argument.
1N/A */
1N/Astatic PRLDAPIOSessionArg *
1N/Aprldap_session_arg_alloc( void )
1N/A{
1N/A PRLDAPIOSessionArg *prsessp;
1N/A
1N/A prsessp = PR_Calloc( 1, sizeof( PRLDAPIOSessionArg ));
1N/A
1N/A if ( NULL != prsessp ) {
1N/A /* copy global defaults to the new session handle */
1N/A prsessp->prsess_io_max_timeout = prldap_default_io_max_timeout;
1N/A }
1N/A
1N/A return( prsessp );
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Aprldap_session_arg_free( PRLDAPIOSessionArg **prsesspp )
1N/A{
1N/A if ( NULL != prsesspp && NULL != *prsesspp ) {
1N/A if ( NULL != (*prsesspp)->prsess_pollds ) {
1N/A PR_Free( (*prsesspp)->prsess_pollds );
1N/A (*prsesspp)->prsess_pollds = NULL;
1N/A }
1N/A PR_Free( *prsesspp );
1N/A *prsesspp = NULL;
1N/A }
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Given an LDAP session handle, retrieve a session argument.
1N/A * Returns an LDAP error code.
1N/A */
1N/Aint
1N/Aprldap_session_arg_from_ld( LDAP *ld, PRLDAPIOSessionArg **sessargpp )
1N/A{
1N/A struct ldap_x_ext_io_fns iofns;
1N/A
1N/A if ( NULL == ld || NULL == sessargpp ) {
1N/A /* XXXmcs: NULL ld's are not supported */
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A memset( &iofns, 0, sizeof(iofns));
1N/A iofns.lextiof_size = LDAP_X_EXTIO_FNS_SIZE;
1N/A if ( ldap_get_option( ld, LDAP_X_OPT_EXTIO_FN_PTRS, (void *)&iofns ) < 0 ) {
1N/A return( ldap_get_lderrno( ld, NULL, NULL ));
1N/A }
1N/A
1N/A if ( NULL == iofns.lextiof_session_arg ) {
1N/A ldap_set_lderrno( ld, LDAP_LOCAL_ERROR, NULL, NULL );
1N/A return( LDAP_LOCAL_ERROR );
1N/A }
1N/A
1N/A *sessargpp = iofns.lextiof_session_arg;
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Given an LDAP session handle, retrieve a socket argument.
1N/A * Returns an LDAP error code.
1N/A */
1N/Aint
1N/Aprldap_socket_arg_from_ld( LDAP *ld, PRLDAPIOSocketArg **sockargpp )
1N/A{
1N/A Sockbuf *sbp;
1N/A struct lber_x_ext_io_fns extiofns;
1N/A
1N/A if ( NULL == ld || NULL == sockargpp ) {
1N/A /* XXXmcs: NULL ld's are not supported */
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( ldap_get_option( ld, LDAP_X_OPT_SOCKBUF, (void *)&sbp ) < 0 ) {
1N/A return( ldap_get_lderrno( ld, NULL, NULL ));
1N/A }
1N/A
1N/A memset( &extiofns, 0, sizeof(extiofns));
1N/A extiofns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
1N/A if ( ber_sockbuf_get_option( sbp, LBER_SOCKBUF_OPT_EXT_IO_FNS,
1N/A (void *)&extiofns ) < 0 ) {
1N/A return( ldap_get_lderrno( ld, NULL, NULL ));
1N/A }
1N/A
1N/A if ( NULL == extiofns.lbextiofn_socket_arg ) {
1N/A ldap_set_lderrno( ld, LDAP_LOCAL_ERROR, NULL, NULL );
1N/A return( LDAP_LOCAL_ERROR );
1N/A }
1N/A
1N/A *sockargpp = extiofns.lbextiofn_socket_arg;
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Allocate a socket argument.
1N/A */
1N/Astatic PRLDAPIOSocketArg *
1N/Aprldap_socket_arg_alloc( PRLDAPIOSessionArg *sessionarg )
1N/A{
1N/A PRLDAPIOSocketArg *prsockp;
1N/A
1N/A prsockp = PR_Calloc( 1, sizeof( PRLDAPIOSocketArg ));
1N/A
1N/A if ( NULL != prsockp && NULL != sessionarg ) {
1N/A /* copy socket defaults from the session */
1N/A prsockp->prsock_io_max_timeout = sessionarg->prsess_io_max_timeout;
1N/A }
1N/A
1N/A return( prsockp );
1N/A}
1N/A
1N/A
1N/Astatic void
1N/Aprldap_socket_arg_free( PRLDAPIOSocketArg **prsockpp )
1N/A{
1N/A if ( NULL != prsockpp && NULL != *prsockpp ) {
1N/A PR_Free( *prsockpp );
1N/A *prsockpp = NULL;
1N/A }
1N/A}
1N/A
1N/A
1N/Astatic void *
1N/Aprldap_safe_realloc( void *ptr, PRUint32 size )
1N/A{
1N/A void *p;
1N/A
1N/A if ( NULL == ptr ) {
1N/A p = PR_Malloc( size );
1N/A } else {
1N/A p = PR_Realloc( ptr, size );
1N/A }
1N/A
1N/A return( p );
1N/A}
1N/A
1N/A
1N/A
1N/A/* returns an LDAP result code */
1N/Aint
1N/Aprldap_set_io_max_timeout( PRLDAPIOSessionArg *prsessp, int io_max_timeout )
1N/A{
1N/A int rc = LDAP_SUCCESS; /* optimistic */
1N/A
1N/A if ( NULL == prsessp ) {
1N/A prldap_default_io_max_timeout = io_max_timeout;
1N/A } else {
1N/A prsessp->prsess_io_max_timeout = io_max_timeout;
1N/A }
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/A/* returns an LDAP result code */
1N/Aint
1N/Aprldap_get_io_max_timeout( PRLDAPIOSessionArg *prsessp, int *io_max_timeoutp )
1N/A{
1N/A int rc = LDAP_SUCCESS; /* optimistic */
1N/A
1N/A if ( NULL == io_max_timeoutp ) {
1N/A rc = LDAP_PARAM_ERROR;
1N/A } else if ( NULL == prsessp ) {
1N/A *io_max_timeoutp = prldap_default_io_max_timeout;
1N/A } else {
1N/A *io_max_timeoutp = prsessp->prsess_io_max_timeout;
1N/A }
1N/A
1N/A return( rc );
1N/A}