tor  0.4.2.0-alpha-dev
Macros | Functions
buffers_tls.c File Reference
#include "orconfig.h"
#include <stddef.h>
#include "lib/buf/buffers.h"
#include "lib/tls/buffers_tls.h"
#include "lib/cc/torint.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/tls/tortls.h"

Go to the source code of this file.

Functions

static int read_to_chunk_tls (buf_t *buf, chunk_t *chunk, tor_tls_t *tls, size_t at_most)
 
int buf_read_from_tls (buf_t *buf, tor_tls_t *tls, size_t at_most)
 
static int flush_chunk_tls (tor_tls_t *tls, buf_t *buf, chunk_t *chunk, size_t sz, size_t *buf_flushlen)
 
int buf_flush_to_tls (buf_t *buf, tor_tls_t *tls, size_t flushlen, size_t *buf_flushlen)
 

Detailed Description

Read and write data on a tor_tls_t connection from a buf_t object.

Definition in file buffers_tls.c.

Function Documentation

◆ buf_flush_to_tls()

int buf_flush_to_tls ( buf_t *  buf,
tor_tls_t tls,
size_t  flushlen,
size_t *  buf_flushlen 
)

As buf_flush_to_socket(), but writes data to a TLS connection. Can write more than flushlen bytes.

Definition at line 142 of file buffers_tls.c.

References tor_assert().

◆ buf_read_from_tls()

int buf_read_from_tls ( buf_t *  buf,
tor_tls_t tls,
size_t  at_most 
)

As read_to_buf, but reads from a TLS connection, and returns a TLS status value rather than the number of bytes read.

Using TLS on OR connections complicates matters in two ways.

First, a TLS stream has its own read buffer independent of the connection's read buffer. (TLS needs to read an entire frame from the network before it can decrypt any data. Thus, trying to read 1 byte from TLS can require that several KB be read from the network and decrypted. The extra data is stored in TLS's decrypt buffer.) Because the data hasn't been read by Tor (it's still inside the TLS), this means that sometimes a connection "has stuff to read" even when poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is used in connection.c to detect TLS objects with non-empty internal buffers and read from them again.

Second, the TLS stream's events do not correspond directly to network events: sometimes, before a TLS stream can read, the network must be ready to write – or vice versa.

Definition at line 64 of file buffers_tls.c.

◆ flush_chunk_tls()

static int flush_chunk_tls ( tor_tls_t tls,
buf_t *  buf,
chunk_t *  chunk,
size_t  sz,
size_t *  buf_flushlen 
)
inlinestatic

Helper for buf_flush_to_tls(): try to write sz bytes from chunk chunk of buffer buf onto socket s. (Tries to write more if there is a forced pending write size.) On success, deduct the bytes written from *buf_flushlen. Return the number of bytes written on success, and a TOR_TLS error code on failure or blocking.

Definition at line 108 of file buffers_tls.c.

References buf_drain(), tor_assert(), tor_tls_get_forced_write_size(), and tor_tls_write().

◆ read_to_chunk_tls()

static int read_to_chunk_tls ( buf_t *  buf,
chunk_t *  chunk,
tor_tls_t tls,
size_t  at_most 
)
inlinestatic

As read_to_chunk(), but return (negative) error code on error, blocking, or TLS, and the number of bytes read otherwise.

Definition at line 29 of file buffers_tls.c.

References tor_assert().