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