From 17431369cce8e4cd01f6f236fb2d6a75c8c79aef Mon Sep 17 00:00:00 2001
From: Stefan Eissing <stefan.eissing@greenbytes.de>
Date: Thu, 5 Mar 2015 11:54:11 -0500
Subject: [PATCH 09/26] Add iovec-based I/O routines

Add "struct iovec" variants to ssl IO
Configurable, disabled by default.

(cherry picked from commit d92bdedc1d5416122e38543a4dfb0b4d97ca8412)

Conflicts:
	include/openssl/ssl.h
	include/openssl/ssl3.h
	ssl/Makefile
	ssl/record/rec_layer_d1.c
	ssl/record/rec_layer_s3.c
	ssl/s3_lib.c
	ssl/ssl_locl.h
---
 Configure                 |   3 +-
 include/openssl/ssl.h     |  48 ++++++++++++++
 include/openssl/ssl3.h    |   1 -
 ssl/Makefile              |  23 ++++++-
 ssl/record/rec_layer_d1.c |  92 ++++++++++++++------------
 ssl/record/rec_layer_s3.c | 161 +++++++++++++++++++++++++++++-----------------
 ssl/record/record.h       |   8 ++-
 ssl/record/ssl3_record.c  |   2 +-
 ssl/s3_lib.c              |  81 +++++++++++++++++++++++
 ssl/s3_msg.c              |   4 +-
 ssl/ssl_bucket.c          | 101 +++++++++++++++++++++++++++++
 ssl/ssl_lib.c             |  33 ++++++++++
 ssl/ssl_locl.h            |  37 +++++++++++
 13 files changed, 484 insertions(+), 110 deletions(-)
 create mode 100644 ssl/ssl_bucket.c

diff --git a/Configure b/Configure
index f0a8acc..1d75931 100755
--- a/Configure
+++ b/Configure
@@ -803,6 +803,7 @@ my %disabled = ( # "what"         => "comment" [or special keyword "experimental
 		 "deprecated" => "default",
 		 "ec_nistp_64_gcc_128" => "default",
 		 "gmp"		  => "default",
+		 "iovec"          => "default",
 		 "jpake"          => "experimental",
 		 "md2"            => "default",
 		 "rc5"            => "default",
@@ -819,7 +820,7 @@ my @experimental = ();
 
 # This is what $depflags will look like with the above defaults
 # (we need this to see if we should advise the user to run "make depend"):
-my $default_depflags = " -DOPENSSL_NO_DEPRECATED -DOPENSSL_NO_EC_NISTP_64_GCC_128 -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MD2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SCTP -DOPENSSL_NO_SSL_TRACE -DOPENSSL_NO_STORE -DOPENSSL_NO_UNIT_TEST";
+my $default_depflags = " -DOPENSSL_NO_DEPRECATED -DOPENSSL_NO_EC_NISTP_64_GCC_128 -DOPENSSL_NO_GMP -DOPENSSL_NO_IOVEC -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MD2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SCTP -DOPENSSL_NO_SSL_TRACE -DOPENSSL_NO_STORE -DOPENSSL_NO_UNIT_TEST";
 
 # Explicit "no-..." options will be collected in %disabled along with the defaults.
 # To remove something from %disabled, use "enable-foo" (unless it's experimental).
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index fc293d5..a8744e4 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -343,6 +343,12 @@ typedef int (*tls_session_secret_cb_fn) (SSL *s, void *secret,
                                          STACK_OF(SSL_CIPHER) *peer_ciphers,
                                          SSL_CIPHER **cipher, void *arg);
 
+# ifndef OPENSSL_NO_IOVEC
+typedef struct iovec ssl_bucket;
+# else  /* !OPENSSL_NO_IOVEC */
+typedef struct ssl_bucket_st ssl_bucket;
+# endif /* OPENSSL_NO_IOVEC */
+
 # ifndef OPENSSL_NO_TLSEXT
 
 /* Typedefs for handling custom extensions */
@@ -946,6 +952,44 @@ typedef struct {
 # define SSL_MAC_FLAG_READ_MAC_STREAM 1
 # define SSL_MAC_FLAG_WRITE_MAC_STREAM 2
 
+# ifndef OPENSSL_NO_IOVEC
+
+#  ifndef WIN32
+#   include <sys/uio.h>
+#  else
+#   ifndef HAVE_STRUCT_IOVEC
+struct iovec {
+    void *iov_base;     /* Pointer to data.  */
+    size_t iov_len;     /* Length of data.  */
+};
+#    define HAVE_STRUCT_IOVEC
+#   endif /* HAVE_STRUCT_IOVEC */
+#  endif /* !WIN32 */
+
+#  define SSL_BUCKET_MAX 32
+
+# else  /* !OPENSSL_NO_IOVEC */
+
+struct ssl_bucket_st {
+    void *iov_base;
+    size_t iov_len;
+};
+
+#  define SSL_BUCKET_MAX 1
+
+# endif /* OPENSSL_NO_IOVEC */
+
+size_t ssl_bucket_len(const ssl_bucket *buckets, unsigned int count);
+int ssl_bucket_same(const ssl_bucket *buckets1, unsigned int count1,
+                    const ssl_bucket *buckets2, unsigned int count2);
+void ssl_bucket_set(ssl_bucket *bucket, void *buf, size_t len);
+size_t ssl_bucket_cpy_out(void *buf, const ssl_bucket *bucket,
+                          unsigned int count, size_t offset, size_t len);
+size_t ssl_bucket_cpy_in(const ssl_bucket *buckets, unsigned int count,
+                         void *buf, size_t len);
+unsigned char *ssl_bucket_get_pointer(const ssl_bucket *buckets, unsigned int count,
+                                      size_t offset, unsigned int *nw);
+
 #ifdef __cplusplus
 }
 #endif
@@ -1599,6 +1643,10 @@ __owur int SSL_connect(SSL *ssl);
 __owur int SSL_read(SSL *ssl, void *buf, int num);
 __owur int SSL_peek(SSL *ssl, void *buf, int num);
 __owur int SSL_write(SSL *ssl, const void *buf, int num);
+# ifndef OPENSSL_NO_IOVEC
+__owur int SSL_readv(SSL *ssl, ssl_bucket *buckets, int count);
+__owur int SSL_writev(SSL *ssl, const ssl_bucket *buckets, int count);
+# endif /* !OPENSSL_NO_IOVEC */
 long SSL_ctrl(SSL *ssl, int cmd, long larg, void *parg);
 long SSL_callback_ctrl(SSL *, int, void (*)(void));
 long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg);
