1N/A/*
1N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
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 * Public interface for libprldap -- use NSPR (Netscape Portable Runtime)
1N/A * I/O, threads, etc. with libldap.
1N/A *
1N/A */
1N/A
1N/A#include "ldappr-int.h"
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_init().
1N/A *
1N/A * Create a new LDAP session handle, but with NSPR I/O, threading, and DNS
1N/A * functions installed.
1N/A *
1N/A * Pass a non-zero value for the 'shared' parameter if you plan to use
1N/A * this LDAP * handle from more than one thread.
1N/A *
1N/A * prldap_init() returns an LDAP session handle (or NULL if an error occurs).
1N/A */
1N/ALDAP * LDAP_CALL
1N/Aprldap_init( const char *defhost, int defport, int shared )
1N/A{
1N/A LDAP *ld;
1N/A
1N/A if (( ld = ldap_init( defhost, defport )) != NULL ) {
1N/A if ( prldap_install_routines( ld, shared ) != LDAP_SUCCESS ) {
1N/A prldap_set_system_errno( EINVAL ); /* XXXmcs: just a guess! */
1N/A ldap_unbind( ld );
1N/A ld = NULL;
1N/A }
1N/A }
1N/A
1N/A return( ld );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_install_routines().
1N/A *
1N/A * Install NSPR I/O, threading, and DNS functions so they will be used by
1N/A * 'ld'.
1N/A *
1N/A * If 'ld' is NULL, the functions are installed as the default functions
1N/A * for all new LDAP * handles).
1N/A *
1N/A * Pass a non-zero value for the 'shared' parameter if you plan to use
1N/A * this LDAP * handle from more than one thread.
1N/A *
1N/A * prldap_install_routines() returns an LDAP API error code (LDAP_SUCCESS
1N/A * if all goes well).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_install_routines( LDAP *ld, int shared )
1N/A{
1N/A
1N/A if ( prldap_install_io_functions( ld, shared ) != 0
1N/A || prldap_install_thread_functions( ld, shared ) != 0
1N/A || prldap_install_dns_functions( ld ) != 0 ) {
1N/A return( ldap_get_lderrno( ld, NULL, NULL ));
1N/A }
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_set_session_option().
1N/A *
1N/A * Given an LDAP session handle or a session argument such is passed to
1N/A * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, set
1N/A * an option that affects the prldap layer.
1N/A *
1N/A * If 'ld' and 'session" are both NULL, the option is set as the default
1N/A * for all new prldap sessions.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_set_session_option( LDAP *ld, void *sessionarg, int option, ... )
1N/A{
1N/A int rc = LDAP_SUCCESS; /* optimistic */
1N/A PRLDAPIOSessionArg *prsessp = NULL;
1N/A va_list ap;
1N/A
1N/A if ( NULL != ld ) {
1N/A if ( LDAP_SUCCESS !=
1N/A ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
1N/A return( rc );
1N/A }
1N/A } else if ( NULL != sessionarg ) {
1N/A prsessp = (PRLDAPIOSessionArg *)sessionarg;
1N/A }
1N/A
1N/A va_start( ap, option );
1N/A switch ( option ) {
1N/A case PRLDAP_OPT_IO_MAX_TIMEOUT:
1N/A rc = prldap_set_io_max_timeout( prsessp, va_arg( ap, int ));
1N/A break;
1N/A default:
1N/A rc = LDAP_PARAM_ERROR;
1N/A }
1N/A va_end( ap );
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_get_session_option().
1N/A *
1N/A * Given an LDAP session handle or a session argument such is passed to
1N/A * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, retrieve
1N/A * the setting for an option that affects the prldap layer.
1N/A *
1N/A * If 'ld' and 'session" are both NULL, the default option value for all new
1N/A * new prldap sessions is retrieved.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
1N/A */
1N/Aint LDAP_CALL prldap_get_session_option( LDAP *ld, void *sessionarg,
1N/A int option, ... )
1N/A{
1N/A int rc = LDAP_SUCCESS; /* optimistic */
1N/A PRLDAPIOSessionArg *prsessp = NULL;
1N/A va_list ap;
1N/A
1N/A if ( NULL != ld ) {
1N/A if ( LDAP_SUCCESS !=
1N/A ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
1N/A return( rc );
1N/A }
1N/A } else if ( NULL != sessionarg ) {
1N/A prsessp = (PRLDAPIOSessionArg *)sessionarg;
1N/A }
1N/A
1N/A va_start( ap, option );
1N/A switch ( option ) {
1N/A case PRLDAP_OPT_IO_MAX_TIMEOUT:
1N/A rc = prldap_get_io_max_timeout( prsessp, va_arg( ap, int * ));
1N/A break;
1N/A default:
1N/A rc = LDAP_PARAM_ERROR;
1N/A }
1N/A va_end( ap );
1N/A
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_set_session_info().
1N/A *
1N/A * Given an LDAP session handle, set some application-specific data.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_set_session_info( LDAP *ld, void *sessionarg, PRLDAPSessionInfo *seip )
1N/A{
1N/A int rc;
1N/A PRLDAPIOSessionArg *prsessp;
1N/A
1N/A if ( seip == NULL || PRLDAP_SESSIONINFO_SIZE != seip->seinfo_size ) {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( NULL != ld ) {
1N/A if ( LDAP_SUCCESS !=
1N/A ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
1N/A return( rc );
1N/A }
1N/A } else if ( NULL != sessionarg ) {
1N/A prsessp = (PRLDAPIOSessionArg *)sessionarg;
1N/A } else {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A prsessp->prsess_appdata = seip->seinfo_appdata;
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_get_session_info().
1N/A *
1N/A * Given an LDAP session handle, retrieve some application-specific data.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
1N/A * which case the fields in the structure that seip points to are filled in).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_get_session_info( LDAP *ld, void *sessionarg, PRLDAPSessionInfo *seip )
1N/A{
1N/A int rc;
1N/A PRLDAPIOSessionArg *prsessp;
1N/A
1N/A if ( seip == NULL || PRLDAP_SESSIONINFO_SIZE != seip->seinfo_size ) {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( NULL != ld ) {
1N/A if ( LDAP_SUCCESS !=
1N/A ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
1N/A return( rc );
1N/A }
1N/A } else if ( NULL != sessionarg ) {
1N/A prsessp = (PRLDAPIOSessionArg *)sessionarg;
1N/A } else {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A seip->seinfo_appdata = prsessp->prsess_appdata;
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_set_socket_info().
1N/A *
1N/A * Given an integer fd and a void * argument such as those passed to the
1N/A * extended I/O callback functions, set socket specific information.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
1N/A *
1N/A * Note: it is only safe to change soinfo_prfd from within the SOCKET
1N/A * extended I/O callback function.
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_set_socket_info( int fd, void *socketarg, PRLDAPSocketInfo *soip )
1N/A{
1N/A PRLDAPIOSocketArg *prsockp;
1N/A
1N/A if ( NULL == socketarg || NULL == soip ||
1N/A PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A prsockp = (PRLDAPIOSocketArg *)socketarg;
1N/A prsockp->prsock_prfd = soip->soinfo_prfd;
1N/A prsockp->prsock_appdata = soip->soinfo_appdata;
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_get_socket_info().
1N/A *
1N/A * Given an integer fd and a void * argument such as those passed to the
1N/A * extended I/O callback functions, retrieve socket specific information.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
1N/A * which case the fields in the structure that soip points to are filled in).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_get_socket_info( int fd, void *socketarg, PRLDAPSocketInfo *soip )
1N/A{
1N/A PRLDAPIOSocketArg *prsockp;
1N/A
1N/A if ( NULL == socketarg || NULL == soip ||
1N/A PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A prsockp = (PRLDAPIOSocketArg *)socketarg;
1N/A soip->soinfo_prfd = prsockp->prsock_prfd;
1N/A soip->soinfo_appdata = prsockp->prsock_appdata;
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A/*
1N/A * Function: prldap_get_default_socket_info().
1N/A *
1N/A * Given an LDAP session handle, retrieve socket specific information.
1N/A * If ld is NULL, LDAP_PARAM_ERROR is returned.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
1N/A * which case the fields in the structure that soip points to are filled in).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_get_default_socket_info( LDAP *ld, PRLDAPSocketInfo *soip )
1N/A{
1N/A int rc;
1N/A PRLDAPIOSocketArg *prsockp;
1N/A
1N/A
1N/A if ( NULL == soip || PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( NULL != ld ) {
1N/A if ( LDAP_SUCCESS !=
1N/A ( rc = prldap_socket_arg_from_ld( ld, &prsockp ))) {
1N/A return( rc );
1N/A }
1N/A } else {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A soip->soinfo_prfd = prsockp->prsock_prfd;
1N/A soip->soinfo_appdata = prsockp->prsock_appdata;
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Function: prldap_set_default_socket_info().
1N/A *
1N/A * Given an LDAP session handle, set socket specific information.
1N/A * If ld is NULL, LDAP_PARAM_ERROR is returned.
1N/A *
1N/A * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
1N/A * which case the fields in the structure that soip points to are filled in).
1N/A */
1N/Aint LDAP_CALL
1N/Aprldap_set_default_socket_info( LDAP *ld, PRLDAPSocketInfo *soip )
1N/A{
1N/A int rc;
1N/A PRLDAPIOSocketArg *prsockp;
1N/A
1N/A
1N/A if ( NULL == soip || PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( NULL != ld ) {
1N/A if ( LDAP_SUCCESS !=
1N/A ( rc = prldap_socket_arg_from_ld( ld, &prsockp ))) {
1N/A return( rc );
1N/A }
1N/A } else {
1N/A ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A prsockp->prsock_prfd = soip->soinfo_prfd;
1N/A prsockp->prsock_appdata = soip->soinfo_appdata;
1N/A
1N/A return( LDAP_SUCCESS );
1N/A}