LCOV - code coverage report
Current view: top level - core/proto - proto_cell.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 31 31 100.0 %
Date: 2021-11-24 03:28:48 Functions: 2 2 100.0 %

          Line data    Source code
       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           6 : 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           6 :   switch (linkproto) {
      33             :   case 1:
      34             :     /* Link protocol version 1 has no variable-length cells. */
      35             :     return 0;
      36           2 :   case 2:
      37             :     /* In link protocol version 2, VERSIONS is the only variable-length cell */
      38           2 :     return command == CELL_VERSIONS;
      39           4 :   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           4 :     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           9 : fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
      58             : {
      59           9 :   char hdr[VAR_CELL_MAX_HEADER_SIZE];
      60           9 :   var_cell_t *result;
      61           9 :   uint8_t command;
      62           9 :   uint16_t length;
      63           9 :   const int wide_circ_ids = linkproto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
      64           9 :   const int circ_id_len = get_circ_id_size(wide_circ_ids);
      65           9 :   const unsigned header_len = get_var_cell_header_size(wide_circ_ids);
      66           9 :   *out = NULL;
      67           9 :   if (buf_datalen(buf) < header_len)
      68             :     return 0;
      69           6 :   buf_peek(buf, hdr, header_len);
      70             : 
      71           6 :   command = get_uint8(hdr + circ_id_len);
      72           6 :   if (!(cell_command_is_var_length(command, linkproto)))
      73             :     return 0;
      74             : 
      75           4 :   length = ntohs(get_uint16(hdr + circ_id_len + 1));
      76           4 :   if (buf_datalen(buf) < (size_t)(header_len+length))
      77             :     return 1;
      78             : 
      79           3 :   result = var_cell_new(length);
      80           3 :   result->command = command;
      81           3 :   if (wide_circ_ids)
      82           1 :     result->circ_id = ntohl(get_uint32(hdr));
      83             :   else
      84           2 :     result->circ_id = ntohs(get_uint16(hdr));
      85             : 
      86           3 :   buf_drain(buf, header_len);
      87           3 :   buf_peek(buf, (char*) result->payload, length);
      88           3 :   buf_drain(buf, length);
      89             : 
      90           3 :   *out = result;
      91           3 :   return 1;
      92             : }

Generated by: LCOV version 1.14