diff --git a/include/openssl/ssl3.h b/include/openssl/ssl3.h
index 06a058f..199ff61 100644
--- a/include/openssl/ssl3.h
+++ b/include/openssl/ssl3.h
@@ -374,7 +374,6 @@ extern "C" {
 /* Set if we encrypt then mac instead of usual mac then encrypt */
 # define TLS1_FLAGS_ENCRYPT_THEN_MAC             0x0100
 
-
 /* SSLv3 */
 /*
  * client
diff --git a/ssl/Makefile b/ssl/Makefile
index d1fc049..00b5cf7 100644
--- a/ssl/Makefile
+++ b/ssl/Makefile
@@ -24,7 +24,7 @@ LIBSRC=	\
 	d1_both.c d1_srtp.c \
 	ssl_lib.c ssl_err2.c ssl_cert.c ssl_sess.c \
 	ssl_ciph.c ssl_stat.c ssl_rsa.c \
-	ssl_asn1.c ssl_txt.c ssl_algs.c ssl_conf.c \
+	ssl_asn1.c ssl_txt.c ssl_algs.c ssl_conf.c ssl_bucket.c \
 	bio_ssl.c ssl_err.c t1_reneg.c tls_srp.c t1_trce.c ssl_utst.c \
 	record/ssl3_buffer.c record/ssl3_record.c record/dtls1_bitmap.c
 LIBOBJ= \
@@ -35,7 +35,7 @@ LIBOBJ= \
 	d1_both.o d1_srtp.o\
 	ssl_lib.o ssl_err2.o ssl_cert.o ssl_sess.o \
 	ssl_ciph.o ssl_stat.o ssl_rsa.o \
-	ssl_asn1.o ssl_txt.o ssl_algs.o ssl_conf.o \
+	ssl_asn1.o ssl_txt.o ssl_algs.o ssl_conf.o ssl_bucket.o \
 	bio_ssl.o ssl_err.o t1_reneg.o tls_srp.o t1_trce.o ssl_utst.o \
 	record/ssl3_buffer.o record/ssl3_record.o record/dtls1_bitmap.o
 
@@ -528,6 +528,25 @@ ssl_asn1.o: ../include/openssl/ssl2.h ../include/openssl/ssl3.h
 ssl_asn1.o: ../include/openssl/stack.h ../include/openssl/symhacks.h
 ssl_asn1.o: ../include/openssl/tls1.h ../include/openssl/x509.h
 ssl_asn1.o: ../include/openssl/x509_vfy.h record/record.h ssl_asn1.c ssl_locl.h
+ssl_bucket.o: ../e_os.h ../include/openssl/asn1.h ../include/openssl/bio.h
+ssl_bucket.o: ../include/openssl/buffer.h ../include/openssl/comp.h
+ssl_bucket.o: ../include/openssl/crypto.h ../include/openssl/dsa.h
+ssl_bucket.o: ../include/openssl/dtls1.h ../include/openssl/e_os2.h
+ssl_bucket.o: ../include/openssl/ec.h ../include/openssl/ecdh.h
+ssl_bucket.o: ../include/openssl/ecdsa.h ../include/openssl/err.h
+ssl_bucket.o: ../include/openssl/evp.h ../include/openssl/hmac.h
+ssl_bucket.o: ../include/openssl/lhash.h ../include/openssl/obj_mac.h
+ssl_bucket.o: ../include/openssl/objects.h ../include/openssl/opensslconf.h
+ssl_bucket.o: ../include/openssl/opensslv.h ../include/openssl/ossl_typ.h
+ssl_bucket.o: ../include/openssl/pem.h ../include/openssl/pem2.h
+ssl_bucket.o: ../include/openssl/pkcs7.h ../include/openssl/pqueue.h
+ssl_bucket.o: ../include/openssl/rsa.h ../include/openssl/safestack.h
+ssl_bucket.o: ../include/openssl/sha.h ../include/openssl/srtp.h
+ssl_bucket.o: ../include/openssl/ssl.h ../include/openssl/ssl2.h
+ssl_bucket.o: ../include/openssl/ssl3.h ../include/openssl/stack.h
+ssl_bucket.o: ../include/openssl/symhacks.h ../include/openssl/tls1.h
+ssl_bucket.o: ../include/openssl/x509.h ../include/openssl/x509_vfy.h
+ssl_bucket.o: record/record.h ssl_bucket.c ssl_locl.h
 ssl_cert.o: ../e_os.h ../include/internal/o_dir.h ../include/openssl/asn1.h
 ssl_cert.o: ../include/openssl/bio.h ../include/openssl/bn.h
 ssl_cert.o: ../include/openssl/buffer.h ../include/openssl/comp.h
diff --git a/ssl/record/rec_layer_d1.c b/ssl/record/rec_layer_d1.c
index 159c222..2c58456 100644
--- a/ssl/record/rec_layer_d1.c
+++ b/ssl/record/rec_layer_d1.c
@@ -226,8 +226,8 @@ void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl)
     memcpy(rl->write_sequence, rl->read_sequence, sizeof(rl->write_sequence));
 }
 
-static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
-                                   int len, int peek);
+static int have_handshake_fragment(SSL *s, int type, ssl_bucket *buckets,
+                                   unsigned int count, int peek);
 
 /* copy buffered record into SSL structure */
 static int dtls1_copy_record(SSL *s, pitem *item)
@@ -395,7 +395,7 @@ int dtls1_process_buffered_records(SSL *s)
  *     Application data protocol
  *             none of our business
  */
-int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
+int dtls1_readv_bytes(SSL *s, int type, ssl_bucket *buckets, unsigned int count, int peek)
 {
     int al, i, j, ret;
     unsigned int n;
@@ -418,7 +418,7 @@ int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     /*
      * check whether there's a handshake message (client hello?) waiting
      */
-    if ((ret = have_handshake_fragment(s, type, buf, len, peek)))
+    if ((ret = have_handshake_fragment(s, type, buckets, count, peek)))
         return ret;
 
     /*
@@ -540,6 +540,7 @@ int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     if (type == SSL3_RECORD_get_type(rr)) {
         /* SSL3_RT_APPLICATION_DATA or
          * SSL3_RT_HANDSHAKE */
+        size_t len = ssl_bucket_len(buckets, count);
         /*
          * make sure that we are not getting application data when we are
          * doing a handshake for the first time
@@ -559,7 +560,8 @@ int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
         else
             n = (unsigned int)len;
 
-        memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
+        ssl_bucket_cpy_in(buckets, count,
+                          &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
         if (!peek) {
             SSL3_RECORD_add_length(rr, -n);
             SSL3_RECORD_add_off(rr, n);
@@ -1032,34 +1034,37 @@ int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     return (-1);
 }
 
+int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
+{
+    ssl_bucket bucket = { (void*)buf, len };
+    return dtls1_readv_bytes(s, type, &bucket, 1, peek);
+}
 
         /*
          * this only happens when a client hello is received and a handshake
          * is started.
          */
-static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
-                                   int len, int peek)
+static int have_handshake_fragment(SSL *s, int type, ssl_bucket *buckets,
+                                   unsigned int count, int peek)
 {
 
     if ((type == SSL3_RT_HANDSHAKE)
             && (s->rlayer.d->handshake_fragment_len > 0))
         /* (partially) satisfy request from storage */
     {
-        unsigned char *src = s->rlayer.d->handshake_fragment;
-        unsigned char *dst = buf;
-        unsigned int k, n;
-
-        /* peek == 0 */
-        n = 0;
-        while ((len > 0) && (s->rlayer.d->handshake_fragment_len > 0)) {
-            *dst++ = *src++;
-            len--;
-            s->rlayer.d->handshake_fragment_len--;
-            n++;
-        }
-        /* move any remaining fragment bytes: */
-        for (k = 0; k < s->rlayer.d->handshake_fragment_len; k++)
-            s->rlayer.d->handshake_fragment[k] = *src++;
+        size_t n = ssl_bucket_cpy_in(buckets, count,
+                                     s->rlayer.d->handshake_fragment,
+                                     s->rlayer.d->handshake_fragment_len);
+        if (peek)
+            return n;
+
+        if (n < s->rlayer.d->handshake_fragment_len) {
+            s->rlayer.d->handshake_fragment_len -= n;
+            memmove(s->rlayer.d->handshake_fragment,
+                    s->rlayer.d->handshake_fragment+n,
+                    s->rlayer.d->handshake_fragment_len);
+        } else
+            s->rlayer.d->handshake_fragment_len = 0;
         return n;
     }
 
@@ -1080,8 +1085,8 @@ int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
     return i;
 }
 
-int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
-                   unsigned int len, int create_empty_fragment)
+int do_dtls1_writev(SSL *s, int type, const ssl_bucket *buckets,
+                    unsigned int count, int create_empty_fragment)
 {
     unsigned char *p, *pseq;
     int i, mac_size, clear = 0;
@@ -1090,6 +1095,7 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
     SSL3_RECORD *wr;
     SSL3_BUFFER *wb;
     SSL_SESSION *sess;
+    size_t len = ssl_bucket_len(buckets,count);
 
     wb = &s->rlayer.wbuf;
 
@@ -1099,7 +1105,7 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
      */
     if (SSL3_BUFFER_get_left(wb) != 0) {
         OPENSSL_assert(0);      /* XDTLS: want to see if we ever get here */
-        return (ssl3_write_pending(s, type, buf, len));
+        return (ssl3_writev_pending(s, type, buckets, count, len, 0));
     }
 
     /* If we have an alert to send, lets send it */
@@ -1169,8 +1175,6 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
 
     /* lets setup the record stuff. */
     SSL3_RECORD_set_data(wr, p + eivlen); /* make room for IV in case of CBC */
-    SSL3_RECORD_set_length(wr, (int)len);
-    SSL3_RECORD_set_input(wr, (unsigned char *)buf);
 
     /*
      * we now 'read' from wr->input, wr->length bytes into wr->data
@@ -1178,14 +1182,14 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
 
     /* first we compress */
     if (s->compress != NULL) {
-        if (!ssl3_do_compress(s)) {
+        if (!ssl3_do_vcompress(s, buckets, count, 0, len)) {
             SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
             goto err;
         }
     } else {
-        memcpy(SSL3_RECORD_get_data(wr), SSL3_RECORD_get_input(wr),
-               SSL3_RECORD_get_length(wr));
-        SSL3_RECORD_reset_input(wr);
+        SSL3_RECORD_set_length(wr, (int)ssl_bucket_cpy_out(SSL3_RECORD_get_data(wr),
+                                                           buckets, count, 0, len));
+        SSL3_RECORD_set_input(wr, (unsigned char *)SSL3_RECORD_get_data(wr));
     }
 
     /*
@@ -1255,21 +1259,19 @@ int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
     SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(wr));
     SSL3_BUFFER_set_offset(wb, 0);
 
-    /*
-     * memorize arguments so that ssl3_write_pending can detect bad write
-     * retries later
-     */
-    s->rlayer.wpend_tot = len;
-    s->rlayer.wpend_buf = buf;
-    s->rlayer.wpend_type = type;
-    s->rlayer.wpend_ret = len;
-
     /* we now just need to write the buffer */
-    return ssl3_write_pending(s, type, buf, len);
+    return ssl3_writev_pending(s, type, buckets, count, len, 1);
  err:
     return -1;
 }
 
+int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
+                   unsigned int len, int create_empty_fragment)
+{
+    ssl_bucket bucket = { (void*)buf, len };
+    return do_dtls1_writev(s, type, &bucket, 1, create_empty_fragment);
+}
+
 DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
                                       unsigned int *is_next_epoch)
 {
@@ -1311,3 +1313,11 @@ void dtls1_reset_seq_numbers(SSL *s, int rw)
 
     memset(seq, 0, seq_bytes);
 }
