buffers.h 4.25 KB
Newer Older
Sebastian Hahn's avatar
Sebastian Hahn committed
1
2
3
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4
 * Copyright (c) 2007-2017, The Tor Project, Inc. */
Sebastian Hahn's avatar
Sebastian Hahn committed
5
6
7
8
9
10
11
/* See LICENSE for licensing information */

/**
 * \file buffers.h
 * \brief Header file for buffers.c.
 **/

12
13
#ifndef TOR_BUFFERS_H
#define TOR_BUFFERS_H
Sebastian Hahn's avatar
Sebastian Hahn committed
14

15
16
17
#include "compat.h"
#include "compat.h"
#include "torint.h"
18
19
#include "testsupport.h"

20
21
22
23
typedef struct buf_t buf_t;

struct tor_compress_state_t;

Sebastian Hahn's avatar
Sebastian Hahn committed
24
25
buf_t *buf_new(void);
buf_t *buf_new_with_capacity(size_t size);
26
size_t buf_get_default_chunk_size(const buf_t *buf);
Sebastian Hahn's avatar
Sebastian Hahn committed
27
28
void buf_free(buf_t *buf);
void buf_clear(buf_t *buf);
29
buf_t *buf_copy(const buf_t *buf);
Sebastian Hahn's avatar
Sebastian Hahn committed
30

Andrea Shepard's avatar
Andrea Shepard committed
31
MOCK_DECL(size_t, buf_datalen, (const buf_t *buf));
Sebastian Hahn's avatar
Sebastian Hahn committed
32
33
34
size_t buf_allocation(const buf_t *buf);
size_t buf_slack(const buf_t *buf);

35
36
37
uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
size_t buf_get_total_allocation(void);

38
int buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most,
39
40
                         int *reached_eof,
                         int *socket_error);
Sebastian Hahn's avatar
Sebastian Hahn committed
41

42
int buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz,
43
                        size_t *buf_flushlen);
Sebastian Hahn's avatar
Sebastian Hahn committed
44

45
int buf_add(buf_t *buf, const char *string, size_t string_len);
46
int buf_add_compress(buf_t *buf, struct tor_compress_state_t *state,
47
                          const char *data, size_t data_len, int done);
48
int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
49
void buf_peek(const buf_t *buf, char *string, size_t string_len);
50
void buf_drain(buf_t *buf, size_t n);
51
int buf_get_bytes(buf_t *buf, char *string, size_t string_len);
52
int buf_get_line(buf_t *buf, char *data_out, size_t *data_len);
Sebastian Hahn's avatar
Sebastian Hahn committed
53

54
#define PEEK_BUF_STARTSWITH_MAX 16
55
int buf_peek_startswith(const buf_t *buf, const char *cmd);
Sebastian Hahn's avatar
Sebastian Hahn committed
56

57
58
int buf_set_to_copy(buf_t **output,
                    const buf_t *input);
59

60
void buf_assert_ok(buf_t *buf);
Sebastian Hahn's avatar
Sebastian Hahn committed
61

62
int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
63
64
void buf_pullup(buf_t *buf, size_t bytes,
                const char **head_out, size_t *len_out);
65

Sebastian Hahn's avatar
Sebastian Hahn committed
66
#ifdef BUFFERS_PRIVATE
67
68
69
#ifdef TOR_UNIT_TESTS
buf_t *buf_new_with_data(const char *cp, size_t sz);
#endif
70
size_t buf_preferred_chunk_size(size_t target);
71
72

#define DEBUG_CHUNK_ALLOC
73
/** A single chunk on a buffer. */
74
typedef struct chunk_t {
75
  struct chunk_t *next; /**< The next chunk on the buffer. */
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  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. */
};
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

chunk_t *buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped);
/** If a read onto the end of a chunk would be smaller than this number, then
 * just start a new chunk. */
#define MIN_READ_LEN 8

/** Return the number of bytes that can be written onto <b>chunk</b> without
 * running out of space. */
static inline size_t
CHUNK_REMAINING_CAPACITY(const chunk_t *chunk)
{
  return (chunk->mem + chunk->memlen) - (chunk->data + chunk->datalen);
}

/** Return the next character in <b>chunk</b> onto which data can be appended.
 * If the chunk is full, this might be off the end of chunk->mem. */
static inline char *
CHUNK_WRITE_PTR(chunk_t *chunk)
{
  return chunk->data + chunk->datalen;
}

122
#endif /* defined(BUFFERS_PRIVATE) */
Sebastian Hahn's avatar
Sebastian Hahn committed
123

124
#endif /* !defined(TOR_BUFFERS_H) */
Sebastian Hahn's avatar
Sebastian Hahn committed
125