proto_cell.c 2.8 KB

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