+
+int dtls1_writev_bytes(SSL *s, int type, const ssl_bucket *buckets,
+                       unsigned int count)
+{
+    OPENSSL_assert(ssl_bucket_len(buckets,count) <= SSL3_RT_MAX_PLAIN_LENGTH);
+    s->rwstate = SSL_NOTHING;
+    return do_dtls1_writev(s, type, buckets,count, 0);
+}
diff --git a/ssl/record/rec_layer_s3.c b/ssl/record/rec_layer_s3.c
index 47a021d..4caa398 100644
--- a/ssl/record/rec_layer_s3.c
+++ b/ssl/record/rec_layer_s3.c
@@ -433,27 +433,50 @@ int ssl3_read_n(SSL *s, int n, int max, int extend)
 }
 
 
+int ssl3_do_vcompress(SSL *ssl, const ssl_bucket *buckets, unsigned int count, size_t offset, size_t len)
+{
+#ifndef OPENSSL_NO_COMP
+    int nlen = 0, i;
+    SSL3_RECORD *wr = &(ssl->rlayer.wrec);
+    for (i = 0; i < count && len > 0; ++i) {
+        size_t blen = buckets[i].iov_len;
+        int n;
+        if (offset >= blen) {
+            offset -= blen;
+            continue;
+        }
+        n = COMP_compress_block(ssl->compress, wr->data+nlen,
+                                SSL3_RT_MAX_COMPRESSED_LENGTH,
+                                (unsigned char*)buckets[i].iov_base + offset,
+                                (int)blen - offset);
+        if (n < 0)
+            return (0);
+        offset = 0;
+        nlen += n;
+        len -= n;
+    }
+    wr->length = nlen;
+    wr->input = wr->data;
+#endif
+    return (1);
+}
+
 /*
  * Call this to write data in records of type 'type' It will return <= 0 if
  * not all data has been sent or non-blocking IO.
  */
