1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 #define BUFFERS_PRIVATE
11 #include "buffers_tls.h"
22 /** As read_to_chunk(), but return (negative) error code on error, blocking,
23 * or TLS, and the number of bytes read otherwise. */
25 read_to_chunk_tls(buf_t
*buf
, chunk_t
*chunk
, tor_tls_t
*tls
,
30 tor_assert(CHUNK_REMAINING_CAPACITY(chunk
) >= at_most
);
31 read_result
= tor_tls_read(tls
, CHUNK_WRITE_PTR(chunk
), at_most
);
34 buf
->datalen
+= read_result
;
35 chunk
->datalen
+= read_result
;
39 /** As read_to_buf, but reads from a TLS connection, and returns a TLS
40 * status value rather than the number of bytes read.
42 * Using TLS on OR connections complicates matters in two ways.
44 * First, a TLS stream has its own read buffer independent of the
45 * connection's read buffer. (TLS needs to read an entire frame from
46 * the network before it can decrypt any data. Thus, trying to read 1
47 * byte from TLS can require that several KB be read from the network
48 * and decrypted. The extra data is stored in TLS's decrypt buffer.)
49 * Because the data hasn't been read by Tor (it's still inside the TLS),
50 * this means that sometimes a connection "has stuff to read" even when
51 * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
52 * used in connection.c to detect TLS objects with non-empty internal
53 * buffers and read from them again.
55 * Second, the TLS stream's events do not correspond directly to network
56 * events: sometimes, before a TLS stream can read, the network must be
57 * ready to write -- or vice versa.
60 buf_read_from_tls(buf_t
*buf
, tor_tls_t
*tls
, size_t at_most
)
63 size_t total_read
= 0;
65 check_no_tls_errors();
67 if (BUG(buf
->datalen
>= INT_MAX
))
69 if (BUG(buf
->datalen
>= INT_MAX
- at_most
))
72 while (at_most
> total_read
) {
73 size_t readlen
= at_most
- total_read
;
75 if (!buf
->tail
|| CHUNK_REMAINING_CAPACITY(buf
->tail
) < MIN_READ_LEN
) {
76 chunk
= buf_add_chunk_with_capacity(buf
, at_most
, 1);
77 if (readlen
> chunk
->memlen
)
78 readlen
= chunk
->memlen
;
80 size_t cap
= CHUNK_REMAINING_CAPACITY(buf
->tail
);
86 r
= read_to_chunk_tls(buf
, chunk
, tls
, readlen
);
89 tor_assert(total_read
+r
< INT_MAX
);
91 if ((size_t)r
< readlen
) /* eof, block, or no more to read. */
94 return (int)total_read
;
97 /** Helper for buf_flush_to_tls(): try to write <b>sz</b> bytes from chunk
98 * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write
99 * more if there is a forced pending write size.) On success, deduct the
100 * bytes written from *<b>buf_flushlen</b>. Return the number of bytes
101 * written on success, and a TOR_TLS error code on failure or blocking.
104 flush_chunk_tls(tor_tls_t
*tls
, buf_t
*buf
, chunk_t
*chunk
,
105 size_t sz
, size_t *buf_flushlen
)
111 forced
= tor_tls_get_forced_write_size(tls
);
116 tor_assert(sz
<= chunk
->datalen
);
121 r
= tor_tls_write(tls
, data
, sz
);
124 if (*buf_flushlen
> (size_t)r
)
129 log_debug(LD_NET
,"flushed %d bytes, %d ready to flush, %d remain.",
130 r
,(int)*buf_flushlen
,(int)buf
->datalen
);
134 /** As buf_flush_to_socket(), but writes data to a TLS connection. Can write
135 * more than <b>flushlen</b> bytes.
138 buf_flush_to_tls(buf_t
*buf
, tor_tls_t
*tls
, size_t flushlen
,
139 size_t *buf_flushlen
)
144 tor_assert(buf_flushlen
);
145 if (BUG(*buf_flushlen
> buf
->datalen
)) {
146 *buf_flushlen
= buf
->datalen
;
148 if (BUG(flushlen
> *buf_flushlen
)) {
149 flushlen
= *buf_flushlen
;
151 sz
= (ssize_t
) flushlen
;
153 /* we want to let tls write even if flushlen is zero, because it might
154 * have a partial record pending */
155 check_no_tls_errors();
160 if ((ssize_t
)buf
->head
->datalen
>= sz
)
163 flushlen0
= buf
->head
->datalen
;
168 r
= flush_chunk_tls(tls
, buf
, buf
->head
, flushlen0
, buf_flushlen
);
173 if (r
== 0) /* Can't flush any more now. */
176 tor_assert(flushed
< INT_MAX
);