Back to index

lightning-sunbird  0.9+nobinonly
ldappr-public.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Public interface for libprldap -- use NSPR (Netscape Portable Runtime)
00040  * I/O, threads, etc. with libldap.
00041  *
00042  */
00043 
00044 #include "ldappr-int.h"
00045 
00046 
00047 /*
00048  * Function: prldap_init().
00049  *
00050  * Create a new LDAP session handle, but with NSPR I/O, threading, and DNS
00051  * functions installed.
00052  *
00053  * Pass a non-zero value for the 'shared' parameter if you plan to use
00054  * this LDAP * handle from more than one thread.
00055  *
00056  * prldap_init() returns an LDAP session handle (or NULL if an error occurs).
00057  */
00058 LDAP * LDAP_CALL
00059 prldap_init( const char *defhost, int defport, int shared )
00060 {
00061     LDAP      *ld;
00062 
00063     if (( ld = ldap_init( defhost, defport )) != NULL ) {
00064        if ( prldap_install_routines( ld, shared ) != LDAP_SUCCESS ) {
00065            prldap_set_system_errno( EINVAL );    /* XXXmcs: just a guess! */
00066            ldap_unbind( ld );
00067            ld = NULL;
00068        }
00069     }
00070 
00071     return( ld );
00072 }
00073 
00074 
00075 /*
00076  * Function: prldap_install_routines().
00077  *
00078  * Install NSPR I/O, threading, and DNS functions so they will be used by
00079  * 'ld'.
00080  *
00081  * If 'ld' is NULL, the functions are installed as the default functions
00082  * for all new LDAP * handles).
00083  *
00084  * Pass a non-zero value for the 'shared' parameter if you plan to use
00085  * this LDAP * handle from more than one thread.
00086  *
00087  * prldap_install_routines() returns an LDAP API error code (LDAP_SUCCESS
00088  * if all goes well).
00089  */
00090 int LDAP_CALL
00091 prldap_install_routines( LDAP *ld, int shared )
00092 {
00093 
00094     if ( prldap_install_io_functions( ld, shared ) != 0
00095               || prldap_install_thread_functions( ld, shared ) != 0
00096               || prldap_install_dns_functions( ld ) != 0 ) {
00097        return( ldap_get_lderrno( ld, NULL, NULL ));
00098     }
00099 
00100     return( LDAP_SUCCESS );
00101 }
00102 
00103 
00104 /*
00105  * Function: prldap_set_session_option().
00106  * 
00107  * Given an LDAP session handle or a session argument such is passed to
00108  * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, set
00109  * an option that affects the prldap layer.
00110  * 
00111  * If 'ld' and 'session" are both NULL, the option is set as the default
00112  * for all new prldap sessions. 
00113  * 
00114  * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
00115  */
00116 int LDAP_CALL
00117 prldap_set_session_option( LDAP *ld, void *sessionarg, int option, ... )
00118 {
00119     int                            rc = LDAP_SUCCESS;   /* optimistic */
00120     PRLDAPIOSessionArg             *prsessp = NULL;
00121     va_list                 ap;
00122 
00123     if ( NULL != ld ) {
00124        if ( LDAP_SUCCESS !=
00125               ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
00126            return( rc );
00127        }
00128     } else if ( NULL != sessionarg ) {
00129        prsessp = (PRLDAPIOSessionArg *)sessionarg;
00130     }
00131 
00132     va_start( ap, option );
00133     switch ( option ) {
00134     case PRLDAP_OPT_IO_MAX_TIMEOUT:
00135        rc = prldap_set_io_max_timeout( prsessp, va_arg( ap, int ));
00136        break;
00137     default:
00138        rc = LDAP_PARAM_ERROR;
00139     }
00140     va_end( ap );
00141 
00142     return( rc );
00143 }
00144 
00145 
00146 /*
00147  * Function: prldap_get_session_option().
00148  *
00149  * Given an LDAP session handle or a session argument such is passed to
00150  * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, retrieve
00151  * the setting for an option that affects the prldap layer.
00152  *
00153  * If 'ld' and 'session" are both NULL, the default option value for all new
00154  * new prldap sessions is retrieved.
00155  *
00156  * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
00157  */
00158 int LDAP_CALL prldap_get_session_option( LDAP *ld, void *sessionarg,
00159         int option, ... )
00160 {
00161     int                            rc = LDAP_SUCCESS;   /* optimistic */
00162     PRLDAPIOSessionArg             *prsessp = NULL;
00163     va_list                 ap;
00164 
00165     if ( NULL != ld ) {
00166        if ( LDAP_SUCCESS !=
00167               ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
00168            return( rc );
00169        }
00170     } else if ( NULL != sessionarg ) {
00171        prsessp = (PRLDAPIOSessionArg *)sessionarg;
00172     }
00173 
00174     va_start( ap, option );
00175     switch ( option ) {
00176     case PRLDAP_OPT_IO_MAX_TIMEOUT:
00177        rc = prldap_get_io_max_timeout( prsessp, va_arg( ap, int * ));
00178        break;
00179     default:
00180        rc = LDAP_PARAM_ERROR;
00181     }
00182     va_end( ap );
00183 
00184     return( rc );
00185 }
00186 
00187 
00188 /*
00189  * Function: prldap_set_session_info().
00190  *
00191  * Given an LDAP session handle, set some application-specific data.
00192  *
00193  * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
00194  */
00195 int LDAP_CALL
00196 prldap_set_session_info( LDAP *ld, void *sessionarg, PRLDAPSessionInfo *seip )
00197 {
00198     int                            rc;
00199     PRLDAPIOSessionArg             *prsessp;
00200 
00201     if ( seip == NULL || PRLDAP_SESSIONINFO_SIZE != seip->seinfo_size ) {
00202        ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
00203        return( LDAP_PARAM_ERROR );
00204     }
00205 
00206     if ( NULL != ld ) {
00207        if ( LDAP_SUCCESS !=
00208               ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
00209            return( rc );
00210        }
00211     } else if ( NULL != sessionarg ) {
00212        prsessp = (PRLDAPIOSessionArg *)sessionarg;
00213     } else {
00214        ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
00215        return( LDAP_PARAM_ERROR );
00216     }
00217 
00218     prsessp->prsess_appdata = seip->seinfo_appdata;
00219     return( LDAP_SUCCESS );
00220 }
00221 
00222 
00223 /*
00224  * Function: prldap_get_session_info().
00225  *
00226  * Given an LDAP session handle, retrieve some application-specific data.
00227  *
00228  * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
00229  * which case the fields in the structure that seip points to are filled in).
00230  */
00231 int LDAP_CALL
00232 prldap_get_session_info( LDAP *ld, void *sessionarg, PRLDAPSessionInfo *seip )
00233 {
00234     int                            rc;
00235     PRLDAPIOSessionArg             *prsessp;
00236 
00237     if ( seip == NULL || PRLDAP_SESSIONINFO_SIZE != seip->seinfo_size ) {
00238        ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
00239        return( LDAP_PARAM_ERROR );
00240     }
00241 
00242     if ( NULL != ld ) {
00243        if ( LDAP_SUCCESS !=
00244               ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) {
00245            return( rc );
00246        }
00247     } else if ( NULL != sessionarg ) {
00248        prsessp = (PRLDAPIOSessionArg *)sessionarg;
00249     } else {
00250        ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL );
00251        return( LDAP_PARAM_ERROR );
00252     }
00253 
00254     seip->seinfo_appdata = prsessp->prsess_appdata;
00255     return( LDAP_SUCCESS );
00256 }
00257 
00258 
00259 /*
00260  * Function: prldap_set_socket_info().
00261  *
00262  * Given an integer fd and a void * argument such as those passed to the
00263  * extended I/O callback functions, set socket specific information.
00264  *
00265  * Returns an LDAP API error code (LDAP_SUCCESS if all goes well).
00266  *
00267  * Note: it is only safe to change soinfo_prfd from within the SOCKET
00268  * extended I/O callback function.
00269  */
00270 int LDAP_CALL
00271 prldap_set_socket_info( int fd, void *socketarg, PRLDAPSocketInfo *soip )
00272 {
00273     PRLDAPIOSocketArg       *prsockp;
00274 
00275     if ( NULL == socketarg || NULL == soip ||
00276               PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) {
00277        return( LDAP_PARAM_ERROR );
00278     }
00279 
00280     prsockp = (PRLDAPIOSocketArg *)socketarg;
00281     prsockp->prsock_prfd = soip->soinfo_prfd;
00282     prsockp->prsock_appdata = soip->soinfo_appdata;
00283 
00284     return( LDAP_SUCCESS );
00285 }
00286 
00287 
00288 /*
00289  * Function: prldap_get_socket_info().
00290  *
00291  * Given an integer fd and a void * argument such as those passed to the
00292  * extended I/O callback functions, retrieve socket specific information.
00293  *
00294  * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in
00295  * which case the fields in the structure that soip points to are filled in).
00296  */
00297 int LDAP_CALL
00298 prldap_get_socket_info( int fd, void *socketarg, PRLDAPSocketInfo *soip )
00299 {
00300     PRLDAPIOSocketArg       *prsockp;
00301 
00302     if ( NULL == socketarg || NULL == soip ||
00303               PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) {
00304        return( LDAP_PARAM_ERROR );
00305     }
00306 
00307     prsockp = (PRLDAPIOSocketArg *)socketarg;
00308     soip->soinfo_prfd = prsockp->prsock_prfd;
00309     soip->soinfo_appdata = prsockp->prsock_appdata;
00310 
00311     return( LDAP_SUCCESS );
00312 }