-int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
+int ssl3_writev_bytes(SSL *s, int type, const ssl_bucket *buckets,
+                      unsigned int count)
 {
-    const unsigned char *buf = buf_;
     int tot;
-    unsigned int n, nw;
+    unsigned int n, nw, len;
 #if !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
     unsigned int max_send_fragment;
-    unsigned int u_len = (unsigned int)len;
+    unsigned int u_len;
 #endif
     SSL3_BUFFER *wb = &s->rlayer.wbuf;
     int i;
 
-    if (len < 0) {
-        SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_SSL_NEGATIVE_LENGTH);
-        return -1;
-    }
-
     s->rwstate = SSL_NOTHING;
     OPENSSL_assert(s->rlayer.wnum <= INT_MAX);
     tot = s->rlayer.wnum;
@@ -473,11 +496,12 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
      * ensure that if we end up with a smaller value of data to write out
      * than the the original len from a write which didn't complete for
      * non-blocking I/O and also somehow ended up avoiding the check for
-     * this in ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be
+     * this in ssl3_writev_pending/SSL_R_BAD_WRITE_RETRY as it must never be
      * possible to end up with (len-tot) as a large number that will then
      * promptly send beyond the end of the users buffer ... so we trap and
      * report the error in a way the user will notice
      */
+    u_len = len = ssl_bucket_len(buckets, count);
     if (len < tot) {
         SSLerr(SSL_F_SSL3_WRITE_BYTES, SSL_R_BAD_LENGTH);
         return (-1);
@@ -488,7 +512,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
      * will happen with non blocking IO
      */
     if (wb->left != 0) {
-        i = ssl3_write_pending(s, type, &buf[tot], s->rlayer.wpend_tot);
+        i = ssl3_writev_pending(s, type, buckets, count, s->rlayer.wpend_tot, 0);
         if (i <= 0) {
             /* XXX should we ssl3_release_write_buffer if i<0? */
             s->rlayer.wnum = tot;
@@ -510,6 +534,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
         EVP_CIPHER_flags(s->enc_write_ctx->cipher) &
         EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) {
         unsigned char aad[13];
+        unsigned char *ptr;
         EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
         int packlen;
 
@@ -562,6 +587,9 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
             else
                 nw = max_send_fragment * (mb_param.interleave = 4);
 
+            /* get pointer into a bucket, and update number to write */
+            ptr = ssl_bucket_get_pointer(buckets, count, tot, &nw);
+
             memcpy(aad, s->rlayer.write_sequence, 8);
             aad[8] = type;
             aad[9] = (unsigned char)(s->version >> 8);
@@ -583,7 +611,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
             }
 
             mb_param.out = wb->buf;
-            mb_param.inp = &buf[tot];
+            mb_param.inp = ptr;
             mb_param.len = nw;
 
             if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
@@ -601,11 +629,12 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
             wb->left = packlen;
 
             s->rlayer.wpend_tot = nw;
-            s->rlayer.wpend_buf = &buf[tot];
+            memcpy(s->rlayer.wpend_bucket, buckets, sizeof(ssl_bucket) * count);
+            s->rlayer.wpend_bucket_count = count;
             s->rlayer.wpend_type = type;
             s->rlayer.wpend_ret = nw;
 
-            i = ssl3_write_pending(s, type, &buf[tot], nw);
+            i = ssl3_writev_pending(s, type, buckets, count, nw, 0);
             if (i <= 0) {
                 if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
                     OPENSSL_free(wb->buf);
@@ -638,7 +667,7 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
         else
             nw = n;
 
-        i = do_ssl3_write(s, type, &(buf[tot]), nw, 0);
+        i = do_ssl3_write(s, type, buckets, count, tot, nw, 0);
         if (i <= 0) {
             /* XXX should we ssl3_release_write_buffer if i<0? */
             s->rlayer.wnum = tot;
@@ -666,7 +695,14 @@ int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len)
     }
 }
 
-int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
+int ssl3_write_bytes(SSL *s, int type, const void *buf, int len)
+{
+    const ssl_bucket bucket = { (void*)buf, len };
+    return (ssl3_writev_bytes(s, type, &bucket, 1));
+}
+
+int do_ssl3_write(SSL *s, int type, const ssl_bucket *buckets,
+                  unsigned int count, unsigned int offset,
                   unsigned int len, int create_empty_fragment)
 {
     unsigned char *p, *plen;
@@ -683,7 +719,7 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
      * will happen with non blocking IO
      */
     if (SSL3_BUFFER_get_left(wb) != 0)
-        return (ssl3_write_pending(s, type, buf, len));
+        return (ssl3_writev_pending(s, type, buckets, count, len, 0));
 
     /* If we have an alert to send, lets send it */
     if (s->s3->alert_dispatch) {
@@ -730,7 +766,7 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
              * 'prefix_len' bytes are sent out later together with the actual
              * payload)
              */
-            prefix_len = do_ssl3_write(s, type, buf, 0, 1);
+            prefix_len = do_ssl3_write(s, type, buckets, count, offset, 0, 1);
             if (prefix_len <= 0)
                 goto err;
 
@@ -806,9 +842,6 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
 
     /* lets setup the record stuff. */
     SSL3_RECORD_set_data(wr, p + eivlen);
-    SSL3_RECORD_set_length(wr, (int)len);
-    SSL3_RECORD_set_input(wr, (unsigned char *)buf);
-
 
     /*
      * we now 'read' from wr->input, wr->length bytes into wr->data
@@ -816,12 +849,13 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
 
     /* first we compress */
     if (s->compress != NULL) {
-        if (!ssl3_do_compress(s)) {
+        if (!ssl3_do_vcompress(s, buckets, count, offset, len)) {
             SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);
             goto err;
         }
     } else {
-        memcpy(wr->data, wr->input, wr->length);
+        wr->length = ssl_bucket_cpy_out(wr->data, buckets, count,
+                                        offset, len);
         SSL3_RECORD_reset_input(wr);
     }
 
@@ -881,33 +915,34 @@ int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
     /* now let's set up wb */
     SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(wr));
 
-    /*
-     * memorize arguments so that ssl3_write_pending can detect bad write
-     * retries later
-     */
-    s->rlayer.wpend_tot = len;
-    s->rlayer.wpend_buf = buf;
-    s->rlayer.wpend_type = type;
-    s->rlayer.wpend_ret = len;
-
     /* we now just need to write the buffer */
-    return ssl3_write_pending(s, type, buf, len);
+    return ssl3_writev_pending(s, type, buckets, count, len, 1);
  err:
     return -1;
 }
 
 /* if s->s3->wbuf.left != 0, we need to call this */
