2010-07-22 00:46:18 +02:00
|
|
|
/* Copyright (c) 2001 Matej Pfajfar.
|
|
|
|
* Copyright (c) 2001-2004, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2016-02-27 18:48:19 +01:00
|
|
|
* Copyright (c) 2007-2016, The Tor Project, Inc. */
|
2010-07-22 00:46:18 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file buffers.h
|
|
|
|
* \brief Header file for buffers.c.
|
|
|
|
**/
|
|
|
|
|
2012-10-12 18:13:10 +02:00
|
|
|
#ifndef TOR_BUFFERS_H
|
|
|
|
#define TOR_BUFFERS_H
|
2010-07-22 00:46:18 +02:00
|
|
|
|
2013-06-06 23:58:28 +02:00
|
|
|
#include "testsupport.h"
|
|
|
|
|
2010-07-22 00:46:18 +02:00
|
|
|
buf_t *buf_new(void);
|
|
|
|
buf_t *buf_new_with_capacity(size_t size);
|
2014-01-04 19:30:56 +01:00
|
|
|
size_t buf_get_default_chunk_size(const buf_t *buf);
|
2010-07-22 00:46:18 +02:00
|
|
|
void buf_free(buf_t *buf);
|
|
|
|
void buf_clear(buf_t *buf);
|
2011-07-18 21:36:20 +02:00
|
|
|
buf_t *buf_copy(const buf_t *buf);
|
2010-07-22 00:46:18 +02:00
|
|
|
|
2014-01-23 15:42:14 +01:00
|
|
|
MOCK_DECL(size_t, buf_datalen, (const buf_t *buf));
|
2010-07-22 00:46:18 +02:00
|
|
|
size_t buf_allocation(const buf_t *buf);
|
|
|
|
size_t buf_slack(const buf_t *buf);
|
|
|
|
|
2013-11-16 00:38:52 +01:00
|
|
|
uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
|
|
|
|
size_t buf_get_total_allocation(void);
|
|
|
|
|
2011-05-23 06:17:48 +02:00
|
|
|
int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
|
2010-07-22 00:46:18 +02:00
|
|
|
int *socket_error);
|
|
|
|
int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
|
|
|
|
|
2011-05-23 06:17:48 +02:00
|
|
|
int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen);
|
2010-07-22 00:46:18 +02:00
|
|
|
int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
|
|
|
|
|
|
|
|
int write_to_buf(const char *string, size_t string_len, buf_t *buf);
|
|
|
|
int write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
|
|
|
|
const char *data, size_t data_len, int done);
|
|
|
|
int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
|
|
|
|
int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
|
|
|
|
int fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto);
|
|
|
|
int fetch_from_buf_http(buf_t *buf,
|
|
|
|
char **headers_out, size_t max_headerlen,
|
|
|
|
char **body_out, size_t *body_used, size_t max_bodylen,
|
|
|
|
int force_complete);
|
2011-06-29 18:31:17 +02:00
|
|
|
socks_request_t *socks_request_new(void);
|
|
|
|
void socks_request_free(socks_request_t *req);
|
2010-07-22 00:46:18 +02:00
|
|
|
int fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
|
|
|
|
int log_sockstype, int safe_socks);
|
|
|
|
int fetch_from_buf_socks_client(buf_t *buf, int state, char **reason);
|
|
|
|
int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
|
|
|
|
|
|
|
|
int peek_buf_has_control0_command(buf_t *buf);
|
|
|
|
|
2012-03-16 14:40:44 +01:00
|
|
|
int fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out);
|
|
|
|
|
2016-08-02 19:40:19 +02:00
|
|
|
int buf_set_to_copy(buf_t **output,
|
|
|
|
const buf_t *input);
|
2011-07-18 21:36:20 +02:00
|
|
|
|
2010-07-22 00:46:18 +02:00
|
|
|
void assert_buf_ok(buf_t *buf);
|
|
|
|
|
|
|
|
#ifdef BUFFERS_PRIVATE
|
2013-06-06 23:58:28 +02:00
|
|
|
STATIC int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
|
2015-09-01 20:36:25 +02:00
|
|
|
STATIC void buf_pullup(buf_t *buf, size_t bytes);
|
2014-01-04 19:30:56 +01:00
|
|
|
void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz);
|
2016-09-13 15:07:12 +02:00
|
|
|
STATIC size_t preferred_chunk_size(size_t target);
|
2014-05-08 18:40:40 +02:00
|
|
|
|
|
|
|
#define DEBUG_CHUNK_ALLOC
|
2015-02-11 15:03:50 +01:00
|
|
|
/** A single chunk on a buffer. */
|
2014-05-08 18:40:40 +02:00
|
|
|
typedef struct chunk_t {
|
2015-02-11 15:03:50 +01:00
|
|
|
struct chunk_t *next; /**< The next chunk on the buffer. */
|
2014-05-08 18:40:40 +02:00
|
|
|
size_t datalen; /**< The number of bytes stored in this chunk */
|
|
|
|
size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
|
|
|
|
#ifdef DEBUG_CHUNK_ALLOC
|
|
|
|
size_t DBG_alloc;
|
|
|
|
#endif
|
|
|
|
char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
|
|
|
|
uint32_t inserted_time; /**< Timestamp in truncated ms since epoch
|
|
|
|
* when this chunk was inserted. */
|
|
|
|
char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
|
|
|
|
* this chunk. */
|
|
|
|
} chunk_t;
|
|
|
|
|
|
|
|
/** Magic value for buf_t.magic, to catch pointer errors. */
|
|
|
|
#define BUFFER_MAGIC 0xB0FFF312u
|
|
|
|
/** A resizeable buffer, optimized for reading and writing. */
|
|
|
|
struct buf_t {
|
|
|
|
uint32_t magic; /**< Magic cookie for debugging: Must be set to
|
|
|
|
* BUFFER_MAGIC. */
|
|
|
|
size_t datalen; /**< How many bytes is this buffer holding right now? */
|
|
|
|
size_t default_chunk_size; /**< Don't allocate any chunks smaller than
|
|
|
|
* this for this buffer. */
|
|
|
|
chunk_t *head; /**< First chunk in the list, or NULL for none. */
|
|
|
|
chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
|
|
|
|
};
|
2010-07-22 00:46:18 +02:00
|
|
|
#endif
|
|
|
|
|
2017-02-13 15:39:46 +01:00
|
|
|
#ifdef BUFFERS_PRIVATE
|
|
|
|
int buf_http_find_content_length(const char *headers, size_t headerlen,
|
|
|
|
size_t *result_out);
|
|
|
|
#endif
|
|
|
|
|
2010-07-22 00:46:18 +02:00
|
|
|
#endif
|
|
|
|
|