proto_ext_or.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 "ext_orport.h"
  9. #include "proto_ext_or.h"
  10. /** The size of the header of an Extended ORPort message: 2 bytes for
  11. * COMMAND, 2 bytes for BODYLEN */
  12. #define EXT_OR_CMD_HEADER_SIZE 4
  13. /** Read <b>buf</b>, which should contain an Extended ORPort message
  14. * from a transport proxy. If well-formed, create and populate
  15. * <b>out</b> with the Extended ORport message. Return 0 if the
  16. * buffer was incomplete, 1 if it was well-formed and -1 if we
  17. * encountered an error while parsing it. */
  18. int
  19. fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out)
  20. {
  21. char hdr[EXT_OR_CMD_HEADER_SIZE];
  22. uint16_t len;
  23. if (buf_datalen(buf) < EXT_OR_CMD_HEADER_SIZE)
  24. return 0;
  25. buf_peek(buf, hdr, sizeof(hdr));
  26. len = ntohs(get_uint16(hdr+2));
  27. if (buf_datalen(buf) < (unsigned)len + EXT_OR_CMD_HEADER_SIZE)
  28. return 0;
  29. *out = ext_or_cmd_new(len);
  30. (*out)->cmd = ntohs(get_uint16(hdr));
  31. (*out)->len = len;
  32. buf_drain(buf, EXT_OR_CMD_HEADER_SIZE);
  33. buf_get_bytes(buf, (*out)->body, len);
  34. return 1;
  35. }