-int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
-                       unsigned int len)
+int ssl3_writev_pending(SSL *s, int type, const ssl_bucket *buckets,
+                        unsigned int count, unsigned int len, int reset)
 {
     int i;
     SSL3_BUFFER *wb = &s->rlayer.wbuf;
 
 /* XXXX */
-    if ((s->rlayer.wpend_tot > (int)len)
-        || ((s->rlayer.wpend_buf != buf) &&
-            !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER))
-        || (s->rlayer.wpend_type != type)) {
+    if (reset) {
+        /* memorize arguments so that ssl3_writev_pending can detect bad write retries later */
+        s->rlayer.wpend_tot = len;
+        OPENSSL_assert(count <= SSL_BUCKET_MAX);
+        memcpy(s->rlayer.wpend_bucket, buckets, sizeof(ssl_bucket)*count);
+        s->rlayer.wpend_bucket_count = count;
+        s->rlayer.wpend_type = type;
+        s->rlayer.wpend_ret = len;
+    } else if ((s->rlayer.wpend_tot > (int)len)
+               || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
+                   && !ssl_bucket_same(s->rlayer.wpend_bucket,
+                                       s->rlayer.wpend_bucket_count,
+                                       buckets, count))
+               || (s->rlayer.wpend_type != type)) {
         SSLerr(SSL_F_SSL3_WRITE_PENDING, SSL_R_BAD_WRITE_RETRY);
         return (-1);
     }
