1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
|
/* sockbuf.c - i/o routines with support for adding i/o layers. */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2024 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/ctype.h>
#include <ac/errno.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/unistd.h>
#ifdef HAVE_IO_H
#include <io.h>
#endif /* HAVE_IO_H */
#if defined( HAVE_FCNTL_H )
#include <fcntl.h>
#endif
#if defined( HAVE_SYS_FILIO_H )
#include <sys/filio.h>
#elif defined( HAVE_SYS_IOCTL_H )
#include <sys/ioctl.h>
#endif
#include "lber-int.h"
#ifndef LBER_MIN_BUFF_SIZE
#define LBER_MIN_BUFF_SIZE 4096
#endif
#ifndef LBER_MAX_BUFF_SIZE
#define LBER_MAX_BUFF_SIZE (65536*256)
#endif
#ifndef LBER_DEFAULT_READAHEAD
#define LBER_DEFAULT_READAHEAD 16384
#endif
Sockbuf *
ber_sockbuf_alloc( void )
{
Sockbuf *sb;
sb = LBER_CALLOC( 1, sizeof( Sockbuf ) );
if( sb == NULL ) return NULL;
ber_int_sb_init( sb );
return sb;
}
void
ber_sockbuf_free( Sockbuf *sb )
{
assert( sb != NULL );
assert( SOCKBUF_VALID( sb ) );
ber_int_sb_close( sb );
ber_int_sb_destroy( sb );
LBER_FREE( sb );
}
/* Return values: -1: error, 0: no operation performed or the answer is false,
* 1: successful operation or the answer is true
*/
int
ber_sockbuf_ctrl( Sockbuf *sb, int opt, void *arg )
{
Sockbuf_IO_Desc *p;
int ret = 0;
assert( sb != NULL );
assert( SOCKBUF_VALID( sb ) );
switch ( opt ) {
case LBER_SB_OPT_HAS_IO:
p = sb->sb_iod;
while ( p && p->sbiod_io != (Sockbuf_IO *)arg ) {
p = p->sbiod_next;
}
if ( p ) {
ret = 1;
}
break;
case LBER_SB_OPT_GET_FD:
if ( arg != NULL ) {
*((ber_socket_t *)arg) = sb->sb_fd;
}
ret = ( sb->sb_fd == AC_SOCKET_INVALID ? -1 : 1);
break;
case LBER_SB_OPT_SET_FD:
sb->sb_fd = *((ber_socket_t *)arg);
ret = 1;
break;
case LBER_SB_OPT_SET_NONBLOCK:
ret = ber_pvt_socket_set_nonblock( sb->sb_fd, arg != NULL)
? -1 : 1;
break;
case LBER_SB_OPT_DRAIN: {
/* Drain the data source to enable possible errors (e.g.
* TLS) to be propagated to the upper layers
*/
char buf[LBER_MIN_BUFF_SIZE];
do {
ret = ber_int_sb_read( sb, buf, sizeof( buf ) );
} while ( ret == sizeof( buf ) );
ret = 1;
} break;
case LBER_SB_OPT_NEEDS_READ:
ret = ( sb->sb_trans_needs_read ? 1 : 0 );
break;
case LBER_SB_OPT_NEEDS_WRITE:
ret = ( sb->sb_trans_needs_write ? 1 : 0 );
break;
case LBER_SB_OPT_GET_MAX_INCOMING:
if ( arg != NULL ) {
*((ber_len_t *)arg) = sb->sb_max_incoming;
}
ret = 1;
break;
case LBER_SB_OPT_SET_MAX_INCOMING:
sb->sb_max_incoming = *((ber_len_t *)arg);
ret = 1;
break;
case LBER_SB_OPT_UNGET_BUF:
#ifdef LDAP_PF_LOCAL_SENDMSG
sb->sb_ungetlen = ((struct berval *)arg)->bv_len;
if ( sb->sb_ungetlen <= sizeof( sb->sb_ungetbuf )) {
AC_MEMCPY( sb->sb_ungetbuf, ((struct berval *)arg)->bv_val,
sb->sb_ungetlen );
ret = 1;
} else {
sb->sb_ungetlen = 0;
ret = -1;
}
#endif
break;
default:
ret = sb->sb_iod->sbiod_io->sbi_ctrl( sb->sb_iod, opt, arg );
break;
}
return ret;
}
int
ber_sockbuf_add_io( Sockbuf *sb, Sockbuf_IO *sbio, int layer, void *arg )
{
Sockbuf_IO_Desc *d, *p, **q;
assert( sb != NULL );
assert( SOCKBUF_VALID( sb ) );
if ( sbio == NULL ) {
return -1;
}
q = &sb->sb_iod;
p = *q;
while ( p && p->sbiod_level > layer ) {
q = &p->sbiod_next;
p = *q;
}
d = LBER_MALLOC( sizeof( *d ) );
if ( d == NULL ) {
return -1;
}
d->sbiod_level = layer;
d->sbiod_sb = sb;
d->sbiod_io = sbio;
memset( &d->sbiod_pvt, '\0', sizeof( d->sbiod_pvt ) );
d->sbiod_next = p;
*q = d;
if ( sbio->sbi_setup != NULL && ( sbio->sbi_setup( d, arg ) < 0 ) ) {
return -1;
}
return 0;
}
int
ber_sockbuf_remove_io( Sockbuf *sb, Sockbuf_IO *sbio, int layer )
{
Sockbuf_IO_Desc *p, **q;
assert( sb != NULL );
assert( SOCKBUF_VALID( sb ) );
if ( sb->sb_iod == NULL ) {
return -1;
}
q = &sb->sb_iod;
while ( *q != NULL ) {
p = *q;
if ( layer == p->sbiod_level && p->sbiod_io == sbio ) {
if ( p->sbiod_io->sbi_remove != NULL &&
p->sbiod_io->sbi_remove( p ) < 0 )
{
return -1;
}
*q = p->sbiod_next;
LBER_FREE( p );
break;
}
q = &p->sbiod_next;
}
return 0;
}
void
ber_pvt_sb_buf_init( Sockbuf_Buf *buf )
{
buf->buf_base = NULL;
buf->buf_ptr = 0;
buf->buf_end = 0;
buf->buf_size = 0;
}
void
ber_pvt_sb_buf_destroy( Sockbuf_Buf *buf )
{
assert( buf != NULL);
if (buf->buf_base) {
LBER_FREE( buf->buf_base );
}
ber_pvt_sb_buf_init( buf );
}
int
ber_pvt_sb_grow_buffer( Sockbuf_Buf *buf, ber_len_t minsize )
{
ber_len_t pw;
char *p;
assert( buf != NULL );
for ( pw = LBER_MIN_BUFF_SIZE; pw < minsize; pw <<= 1 ) {
if (pw > LBER_MAX_BUFF_SIZE) return -1;
}
if ( buf->buf_size < pw ) {
p = LBER_REALLOC( buf->buf_base, pw );
if ( p == NULL ) return -1;
buf->buf_base = p;
buf->buf_size = pw;
}
return 0;
}
ber_len_t
ber_pvt_sb_copy_out( Sockbuf_Buf *sbb, char *buf, ber_len_t len )
{
ber_len_t max;
assert( buf != NULL );
assert( sbb != NULL );
#if 0
assert( sbb->buf_size > 0 );
#endif
max = sbb->buf_end - sbb->buf_ptr;
max = ( max < len) ? max : len;
if ( max ) {
AC_MEMCPY( buf, sbb->buf_base + sbb->buf_ptr, max );
sbb->buf_ptr += max;
if ( sbb->buf_ptr >= sbb->buf_end ) {
sbb->buf_ptr = sbb->buf_end = 0;
}
}
return max;
}
ber_slen_t
ber_pvt_sb_do_write( Sockbuf_IO_Desc *sbiod, Sockbuf_Buf *buf_out )
{
ber_len_t to_go;
ber_slen_t ret;
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
to_go = buf_out->buf_end - buf_out->buf_ptr;
assert( to_go > 0 );
for(;;) {
ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf_out->buf_base +
buf_out->buf_ptr, to_go );
#ifdef EINTR
if ((ret<0) && (errno==EINTR)) continue;
#endif
break;
}
if ( ret <= 0 ) return ret;
buf_out->buf_ptr += ret;
if (buf_out->buf_ptr == buf_out->buf_end) {
buf_out->buf_end = buf_out->buf_ptr = 0;
}
return ret;
}
int
ber_pvt_socket_set_nonblock( ber_socket_t sd, int nb )
{
#ifdef HAVE_FCNTL
int flags = fcntl( sd, F_GETFL);
if( nb ) {
flags |= O_NONBLOCK;
} else {
flags &= ~O_NONBLOCK;
}
return fcntl( sd, F_SETFL, flags );
#elif defined( FIONBIO )
ioctl_t status = nb ? 1 : 0;
return ioctl( sd, FIONBIO, &status );
#endif
}
int
ber_int_sb_init( Sockbuf *sb )
{
assert( sb != NULL);
sb->sb_valid=LBER_VALID_SOCKBUF;
sb->sb_options = 0;
sb->sb_debug = ber_int_debug;
sb->sb_fd = AC_SOCKET_INVALID;
sb->sb_iod = NULL;
sb->sb_trans_needs_read = 0;
sb->sb_trans_needs_write = 0;
assert( SOCKBUF_VALID( sb ) );
return 0;
}
int
ber_int_sb_close( Sockbuf *sb )
{
Sockbuf_IO_Desc *p;
assert( sb != NULL);
p = sb->sb_iod;
while ( p ) {
if ( p->sbiod_io->sbi_close && p->sbiod_io->sbi_close( p ) < 0 ) {
return -1;
}
p = p->sbiod_next;
}
sb->sb_fd = AC_SOCKET_INVALID;
return 0;
}
int
ber_int_sb_destroy( Sockbuf *sb )
{
Sockbuf_IO_Desc *p;
assert( sb != NULL);
assert( SOCKBUF_VALID( sb ) );
while ( sb->sb_iod ) {
p = sb->sb_iod->sbiod_next;
ber_sockbuf_remove_io( sb, sb->sb_iod->sbiod_io,
sb->sb_iod->sbiod_level );
sb->sb_iod = p;
}
return ber_int_sb_init( sb );
}
ber_slen_t
ber_int_sb_read( Sockbuf *sb, void *buf, ber_len_t len )
{
ber_slen_t ret;
assert( buf != NULL );
assert( sb != NULL);
assert( sb->sb_iod != NULL );
assert( SOCKBUF_VALID( sb ) );
for (;;) {
ret = sb->sb_iod->sbiod_io->sbi_read( sb->sb_iod, buf, len );
#ifdef EINTR
if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
#endif
break;
}
return ret;
}
ber_slen_t
ber_int_sb_write( Sockbuf *sb, void *buf, ber_len_t len )
{
ber_slen_t ret;
assert( buf != NULL );
assert( sb != NULL);
assert( sb->sb_iod != NULL );
assert( SOCKBUF_VALID( sb ) );
for (;;) {
ret = sb->sb_iod->sbiod_io->sbi_write( sb->sb_iod, buf, len );
#ifdef EINTR
if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
#endif
break;
}
return ret;
}
/*
* Support for TCP
*/
static ber_slen_t
sb_stream_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
assert( sbiod != NULL);
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
#if defined(MACOS)
/*
* MacTCP/OpenTransport
*/
return tcpread( sbiod->sbiod_sb->sb_fd, 0, (unsigned char *)buf,
len, NULL );
#elif defined( HAVE_PCNFS ) || \
defined( HAVE_WINSOCK ) || defined ( __BEOS__ )
/*
* PCNFS (under DOS)
*/
/*
* Windows Socket API (under DOS/Windows 3.x)
*/
/*
* 32-bit Windows Socket API (under Windows NT or Windows 95)
*/
return recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
#elif defined( HAVE_NCSA )
/*
* NCSA Telnet TCP/IP stack (under DOS)
*/
return nread( sbiod->sbiod_sb->sb_fd, buf, len );
#else
return read( sbiod->sbiod_sb->sb_fd, buf, len );
#endif
}
static ber_slen_t
sb_stream_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
assert( sbiod != NULL);
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
#if defined(MACOS)
/*
* MacTCP/OpenTransport
*/
#define MAX_WRITE 65535
return tcpwrite( sbiod->sbiod_sb->sb_fd, (unsigned char *)buf,
(len<MAX_WRITE) ? len : MAX_WRITE );
#elif defined( HAVE_PCNFS) \
|| defined( HAVE_WINSOCK) || defined ( __BEOS__ )
/*
* PCNFS (under DOS)
*/
/*
* Windows Socket API (under DOS/Windows 3.x)
*/
/*
* 32-bit Windows Socket API (under Windows NT or Windows 95)
*/
return send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
#elif defined(HAVE_NCSA)
return netwrite( sbiod->sbiod_sb->sb_fd, buf, len );
#elif defined(VMS)
/*
* VMS -- each write must be 64K or smaller
*/
#define MAX_WRITE 65535
return write( sbiod->sbiod_sb->sb_fd, buf,
(len<MAX_WRITE) ? len : MAX_WRITE);
#else
return write( sbiod->sbiod_sb->sb_fd, buf, len );
#endif
}
static int
sb_stream_close( Sockbuf_IO_Desc *sbiod )
{
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
if ( sbiod->sbiod_sb->sb_fd != AC_SOCKET_INVALID )
tcp_close( sbiod->sbiod_sb->sb_fd );
return 0;
}
/* The argument is a pointer to the socket descriptor */
static int
sb_stream_setup( Sockbuf_IO_Desc *sbiod, void *arg ) {
assert( sbiod != NULL );
if ( arg != NULL ) {
sbiod->sbiod_sb->sb_fd = *((int *)arg);
}
return 0;
}
static int
sb_stream_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) {
/* This is an end IO descriptor */
return 0;
}
Sockbuf_IO ber_sockbuf_io_tcp = {
sb_stream_setup, /* sbi_setup */
NULL, /* sbi_remove */
sb_stream_ctrl, /* sbi_ctrl */
sb_stream_read, /* sbi_read */
sb_stream_write, /* sbi_write */
sb_stream_close /* sbi_close */
};
/*
* Support for readahead (UDP needs it)
*/
static int
sb_rdahead_setup( Sockbuf_IO_Desc *sbiod, void *arg )
{
Sockbuf_Buf *p;
assert( sbiod != NULL );
p = LBER_MALLOC( sizeof( *p ) );
if ( p == NULL ) return -1;
ber_pvt_sb_buf_init( p );
if ( arg == NULL ) {
ber_pvt_sb_grow_buffer( p, LBER_DEFAULT_READAHEAD );
} else {
ber_pvt_sb_grow_buffer( p, *((int *)arg) );
}
sbiod->sbiod_pvt = p;
return 0;
}
static int
sb_rdahead_remove( Sockbuf_IO_Desc *sbiod )
{
Sockbuf_Buf *p;
assert( sbiod != NULL );
p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
if ( p->buf_ptr != p->buf_end ) return -1;
ber_pvt_sb_buf_destroy( (Sockbuf_Buf *)(sbiod->sbiod_pvt) );
LBER_FREE( sbiod->sbiod_pvt );
sbiod->sbiod_pvt = NULL;
return 0;
}
static ber_slen_t
sb_rdahead_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
Sockbuf_Buf *p;
ber_slen_t bufptr = 0, ret, max;
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
assert( sbiod->sbiod_next != NULL );
p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
assert( p->buf_size > 0 );
/* Are there anything left in the buffer? */
ret = ber_pvt_sb_copy_out( p, buf, len );
bufptr += ret;
len -= ret;
if ( len == 0 ) return bufptr;
max = p->buf_size - p->buf_end;
ret = 0;
while ( max > 0 ) {
ret = LBER_SBIOD_READ_NEXT( sbiod, p->buf_base + p->buf_end,
max );
#ifdef EINTR
if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
#endif
break;
}
if ( ret < 0 ) {
return ( bufptr ? bufptr : ret );
}
p->buf_end += ret;
bufptr += ber_pvt_sb_copy_out( p, (char *) buf + bufptr, len );
return bufptr;
}
static ber_slen_t
sb_rdahead_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
assert( sbiod != NULL );
assert( sbiod->sbiod_next != NULL );
return LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
}
static int
sb_rdahead_close( Sockbuf_IO_Desc *sbiod )
{
assert( sbiod != NULL );
/* Just erase the buffer */
ber_pvt_sb_buf_destroy((Sockbuf_Buf *)sbiod->sbiod_pvt);
return 0;
}
static int
sb_rdahead_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
{
Sockbuf_Buf *p;
p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
if ( opt == LBER_SB_OPT_DATA_READY ) {
if ( p->buf_ptr != p->buf_end ) {
return 1;
}
} else if ( opt == LBER_SB_OPT_SET_READAHEAD ) {
if ( p->buf_size >= *((ber_len_t *)arg) ) {
return 0;
}
return ( ber_pvt_sb_grow_buffer( p, *((int *)arg) ) ?
-1 : 1 );
}
return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
}
Sockbuf_IO ber_sockbuf_io_readahead = {
sb_rdahead_setup, /* sbi_setup */
sb_rdahead_remove, /* sbi_remove */
sb_rdahead_ctrl, /* sbi_ctrl */
sb_rdahead_read, /* sbi_read */
sb_rdahead_write, /* sbi_write */
sb_rdahead_close /* sbi_close */
};
/*
* Support for simple file IO
*/
static ber_slen_t
sb_fd_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
assert( sbiod != NULL);
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
#ifdef LDAP_PF_LOCAL_SENDMSG
if ( sbiod->sbiod_sb->sb_ungetlen ) {
ber_len_t blen = sbiod->sbiod_sb->sb_ungetlen;
if ( blen > len )
blen = len;
AC_MEMCPY( buf, sbiod->sbiod_sb->sb_ungetbuf, blen );
buf = (char *) buf + blen;
len -= blen;
sbiod->sbiod_sb->sb_ungetlen -= blen;
if ( sbiod->sbiod_sb->sb_ungetlen ) {
AC_MEMCPY( sbiod->sbiod_sb->sb_ungetbuf,
sbiod->sbiod_sb->sb_ungetbuf+blen,
sbiod->sbiod_sb->sb_ungetlen );
}
if ( len == 0 )
return blen;
}
#endif
return read( sbiod->sbiod_sb->sb_fd, buf, len );
}
static ber_slen_t
sb_fd_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
assert( sbiod != NULL);
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
return write( sbiod->sbiod_sb->sb_fd, buf, len );
}
static int
sb_fd_close( Sockbuf_IO_Desc *sbiod )
{
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
if ( sbiod->sbiod_sb->sb_fd != AC_SOCKET_INVALID )
close( sbiod->sbiod_sb->sb_fd );
return 0;
}
/* The argument is a pointer to the file descriptor */
static int
sb_fd_setup( Sockbuf_IO_Desc *sbiod, void *arg ) {
assert( sbiod != NULL );
if ( arg != NULL )
sbiod->sbiod_sb->sb_fd = *((int *)arg);
return 0;
}
static int
sb_fd_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) {
/* This is an end IO descriptor */
return 0;
}
Sockbuf_IO ber_sockbuf_io_fd = {
sb_fd_setup, /* sbi_setup */
NULL, /* sbi_remove */
sb_fd_ctrl, /* sbi_ctrl */
sb_fd_read, /* sbi_read */
sb_fd_write, /* sbi_write */
sb_fd_close /* sbi_close */
};
/*
* Debugging layer
*/
static int
sb_debug_setup( Sockbuf_IO_Desc *sbiod, void *arg )
{
assert( sbiod != NULL );
if ( arg == NULL ) arg = "sockbuf_";
sbiod->sbiod_pvt = LBER_MALLOC( strlen( arg ) + 1 );
if ( sbiod->sbiod_pvt == NULL ) return -1;
strcpy( (char *)sbiod->sbiod_pvt, (char *)arg );
return 0;
}
static int
sb_debug_remove( Sockbuf_IO_Desc *sbiod )
{
assert( sbiod != NULL );
assert( sbiod->sbiod_pvt != NULL );
LBER_FREE( sbiod->sbiod_pvt );
sbiod->sbiod_pvt = NULL;
return 0;
}
static int
sb_debug_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
{
return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
}
static ber_slen_t
sb_debug_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
ber_slen_t ret;
char ebuf[128];
ret = LBER_SBIOD_READ_NEXT( sbiod, buf, len );
if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
int err = sock_errno();
if ( ret < 0 ) {
ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
"%sread: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt,
(long)len, AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
} else {
ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
"%sread: want=%ld, got=%ld\n", (char *)sbiod->sbiod_pvt,
(long)len, (long)ret );
ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
(const char *)buf, ret );
}
sock_errset(err);
}
return ret;
}
static ber_slen_t
sb_debug_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
ber_slen_t ret;
char ebuf[128];
ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
int err = sock_errno();
if ( ret < 0 ) {
ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
"%swrite: want=%ld error=%s\n",
(char *)sbiod->sbiod_pvt, (long)len,
AC_STRERROR_R( err, ebuf, sizeof ebuf ) );
} else {
ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
"%swrite: want=%ld, written=%ld\n",
(char *)sbiod->sbiod_pvt, (long)len, (long)ret );
ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
(const char *)buf, ret );
}
sock_errset(err);
}
return ret;
}
Sockbuf_IO ber_sockbuf_io_debug = {
sb_debug_setup, /* sbi_setup */
sb_debug_remove, /* sbi_remove */
sb_debug_ctrl, /* sbi_ctrl */
sb_debug_read, /* sbi_read */
sb_debug_write, /* sbi_write */
NULL /* sbi_close */
};
#ifdef LDAP_CONNECTIONLESS
/*
* Support for UDP (CLDAP)
*
* All I/O at this level must be atomic. For ease of use, the sb_readahead
* must be used above this module. All data reads and writes are prefixed
* with a sockaddr_storage containing the address of the remote entity. Upper levels
* must read and write this sockaddr_storage before doing the usual ber_printf/scanf
* operations on LDAP messages.
*/
static int
sb_dgram_setup( Sockbuf_IO_Desc *sbiod, void *arg )
{
assert( sbiod != NULL);
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
if ( arg != NULL ) sbiod->sbiod_sb->sb_fd = *((int *)arg);
return 0;
}
static ber_slen_t
sb_dgram_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
ber_slen_t rc;
ber_socklen_t addrlen;
struct sockaddr *src;
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
assert( buf != NULL );
addrlen = sizeof( struct sockaddr_storage );
src = buf;
buf = (char *) buf + addrlen;
len -= addrlen;
rc = recvfrom( sbiod->sbiod_sb->sb_fd, buf, len, 0, src, &addrlen );
return rc > 0 ? rc+sizeof(struct sockaddr_storage) : rc;
}
static ber_slen_t
sb_dgram_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
{
ber_slen_t rc;
struct sockaddr *dst;
socklen_t dstsize;
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
assert( buf != NULL );
dst = buf;
buf = (char *) buf + sizeof( struct sockaddr_storage );
len -= sizeof( struct sockaddr_storage );
dstsize = dst->sa_family == AF_INET ? sizeof( struct sockaddr_in )
#ifdef LDAP_PF_INET6
: dst->sa_family == AF_INET6 ? sizeof( struct sockaddr_in6 )
#endif
: sizeof( struct sockaddr_storage );
rc = sendto( sbiod->sbiod_sb->sb_fd, buf, len, 0, dst, dstsize );
if ( rc < 0 ) return -1;
/* fake error if write was not atomic */
if (rc < len) {
# ifdef EMSGSIZE
errno = EMSGSIZE;
# endif
return -1;
}
rc = len + sizeof(struct sockaddr_storage);
return rc;
}
static int
sb_dgram_close( Sockbuf_IO_Desc *sbiod )
{
assert( sbiod != NULL );
assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
if ( sbiod->sbiod_sb->sb_fd != AC_SOCKET_INVALID )
tcp_close( sbiod->sbiod_sb->sb_fd );
return 0;
}
static int
sb_dgram_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
{
/* This is an end IO descriptor */
return 0;
}
Sockbuf_IO ber_sockbuf_io_udp =
{
sb_dgram_setup, /* sbi_setup */
NULL, /* sbi_remove */
sb_dgram_ctrl, /* sbi_ctrl */
sb_dgram_read, /* sbi_read */
sb_dgram_write, /* sbi_write */
sb_dgram_close /* sbi_close */
};
#endif /* LDAP_CONNECTIONLESS */
|