buffers.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2015, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file buffers.h
  8. * \brief Header file for buffers.c.
  9. **/
  10. #ifndef TOR_BUFFERS_H
  11. #define TOR_BUFFERS_H
  12. #include "testsupport.h"
  13. buf_t *buf_new(void);
  14. buf_t *buf_new_with_capacity(size_t size);
  15. size_t buf_get_default_chunk_size(const buf_t *buf);
  16. void buf_free(buf_t *buf);
  17. void buf_clear(buf_t *buf);
  18. buf_t *buf_copy(const buf_t *buf);
  19. MOCK_DECL(size_t, buf_datalen, (const buf_t *buf));
  20. size_t buf_allocation(const buf_t *buf);
  21. size_t buf_slack(const buf_t *buf);
  22. uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
  23. size_t buf_get_total_allocation(void);
  24. int read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
  25. int *socket_error);
  26. int read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf);
  27. int flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen);
  28. int flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen);
  29. int write_to_buf(const char *string, size_t string_len, buf_t *buf);
  30. int write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  31. const char *data, size_t data_len, int done);
  32. int move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
  33. int fetch_from_buf(char *string, size_t string_len, buf_t *buf);
  34. int fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto);
  35. int fetch_from_buf_http(buf_t *buf,
  36. char **headers_out, size_t max_headerlen,
  37. char **body_out, size_t *body_used, size_t max_bodylen,
  38. int force_complete);
  39. socks_request_t *socks_request_new(void);
  40. void socks_request_free(socks_request_t *req);
  41. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  42. int log_sockstype, int safe_socks);
  43. int fetch_from_buf_socks_client(buf_t *buf, int state, char **reason);
  44. int fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len);
  45. int peek_buf_has_control0_command(buf_t *buf);
  46. int fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out);
  47. #ifdef USE_BUFFEREVENTS
  48. int fetch_var_cell_from_evbuffer(struct evbuffer *buf, var_cell_t **out,
  49. int linkproto);
  50. int fetch_from_evbuffer_socks(struct evbuffer *buf, socks_request_t *req,
  51. int log_sockstype, int safe_socks);
  52. int fetch_from_evbuffer_socks_client(struct evbuffer *buf, int state,
  53. char **reason);
  54. int fetch_from_evbuffer_http(struct evbuffer *buf,
  55. char **headers_out, size_t max_headerlen,
  56. char **body_out, size_t *body_used, size_t max_bodylen,
  57. int force_complete);
  58. int peek_evbuffer_has_control0_command(struct evbuffer *buf);
  59. int write_to_evbuffer_zlib(struct evbuffer *buf, tor_zlib_state_t *state,
  60. const char *data, size_t data_len,
  61. int done);
  62. int fetch_ext_or_command_from_evbuffer(struct evbuffer *buf,
  63. ext_or_cmd_t **out);
  64. #endif
  65. #ifdef USE_BUFFEREVENTS
  66. #define generic_buffer_new() evbuffer_new()
  67. #define generic_buffer_len(b) evbuffer_get_length((b))
  68. #define generic_buffer_add(b,dat,len) evbuffer_add((b),(dat),(len))
  69. #define generic_buffer_get(b,buf,buflen) evbuffer_remove((b),(buf),(buflen))
  70. #define generic_buffer_clear(b) evbuffer_drain((b), evbuffer_get_length((b)))
  71. #define generic_buffer_free(b) evbuffer_free((b))
  72. #define generic_buffer_fetch_ext_or_cmd(b, out) \
  73. fetch_ext_or_command_from_evbuffer((b), (out))
  74. #else
  75. #define generic_buffer_new() buf_new()
  76. #define generic_buffer_len(b) buf_datalen((b))
  77. #define generic_buffer_add(b,dat,len) write_to_buf((dat),(len),(b))
  78. #define generic_buffer_get(b,buf,buflen) fetch_from_buf((buf),(buflen),(b))
  79. #define generic_buffer_clear(b) buf_clear((b))
  80. #define generic_buffer_free(b) buf_free((b))
  81. #define generic_buffer_fetch_ext_or_cmd(b, out) \
  82. fetch_ext_or_command_from_buf((b), (out))
  83. #endif
  84. int generic_buffer_set_to_copy(generic_buffer_t **output,
  85. const generic_buffer_t *input);
  86. void assert_buf_ok(buf_t *buf);
  87. #ifdef BUFFERS_PRIVATE
  88. STATIC int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
  89. STATIC void buf_pullup(buf_t *buf, size_t bytes, int nulterminate);
  90. void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz);
  91. #define DEBUG_CHUNK_ALLOC
  92. /** A single chunk on a buffer. */
  93. typedef struct chunk_t {
  94. struct chunk_t *next; /**< The next chunk on the buffer. */
  95. size_t datalen; /**< The number of bytes stored in this chunk */
  96. size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
  97. #ifdef DEBUG_CHUNK_ALLOC
  98. size_t DBG_alloc;
  99. #endif
  100. char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
  101. uint32_t inserted_time; /**< Timestamp in truncated ms since epoch
  102. * when this chunk was inserted. */
  103. char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
  104. * this chunk. */
  105. } chunk_t;
  106. /** Magic value for buf_t.magic, to catch pointer errors. */
  107. #define BUFFER_MAGIC 0xB0FFF312u
  108. /** A resizeable buffer, optimized for reading and writing. */
  109. struct buf_t {
  110. uint32_t magic; /**< Magic cookie for debugging: Must be set to
  111. * BUFFER_MAGIC. */
  112. size_t datalen; /**< How many bytes is this buffer holding right now? */
  113. size_t default_chunk_size; /**< Don't allocate any chunks smaller than
  114. * this for this buffer. */
  115. chunk_t *head; /**< First chunk in the list, or NULL for none. */
  116. chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
  117. };
  118. #endif
  119. #endif