@@ -971,7 +1006,8 @@ int ssl3_write_pending(SSL *s, int type, const unsigned char *buf,
  *     Application data protocol
  *             none of our business
  */
-int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
+int ssl3_readv_bytes(SSL *s, int type, ssl_bucket *buckets,
+                     unsigned int count, int peek)
 {
     int al, i, j, ret;
     unsigned int n;
@@ -995,22 +1031,19 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
         /* (partially) satisfy request from storage */
     {
-        unsigned char *src = s->rlayer.handshake_fragment;
-        unsigned char *dst = buf;
-        unsigned int k;
-
-        /* peek == 0 */
-        n = 0;
-        while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
-            *dst++ = *src++;
-            len--;
-            s->rlayer.handshake_fragment_len--;
-            n++;
+        int copied = ssl_bucket_cpy_in(buckets, count,
+                                       s->rlayer.handshake_fragment,
+                                       s->rlayer.handshake_fragment_len);
+        if (copied > 0 && copied < s->rlayer.handshake_fragment_len) {
+            s->rlayer.handshake_fragment_len -= copied;
+            memmove(s->rlayer.handshake_fragment,
+                    s->rlayer.handshake_fragment + copied,
+                    s->rlayer.handshake_fragment_len);
+        } else {
+            s->rlayer.handshake_fragment_len = 0;
         }
-        /* move any remaining fragment bytes: */
-        for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
-            s->rlayer.handshake_fragment[k] = *src++;
-        return n;
+
+        return (copied);
     }
 
     /*
@@ -1031,10 +1064,10 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     s->rwstate = SSL_NOTHING;
 
     /*-
-     * s->s3->rrec.type         - is the type of record
-     * s->s3->rrec.data,    - data
-     * s->s3->rrec.off,     - offset into 'data' for next read
-     * s->s3->rrec.length,  - number of bytes.
+     * s->rlayer.rrec.type    - is the type of record
+     * s->rlayer.rrec.data    - data
+     * s->rlayer.rrec.off     - offset into 'data' for next read
+     * s->rlayer.rrec.length  - number of bytes.
      */
     rr = &s->rlayer.rrec;
 
@@ -1069,6 +1102,7 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     if (type == SSL3_RECORD_get_type(rr)) {
         /* SSL3_RT_APPLICATION_DATA or
          * SSL3_RT_HANDSHAKE */
+        size_t len;
         /*
          * make sure that we are not getting application data when we are
          * doing a handshake for the first time
@@ -1080,6 +1114,7 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
             goto f_err;
         }
 
+        len = ssl_bucket_len(buckets, count);
         if (len <= 0)
             return (len);
 
@@ -1088,7 +1123,7 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
         else
             n = (unsigned int)len;
 
-        memcpy(buf, &(rr->data[rr->off]), n);
+        ssl_bucket_cpy_in(buckets, count, &(rr->data[rr->off]), n);
         if (!peek) {
             SSL3_RECORD_add_length(rr, -n);
             SSL3_RECORD_add_off(rr, n);
@@ -1481,6 +1516,12 @@ int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
     return (-1);
 }
 
+int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek)
+{
+    ssl_bucket bucket = { buf, len };
+    return (ssl3_readv_bytes(s, type, &bucket, 1, peek));
+}
+
 void ssl3_record_sequence_update(unsigned char *seq)
 {
     int i;
diff --git a/ssl/record/record.h b/ssl/record/record.h
index cf1607c..b930793 100644
--- a/ssl/record/record.h
+++ b/ssl/record/record.h
@@ -286,7 +286,8 @@ typedef struct record_layer_st {
     int wpend_type;
     /* number of bytes submitted */
     int wpend_ret;
-    const unsigned char *wpend_buf;
+    ssl_bucket wpend_bucket[SSL_BUCKET_MAX];
+    int wpend_bucket_count;
 
     unsigned char read_sequence[8];
     unsigned char write_sequence[8];
@@ -329,8 +330,9 @@ int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
 int RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
 __owur int ssl3_pending(const SSL *s);
 __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, int len);
-__owur int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
-                         unsigned int len, int create_empty_fragment);
+__owur int do_ssl3_write(SSL *s, int type, const ssl_bucket *buckets,
+                         unsigned int count, unsigned int offset, unsigned int len,
+                         int create_empty_fragment);
 __owur int ssl3_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek);
 __owur int ssl3_setup_buffers(SSL *s);
 __owur int ssl3_enc(SSL *s, int send_data);
diff --git a/ssl/record/ssl3_record.c b/ssl/record/ssl3_record.c
index 5070bc3..941ef8c 100644
--- a/ssl/record/ssl3_record.c
+++ b/ssl/record/ssl3_record.c
@@ -182,7 +182,7 @@ void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
  * ssl->s3->rrec.data,   - data
  * ssl->s3->rrec.length, - number of bytes
  */
-/* used only by ssl3_read_bytes */
+/* used only by ssl3_readv_bytes */
 int ssl3_get_record(SSL *s)
 {
     int ssl_major, ssl_minor, al;
diff --git a/ssl/s3_lib.c b/ssl/s3_lib.c
index 9404417..451a554 100644
--- a/ssl/s3_lib.c
+++ b/ssl/s3_lib.c
@@ -4274,3 +4274,84 @@ int ssl_fill_hello_random(SSL *s, int server, unsigned char *result, int len)
     } else
         return RAND_bytes(result, len);
 }
