relay.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file relay.h
  8. * \brief Header file for relay.c.
  9. **/
  10. #ifndef TOR_RELAY_H
  11. #define TOR_RELAY_H
  12. #include <stdint.h>
  13. #include "core/or/or.h"
  14. extern uint64_t stats_n_relay_cells_relayed;
  15. extern uint64_t stats_n_relay_cells_delivered;
  16. extern uint64_t stats_n_circ_max_cell_reached;
  17. void relay_consensus_has_changed(const networkstatus_t *ns);
  18. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  19. cell_direction_t cell_direction);
  20. size_t cell_queues_get_total_allocation(void);
  21. void relay_header_pack(uint8_t *dest, const relay_header_t *src);
  22. void relay_header_unpack(relay_header_t *dest, const uint8_t *src);
  23. MOCK_DECL(int,
  24. relay_send_command_from_edge_,(streamid_t stream_id, circuit_t *circ,
  25. uint8_t relay_command, const char *payload,
  26. size_t payload_len, crypt_path_t *cpath_layer,
  27. const char *filename, int lineno));
  28. /* Indicates to relay_send_command_from_edge() that it is a control cell. */
  29. #define CONTROL_CELL_ID 0
  30. #define relay_send_command_from_edge(stream_id, circ, relay_command, payload, \
  31. payload_len, cpath_layer) \
  32. relay_send_command_from_edge_((stream_id), (circ), (relay_command), \
  33. (payload), (payload_len), (cpath_layer), \
  34. __FILE__, __LINE__)
  35. int connection_edge_send_command(edge_connection_t *fromconn,
  36. uint8_t relay_command, const char *payload,
  37. size_t payload_len);
  38. int connection_edge_package_raw_inbuf(edge_connection_t *conn,
  39. int package_partial,
  40. int *max_cells);
  41. void connection_edge_consider_sending_sendme(edge_connection_t *conn);
  42. void circuit_reset_sendme_randomness(circuit_t *circ);
  43. extern uint64_t stats_n_data_cells_packaged;
  44. extern uint64_t stats_n_data_bytes_packaged;
  45. extern uint64_t stats_n_data_cells_received;
  46. extern uint64_t stats_n_data_bytes_received;
  47. void dump_cell_pool_usage(int severity);
  48. size_t packed_cell_mem_cost(void);
  49. int have_been_under_memory_pressure(void);
  50. /* For channeltls.c */
  51. void packed_cell_free_(packed_cell_t *cell);
  52. #define packed_cell_free(cell) \
  53. FREE_AND_NULL(packed_cell_t, packed_cell_free_, (cell))
  54. void cell_queue_init(cell_queue_t *queue);
  55. void cell_queue_clear(cell_queue_t *queue);
  56. void cell_queue_append(cell_queue_t *queue, packed_cell_t *cell);
  57. void cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
  58. int exitward, const cell_t *cell,
  59. int wide_circ_ids, int use_stats);
  60. void append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
  61. cell_t *cell, cell_direction_t direction,
  62. streamid_t fromstream);
  63. void destroy_cell_queue_init(destroy_cell_queue_t *queue);
  64. void destroy_cell_queue_clear(destroy_cell_queue_t *queue);
  65. void destroy_cell_queue_append(destroy_cell_queue_t *queue,
  66. circid_t circid,
  67. uint8_t reason);
  68. void channel_unlink_all_circuits(channel_t *chan, smartlist_t *detached_out);
  69. MOCK_DECL(int, channel_flush_from_first_active_circuit,
  70. (channel_t *chan, int max));
  71. MOCK_DECL(int, circuit_package_relay_cell, (cell_t *cell, circuit_t *circ,
  72. cell_direction_t cell_direction,
  73. crypt_path_t *layer_hint, streamid_t on_stream,
  74. const char *filename, int lineno));
  75. void update_circuit_on_cmux_(circuit_t *circ, cell_direction_t direction,
  76. const char *file, int lineno);
  77. #define update_circuit_on_cmux(circ, direction) \
  78. update_circuit_on_cmux_((circ), (direction), SHORT_FILE__, __LINE__)
  79. int append_address_to_payload(uint8_t *payload_out, const tor_addr_t *addr);
  80. const uint8_t *decode_address_from_payload(tor_addr_t *addr_out,
  81. const uint8_t *payload,
  82. int payload_len);
  83. void circuit_clear_cell_queue(circuit_t *circ, channel_t *chan);
  84. circid_t packed_cell_get_circid(const packed_cell_t *cell, int wide_circ_ids);
  85. uint8_t packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids);
  86. #ifdef RELAY_PRIVATE
  87. STATIC int
  88. handle_relay_cell_command(cell_t *cell, circuit_t *circ,
  89. edge_connection_t *conn, crypt_path_t *layer_hint,
  90. relay_header_t *rh, int optimistic_data);
  91. STATIC int connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
  92. tor_addr_t *addr_out, int *ttl_out);
  93. /** An address-and-ttl tuple as yielded by resolved_cell_parse */
  94. typedef struct address_ttl_s {
  95. tor_addr_t addr;
  96. char *hostname;
  97. int ttl;
  98. } address_ttl_t;
  99. STATIC void address_ttl_free_(address_ttl_t *addr);
  100. #define address_ttl_free(addr) \
  101. FREE_AND_NULL(address_ttl_t, address_ttl_free_, (addr))
  102. STATIC int resolved_cell_parse(const cell_t *cell, const relay_header_t *rh,
  103. smartlist_t *addresses_out, int *errcode_out);
  104. STATIC int connection_edge_process_resolved_cell(edge_connection_t *conn,
  105. const cell_t *cell,
  106. const relay_header_t *rh);
  107. STATIC packed_cell_t *packed_cell_new(void);
  108. STATIC packed_cell_t *cell_queue_pop(cell_queue_t *queue);
  109. STATIC destroy_cell_t *destroy_cell_queue_pop(destroy_cell_queue_t *queue);
  110. STATIC int cell_queues_check_size(void);
  111. STATIC int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  112. edge_connection_t *conn,
  113. crypt_path_t *layer_hint);
  114. STATIC size_t get_pad_cell_offset(size_t payload_len);
  115. STATIC size_t connection_edge_get_inbuf_bytes_to_package(size_t n_available,
  116. int package_partial,
  117. circuit_t *on_circuit);
  118. #endif /* defined(RELAY_PRIVATE) */
  119. #endif /* !defined(TOR_RELAY_H) */