tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

relay.h (7192B)


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