+
+#ifndef OPENSSL_NO_IOVEC
+
+static int ssl3_readv_internal(SSL *s, ssl_bucket *buckets, unsigned int count, int peek)
+{
+    int ret;
+
+    clear_sys_error();
+    if (s->s3->renegotiate)
+        ssl3_renegotiate_check(s);
+    s->s3->in_read_app_data = 1;
+    ret = s->method->ssl_readv_bytes(s, SSL3_RT_APPLICATION_DATA,
+                                     buckets, count, peek);
+    if ((ret == -1) && (s->s3->in_read_app_data == 2)) {
+        /* ssl3_read_bytes decided to call s->handshake_func, which
+         * called ssl3_read_bytes to read handshake data.
+         * However, ssl3_read_bytes actually found application data
+         * and thinks that application data makes sense here; so disable
+         * handshake processing and try to read application data again. */
+        s->in_handshake++;
+        ret = s->method->ssl_readv_bytes(s, SSL3_RT_APPLICATION_DATA,
+                                         buckets, count, peek);
+        s->in_handshake--;
+    }
+    else
+        s->s3->in_read_app_data = 0;
+
+    return (ret);
+}
+
+int ssl3_readv(SSL *s, ssl_bucket *buckets, unsigned int count)
+{
+    return (ssl3_readv_internal(s, buckets, count, 0));
+}
+
+int ssl3_writev(SSL *s, const ssl_bucket *buckets, unsigned int count)
+{
+    int ret, n;
+
+    clear_sys_error();
+    if (s->s3->renegotiate)
+        ssl3_renegotiate_check(s);
+
+    /* This is an experimental flag that sends the
+     * last handshake message in the same packet as the first
+     * use data - used to see if it helps the TCP protocol during
+     * session-id reuse */
+    /* The second test is because the buffer may have been removed */
+    if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio)) {
+        /* First time through, we write into the buffer */
+        if (s->s3->delay_buf_pop_ret == 0) {
+            ret = ssl3_writev_bytes(s, SSL3_RT_APPLICATION_DATA,
+                                    buckets, count);
+            if (ret <= 0)
+                return (ret);
+
+            s->s3->delay_buf_pop_ret = ret;
+        }
+
+        s->rwstate = SSL_WRITING;
+        n = BIO_flush(s->wbio);
+        if (n <= 0)
+            return (n);
+        s->rwstate = SSL_NOTHING;
+
+        /* We have flushed the buffer, so remove it */
+        ssl_free_wbio_buffer(s);
+        s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
+
+        ret = s->s3->delay_buf_pop_ret;
+        s->s3->delay_buf_pop_ret = 0;
+    } else {
+        ret = s->method->ssl_writev_bytes(s, SSL3_RT_APPLICATION_DATA,
+                                          buckets, count);
+        if (ret <= 0)
+            return (ret);
+    }
+    return (ret);
+}
+
+#endif /* !OPENSSL_NO_IOVEC */
diff --git a/ssl/s3_msg.c b/ssl/s3_msg.c
index fcf4744..ec562df 100644
--- a/ssl/s3_msg.c
+++ b/ssl/s3_msg.c
@@ -194,9 +194,11 @@ int ssl3_dispatch_alert(SSL *s)
 {
     int i, j;
     void (*cb) (const SSL *ssl, int type, int val) = NULL;
+    ssl_bucket bucket;
 
     s->s3->alert_dispatch = 0;
-    i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
+    ssl_bucket_set(&bucket, &s->s3->send_alert[0], 2);
+    i = do_ssl3_write(s, SSL3_RT_ALERT, &bucket, 1, 0, 2, 0);
     if (i <= 0) {
         s->s3->alert_dispatch = 1;
     } else {
diff --git a/ssl/ssl_bucket.c b/ssl/ssl_bucket.c
new file mode 100644
index 0000000..557aee2
--- /dev/null
+++ b/ssl/ssl_bucket.c
@@ -0,0 +1,101 @@
+/*
+ *  ssl_bucket.c
+ *
+ *  Created by Stefan Eissing on 08.07.14.
+ *  Copyright (c) 2014 Akamai and greenbytes. All rights reserved.
+ */
+
+#include <stdio.h>
+
+#include "ssl_locl.h"
+
+size_t ssl_bucket_len(const ssl_bucket *buckets, unsigned int count)
+{
+    size_t len = 0;
+    unsigned int i;
+    for(i = 0; i < count; ++i)
+        len += buckets[i].iov_len;
+    return len;
+}
+
+int ssl_bucket_same(const ssl_bucket *buckets1, unsigned int count1, const ssl_bucket *buckets2, unsigned int count2)
+{
+    return ((count1 == count2) &&
+            (memcmp(buckets1, buckets2, count2 * sizeof(ssl_bucket)) == 0));
+}
+
+void ssl_bucket_set(ssl_bucket *bucket, void *buf, size_t len)
+{
+    bucket->iov_base = buf;
+    bucket->iov_len = len;
+}
+
+size_t ssl_bucket_cpy_out(void *buf, const ssl_bucket *buckets, unsigned int count, size_t offset, size_t len)
+{
+    int j, i = 0;
+    size_t copied = 0;
+
+    if (count == 1) {
+        len = (len <= buckets[0].iov_len)? len : buckets[0].iov_len;
+        memcpy(buf, (unsigned char*)buckets[0].iov_base + offset, len);
+        return (len);
+    }
+
+    while (i < count && offset > buckets[i].iov_len) {
+        offset -= buckets[i].iov_len;
+        ++i;
+    }
+
+    while (i < count && len > 0) {
+        j = buckets[i].iov_len - offset;
+        if (j > len)
+            j = len;
+        memcpy(buf, (unsigned char*)buckets[i].iov_base + offset, j);
+        buf = (unsigned char*)buf + j;
+        copied += j;
+        len -= j;
+        offset = 0;
+        ++i;
+    }
+    return (copied);
+}
+
+size_t ssl_bucket_cpy_in(const ssl_bucket *buckets, unsigned int count, void *buf, size_t len)
+{
+    size_t copied = 0, i;
+
+    if (count == 1) {
+        len = (len <= buckets[0].iov_len) ? len : buckets[0].iov_len;
+        memcpy(buckets[0].iov_base, buf, len);
+        return (len);
+    }
+
+    for(i = 0; i < count && len > 0; ++i) {
+        int j = buckets[i].iov_len;
+        if (len < j)
+            j = len;
+        memcpy(buckets[i].iov_base, buf, j);
+        buf = (unsigned char*)buf + j;
+        copied += j;
+        len -= j;
+    }
+    return (copied);
+}
+
+unsigned char *ssl_bucket_get_pointer(const ssl_bucket *buckets,
+                                      unsigned int count,
+                                      size_t offset, unsigned int *nw)
+{
+    int i = 0;
+    while (i < count && offset > buckets[i].iov_len) {
+        offset -= buckets[i].iov_len;
+        ++i;
+    }
+
+    if (i == count)
+        return NULL;
+    if (*nw > (buckets[i].iov_len - offset))
+        *nw = buckets[i].iov_len - offset;
+    return ((unsigned char*)buckets[i].iov_base) + offset;
+
+}
diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
index 4b2b7e9..643b356 100644
--- a/ssl/ssl_lib.c
+++ b/ssl/ssl_lib.c
@@ -980,6 +980,39 @@ int SSL_write(SSL *s, const void *buf, int num)
     return (s->method->ssl_write(s, buf, num));
 }
 
+#ifndef OPENSSL_NO_IOVEC
+
+int SSL_readv(SSL *s, ssl_bucket *buckets, int count)
+{
+    if (s->handshake_func == 0) {
+        SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
+        return -1;
+    }
+
+    if ((s->shutdown & SSL_RECEIVED_SHUTDOWN)) {
+        s->rwstate=SSL_NOTHING;
+        return(0);
+    }
+    return (s->method->ssl_readv(s, buckets, count));
+}
+
+int SSL_writev(SSL *s, const ssl_bucket *buckets, int count)
+{
+    if (s->handshake_func == 0) {
+        SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
+        return -1;
+    }
+
+    if ((s->shutdown & SSL_SENT_SHUTDOWN)) {
+        s->rwstate=SSL_NOTHING;
+        SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN);
+        return(-1);
+    }
+    return (s->method->ssl_writev(s, buckets, count));
+}
+
+#endif /* !OPENSSL_NO_IOVEC */
+
 int SSL_shutdown(SSL *s)
 {
     /*
diff --git a/ssl/ssl_locl.h b/ssl/ssl_locl.h
index 239aadb..16e6790 100644
--- a/ssl/ssl_locl.h
+++ b/ssl/ssl_locl.h
@@ -557,6 +557,14 @@ struct ssl_method_st {
     int (*ssl_read_bytes) (SSL *s, int type, unsigned char *buf, int len,
                            int peek);
     int (*ssl_write_bytes) (SSL *s, int type, const void *buf_, int len);
+# ifndef OPENSSL_NO_IOVEC
+    int (*ssl_readv)(SSL *s, ssl_bucket *buckets, unsigned int count);
+    int (*ssl_writev)(SSL *s, const ssl_bucket *buckets, unsigned int count);
+    int (*ssl_readv_bytes)(SSL *s, int type, ssl_bucket *buckets,
+                           unsigned int count, int peek);
+    int (*ssl_writev_bytes)(SSL *s, int type, const ssl_bucket *buckets,
+                            unsigned int count);
+# endif /* !OPENSSL_NO_IOVEC */
     int (*ssl_dispatch_alert) (SSL *s);
     long (*ssl_ctrl) (SSL *s, int cmd, long larg, void *parg);
     long (*ssl_ctx_ctrl) (SSL_CTX *ctx, int cmd, long larg, void *parg);
@@ -1779,6 +1787,28 @@ extern const SSL3_ENC_METHOD SSLv3_enc_data;
 extern const SSL3_ENC_METHOD DTLSv1_enc_data;
 extern const SSL3_ENC_METHOD DTLSv1_2_enc_data;
 
+# ifndef OPENSSL_NO_IOVEC
+
+int ssl3_readv(SSL *s, ssl_bucket *buckets, unsigned int count);
+int ssl3_writev(SSL *s, const ssl_bucket *buckets, unsigned int count);
+
+int ssl3_readv_bytes(SSL *s, int type, ssl_bucket *buckets, unsigned int count, int peek);
+int ssl3_writev_bytes(SSL *s, int type, const ssl_bucket *buckets, unsigned int count);
+
+int dtls1_readv_bytes(SSL *s, int type, ssl_bucket *buckets, unsigned int count, int peek);
+int dtls1_writev_bytes(SSL *s, int type, const ssl_bucket *buckets, unsigned int count);
+
+#  define OPENSSL_SSL3_IOVEC_FNS ssl3_readv, ssl3_writev, ssl3_readv_bytes, ssl3_writev_bytes,
+#  define OPENSSL_DTLS1_IOVEC_FNS ssl3_readv, ssl3_writev, dtls1_readv_bytes, dtls1_writev_bytes,
+
+# else  /* !OPENSSL_NO_IOVEC */
+
+#  define OPENSSL_SSL3_IOVEC_FNS
+#  define OPENSSL_DTLS1_IOVEC_FNS
+
+# endif /* OPENSSL_NO_IOVEC */
+
+
 # define IMPLEMENT_tls_meth_func(version, func_name, s_accept, s_connect, \
                                 s_get_meth, enc_data) \
 const SSL_METHOD *func_name(void)  \
