Merge remote-tracking branch 'public/bug23985_029' into maint-0.2.9
[tor.git] / src / or / buffers.h
blob52b21d5885a38d08cd2d830bb964fc738a873a42
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-2016, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file buffers.h
9 * \brief Header file for buffers.c.
10 **/
12 #ifndef TOR_BUFFERS_H
13 #define TOR_BUFFERS_H
15 #include "testsupport.h"
17 buf_t *buf_new(void);
18 buf_t *buf_new_with_capacity(size_t size);
19 size_t buf_get_default_chunk_size(const buf_t *buf);
20 void buf_free(buf_t *buf);
21 void buf_clear(buf_t *buf);
22 buf_t *buf_copy(const buf_t *buf);
24 MOCK_DECL(size_t, buf_datalen, (const buf_t *buf));
25 size_t buf_allocation(const buf_t *buf);
26 size_t buf_slack(const buf_t *buf);
28 uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
29 size_t buf_get_total_allocation(void);
31 int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
32 int *socket_error);
33 int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
35 int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen);
36 int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
38 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
39 int write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
40 const char *data, size_t data_len, int done);
41 int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
42 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
43 int fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto);
44 int fetch_from_buf_http(buf_t *buf,
45 char **headers_out, size_t max_headerlen,
46 char **body_out, size_t *body_used, size_t max_bodylen,
47 int force_complete);
48 socks_request_t *socks_request_new(void);
49 void socks_request_free(socks_request_t *req);
50 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
51 int log_sockstype, int safe_socks);
52 int fetch_from_buf_socks_client(buf_t *buf, int state, char **reason);
53 int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
55 int peek_buf_has_control0_command(buf_t *buf);
57 int fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out);
59 int buf_set_to_copy(buf_t **output,
60 const buf_t *input);
62 void assert_buf_ok(buf_t *buf);
64 #ifdef BUFFERS_PRIVATE
65 STATIC int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
66 STATIC void buf_pullup(buf_t *buf, size_t bytes);
67 void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz);
68 STATIC size_t preferred_chunk_size(size_t target);
70 #define DEBUG_CHUNK_ALLOC
71 /** A single chunk on a buffer. */
72 typedef struct chunk_t {
73 struct chunk_t *next; /**< The next chunk on the buffer. */
74 size_t datalen; /**< The number of bytes stored in this chunk */
75 size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
76 #ifdef DEBUG_CHUNK_ALLOC
77 size_t DBG_alloc;
78 #endif
79 char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
80 uint32_t inserted_time; /**< Timestamp in truncated ms since epoch
81 * when this chunk was inserted. */
82 char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
83 * this chunk. */
84 } chunk_t;
86 /** Magic value for buf_t.magic, to catch pointer errors. */
87 #define BUFFER_MAGIC 0xB0FFF312u
88 /** A resizeable buffer, optimized for reading and writing. */
89 struct buf_t {
90 uint32_t magic; /**< Magic cookie for debugging: Must be set to
91 * BUFFER_MAGIC. */
92 size_t datalen; /**< How many bytes is this buffer holding right now? */
93 size_t default_chunk_size; /**< Don't allocate any chunks smaller than
94 * this for this buffer. */
95 chunk_t *head; /**< First chunk in the list, or NULL for none. */
96 chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
98 #endif
100 #endif