proto_cell.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "or.h"
  7. #include "buffers.h"
  8. #include "proto_cell.h"
  9. #include "connection_or.h"
  10. /** True iff the cell command <b>command</b> is one that implies a
  11. * variable-length cell in Tor link protocol <b>linkproto</b>. */
  12. static inline int
  13. cell_command_is_var_length(uint8_t command, int linkproto)
  14. {
  15. /* If linkproto is v2 (2), CELL_VERSIONS is the only variable-length cells
  16. * work as implemented here. If it's 1, there are no variable-length cells.
  17. * Tor does not support other versions right now, and so can't negotiate
  18. * them.
  19. */
  20. switch (linkproto) {
  21. case 1:
  22. /* Link protocol version 1 has no variable-length cells. */
  23. return 0;
  24. case 2:
  25. /* In link protocol version 2, VERSIONS is the only variable-length cell */
  26. return command == CELL_VERSIONS;
  27. case 0:
  28. case 3:
  29. default:
  30. /* In link protocol version 3 and later, and in version "unknown",
  31. * commands 128 and higher indicate variable-length. VERSIONS is
  32. * grandfathered in. */
  33. return command == CELL_VERSIONS || command >= 128;
  34. }
  35. }
  36. /** Check <b>buf</b> for a variable-length cell according to the rules of link
  37. * protocol version <b>linkproto</b>. If one is found, pull it off the buffer
  38. * and assign a newly allocated var_cell_t to *<b>out</b>, and return 1.
  39. * Return 0 if whatever is on the start of buf_t is not a variable-length
  40. * cell. Return 1 and set *<b>out</b> to NULL if there seems to be the start
  41. * of a variable-length cell on <b>buf</b>, but the whole thing isn't there
  42. * yet. */
  43. int
  44. fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
  45. {
  46. char hdr[VAR_CELL_MAX_HEADER_SIZE];
  47. var_cell_t *result;
  48. uint8_t command;
  49. uint16_t length;
  50. const int wide_circ_ids = linkproto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
  51. const int circ_id_len = get_circ_id_size(wide_circ_ids);
  52. const unsigned header_len = get_var_cell_header_size(wide_circ_ids);
  53. *out = NULL;
  54. if (buf_datalen(buf) < header_len)
  55. return 0;
  56. buf_peek(buf, hdr, header_len);
  57. command = get_uint8(hdr + circ_id_len);
  58. if (!(cell_command_is_var_length(command, linkproto)))
  59. return 0;
  60. length = ntohs(get_uint16(hdr + circ_id_len + 1));
  61. if (buf_datalen(buf) < (size_t)(header_len+length))
  62. return 1;
  63. result = var_cell_new(length);
  64. result->command = command;
  65. if (wide_circ_ids)
  66. result->circ_id = ntohl(get_uint32(hdr));
  67. else
  68. result->circ_id = ntohs(get_uint16(hdr));
  69. buf_drain(buf, header_len);
  70. buf_peek(buf, (char*) result->payload, length);
  71. buf_drain(buf, length);
  72. *out = result;
  73. return 1;
  74. }