1 /* io.c - ber general i/o routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
27 * This work was originally developed by the University of Michigan
28 * (as part of U-MICH LDAP).
35 #include <ac/stdlib.h>
39 #include <ac/socket.h>
40 #include <ac/string.h>
41 #include <ac/unistd.h>
56 ber_len_t actuallen, nleft;
58 assert( ber != NULL );
59 assert( buf != NULL );
61 assert( LBER_VALID( ber ) );
63 nleft = ber_pvt_ber_remaining( ber );
64 actuallen = nleft < len ? nleft : len;
66 AC_MEMCPY( buf, ber->ber_ptr, actuallen );
68 ber->ber_ptr += actuallen;
70 return( (ber_slen_t) actuallen );
80 assert( ber != NULL );
81 assert( buf != NULL );
83 assert( LBER_VALID( ber ) );
85 if ( nosos || ber->ber_sos == NULL ) {
86 if ( ber->ber_ptr + len > ber->ber_end ) {
87 if ( ber_realloc( ber, len ) != 0 ) return( -1 );
89 AC_MEMCPY( ber->ber_ptr, buf, (size_t)len );
91 return( (ber_slen_t) len );
94 if ( ber->ber_sos->sos_ptr + len > ber->ber_end ) {
95 if ( ber_realloc( ber, len ) != 0 ) return( -1 );
97 AC_MEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
98 ber->ber_sos->sos_ptr += len;
99 ber->ber_sos->sos_clen += len;
100 return( (ber_slen_t) len );
105 ber_realloc( BerElement *ber, ber_len_t len )
112 assert( ber != NULL );
114 assert( LBER_VALID( ber ) );
116 total = ber_pvt_ber_total( ber );
118 #define LBER_EXBUFSIZ 4060 /* a few words less than 2^N for binary buddy */
119 #if defined( LBER_EXBUFSIZ ) && LBER_EXBUFSIZ > 0
121 /* don't realloc by small amounts */
122 total += len < LBER_EXBUFSIZ ? LBER_EXBUFSIZ : len;
124 { /* not sure what value this adds */
125 ber_len_t have = (total + (LBER_EXBUFSIZE - 1)) / LBER_EXBUFSIZ;
126 ber_len_t need = (len + (LBER_EXBUFSIZ - 1)) / LBER_EXBUFSIZ;
127 total = ( have + need ) * LBER_EXBUFSIZ;
131 total += len; /* realloc just what's needed */
134 oldbuf = ber->ber_buf;
136 ber->ber_buf = (char *) ber_memrealloc_x( oldbuf, total, ber->ber_memctx );
138 if ( ber->ber_buf == NULL ) {
139 ber->ber_buf = oldbuf;
143 ber->ber_end = ber->ber_buf + total;
146 * If the stinking thing was moved, we need to go through and
147 * reset all the sos and ber pointers. Offsets would've been
148 * a better idea... oh well.
151 if ( ber->ber_buf != oldbuf ) {
152 ber->ber_ptr = ber->ber_buf + (ber->ber_ptr - oldbuf);
154 for ( s = ber->ber_sos; s != NULL; s = s->sos_next ) {
155 off = s->sos_first - oldbuf;
156 s->sos_first = ber->ber_buf + off;
158 off = s->sos_ptr - oldbuf;
159 s->sos_ptr = ber->ber_buf + off;
167 ber_free_buf( BerElement *ber )
171 assert( LBER_VALID( ber ) );
173 if ( ber->ber_buf) ber_memfree_x( ber->ber_buf, ber->ber_memctx );
175 for( s = ber->ber_sos ; s != NULL ; s = next ) {
177 ber_memfree_x( s, ber->ber_memctx );
182 ber->ber_valid = LBER_UNINITIALIZED;
186 ber_free( BerElement *ber, int freebuf )
188 #ifdef LDAP_MEMORY_DEBUG
189 assert( ber != NULL );
196 if( freebuf ) ber_free_buf( ber );
198 ber_memfree_x( (char *) ber, ber->ber_memctx );
202 ber_flush( Sockbuf *sb, BerElement *ber, int freeit )
207 assert( sb != NULL );
208 assert( ber != NULL );
210 assert( SOCKBUF_VALID( sb ) );
211 assert( LBER_VALID( ber ) );
213 if ( ber->ber_rwptr == NULL ) {
214 ber->ber_rwptr = ber->ber_buf;
216 towrite = ber->ber_ptr - ber->ber_rwptr;
218 if ( sb->sb_debug ) {
219 ber_log_printf( LDAP_DEBUG_TRACE, sb->sb_debug,
220 "ber_flush: %ld bytes to sd %ld%s\n",
221 towrite, (long) sb->sb_fd,
222 ber->ber_rwptr != ber->ber_buf ? " (re-flush)" : "" );
223 ber_log_bprint( LDAP_DEBUG_PACKETS, sb->sb_debug,
224 ber->ber_rwptr, towrite );
227 while ( towrite > 0 ) {
230 rc = ber_int_sb_write( sb, ber->ber_rwptr, 1 );
232 rc = ber_int_sb_write( sb, ber->ber_rwptr, towrite );
238 ber->ber_rwptr += rc;
241 if ( freeit ) ber_free( ber, 1 );
247 ber_alloc_t( int options )
251 ber_int_options.lbo_valid = LBER_INITIALIZED;
253 ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
259 ber->ber_valid = LBER_VALID_BERELEMENT;
260 ber->ber_tag = LBER_DEFAULT;
261 ber->ber_options = options;
262 ber->ber_debug = ber_int_debug;
264 assert( LBER_VALID( ber ) );
269 ber_alloc( void ) /* deprecated */
271 return ber_alloc_t( 0 );
275 der_alloc( void ) /* deprecated */
277 return ber_alloc_t( LBER_USE_DER );
281 ber_dup( BerElement *ber )
285 assert( ber != NULL );
286 assert( LBER_VALID( ber ) );
288 if ( (new = ber_alloc_t( ber->ber_options )) == NULL ) {
294 assert( LBER_VALID( new ) );
300 ber_init2( BerElement *ber, struct berval *bv, int options )
302 assert( ber != NULL );
304 ber_int_options.lbo_valid = LBER_INITIALIZED;
306 (void) memset( (char *)ber, '\0', sizeof( BerElement ));
307 ber->ber_valid = LBER_VALID_BERELEMENT;
308 ber->ber_tag = LBER_DEFAULT;
309 ber->ber_options = (char) options;
310 ber->ber_debug = ber_int_debug;
313 ber->ber_buf = bv->bv_val;
314 ber->ber_ptr = ber->ber_buf;
315 ber->ber_end = ber->ber_buf + bv->bv_len;
318 assert( LBER_VALID( ber ) );
321 /* OLD U-Mich ber_init() */
323 ber_init_w_nullc( BerElement *ber, int options )
325 ber_init2( ber, NULL, options );
328 /* New C-API ber_init() */
329 /* This function constructs a BerElement containing a copy
330 ** of the data in the bv argument.
333 ber_init( struct berval *bv )
337 assert( bv != NULL );
339 ber_int_options.lbo_valid = LBER_INITIALIZED;
345 ber = ber_alloc_t( 0 );
348 /* allocation failed */
353 if ( ((ber_len_t) ber_write ( ber, bv->bv_val, bv->bv_len, 0 ))
356 /* write failed, so free and return NULL */
361 ber_reset( ber, 1 ); /* reset the pointer to the start of the buffer */
365 /* New C-API ber_flatten routine */
366 /* This routine allocates a struct berval whose contents are a BER
367 ** encoding taken from the ber argument. The bvPtr pointer points to
368 ** the returned berval.
370 ** ber_flatten2 is the same, but uses a struct berval passed by
371 ** the caller. If alloc is 0 the returned bv uses the ber buf directly.
378 assert( bv != NULL );
380 ber_int_options.lbo_valid = LBER_INITIALIZED;
387 /* ber is null, create an empty berval */
392 /* copy the berval */
393 ber_len_t len = ber_pvt_ber_write( ber );
396 bv->bv_val = (char *) ber_memalloc_x( len + 1, ber->ber_memctx );
397 if ( bv->bv_val == NULL ) {
400 AC_MEMCPY( bv->bv_val, ber->ber_buf, len );
402 bv->bv_val = ber->ber_buf;
404 bv->bv_val[len] = '\0';
412 struct berval **bvPtr)
417 assert( bvPtr != NULL );
419 ber_int_options.lbo_valid = LBER_INITIALIZED;
425 bv = ber_memalloc_x( sizeof(struct berval), ber->ber_memctx );
429 rc = ber_flatten2(ber, bv, 1);
431 ber_memfree_x(bv, ber->ber_memctx);
439 ber_reset( BerElement *ber, int was_writing )
441 assert( ber != NULL );
442 assert( LBER_VALID( ber ) );
445 ber->ber_end = ber->ber_ptr;
446 ber->ber_ptr = ber->ber_buf;
449 ber->ber_ptr = ber->ber_end;
452 ber->ber_rwptr = NULL;
456 * A rewrite of ber_get_next that can safely be called multiple times
457 * for the same packet. It will simply continue where it stopped until
458 * a full packet is read.
469 assert( sb != NULL );
470 assert( len != NULL );
471 assert( ber != NULL );
473 assert( SOCKBUF_VALID( sb ) );
474 assert( LBER_VALID( ber ) );
476 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
480 * Any ber element looks like this: tag length contents.
481 * Assuming everything's ok, we return the tag byte (we
482 * can assume a single byte), return the length in len,
483 * and the rest of the undecoded element in buf.
486 * 1) small tags (less than 128)
487 * 2) definite lengths
488 * 3) primitive encodings used whenever possible
490 * The code also handles multi-byte tags. The first few bytes
491 * of the message are read to check for multi-byte tags and
492 * lengths. These bytes are temporarily stored in the ber_tag,
493 * ber_len, and ber_usertag fields of the berelement until
494 * tag/len parsing is complete. After this parsing, any leftover
495 * bytes and the rest of the message are copied into the ber_buf.
497 * We expect tag and len to be at most 32 bits wide.
500 if (ber->ber_rwptr == NULL) {
501 assert( ber->ber_buf == NULL );
502 ber->ber_rwptr = (char *) &ber->ber_len-1;
503 ber->ber_ptr = ber->ber_rwptr;
507 while (ber->ber_rwptr > (char *)&ber->ber_tag && ber->ber_rwptr <
508 (char *)&ber->ber_len + LENSIZE*2 -1) {
510 char buf[sizeof(ber->ber_len)-1];
514 sblen=ber_int_sb_read( sb, ber->ber_rwptr,
515 ((char *)&ber->ber_len + LENSIZE*2 - 1)-ber->ber_rwptr);
516 if (sblen<=0) return LBER_DEFAULT;
517 ber->ber_rwptr += sblen;
519 /* We got at least one byte, try to parse the tag. */
520 if (ber->ber_ptr == (char *)&ber->ber_len-1) {
522 unsigned char *p = (unsigned char *)ber->ber_ptr;
524 if ((tag & LBER_BIG_TAG_MASK) == LBER_BIG_TAG_MASK) {
526 for (i=1; (char *)p<ber->ber_rwptr; i++) {
529 if (!(tag & LBER_MORE_TAG_MASK))
531 /* Is the tag too big? */
532 if (i == sizeof(ber_tag_t)-1) {
537 /* Did we run out of bytes? */
538 if ((char *)p == ber->ber_rwptr) {
539 #if defined( EWOULDBLOCK )
541 #elif defined( EAGAIN )
548 ber->ber_ptr = (char *)p;
551 if ( ber->ber_ptr == ber->ber_rwptr ) {
552 #if defined( EWOULDBLOCK )
554 #elif defined( EAGAIN )
560 /* Now look for the length */
561 if (*ber->ber_ptr & 0x80) { /* multi-byte */
563 unsigned char *p = (unsigned char *)ber->ber_ptr;
564 int llen = *p++ & 0x7f;
565 if (llen > (int)sizeof(ber_len_t)) {
569 /* Not enough bytes? */
570 if (ber->ber_rwptr - (char *)p < llen) {
571 #if defined( EWOULDBLOCK )
573 #elif defined( EAGAIN )
578 for (i=0; i<llen; i++) {
582 ber->ber_ptr = (char *)p;
584 tlen = *(unsigned char *)ber->ber_ptr++;
587 /* Are there leftover data bytes inside ber->ber_len? */
588 if (ber->ber_ptr < (char *)&ber->ber_usertag) {
589 if (ber->ber_rwptr < (char *)&ber->ber_usertag) {
590 sblen = ber->ber_rwptr - ber->ber_ptr;
592 sblen = (char *)&ber->ber_usertag - ber->ber_ptr;
594 AC_MEMCPY(buf, ber->ber_ptr, sblen);
595 ber->ber_ptr += sblen;
601 /* now fill the buffer. */
603 /* make sure length is reasonable */
604 if ( ber->ber_len == 0 ) {
609 if ( sb->sb_max_incoming && ber->ber_len > sb->sb_max_incoming ) {
610 ber_log_printf( LDAP_DEBUG_CONNS, ber->ber_debug,
611 "ber_get_next: sockbuf_max_incoming exceeded "
612 "(%ld > %ld)\n", ber->ber_len, sb->sb_max_incoming );
617 if (ber->ber_buf==NULL) {
618 ber_len_t l = ber->ber_rwptr - ber->ber_ptr;
619 /* ber->ber_ptr is always <= ber->ber->ber_rwptr.
620 * make sure ber->ber_len agrees with what we've
623 if ( ber->ber_len < sblen + l ) {
627 ber->ber_buf = (char *) ber_memalloc_x( ber->ber_len + 1, ber->ber_memctx );
628 if (ber->ber_buf==NULL) {
631 ber->ber_end = ber->ber_buf + ber->ber_len;
633 AC_MEMCPY(ber->ber_buf, buf, sblen);
636 AC_MEMCPY(ber->ber_buf + sblen, ber->ber_ptr, l);
639 *ber->ber_end = '\0';
640 ber->ber_ptr = ber->ber_buf;
641 ber->ber_usertag = 0;
642 if ((ber_len_t)sblen == ber->ber_len) {
645 ber->ber_rwptr = ber->ber_buf + sblen;
649 if ((ber->ber_rwptr>=ber->ber_buf) && (ber->ber_rwptr<ber->ber_end)) {
653 to_go = ber->ber_end - ber->ber_rwptr;
657 res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
658 if (res<=0) return LBER_DEFAULT;
662 #if defined( EWOULDBLOCK )
664 #elif defined( EAGAIN )
670 ber->ber_rwptr = NULL;
672 if ( ber->ber_debug ) {
673 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
674 "ber_get_next: tag 0x%lx len %ld contents:\n",
675 ber->ber_tag, ber->ber_len );
676 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
678 return (ber->ber_tag);
681 assert( 0 ); /* ber structure is messed up ?*/
686 ber_start( BerElement* ber )
692 ber_len( BerElement* ber )
694 return ( ber->ber_end - ber->ber_buf );
698 ber_ptrlen( BerElement* ber )
700 return ( ber->ber_ptr - ber->ber_buf );
704 ber_rewind ( BerElement * ber )
706 ber->ber_rwptr = NULL;
708 ber->ber_end = ber->ber_ptr;
709 ber->ber_ptr = ber->ber_buf;
713 ber_remaining( BerElement * ber )
715 return ber_pvt_ber_remaining( ber );