options.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2022 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. #include "portable.h"
  16. #include <ac/stdlib.h>
  17. #include <ac/string.h>
  18. #include <ac/stdarg.h>
  19. #include "lber-int.h"
  20. char ber_pvt_opt_on; /* used to get a non-NULL address for *_OPT_ON */
  21. struct lber_options ber_int_options = {
  22. LBER_UNINITIALIZED, 0, 0 };
  23. static BerMemoryFunctions ber_int_memory_fns_datum;
  24. int
  25. ber_get_option(
  26. void *item,
  27. int option,
  28. void *outvalue)
  29. {
  30. const BerElement *ber;
  31. const Sockbuf *sb;
  32. if(outvalue == NULL) {
  33. /* no place to get to */
  34. ber_errno = LBER_ERROR_PARAM;
  35. return LBER_OPT_ERROR;
  36. }
  37. if(item == NULL) {
  38. switch ( option ) {
  39. case LBER_OPT_BER_DEBUG:
  40. * (int *) outvalue = ber_int_debug;
  41. return LBER_OPT_SUCCESS;
  42. case LBER_OPT_MEMORY_INUSE:
  43. /* The memory inuse is a global variable on kernel implementations.
  44. * This means that memory debug is shared by all LDAP processes
  45. * so for this variable to have much meaning, only one LDAP process
  46. * should be running and memory inuse should be initialized to zero
  47. * using the lber_set_option() function during startup.
  48. * The counter is not accurate for multithreaded ldap applications.
  49. */
  50. #ifdef LDAP_MEMORY_DEBUG
  51. * (int *) outvalue = ber_int_meminuse;
  52. return LBER_OPT_SUCCESS;
  53. #else
  54. return LBER_OPT_ERROR;
  55. #endif
  56. case LBER_OPT_LOG_PRINT_FILE:
  57. *((FILE**)outvalue) = (FILE*)ber_pvt_err_file;
  58. return LBER_OPT_SUCCESS;
  59. case LBER_OPT_LOG_PRINT_FN:
  60. *(BER_LOG_PRINT_FN *)outvalue = ber_pvt_log_print;
  61. return LBER_OPT_SUCCESS;
  62. }
  63. ber_errno = LBER_ERROR_PARAM;
  64. return LBER_OPT_ERROR;
  65. }
  66. ber = item;
  67. sb = item;
  68. switch(option) {
  69. case LBER_OPT_BER_OPTIONS:
  70. assert( LBER_VALID( ber ) );
  71. * (int *) outvalue = ber->ber_options;
  72. return LBER_OPT_SUCCESS;
  73. case LBER_OPT_BER_DEBUG:
  74. assert( LBER_VALID( ber ) );
  75. * (int *) outvalue = ber->ber_debug;
  76. return LBER_OPT_SUCCESS;
  77. case LBER_OPT_BER_REMAINING_BYTES:
  78. assert( LBER_VALID( ber ) );
  79. *((ber_len_t *) outvalue) = ber_pvt_ber_remaining(ber);
  80. return LBER_OPT_SUCCESS;
  81. case LBER_OPT_BER_TOTAL_BYTES:
  82. assert( LBER_VALID( ber ) );
  83. *((ber_len_t *) outvalue) = ber_pvt_ber_total(ber);
  84. return LBER_OPT_SUCCESS;
  85. case LBER_OPT_BER_BYTES_TO_WRITE:
  86. assert( LBER_VALID( ber ) );
  87. *((ber_len_t *) outvalue) = ber_pvt_ber_write(ber);
  88. return LBER_OPT_SUCCESS;
  89. case LBER_OPT_BER_MEMCTX:
  90. assert( LBER_VALID( ber ) );
  91. *((void **) outvalue) = ber->ber_memctx;
  92. return LBER_OPT_SUCCESS;
  93. default:
  94. /* bad param */
  95. ber_errno = LBER_ERROR_PARAM;
  96. break;
  97. }
  98. return LBER_OPT_ERROR;
  99. }
  100. int
  101. ber_set_option(
  102. void *item,
  103. int option,
  104. LDAP_CONST void *invalue)
  105. {
  106. BerElement *ber;
  107. Sockbuf *sb;
  108. if(invalue == NULL) {
  109. /* no place to set from */
  110. ber_errno = LBER_ERROR_PARAM;
  111. return LBER_OPT_ERROR;
  112. }
  113. if(item == NULL) {
  114. switch ( option ) {
  115. case LBER_OPT_BER_DEBUG:
  116. ber_int_debug = * (const int *) invalue;
  117. return LBER_OPT_SUCCESS;
  118. case LBER_OPT_LOG_PRINT_FN:
  119. ber_pvt_log_print = (BER_LOG_PRINT_FN) invalue;
  120. return LBER_OPT_SUCCESS;
  121. case LBER_OPT_LOG_PRINT_FILE:
  122. ber_pvt_err_file = (void *) invalue;
  123. return LBER_OPT_SUCCESS;
  124. case LBER_OPT_MEMORY_INUSE:
  125. /* The memory inuse is a global variable on kernel implementations.
  126. * This means that memory debug is shared by all LDAP processes
  127. * so for this variable to have much meaning, only one LDAP process
  128. * should be running and memory inuse should be initialized to zero
  129. * using the lber_set_option() function during startup.
  130. * The counter is not accurate for multithreaded applications.
  131. */
  132. #ifdef LDAP_MEMORY_DEBUG
  133. ber_int_meminuse = * (int *) invalue;
  134. return LBER_OPT_SUCCESS;
  135. #else
  136. return LBER_OPT_ERROR;
  137. #endif
  138. case LBER_OPT_MEMORY_FNS:
  139. if ( ber_int_memory_fns == NULL )
  140. {
  141. const BerMemoryFunctions *f =
  142. (const BerMemoryFunctions *) invalue;
  143. /* make sure all functions are provided */
  144. if(!( f->bmf_malloc && f->bmf_calloc
  145. && f->bmf_realloc && f->bmf_free ))
  146. {
  147. ber_errno = LBER_ERROR_PARAM;
  148. return LBER_OPT_ERROR;
  149. }
  150. ber_int_memory_fns = &ber_int_memory_fns_datum;
  151. AC_MEMCPY(ber_int_memory_fns, f,
  152. sizeof(BerMemoryFunctions));
  153. return LBER_OPT_SUCCESS;
  154. }
  155. break;
  156. case LBER_OPT_LOG_PROC:
  157. ber_int_log_proc = (BER_LOG_FN)invalue;
  158. return LBER_OPT_SUCCESS;
  159. }
  160. ber_errno = LBER_ERROR_PARAM;
  161. return LBER_OPT_ERROR;
  162. }
  163. ber = item;
  164. sb = item;
  165. switch(option) {
  166. case LBER_OPT_BER_OPTIONS:
  167. assert( LBER_VALID( ber ) );
  168. ber->ber_options = * (const int *) invalue;
  169. return LBER_OPT_SUCCESS;
  170. case LBER_OPT_BER_DEBUG:
  171. assert( LBER_VALID( ber ) );
  172. ber->ber_debug = * (const int *) invalue;
  173. return LBER_OPT_SUCCESS;
  174. case LBER_OPT_BER_REMAINING_BYTES:
  175. assert( LBER_VALID( ber ) );
  176. ber->ber_end = &ber->ber_ptr[* (const ber_len_t *) invalue];
  177. return LBER_OPT_SUCCESS;
  178. case LBER_OPT_BER_TOTAL_BYTES:
  179. assert( LBER_VALID( ber ) );
  180. ber->ber_end = &ber->ber_buf[* (const ber_len_t *) invalue];
  181. return LBER_OPT_SUCCESS;
  182. case LBER_OPT_BER_BYTES_TO_WRITE:
  183. assert( LBER_VALID( ber ) );
  184. ber->ber_ptr = &ber->ber_buf[* (const ber_len_t *) invalue];
  185. return LBER_OPT_SUCCESS;
  186. case LBER_OPT_BER_MEMCTX:
  187. assert( LBER_VALID( ber ) );
  188. ber->ber_memctx = *(void **)invalue;
  189. return LBER_OPT_SUCCESS;
  190. default:
  191. /* bad param */
  192. ber_errno = LBER_ERROR_PARAM;
  193. break;
  194. }
  195. return LBER_OPT_ERROR;
  196. }