Back to index

openldap  2.4.31
lber-int.h
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
00016  * All rights reserved.
00017  *
00018  * Redistribution and use in source and binary forms are permitted
00019  * provided that this notice is preserved and that due credit is given
00020  * to the University of Michigan at Ann Arbor. The name of the University
00021  * may not be used to endorse or promote products derived from this
00022  * software without specific prior written permission. This software
00023  * is provided ``as is'' without express or implied warranty.
00024  */
00025 
00026 #ifndef _LBER_INT_H
00027 #define _LBER_INT_H
00028 
00029 #include "lber.h"
00030 #include "ldap_log.h"
00031 #include "lber_pvt.h"
00032 #include "ldap_queue.h"
00033 
00034 LDAP_BEGIN_DECL
00035 
00036 typedef void (*BER_LOG_FN)(FILE *file,
00037        const char *subsys, int level, const char *fmt, ... );
00038 
00039 LBER_V (BER_ERRNO_FN) ber_int_errno_fn;
00040 
00041 #ifdef LDAP_MEMORY_TRACE
00042 # ifndef LDAP_MEMORY_DEBUG
00043 #  define LDAP_MEMORY_DEBUG 1
00044 # endif
00045 #endif
00046 
00047 #ifdef LDAP_MEMORY_DEBUG
00048 LBER_V (long) ber_int_meminuse;
00049 #endif
00050 #if defined(LDAP_MEMORY_DEBUG) && ((LDAP_MEMORY_DEBUG +0) & 2)
00051 # define LDAP_MEMORY_DEBUG_ASSERT assert
00052 #else
00053 # define LDAP_MEMORY_DEBUG_ASSERT(expr) ((void) 0)
00054 #endif
00055 
00056 struct lber_options {
00057        short lbo_valid;
00058        unsigned short              lbo_options;
00059        int                  lbo_debug;
00060 };
00061 
00062 LBER_F( int ) ber_pvt_log_output(
00063        const char *subsystem,
00064        int level,
00065        const char *fmt, ... );
00066 
00067 #define LBER_UNINITIALIZED         0x0
00068 #define LBER_INITIALIZED           0x1
00069 #define LBER_VALID_BERELEMENT      0x2
00070 #define LBER_VALID_SOCKBUF         0x3
00071 
00072 LBER_V (struct lber_options) ber_int_options;
00073 #define ber_int_debug ber_int_options.lbo_debug
00074 
00075 /* Data encoded in ASN.1 BER format */
00076 struct berelement {
00077        struct        lber_options ber_opts;
00078 #define ber_valid           ber_opts.lbo_valid
00079 #define ber_options         ber_opts.lbo_options
00080 #define ber_debug           ber_opts.lbo_debug
00081 
00082        /*
00083         * The members below, when not NULL/LBER_DEFAULT/etc, are:
00084         *   ber_buf       Data buffer.  Other pointers normally point into it.
00085         *   ber_rwptr     Read/write cursor for Sockbuf I/O.
00086         *   ber_memctx    Context passed to ber_memalloc() & co.
00087         * When decoding data (reading it from the BerElement):
00088         *   ber_end       End of BER data.
00089         *   ber_ptr       Read cursor, except for 1st octet of tags.
00090         *   ber_tag       1st octet of next tag, saved from *ber_ptr when
00091         *                 ber_ptr may be pointing at a tag and is >ber_buf.
00092         *                 The octet *ber_ptr itself may get overwritten with
00093         *                 a \0, to terminate the preceding element.
00094         * When encoding data (writing it to the BerElement):
00095         *   ber_end       End of allocated buffer - 1 (allowing a final \0).
00096         *   ber_ptr       Last complete BER element (normally write cursor).
00097         *   ber_sos_ptr   NULL or write cursor for incomplete sequence or set.
00098         *   ber_sos_inner offset(seq/set length octets) if ber_sos_ptr!=NULL.
00099         *   ber_tag       Default tag for next ber_printf() element.
00100         *   ber_usertag   Boolean set by ber_printf "!" if it sets ber_tag.
00101         *   ber_len       Reused for ber_sos_inner.
00102         * When output to a Sockbuf:
00103         *   ber_ptr       End of encoded data to write.
00104         * When input from a Sockbuf:
00105         *   See ber_get_next().
00106         */
00107 
00108        /* Do not change the order of these 3 fields! see ber_get_next */
00109        ber_tag_t     ber_tag;
00110        ber_len_t     ber_len;
00111        ber_tag_t     ber_usertag;
00112 
00113        char          *ber_buf;
00114        char          *ber_ptr;
00115        char          *ber_end;
00116 
00117        char          *ber_sos_ptr;
00118 #      define        ber_sos_inner ber_len /* reused for binary compat */
00119 
00120        char          *ber_rwptr;
00121        void          *ber_memctx;
00122 };
00123 #define LBER_VALID(ber)     ((ber)->ber_valid==LBER_VALID_BERELEMENT)
00124 
00125 #define ber_pvt_ber_remaining(ber) ((ber)->ber_end - (ber)->ber_ptr)
00126 #define ber_pvt_ber_total(ber)            ((ber)->ber_end - (ber)->ber_buf)
00127 #define ber_pvt_ber_write(ber)            ((ber)->ber_ptr - (ber)->ber_buf)
00128 
00129 struct sockbuf {
00130        struct lber_options sb_opts;
00131        Sockbuf_IO_Desc             *sb_iod;             /* I/O functions */
00132 #define       sb_valid             sb_opts.lbo_valid
00133 #define       sb_options           sb_opts.lbo_options
00134 #define       sb_debug             sb_opts.lbo_debug
00135        ber_socket_t         sb_fd;
00136        ber_len_t                   sb_max_incoming;
00137        unsigned int         sb_trans_needs_read:1;
00138        unsigned int         sb_trans_needs_write:1;
00139 #ifdef LDAP_PF_LOCAL_SENDMSG
00140        char                        sb_ungetlen;
00141        char                        sb_ungetbuf[8];
00142 #endif
00143 };
00144 
00145 #define SOCKBUF_VALID( sb ) ( (sb)->sb_valid == LBER_VALID_SOCKBUF )
00146 
00147 
00148 /*
00149  * decode.c, encode.c
00150  */
00151 
00152 /* Simplest OID max-DER-component to implement in both decode and encode */
00153 #define LBER_OID_COMPONENT_MAX ((unsigned long)-1 - 128)
00154 
00155 
00156 /*
00157  * io.c
00158  */
00159 LBER_F( int )
00160 ber_realloc LDAP_P((
00161        BerElement *ber,
00162        ber_len_t len ));
00163 
00164 LBER_F (char *) ber_start LDAP_P(( BerElement * ));
00165 LBER_F (int) ber_len LDAP_P(( BerElement * ));
00166 LBER_F (int) ber_ptrlen LDAP_P(( BerElement * ));
00167 LBER_F (void) ber_rewind LDAP_P(( BerElement * ));
00168 
00169 /*
00170  * bprint.c
00171  */
00172 #define ber_log_printf ber_pvt_log_printf
00173 
00174 LBER_F( int )
00175 ber_log_bprint LDAP_P((
00176        int errlvl,
00177        int loglvl,
00178        const char *data,
00179        ber_len_t len ));
00180 
00181 LBER_F( int )
00182 ber_log_dump LDAP_P((
00183        int errlvl,
00184        int loglvl,
00185        BerElement *ber,
00186        int inout ));
00187 
00188 LBER_V (BER_LOG_FN) ber_int_log_proc;
00189 LBER_V (FILE *) ber_pvt_err_file;
00190 
00191 /* memory.c */
00192        /* simple macros to realloc for now */
00193 LBER_V (BerMemoryFunctions *)      ber_int_memory_fns;
00194 LBER_F (char *)      ber_strndup( LDAP_CONST char *, ber_len_t );
00195 LBER_F (char *)      ber_strndup_x( LDAP_CONST char *, ber_len_t, void *ctx );
00196 
00197 #define LBER_MALLOC(s)             ber_memalloc((s))
00198 #define LBER_CALLOC(n,s)    ber_memcalloc((n),(s))
00199 #define LBER_REALLOC(p,s)   ber_memrealloc((p),(s))
00200 #define LBER_FREE(p)        ber_memfree((p))     
00201 #define LBER_VFREE(v)              ber_memvfree((void**)(v))
00202 #define LBER_STRDUP(s)             ber_strdup((s))
00203 #define LBER_STRNDUP(s,l)   ber_strndup((s),(l))
00204 
00205 /* sockbuf.c */
00206 
00207 LBER_F(       int )
00208 ber_int_sb_init LDAP_P(( Sockbuf *sb ));
00209 
00210 LBER_F( int )
00211 ber_int_sb_close LDAP_P(( Sockbuf *sb ));
00212 
00213 LBER_F(       int )
00214 ber_int_sb_destroy LDAP_P(( Sockbuf *sb ));
00215 
00216 LBER_F( ber_slen_t )
00217 ber_int_sb_read LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len ));
00218 
00219 LBER_F( ber_slen_t )
00220 ber_int_sb_write LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len ));
00221 
00222 LDAP_END_DECL
00223 
00224 #endif /* _LBER_INT_H */