In routerlist_assert_ok(), check r2 before taking &(r2->cache_info)
[tor.git] / src / or / buffers.h
blobc90e14750e81e64c2bc25155fe843eb6e92bb71d
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-2013, 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);
23 void buf_shrink(buf_t *buf);
24 size_t buf_shrink_freelists(int free_all);
25 void buf_dump_freelist_sizes(int severity);
27 size_t buf_datalen(const buf_t *buf);
28 size_t buf_allocation(const buf_t *buf);
29 size_t buf_slack(const buf_t *buf);
31 uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
32 size_t buf_get_total_allocation(void);
34 int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
35 int *socket_error);
36 int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
38 int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen);
39 int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
41 int write_to_buf(const char *string, size_t string_len, buf_t *buf);
42 int write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
43 const char *data, size_t data_len, int done);
44 int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
45 int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
46 int fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto);
47 int fetch_from_buf_http(buf_t *buf,
48 char **headers_out, size_t max_headerlen,
49 char **body_out, size_t *body_used, size_t max_bodylen,
50 int force_complete);
51 socks_request_t *socks_request_new(void);
52 void socks_request_free(socks_request_t *req);
53 int fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
54 int log_sockstype, int safe_socks);
55 int fetch_from_buf_socks_client(buf_t *buf, int state, char **reason);
56 int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
58 int peek_buf_has_control0_command(buf_t *buf);
60 int fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out);
62 #ifdef USE_BUFFEREVENTS
63 int fetch_var_cell_from_evbuffer(struct evbuffer *buf, var_cell_t **out,
64 int linkproto);
65 int fetch_from_evbuffer_socks(struct evbuffer *buf, socks_request_t *req,
66 int log_sockstype, int safe_socks);
67 int fetch_from_evbuffer_socks_client(struct evbuffer *buf, int state,
68 char **reason);
69 int fetch_from_evbuffer_http(struct evbuffer *buf,
70 char **headers_out, size_t max_headerlen,
71 char **body_out, size_t *body_used, size_t max_bodylen,
72 int force_complete);
73 int peek_evbuffer_has_control0_command(struct evbuffer *buf);
74 int write_to_evbuffer_zlib(struct evbuffer *buf, tor_zlib_state_t *state,
75 const char *data, size_t data_len,
76 int done);
77 int fetch_ext_or_command_from_evbuffer(struct evbuffer *buf,
78 ext_or_cmd_t **out);
79 #endif
81 #ifdef USE_BUFFEREVENTS
82 #define generic_buffer_new() evbuffer_new()
83 #define generic_buffer_len(b) evbuffer_get_length((b))
84 #define generic_buffer_add(b,dat,len) evbuffer_add((b),(dat),(len))
85 #define generic_buffer_get(b,buf,buflen) evbuffer_remove((b),(buf),(buflen))
86 #define generic_buffer_clear(b) evbuffer_drain((b), evbuffer_get_length((b)))
87 #define generic_buffer_free(b) evbuffer_free((b))
88 #define generic_buffer_fetch_ext_or_cmd(b, out) \
89 fetch_ext_or_command_from_evbuffer((b), (out))
90 #else
91 #define generic_buffer_new() buf_new()
92 #define generic_buffer_len(b) buf_datalen((b))
93 #define generic_buffer_add(b,dat,len) write_to_buf((dat),(len),(b))
94 #define generic_buffer_get(b,buf,buflen) fetch_from_buf((buf),(buflen),(b))
95 #define generic_buffer_clear(b) buf_clear((b))
96 #define generic_buffer_free(b) buf_free((b))
97 #define generic_buffer_fetch_ext_or_cmd(b, out) \
98 fetch_ext_or_command_from_buf((b), (out))
99 #endif
100 int generic_buffer_set_to_copy(generic_buffer_t **output,
101 const generic_buffer_t *input);
103 void assert_buf_ok(buf_t *buf);
105 #ifdef BUFFERS_PRIVATE
106 STATIC int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
107 STATIC void buf_pullup(buf_t *buf, size_t bytes, int nulterminate);
108 void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz);
110 #define DEBUG_CHUNK_ALLOC
111 /** A single chunk on a buffer or in a freelist. */
112 typedef struct chunk_t {
113 struct chunk_t *next; /**< The next chunk on the buffer or freelist. */
114 size_t datalen; /**< The number of bytes stored in this chunk */
115 size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
116 #ifdef DEBUG_CHUNK_ALLOC
117 size_t DBG_alloc;
118 #endif
119 char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
120 uint32_t inserted_time; /**< Timestamp in truncated ms since epoch
121 * when this chunk was inserted. */
122 char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
123 * this chunk. */
124 } chunk_t;
126 /** Magic value for buf_t.magic, to catch pointer errors. */
127 #define BUFFER_MAGIC 0xB0FFF312u
128 /** A resizeable buffer, optimized for reading and writing. */
129 struct buf_t {
130 uint32_t magic; /**< Magic cookie for debugging: Must be set to
131 * BUFFER_MAGIC. */
132 size_t datalen; /**< How many bytes is this buffer holding right now? */
133 size_t default_chunk_size; /**< Don't allocate any chunks smaller than
134 * this for this buffer. */
135 chunk_t *head; /**< First chunk in the list, or NULL for none. */
136 chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
138 #endif
140 #endif