Tor  0.4.7.0-alpha-dev
proto_cell.c
Go to the documentation of this file.
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 proto_cell.c
9  * @brief Decodes Tor cells from buffers.
10  **/
11 /* Right now it only handles variable-length cells, but eventually
12  * we should refactor other cell-reading code into here. */
13 
14 #include "core/or/or.h"
15 #include "lib/buf/buffers.h"
16 #include "core/proto/proto_cell.h"
17 
18 #include "core/or/connection_or.h"
19 
20 #include "core/or/var_cell_st.h"
21 
22 /** True iff the cell command <b>command</b> is one that implies a
23  * variable-length cell in Tor link protocol <b>linkproto</b>. */
24 static inline int
25 cell_command_is_var_length(uint8_t command, int linkproto)
26 {
27  /* If linkproto is v2 (2), CELL_VERSIONS is the only variable-length cells
28  * work as implemented here. If it's 1, there are no variable-length cells.
29  * Tor does not support other versions right now, and so can't negotiate
30  * them.
31  */
32  switch (linkproto) {
33  case 1:
34  /* Link protocol version 1 has no variable-length cells. */
35  return 0;
36  case 2:
37  /* In link protocol version 2, VERSIONS is the only variable-length cell */
38  return command == CELL_VERSIONS;
39  case 0:
40  case 3:
41  default:
42  /* In link protocol version 3 and later, and in version "unknown",
43  * commands 128 and higher indicate variable-length. VERSIONS is
44  * grandfathered in. */
45  return command == CELL_VERSIONS || command >= 128;
46  }
47 }
48 
49 /** Check <b>buf</b> for a variable-length cell according to the rules of link
50  * protocol version <b>linkproto</b>. If one is found, pull it off the buffer
51  * and assign a newly allocated var_cell_t to *<b>out</b>, and return 1.
52  * Return 0 if whatever is on the start of buf_t is not a variable-length
53  * cell. Return 1 and set *<b>out</b> to NULL if there seems to be the start
54  * of a variable-length cell on <b>buf</b>, but the whole thing isn't there
55  * yet. */
56 int
57 fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
58 {
59  char hdr[VAR_CELL_MAX_HEADER_SIZE];
60  var_cell_t *result;
61  uint8_t command;
62  uint16_t length;
63  const int wide_circ_ids = linkproto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
64  const int circ_id_len = get_circ_id_size(wide_circ_ids);
65  const unsigned header_len = get_var_cell_header_size(wide_circ_ids);
66  *out = NULL;
67  if (buf_datalen(buf) < header_len)
68  return 0;
69  buf_peek(buf, hdr, header_len);
70 
71  command = get_uint8(hdr + circ_id_len);
72  if (!(cell_command_is_var_length(command, linkproto)))
73  return 0;
74 
75  length = ntohs(get_uint16(hdr + circ_id_len + 1));
76  if (buf_datalen(buf) < (size_t)(header_len+length))
77  return 1;
78 
79  result = var_cell_new(length);
80  result->command = command;
81  if (wide_circ_ids)
82  result->circ_id = ntohl(get_uint32(hdr));
83  else
84  result->circ_id = ntohs(get_uint16(hdr));
85 
86  buf_drain(buf, header_len);
87  buf_peek(buf, (char*) result->payload, length);
88  buf_drain(buf, length);
89 
90  *out = result;
91  return 1;
92 }
void buf_drain(buf_t *buf, size_t n)
Definition: buffers.c:330
size_t buf_datalen(const buf_t *buf)
Definition: buffers.c:394
void buf_peek(const buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:610
Header file for buffers.c.
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:42
static uint8_t get_uint8(const void *cp)
Definition: bytes.h:23
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:54
tor_cmdline_mode_t command
Definition: config.c:2440
var_cell_t * var_cell_new(uint16_t payload_len)
Header file for connection_or.c.
Master header file for Tor-specific functionality.
#define VAR_CELL_MAX_HEADER_SIZE
Definition: or.h:463
int fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
Definition: proto_cell.c:57
static int cell_command_is_var_length(uint8_t command, int linkproto)
Definition: proto_cell.c:25
Header for proto_cell.c.
uint8_t command
Definition: var_cell_st.h:18
circid_t circ_id
Definition: var_cell_st.h:20
uint8_t payload[FLEXIBLE_ARRAY_MEMBER]
Definition: var_cell_st.h:24
Variable-length cell structure.