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