1 /* sockbuf.c - i/o routines with support for adding i/o layers. */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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>.
21 #include <ac/stdlib.h>
25 #include <ac/socket.h>
26 #include <ac/string.h>
27 #include <ac/unistd.h>
31 #endif /* HAVE_IO_H */
33 #if defined( HAVE_FCNTL_H )
37 #if defined( HAVE_SYS_FILIO_H )
38 #include <sys/filio.h>
39 #elif defined( HAVE_SYS_IOCTL_H )
40 #include <sys/ioctl.h>
45 #ifndef LBER_MIN_BUFF_SIZE
46 #define LBER_MIN_BUFF_SIZE 4096
48 #ifndef LBER_MAX_BUFF_SIZE
49 #define LBER_MAX_BUFF_SIZE (65536*256)
51 #ifndef LBER_DEFAULT_READAHEAD
52 #define LBER_DEFAULT_READAHEAD 16384
56 ber_sockbuf_alloc( void )
60 sb = LBER_CALLOC( 1, sizeof( Sockbuf ) );
62 if( sb == NULL ) return NULL;
64 ber_int_sb_init( sb );
69 ber_sockbuf_free( Sockbuf *sb )
72 assert( SOCKBUF_VALID( sb ) );
74 ber_int_sb_close( sb );
75 ber_int_sb_destroy( sb );
79 /* Return values: -1: error, 0: no operation performed or the answer is false,
80 * 1: successful operation or the answer is true
83 ber_sockbuf_ctrl( Sockbuf *sb, int opt, void *arg )
89 assert( SOCKBUF_VALID( sb ) );
92 case LBER_SB_OPT_HAS_IO:
94 while ( p && p->sbiod_io != (Sockbuf_IO *)arg ) {
103 case LBER_SB_OPT_GET_FD:
105 *((ber_socket_t *)arg) = sb->sb_fd;
107 ret = ( sb->sb_fd == AC_SOCKET_INVALID ? -1 : 1);
110 case LBER_SB_OPT_SET_FD:
111 sb->sb_fd = *((ber_socket_t *)arg);
115 case LBER_SB_OPT_SET_NONBLOCK:
116 ret = ber_pvt_socket_set_nonblock( sb->sb_fd, arg != NULL)
120 case LBER_SB_OPT_DRAIN: {
121 /* Drain the data source to enable possible errors (e.g.
122 * TLS) to be propagated to the upper layers
124 char buf[LBER_MIN_BUFF_SIZE];
127 ret = ber_int_sb_read( sb, buf, sizeof( buf ) );
128 } while ( ret == sizeof( buf ) );
133 case LBER_SB_OPT_NEEDS_READ:
134 ret = ( sb->sb_trans_needs_read ? 1 : 0 );
137 case LBER_SB_OPT_NEEDS_WRITE:
138 ret = ( sb->sb_trans_needs_write ? 1 : 0 );
141 case LBER_SB_OPT_GET_MAX_INCOMING:
143 *((ber_len_t *)arg) = sb->sb_max_incoming;
148 case LBER_SB_OPT_SET_MAX_INCOMING:
149 sb->sb_max_incoming = *((ber_len_t *)arg);
154 ret = sb->sb_iod->sbiod_io->sbi_ctrl( sb->sb_iod, opt, arg );
162 ber_sockbuf_add_io( Sockbuf *sb, Sockbuf_IO *sbio, int layer, void *arg )
164 Sockbuf_IO_Desc *d, *p, **q;
166 assert( sb != NULL );
167 assert( SOCKBUF_VALID( sb ) );
169 if ( sbio == NULL ) {
175 while ( p && p->sbiod_level > layer ) {
180 d = LBER_MALLOC( sizeof( *d ) );
185 d->sbiod_level = layer;
188 memset( &d->sbiod_pvt, '\0', sizeof( d->sbiod_pvt ) );
192 if ( sbio->sbi_setup != NULL && ( sbio->sbi_setup( d, arg ) < 0 ) ) {
200 ber_sockbuf_remove_io( Sockbuf *sb, Sockbuf_IO *sbio, int layer )
202 Sockbuf_IO_Desc *p, **q;
204 assert( sb != NULL );
205 assert( SOCKBUF_VALID( sb ) );
207 if ( sb->sb_iod == NULL ) {
212 while ( *q != NULL ) {
214 if ( layer == p->sbiod_level && p->sbiod_io == sbio ) {
215 if ( p->sbiod_io->sbi_remove != NULL &&
216 p->sbiod_io->sbi_remove( p ) < 0 )
231 ber_pvt_sb_buf_init( Sockbuf_Buf *buf )
233 buf->buf_base = NULL;
240 ber_pvt_sb_buf_destroy( Sockbuf_Buf *buf )
242 assert( buf != NULL);
245 LBER_FREE( buf->buf_base );
247 ber_pvt_sb_buf_init( buf );
251 ber_pvt_sb_grow_buffer( Sockbuf_Buf *buf, ber_len_t minsize )
256 assert( buf != NULL );
258 for ( pw = LBER_MIN_BUFF_SIZE; pw < minsize; pw <<= 1 ) {
259 if (pw > LBER_MAX_BUFF_SIZE) return -1;
262 if ( buf->buf_size < pw ) {
263 p = LBER_REALLOC( buf->buf_base, pw );
264 if ( p == NULL ) return -1;
272 ber_pvt_sb_copy_out( Sockbuf_Buf *sbb, char *buf, ber_len_t len )
276 assert( buf != NULL );
277 assert( sbb != NULL );
279 assert( sbb->buf_size > 0 );
282 max = sbb->buf_end - sbb->buf_ptr;
283 max = ( max < len) ? max : len;
285 AC_MEMCPY( buf, sbb->buf_base + sbb->buf_ptr, max );
287 if ( sbb->buf_ptr >= sbb->buf_end ) {
288 sbb->buf_ptr = sbb->buf_end = 0;
295 ber_pvt_sb_do_write( Sockbuf_IO_Desc *sbiod, Sockbuf_Buf *buf_out )
300 assert( sbiod != NULL );
301 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
303 to_go = buf_out->buf_end - buf_out->buf_ptr;
307 ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf_out->buf_base +
308 buf_out->buf_ptr, to_go );
310 if ((ret<0) && (errno==EINTR)) continue;
315 if ( ret <= 0 ) return ret;
317 buf_out->buf_ptr += ret;
318 if (buf_out->buf_ptr == buf_out->buf_end) {
319 buf_out->buf_end = buf_out->buf_ptr = 0;
326 ber_pvt_socket_set_nonblock( ber_socket_t sd, int nb )
329 int flags = fcntl( sd, F_GETFL);
333 flags &= ~O_NONBLOCK;
335 return fcntl( sd, F_SETFL, flags );
337 #elif defined( FIONBIO )
338 ioctl_t status = nb ? 1 : 0;
339 return ioctl( sd, FIONBIO, &status );
344 ber_int_sb_init( Sockbuf *sb )
348 sb->sb_valid=LBER_VALID_SOCKBUF;
350 sb->sb_debug = ber_int_debug;
351 sb->sb_fd = AC_SOCKET_INVALID;
353 sb->sb_trans_needs_read = 0;
354 sb->sb_trans_needs_write = 0;
356 assert( SOCKBUF_VALID( sb ) );
361 ber_int_sb_close( Sockbuf *sb )
369 if ( p->sbiod_io->sbi_close && p->sbiod_io->sbi_close( p ) < 0 ) {
375 sb->sb_fd = AC_SOCKET_INVALID;
381 ber_int_sb_destroy( Sockbuf *sb )
386 assert( SOCKBUF_VALID( sb ) );
388 while ( sb->sb_iod ) {
389 p = sb->sb_iod->sbiod_next;
390 ber_sockbuf_remove_io( sb, sb->sb_iod->sbiod_io,
391 sb->sb_iod->sbiod_level );
395 return ber_int_sb_init( sb );
399 ber_int_sb_read( Sockbuf *sb, void *buf, ber_len_t len )
403 assert( buf != NULL );
405 assert( sb->sb_iod != NULL );
406 assert( SOCKBUF_VALID( sb ) );
409 ret = sb->sb_iod->sbiod_io->sbi_read( sb->sb_iod, buf, len );
412 if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
421 ber_int_sb_write( Sockbuf *sb, void *buf, ber_len_t len )
425 assert( buf != NULL );
427 assert( sb->sb_iod != NULL );
428 assert( SOCKBUF_VALID( sb ) );
431 ret = sb->sb_iod->sbiod_io->sbi_write( sb->sb_iod, buf, len );
434 if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
447 sb_stream_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
449 assert( sbiod != NULL);
450 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
454 * MacTCP/OpenTransport
456 return tcpread( sbiod->sbiod_sb->sb_fd, 0, (unsigned char *)buf,
459 #elif defined( HAVE_PCNFS ) || \
460 defined( HAVE_WINSOCK ) || defined ( __BEOS__ )
465 * Windows Socket API (under DOS/Windows 3.x)
468 * 32-bit Windows Socket API (under Windows NT or Windows 95)
473 rc = recv( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
479 err = WSAGetLastError();
487 #elif defined( HAVE_NCSA )
489 * NCSA Telnet TCP/IP stack (under DOS)
491 return nread( sbiod->sbiod_sb->sb_fd, buf, len );
494 return read( sbiod->sbiod_sb->sb_fd, buf, len );
499 sb_stream_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
501 assert( sbiod != NULL);
502 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
506 * MacTCP/OpenTransport
508 #define MAX_WRITE 65535
509 return tcpwrite( sbiod->sbiod_sb->sb_fd, (unsigned char *)buf,
510 (len<MAX_WRITE) ? len : MAX_WRITE );
512 #elif defined( HAVE_PCNFS) \
513 || defined( HAVE_WINSOCK) || defined ( __BEOS__ )
518 * Windows Socket API (under DOS/Windows 3.x)
521 * 32-bit Windows Socket API (under Windows NT or Windows 95)
524 int rc = send( sbiod->sbiod_sb->sb_fd, buf, len, 0 );
529 err = WSAGetLastError();
536 #elif defined(HAVE_NCSA)
537 return netwrite( sbiod->sbiod_sb->sb_fd, buf, len );
541 * VMS -- each write must be 64K or smaller
543 #define MAX_WRITE 65535
544 return write( sbiod->sbiod_sb->sb_fd, buf,
545 (len<MAX_WRITE) ? len : MAX_WRITE);
547 return write( sbiod->sbiod_sb->sb_fd, buf, len );
552 sb_stream_close( Sockbuf_IO_Desc *sbiod )
554 assert( sbiod != NULL );
555 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
556 tcp_close( sbiod->sbiod_sb->sb_fd );
560 /* The argument is a pointer to the socket descriptor */
562 sb_stream_setup( Sockbuf_IO_Desc *sbiod, void *arg ) {
563 assert( sbiod != NULL );
566 sbiod->sbiod_sb->sb_fd = *((int *)arg);
572 sb_stream_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) {
573 /* This is an end IO descriptor */
577 Sockbuf_IO ber_sockbuf_io_tcp = {
578 sb_stream_setup, /* sbi_setup */
579 NULL, /* sbi_remove */
580 sb_stream_ctrl, /* sbi_ctrl */
581 sb_stream_read, /* sbi_read */
582 sb_stream_write, /* sbi_write */
583 sb_stream_close /* sbi_close */
588 * Support for readahead (UDP needs it)
592 sb_rdahead_setup( Sockbuf_IO_Desc *sbiod, void *arg )
596 assert( sbiod != NULL );
598 p = LBER_MALLOC( sizeof( *p ) );
599 if ( p == NULL ) return -1;
601 ber_pvt_sb_buf_init( p );
604 ber_pvt_sb_grow_buffer( p, LBER_DEFAULT_READAHEAD );
606 ber_pvt_sb_grow_buffer( p, *((int *)arg) );
609 sbiod->sbiod_pvt = p;
614 sb_rdahead_remove( Sockbuf_IO_Desc *sbiod )
618 assert( sbiod != NULL );
620 p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
622 if ( p->buf_ptr != p->buf_end ) return -1;
624 ber_pvt_sb_buf_destroy( (Sockbuf_Buf *)(sbiod->sbiod_pvt) );
625 LBER_FREE( sbiod->sbiod_pvt );
626 sbiod->sbiod_pvt = NULL;
632 sb_rdahead_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
635 ber_slen_t bufptr = 0, ret, max;
637 assert( sbiod != NULL );
638 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
639 assert( sbiod->sbiod_next != NULL );
641 p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
643 assert( p->buf_size > 0 );
645 /* Are there anything left in the buffer? */
646 ret = ber_pvt_sb_copy_out( p, buf, len );
650 if ( len == 0 ) return bufptr;
652 max = p->buf_size - p->buf_end;
655 ret = LBER_SBIOD_READ_NEXT( sbiod, p->buf_base + p->buf_end,
658 if ( ( ret < 0 ) && ( errno == EINTR ) ) continue;
664 return ( bufptr ? bufptr : ret );
668 bufptr += ber_pvt_sb_copy_out( p, (char *) buf + bufptr, len );
673 sb_rdahead_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
675 assert( sbiod != NULL );
676 assert( sbiod->sbiod_next != NULL );
678 return LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
682 sb_rdahead_close( Sockbuf_IO_Desc *sbiod )
684 assert( sbiod != NULL );
686 /* Just erase the buffer */
687 ber_pvt_sb_buf_destroy((Sockbuf_Buf *)sbiod->sbiod_pvt);
692 sb_rdahead_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
696 p = (Sockbuf_Buf *)sbiod->sbiod_pvt;
698 if ( opt == LBER_SB_OPT_DATA_READY ) {
699 if ( p->buf_ptr != p->buf_end ) {
703 } else if ( opt == LBER_SB_OPT_SET_READAHEAD ) {
704 if ( p->buf_size >= *((ber_len_t *)arg) ) {
707 return ( ber_pvt_sb_grow_buffer( p, *((int *)arg) ) ?
711 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
714 Sockbuf_IO ber_sockbuf_io_readahead = {
715 sb_rdahead_setup, /* sbi_setup */
716 sb_rdahead_remove, /* sbi_remove */
717 sb_rdahead_ctrl, /* sbi_ctrl */
718 sb_rdahead_read, /* sbi_read */
719 sb_rdahead_write, /* sbi_write */
720 sb_rdahead_close /* sbi_close */
724 * Support for simple file IO
728 sb_fd_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
730 assert( sbiod != NULL);
731 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
733 return read( sbiod->sbiod_sb->sb_fd, buf, len );
737 sb_fd_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
739 assert( sbiod != NULL);
740 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
742 return write( sbiod->sbiod_sb->sb_fd, buf, len );
746 sb_fd_close( Sockbuf_IO_Desc *sbiod )
748 assert( sbiod != NULL );
749 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
751 close( sbiod->sbiod_sb->sb_fd );
755 /* The argument is a pointer to the file descriptor */
757 sb_fd_setup( Sockbuf_IO_Desc *sbiod, void *arg ) {
758 assert( sbiod != NULL );
761 sbiod->sbiod_sb->sb_fd = *((int *)arg);
766 sb_fd_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg ) {
767 /* This is an end IO descriptor */
771 Sockbuf_IO ber_sockbuf_io_fd = {
772 sb_fd_setup, /* sbi_setup */
773 NULL, /* sbi_remove */
774 sb_fd_ctrl, /* sbi_ctrl */
775 sb_fd_read, /* sbi_read */
776 sb_fd_write, /* sbi_write */
777 sb_fd_close /* sbi_close */
785 sb_debug_setup( Sockbuf_IO_Desc *sbiod, void *arg )
787 assert( sbiod != NULL );
789 if ( arg == NULL ) arg = "sockbuf_";
791 sbiod->sbiod_pvt = LBER_MALLOC( strlen( arg ) + 1 );
792 if ( sbiod->sbiod_pvt == NULL ) return -1;
794 strcpy( (char *)sbiod->sbiod_pvt, (char *)arg );
799 sb_debug_remove( Sockbuf_IO_Desc *sbiod )
801 assert( sbiod != NULL );
802 assert( sbiod->sbiod_pvt != NULL );
804 LBER_FREE( sbiod->sbiod_pvt );
805 sbiod->sbiod_pvt = NULL;
810 sb_debug_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
812 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
816 sb_debug_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
821 ret = LBER_SBIOD_READ_NEXT( sbiod, buf, len );
822 if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
825 ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
826 "%sread: want=%ld error=%s\n", (char *)sbiod->sbiod_pvt,
827 (long)len, AC_STRERROR_R( errno, ebuf, sizeof ebuf ) );
829 ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
830 "%sread: want=%ld, got=%ld\n", (char *)sbiod->sbiod_pvt,
831 (long)len, (long)ret );
832 ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
833 (const char *)buf, ret );
841 sb_debug_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
846 ret = LBER_SBIOD_WRITE_NEXT( sbiod, buf, len );
847 if (sbiod->sbiod_sb->sb_debug & LDAP_DEBUG_PACKETS) {
850 ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
851 "%swrite: want=%ld error=%s\n",
852 (char *)sbiod->sbiod_pvt, (long)len,
853 AC_STRERROR_R( errno, ebuf, sizeof ebuf ) );
855 ber_log_printf( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
856 "%swrite: want=%ld, written=%ld\n",
857 (char *)sbiod->sbiod_pvt, (long)len, (long)ret );
858 ber_log_bprint( LDAP_DEBUG_PACKETS, sbiod->sbiod_sb->sb_debug,
859 (const char *)buf, ret );
867 Sockbuf_IO ber_sockbuf_io_debug = {
868 sb_debug_setup, /* sbi_setup */
869 sb_debug_remove, /* sbi_remove */
870 sb_debug_ctrl, /* sbi_ctrl */
871 sb_debug_read, /* sbi_read */
872 sb_debug_write, /* sbi_write */
876 #ifdef LDAP_CONNECTIONLESS
879 * Support for UDP (CLDAP)
881 * All I/O at this level must be atomic. For ease of use, the sb_readahead
882 * must be used above this module. All data reads and writes are prefixed
883 * with a sockaddr containing the address of the remote entity. Upper levels
884 * must read and write this sockaddr before doing the usual ber_printf/scanf
885 * operations on LDAP messages.
889 sb_dgram_setup( Sockbuf_IO_Desc *sbiod, void *arg )
891 assert( sbiod != NULL);
892 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
894 if ( arg != NULL ) sbiod->sbiod_sb->sb_fd = *((int *)arg);
899 sb_dgram_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
903 struct sockaddr *src;
905 assert( sbiod != NULL );
906 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
907 assert( buf != NULL );
909 addrlen = sizeof( struct sockaddr );
913 rc = recvfrom( sbiod->sbiod_sb->sb_fd, buf, len, 0, src, &addrlen );
915 return rc > 0 ? rc+sizeof(struct sockaddr) : rc;
919 sb_dgram_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len )
922 struct sockaddr *dst;
924 assert( sbiod != NULL );
925 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
926 assert( buf != NULL );
929 buf += sizeof( struct sockaddr );
930 len -= sizeof( struct sockaddr );
932 rc = sendto( sbiod->sbiod_sb->sb_fd, buf, len, 0, dst,
933 sizeof( struct sockaddr ) );
935 if ( rc < 0 ) return -1;
937 /* fake error if write was not atomic */
944 rc = len + sizeof(struct sockaddr);
949 sb_dgram_close( Sockbuf_IO_Desc *sbiod )
951 assert( sbiod != NULL );
952 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
954 tcp_close( sbiod->sbiod_sb->sb_fd );
959 sb_dgram_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
961 /* This is an end IO descriptor */
965 Sockbuf_IO ber_sockbuf_io_udp =
967 sb_dgram_setup, /* sbi_setup */
968 NULL, /* sbi_remove */
969 sb_dgram_ctrl, /* sbi_ctrl */
970 sb_dgram_read, /* sbi_read */
971 sb_dgram_write, /* sbi_write */
972 sb_dgram_close /* sbi_close */
975 #endif /* LDAP_CONNECTIONLESS */