tor  0.4.2.0-alpha-dev
proto_cell.c
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 #include "core/or/or.h"
8 #include "lib/buf/buffers.h"
9 #include "core/proto/proto_cell.h"
10 
11 #include "core/or/connection_or.h"
12 
13 #include "core/or/var_cell_st.h"
14 
17 static inline int
18 cell_command_is_var_length(uint8_t command, int linkproto)
19 {
20  /* If linkproto is v2 (2), CELL_VERSIONS is the only variable-length cells
21  * work as implemented here. If it's 1, there are no variable-length cells.
22  * Tor does not support other versions right now, and so can't negotiate
23  * them.
24  */
25  switch (linkproto) {
26  case 1:
27  /* Link protocol version 1 has no variable-length cells. */
28  return 0;
29  case 2:
30  /* In link protocol version 2, VERSIONS is the only variable-length cell */
31  return command == CELL_VERSIONS;
32  case 0:
33  case 3:
34  default:
35  /* In link protocol version 3 and later, and in version "unknown",
36  * commands 128 and higher indicate variable-length. VERSIONS is
37  * grandfathered in. */
38  return command == CELL_VERSIONS || command >= 128;
39  }
40 }
41 
49 int
50 fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
51 {
52  char hdr[VAR_CELL_MAX_HEADER_SIZE];
53  var_cell_t *result;
54  uint8_t command;
55  uint16_t length;
56  const int wide_circ_ids = linkproto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
57  const int circ_id_len = get_circ_id_size(wide_circ_ids);
58  const unsigned header_len = get_var_cell_header_size(wide_circ_ids);
59  *out = NULL;
60  if (buf_datalen(buf) < header_len)
61  return 0;
62  buf_peek(buf, hdr, header_len);
63 
64  command = get_uint8(hdr + circ_id_len);
65  if (!(cell_command_is_var_length(command, linkproto)))
66  return 0;
67 
68  length = ntohs(get_uint16(hdr + circ_id_len + 1));
69  if (buf_datalen(buf) < (size_t)(header_len+length))
70  return 1;
71 
72  result = var_cell_new(length);
73  result->command = command;
74  if (wide_circ_ids)
75  result->circ_id = ntohl(get_uint32(hdr));
76  else
77  result->circ_id = ntohs(get_uint16(hdr));
78 
79  buf_drain(buf, header_len);
80  buf_peek(buf, (char*) result->payload, length);
81  buf_drain(buf, length);
82 
83  *out = result;
84  return 1;
85 }
86 
var_cell_t * var_cell_new(uint16_t payload_len)
uint8_t payload[FLEXIBLE_ARRAY_MEMBER]
Definition: var_cell_st.h:19
void buf_peek(const buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:607
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:49
Master header file for Tor-specific functionality.
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:37
#define VAR_CELL_MAX_HEADER_SIZE
Definition: or.h:582
void buf_drain(buf_t *buf, size_t n)
Definition: buffers.c:328
circid_t circ_id
Definition: var_cell_st.h:15
Header file for buffers.c.
uint8_t command
Definition: var_cell_st.h:13
Header file for connection_or.c.