Back to index

openldap  2.4.31
options.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 
00016 #include "portable.h"
00017 
00018 #include <ac/stdlib.h>
00019 #include <ac/string.h>
00020 #include <ac/stdarg.h>
00021 #include "lber-int.h"
00022 
00023 char ber_pvt_opt_on; /* used to get a non-NULL address for *_OPT_ON */
00024 
00025 struct lber_options ber_int_options = {
00026        LBER_UNINITIALIZED, 0, 0 };
00027 
00028 static BerMemoryFunctions   ber_int_memory_fns_datum;
00029 
00030 int
00031 ber_get_option(
00032        void   *item,
00033        int           option,
00034        void   *outvalue)
00035 {
00036        const BerElement *ber;
00037        const Sockbuf *sb;
00038 
00039        if(outvalue == NULL) {
00040               /* no place to get to */
00041               ber_errno = LBER_ERROR_PARAM;
00042               return LBER_OPT_ERROR;
00043        }
00044 
00045        if(item == NULL) {
00046               switch ( option ) {
00047               case LBER_OPT_BER_DEBUG:
00048                      * (int *) outvalue = ber_int_debug;
00049                      return LBER_OPT_SUCCESS;
00050 
00051               case LBER_OPT_MEMORY_INUSE:
00052                      /* The memory inuse is a global variable on kernal implementations.
00053                       * This means that memory debug is shared by all LDAP processes
00054                       * so for this variable to have much meaning, only one LDAP process
00055                       * should be running and memory inuse should be initialized to zero
00056                       * using the lber_set_option() function during startup.
00057                       * The counter is not accurate for multithreaded ldap applications.
00058                       */
00059 #ifdef LDAP_MEMORY_DEBUG
00060                      * (int *) outvalue = ber_int_meminuse;
00061                      return LBER_OPT_SUCCESS;
00062 #else
00063                      return LBER_OPT_ERROR;
00064 #endif
00065 
00066               case LBER_OPT_LOG_PRINT_FILE:
00067                      *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
00068                      return LBER_OPT_SUCCESS;
00069               }
00070 
00071               ber_errno = LBER_ERROR_PARAM;
00072               return LBER_OPT_ERROR;
00073        }
00074 
00075        ber = item;
00076        sb = item;
00077 
00078        switch(option) {
00079        case LBER_OPT_BER_OPTIONS:
00080               assert( LBER_VALID( ber ) );
00081               * (int *) outvalue = ber->ber_options;
00082               return LBER_OPT_SUCCESS;
00083 
00084        case LBER_OPT_BER_DEBUG:
00085               assert( LBER_VALID( ber ) );
00086               * (int *) outvalue = ber->ber_debug;
00087               return LBER_OPT_SUCCESS;
00088 
00089        case LBER_OPT_BER_REMAINING_BYTES:
00090               assert( LBER_VALID( ber ) );
00091               *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
00092               return LBER_OPT_SUCCESS;
00093 
00094        case LBER_OPT_BER_TOTAL_BYTES:
00095               assert( LBER_VALID( ber ) );
00096               *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
00097               return LBER_OPT_SUCCESS;
00098 
00099        case LBER_OPT_BER_BYTES_TO_WRITE:
00100               assert( LBER_VALID( ber ) );
00101               *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
00102               return LBER_OPT_SUCCESS;
00103 
00104        case LBER_OPT_BER_MEMCTX:
00105               assert( LBER_VALID( ber ) );
00106               *((void **) outvalue) = ber->ber_memctx;
00107               return LBER_OPT_SUCCESS;
00108        
00109        default:
00110               /* bad param */
00111               ber_errno = LBER_ERROR_PARAM;
00112               break;
00113        }
00114 
00115        return LBER_OPT_ERROR;
00116 }
00117 
00118 int
00119 ber_set_option(
00120        void   *item,
00121        int           option,
00122        LDAP_CONST void      *invalue)
00123 {
00124        BerElement *ber;
00125        Sockbuf *sb;
00126 
00127        if(invalue == NULL) {
00128               /* no place to set from */
00129               ber_errno = LBER_ERROR_PARAM;
00130               return LBER_OPT_ERROR;
00131        }
00132 
00133        if(item == NULL) {
00134               switch ( option ) {
00135               case LBER_OPT_BER_DEBUG:
00136                      ber_int_debug = * (const int *) invalue;
00137                      return LBER_OPT_SUCCESS;
00138 
00139               case LBER_OPT_LOG_PRINT_FN:
00140                      ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
00141                      return LBER_OPT_SUCCESS;
00142 
00143               case LBER_OPT_LOG_PRINT_FILE:
00144                      ber_pvt_err_file = (void *) invalue;
00145                      return LBER_OPT_SUCCESS;
00146 
00147               case LBER_OPT_MEMORY_INUSE:
00148                      /* The memory inuse is a global variable on kernal implementations.
00149                       * This means that memory debug is shared by all LDAP processes
00150                       * so for this variable to have much meaning, only one LDAP process
00151                       * should be running and memory inuse should be initialized to zero
00152                       * using the lber_set_option() function during startup.
00153                       * The counter is not accurate for multithreaded applications.
00154                       */
00155 #ifdef LDAP_MEMORY_DEBUG
00156                      ber_int_meminuse = * (int *) invalue;
00157                      return LBER_OPT_SUCCESS;
00158 #else
00159                      return LBER_OPT_ERROR;
00160 #endif
00161               case LBER_OPT_MEMORY_FNS:
00162                      if ( ber_int_memory_fns == NULL )
00163                      {
00164                             const BerMemoryFunctions *f =
00165                                    (const BerMemoryFunctions *) invalue;
00166                             /* make sure all functions are provided */
00167                             if(!( f->bmf_malloc && f->bmf_calloc
00168                                    && f->bmf_realloc && f->bmf_free ))
00169                             {
00170                                    ber_errno = LBER_ERROR_PARAM;
00171                                    return LBER_OPT_ERROR;
00172                             }
00173 
00174                             ber_int_memory_fns = &ber_int_memory_fns_datum;
00175 
00176                             AC_MEMCPY(ber_int_memory_fns, f,
00177                                     sizeof(BerMemoryFunctions));
00178 
00179                             return LBER_OPT_SUCCESS;
00180                      }
00181                      break;
00182 
00183               case LBER_OPT_LOG_PROC:
00184                      ber_int_log_proc = (BER_LOG_FN)invalue;
00185                      return LBER_OPT_SUCCESS;
00186               }
00187 
00188               ber_errno = LBER_ERROR_PARAM;
00189               return LBER_OPT_ERROR;
00190        }
00191 
00192        ber = item;
00193        sb = item;
00194 
00195        switch(option) {
00196        case LBER_OPT_BER_OPTIONS:
00197               assert( LBER_VALID( ber ) );
00198               ber->ber_options = * (const int *) invalue;
00199               return LBER_OPT_SUCCESS;
00200 
00201        case LBER_OPT_BER_DEBUG:
00202               assert( LBER_VALID( ber ) );
00203               ber->ber_debug = * (const int *) invalue;
00204               return LBER_OPT_SUCCESS;
00205 
00206        case LBER_OPT_BER_REMAINING_BYTES:
00207               assert( LBER_VALID( ber ) );
00208               ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
00209               return LBER_OPT_SUCCESS;
00210 
00211        case LBER_OPT_BER_TOTAL_BYTES:
00212               assert( LBER_VALID( ber ) );
00213               ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
00214               return LBER_OPT_SUCCESS;
00215 
00216        case LBER_OPT_BER_BYTES_TO_WRITE:
00217               assert( LBER_VALID( ber ) );
00218               ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
00219               return LBER_OPT_SUCCESS;
00220 
00221        case LBER_OPT_BER_MEMCTX:
00222               assert( LBER_VALID( ber ) );
00223               ber->ber_memctx = *(void **)invalue;
00224               return LBER_OPT_SUCCESS;
00225 
00226        default:
00227               /* bad param */
00228               ber_errno = LBER_ERROR_PARAM;
00229               break;
00230        }
00231 
00232        return LBER_OPT_ERROR;
00233 }