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