@@ -1799,6 +1829,7 @@ const SSL_METHOD *func_name(void)  \
                 ssl3_get_message, \
                 ssl3_read_bytes, \
                 ssl3_write_bytes, \
+                OPENSSL_SSL3_IOVEC_FNS \
                 ssl3_dispatch_alert, \
                 ssl3_ctrl, \
                 ssl3_ctx_ctrl, \
@@ -1837,6 +1868,7 @@ const SSL_METHOD *func_name(void)  \
                 ssl3_get_message, \
                 ssl3_read_bytes, \
                 ssl3_write_bytes, \
+                OPENSSL_SSL3_IOVEC_FNS \
                 ssl3_dispatch_alert, \
                 ssl3_ctrl, \
                 ssl3_ctx_ctrl, \
@@ -1876,6 +1908,7 @@ const SSL_METHOD *func_name(void)  \
                 dtls1_get_message, \
                 dtls1_read_bytes, \
                 dtls1_write_app_data_bytes, \
+                OPENSSL_DTLS1_IOVEC_FNS \
                 dtls1_dispatch_alert, \
                 dtls1_ctrl, \
                 ssl3_ctx_ctrl, \
@@ -2042,6 +2075,10 @@ __owur int dtls1_read_failed(SSL *s, int code);
 __owur int dtls1_buffer_message(SSL *s, int ccs);
 __owur int dtls1_retransmit_message(SSL *s, unsigned short seq,
                              unsigned long frag_off, int *found);
+__owur int ssl3_do_vcompress(SSL *ssl, const ssl_bucket *buckets, unsigned int count,
+                             size_t offset, size_t len);
+__owur int ssl3_writev_pending(SSL *s, int type, const ssl_bucket *buckets, unsigned int count,
+                               unsigned int len, int reset);
 __owur int dtls1_get_queue_priority(unsigned short seq, int is_ccs);
 int dtls1_retransmit_buffered_messages(SSL *s);
 void dtls1_clear_record_buffer(SSL *s);
-- 
2.3.2 (Apple Git-55)

