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