1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate 
7*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public License
13*7c478bd9Sstevel@tonic-gate  * Version 1.0 (the "NPL"); you may not use this file except in
14*7c478bd9Sstevel@tonic-gate  * compliance with the NPL.  You may obtain a copy of the NPL at
15*7c478bd9Sstevel@tonic-gate  * http://www.mozilla.org/NPL/
16*7c478bd9Sstevel@tonic-gate  *
17*7c478bd9Sstevel@tonic-gate  * Software distributed under the NPL is distributed on an "AS IS" basis,
18*7c478bd9Sstevel@tonic-gate  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
19*7c478bd9Sstevel@tonic-gate  * for the specific language governing rights and limitations under the
20*7c478bd9Sstevel@tonic-gate  * NPL.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
26*7c478bd9Sstevel@tonic-gate  */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990 Regents of the University of Michigan.
30*7c478bd9Sstevel@tonic-gate  * All rights reserved.
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
33*7c478bd9Sstevel@tonic-gate  * provided that this notice is preserved and that due credit is given
34*7c478bd9Sstevel@tonic-gate  * to the University of Michigan at Ann Arbor. The name of the University
35*7c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
36*7c478bd9Sstevel@tonic-gate  * software without specific prior written permission. This software
37*7c478bd9Sstevel@tonic-gate  * is provided ``as is'' without express or implied warranty.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate /* lbet-int.h - internal header file for liblber */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #ifndef _LBERINT_H
42*7c478bd9Sstevel@tonic-gate #define _LBERINT_H
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
45*7c478bd9Sstevel@tonic-gate extern "C" {
46*7c478bd9Sstevel@tonic-gate #endif
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #include <stdio.h>
49*7c478bd9Sstevel@tonic-gate #include <ctype.h>
50*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
51*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
52*7c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
53*7c478bd9Sstevel@tonic-gate #include <sasl/sasl.h>
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #ifdef macintosh
57*7c478bd9Sstevel@tonic-gate # include "ldap-macos.h"
58*7c478bd9Sstevel@tonic-gate #else /* macintosh */
59*7c478bd9Sstevel@tonic-gate #if !defined(BSDI)
60*7c478bd9Sstevel@tonic-gate # include <malloc.h>
61*7c478bd9Sstevel@tonic-gate #endif
62*7c478bd9Sstevel@tonic-gate # include <errno.h>
63*7c478bd9Sstevel@tonic-gate # include <sys/types.h>
64*7c478bd9Sstevel@tonic-gate #if defined(SUNOS4) || defined(SCOOS)
65*7c478bd9Sstevel@tonic-gate # include <sys/time.h>
66*7c478bd9Sstevel@tonic-gate #endif
67*7c478bd9Sstevel@tonic-gate #if defined( _WINDOWS )
68*7c478bd9Sstevel@tonic-gate #  define WIN32_LEAN_AND_MEAN
69*7c478bd9Sstevel@tonic-gate #  include <windows.h>
70*7c478bd9Sstevel@tonic-gate #  include <time.h>
71*7c478bd9Sstevel@tonic-gate /* No stderr in a 16-bit Windows DLL */
72*7c478bd9Sstevel@tonic-gate #  if defined(_WINDLL) && !defined(_WIN32)
73*7c478bd9Sstevel@tonic-gate #    define USE_DBG_WIN
74*7c478bd9Sstevel@tonic-gate #  endif
75*7c478bd9Sstevel@tonic-gate # else
76*7c478bd9Sstevel@tonic-gate #if !defined(XP_OS2)
77*7c478bd9Sstevel@tonic-gate /* #  include <sys/varargs.h> */
78*7c478bd9Sstevel@tonic-gate #  include <sys/socket.h>
79*7c478bd9Sstevel@tonic-gate #  include <netinet/in.h>
80*7c478bd9Sstevel@tonic-gate #  include <unistd.h>
81*7c478bd9Sstevel@tonic-gate #endif
82*7c478bd9Sstevel@tonic-gate # endif /* defined( _WINDOWS ) */
83*7c478bd9Sstevel@tonic-gate #endif /* macintosh */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate #include <memory.h>
86*7c478bd9Sstevel@tonic-gate #include <string.h>
87*7c478bd9Sstevel@tonic-gate #include "portable.h"
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #ifdef _WINDOWS
90*7c478bd9Sstevel@tonic-gate #include <winsock.h>
91*7c478bd9Sstevel@tonic-gate #include <io.h>
92*7c478bd9Sstevel@tonic-gate #endif /* _WINDOWS */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate #ifdef XP_OS2
95*7c478bd9Sstevel@tonic-gate #include <os2sock.h>
96*7c478bd9Sstevel@tonic-gate #include <io.h>
97*7c478bd9Sstevel@tonic-gate #endif /* XP_OS2 */
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /* No stderr in a 16-bit Windows DLL */
100*7c478bd9Sstevel@tonic-gate #if defined(_WINDLL) && !defined(_WIN32)
101*7c478bd9Sstevel@tonic-gate #define stderr NULL
102*7c478bd9Sstevel@tonic-gate #endif
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate #include "lber.h"
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
107*7c478bd9Sstevel@tonic-gate #include <libintl.h>
108*7c478bd9Sstevel@tonic-gate #include "solaris-int.h"
109*7c478bd9Sstevel@tonic-gate #endif
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate #ifdef macintosh
112*7c478bd9Sstevel@tonic-gate #define NSLDAPI_LBER_SOCKET_IS_PTR
113*7c478bd9Sstevel@tonic-gate #endif
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate #define OLD_LBER_SEQUENCE	0x10	/* w/o constructed bit - broken */
116*7c478bd9Sstevel@tonic-gate #define OLD_LBER_SET		0x11	/* w/o constructed bit - broken */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate #ifndef _IFP
119*7c478bd9Sstevel@tonic-gate #define _IFP
120*7c478bd9Sstevel@tonic-gate typedef int (LDAP_C LDAP_CALLBACK *IFP)();
121*7c478bd9Sstevel@tonic-gate #endif
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate typedef struct seqorset {
124*7c478bd9Sstevel@tonic-gate 	ber_len_t	sos_clen;
125*7c478bd9Sstevel@tonic-gate 	ber_tag_t	sos_tag;
126*7c478bd9Sstevel@tonic-gate 	char		*sos_first;
127*7c478bd9Sstevel@tonic-gate 	char		*sos_ptr;
128*7c478bd9Sstevel@tonic-gate 	struct seqorset	*sos_next;
129*7c478bd9Sstevel@tonic-gate } Seqorset;
130*7c478bd9Sstevel@tonic-gate #define NULLSEQORSET	((Seqorset *) 0)
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate #define SOS_STACK_SIZE 8 /* depth of the pre-allocated sos structure stack */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate struct berelement {
135*7c478bd9Sstevel@tonic-gate 	char		*ber_buf;
136*7c478bd9Sstevel@tonic-gate 	char		*ber_ptr;
137*7c478bd9Sstevel@tonic-gate 	char		*ber_end;
138*7c478bd9Sstevel@tonic-gate 	struct seqorset	*ber_sos;
139*7c478bd9Sstevel@tonic-gate 	ber_tag_t	ber_tag;
140*7c478bd9Sstevel@tonic-gate 	ber_len_t	ber_len;
141*7c478bd9Sstevel@tonic-gate 	int		ber_usertag;
142*7c478bd9Sstevel@tonic-gate 	char		ber_options;
143*7c478bd9Sstevel@tonic-gate 	char		*ber_rwptr;
144*7c478bd9Sstevel@tonic-gate 	BERTranslateProc ber_encode_translate_proc;
145*7c478bd9Sstevel@tonic-gate 	BERTranslateProc ber_decode_translate_proc;
146*7c478bd9Sstevel@tonic-gate 	int		ber_flags;
147*7c478bd9Sstevel@tonic-gate #define LBER_FLAG_NO_FREE_BUFFER	1	/* don't free ber_buf */
148*7c478bd9Sstevel@tonic-gate 	int		ber_sos_stack_posn;
149*7c478bd9Sstevel@tonic-gate 	Seqorset	ber_sos_stack[SOS_STACK_SIZE];
150*7c478bd9Sstevel@tonic-gate };
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate #ifndef _SOLARIS_SDK
153*7c478bd9Sstevel@tonic-gate #define NULLBER ((BerElement *)NULL)
154*7c478bd9Sstevel@tonic-gate #endif
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
157*7c478bd9Sstevel@tonic-gate void ber_dump( BerElement *ber, int inout );
158*7c478bd9Sstevel@tonic-gate #endif
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /*
163*7c478bd9Sstevel@tonic-gate  * structure for read/write I/O callback functions.
164*7c478bd9Sstevel@tonic-gate  */
165*7c478bd9Sstevel@tonic-gate struct nslberi_io_fns {
166*7c478bd9Sstevel@tonic-gate     LDAP_IOF_READ_CALLBACK	*lbiof_read;
167*7c478bd9Sstevel@tonic-gate     LDAP_IOF_WRITE_CALLBACK	*lbiof_write;
168*7c478bd9Sstevel@tonic-gate };
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate struct sockbuf {
172*7c478bd9Sstevel@tonic-gate 	LBER_SOCKET	sb_sd;
173*7c478bd9Sstevel@tonic-gate 	BerElement	sb_ber;
174*7c478bd9Sstevel@tonic-gate 	int		sb_naddr;	/* > 0 implies using CLDAP (UDP) */
175*7c478bd9Sstevel@tonic-gate 	void		*sb_useaddr;	/* pointer to sockaddr to use next */
176*7c478bd9Sstevel@tonic-gate 	void		*sb_fromaddr;	/* pointer to message source sockaddr */
177*7c478bd9Sstevel@tonic-gate 	void		**sb_addrs;	/* actually an array of pointers to
178*7c478bd9Sstevel@tonic-gate 					   sockaddrs */
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	int		sb_options;	/* to support copying ber elements */
181*7c478bd9Sstevel@tonic-gate 	LBER_SOCKET	sb_copyfd;	/* for LBER_SOCKBUF_OPT_TO_FILE* opts */
182*7c478bd9Sstevel@tonic-gate 	ber_uint_t	sb_max_incoming;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	struct nslberi_io_fns
185*7c478bd9Sstevel@tonic-gate 			sb_io_fns;	/* classic I/O callback functions */
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	struct lber_x_ext_io_fns
188*7c478bd9Sstevel@tonic-gate 			sb_ext_io_fns;	/* extended I/O callback functions */
189*7c478bd9Sstevel@tonic-gate #ifdef LDAP_SASLIO_HOOKS
190*7c478bd9Sstevel@tonic-gate         sasl_conn_t     *sb_sasl_ctx;   /* pointer to sasl context */
191*7c478bd9Sstevel@tonic-gate         char            *sb_sasl_ibuf;  /* sasl decrypted input buffer */
192*7c478bd9Sstevel@tonic-gate         char            *sb_sasl_iptr;  /* current location in buffer */
193*7c478bd9Sstevel@tonic-gate         int             sb_sasl_bfsz;   /* Alloc'd size of input buffer */
194*7c478bd9Sstevel@tonic-gate         int             sb_sasl_ilen;   /* remaining length to process */
195*7c478bd9Sstevel@tonic-gate         struct lber_x_ext_io_fns
196*7c478bd9Sstevel@tonic-gate                         sb_sasl_fns;    /* sasl redirect copy ext I/O funcs */
197*7c478bd9Sstevel@tonic-gate         void            *sb_sasl_prld;  /* reverse ld pointer for callbacks */
198*7c478bd9Sstevel@tonic-gate #endif
199*7c478bd9Sstevel@tonic-gate };
200*7c478bd9Sstevel@tonic-gate #define NULLSOCKBUF	((Sockbuf *)NULL)
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate #ifndef NSLBERI_LBER_INT_FRIEND
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * Everything from this point on is excluded if NSLBERI_LBER_INT_FRIEND is
206*7c478bd9Sstevel@tonic-gate  * defined.  The code under ../libraries/libldap defines this.
207*7c478bd9Sstevel@tonic-gate  */
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate #define READBUFSIZ	8192
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate /*
212*7c478bd9Sstevel@tonic-gate  * macros used to check validity of data structures and parameters
213*7c478bd9Sstevel@tonic-gate  */
214*7c478bd9Sstevel@tonic-gate #define NSLBERI_VALID_BERELEMENT_POINTER( ber ) \
215*7c478bd9Sstevel@tonic-gate 	( (ber) != NULLBER )
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate #define NSLBERI_VALID_SOCKBUF_POINTER( sb ) \
218*7c478bd9Sstevel@tonic-gate 	( (sb) != NULLSOCKBUF )
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate #if defined(_WIN32) && defined(_ALPHA)
222*7c478bd9Sstevel@tonic-gate #define LBER_HTONL( _l ) \
223*7c478bd9Sstevel@tonic-gate 	    ((((_l)&0xff)<<24) + (((_l)&0xff00)<<8) + \
224*7c478bd9Sstevel@tonic-gate 	     (((_l)&0xff0000)>>8) + (((_l)&0xff000000)>>24))
225*7c478bd9Sstevel@tonic-gate #define LBER_NTOHL(_l) LBER_HTONL(_l)
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate #elif !defined(__alpha) || defined(VMS)
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate #define LBER_HTONL( l )	htonl( l )
230*7c478bd9Sstevel@tonic-gate #define LBER_NTOHL( l )	ntohl( l )
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate #else /* __alpha */
233*7c478bd9Sstevel@tonic-gate /*
234*7c478bd9Sstevel@tonic-gate  * htonl and ntohl on the DEC Alpha under OSF 1 seem to only swap the
235*7c478bd9Sstevel@tonic-gate  * lower-order 32-bits of a (64-bit) long, so we define correct versions
236*7c478bd9Sstevel@tonic-gate  * here.
237*7c478bd9Sstevel@tonic-gate  */
238*7c478bd9Sstevel@tonic-gate #define LBER_HTONL( l )	(((long)htonl( (l) & 0x00000000FFFFFFFF )) << 32 \
239*7c478bd9Sstevel@tonic-gate     			| htonl( ( (l) & 0xFFFFFFFF00000000 ) >> 32 ))
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate #define LBER_NTOHL( l )	(((long)ntohl( (l) & 0x00000000FFFFFFFF )) << 32 \
242*7c478bd9Sstevel@tonic-gate     			| ntohl( ( (l) & 0xFFFFFFFF00000000 ) >> 32 ))
243*7c478bd9Sstevel@tonic-gate #endif /* __alpha */
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate /* function prototypes */
247*7c478bd9Sstevel@tonic-gate #ifdef LDAP_DEBUG
248*7c478bd9Sstevel@tonic-gate void lber_bprint( char *data, int len );
249*7c478bd9Sstevel@tonic-gate #endif
250*7c478bd9Sstevel@tonic-gate void ber_err_print( char *data );
251*7c478bd9Sstevel@tonic-gate void *nslberi_malloc( size_t size );
252*7c478bd9Sstevel@tonic-gate void *nslberi_calloc( size_t nelem, size_t elsize );
253*7c478bd9Sstevel@tonic-gate void *nslberi_realloc( void *ptr, size_t size );
254*7c478bd9Sstevel@tonic-gate void nslberi_free( void *ptr );
255*7c478bd9Sstevel@tonic-gate int nslberi_ber_realloc( BerElement *ber, ber_len_t len );
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate /* blame: dboreham
260*7c478bd9Sstevel@tonic-gate  * slapd spends much of its time doing memcpy's for the ber code.
261*7c478bd9Sstevel@tonic-gate  * Most of these are single-byte, so we special-case those and speed
262*7c478bd9Sstevel@tonic-gate  * things up considerably.
263*7c478bd9Sstevel@tonic-gate  */
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate #ifdef sunos4
266*7c478bd9Sstevel@tonic-gate #define THEMEMCPY( d, s, n )	bcopy( s, d, n )
267*7c478bd9Sstevel@tonic-gate #else /* sunos4 */
268*7c478bd9Sstevel@tonic-gate #define THEMEMCPY( d, s, n )	memmove( d, s, n )
269*7c478bd9Sstevel@tonic-gate #endif /* sunos4 */
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate #ifdef SAFEMEMCPY
272*7c478bd9Sstevel@tonic-gate #undef SAFEMEMCPY
273*7c478bd9Sstevel@tonic-gate #define SAFEMEMCPY(d,s,n) if (1 == n) *((char*)d) = *((char*)s); else THEMEMCPY(d,s,n);
274*7c478bd9Sstevel@tonic-gate #endif
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * Memory allocation done in liblber should all go through one of the
278*7c478bd9Sstevel@tonic-gate  * following macros. This is so we can plug-in alternative memory
279*7c478bd9Sstevel@tonic-gate  * allocators, etc. as the need arises.
280*7c478bd9Sstevel@tonic-gate  */
281*7c478bd9Sstevel@tonic-gate #define NSLBERI_MALLOC( size )		nslberi_malloc( size )
282*7c478bd9Sstevel@tonic-gate #define NSLBERI_CALLOC( nelem, elsize )	nslberi_calloc( nelem, elsize )
283*7c478bd9Sstevel@tonic-gate #define NSLBERI_REALLOC( ptr, size )	nslberi_realloc( ptr, size )
284*7c478bd9Sstevel@tonic-gate #define NSLBERI_FREE( ptr )		nslberi_free( ptr )
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate /* allow the library to access the debug variable */
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate extern int lber_debug;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate #endif /* !NSLBERI_LBER_INT_FRIEND */
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate #endif
296*7c478bd9Sstevel@tonic-gate #endif /* _LBERINT_H */
297