Tor  0.4.7.0-alpha-dev
proto_ext_or.c
Go to the documentation of this file.
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_ext_or.c
9  * @brief Parsing/encoding for the extended OR protocol.
10  **/
11 
12 #include "core/or/or.h"
13 #include "lib/buf/buffers.h"
16 
17 /** The size of the header of an Extended ORPort message: 2 bytes for
18  * COMMAND, 2 bytes for BODYLEN */
19 #define EXT_OR_CMD_HEADER_SIZE 4
20 
21 /** Read <b>buf</b>, which should contain an Extended ORPort message
22  * from a transport proxy. If well-formed, create and populate
23  * <b>out</b> with the Extended ORport message. Return 0 if the
24  * buffer was incomplete, 1 if it was well-formed and -1 if we
25  * encountered an error while parsing it. */
26 int
28 {
29  char hdr[EXT_OR_CMD_HEADER_SIZE];
30  uint16_t len;
31 
33  return 0;
34  buf_peek(buf, hdr, sizeof(hdr));
35  len = ntohs(get_uint16(hdr+2));
36  if (buf_datalen(buf) < (unsigned)len + EXT_OR_CMD_HEADER_SIZE)
37  return 0;
38  *out = ext_or_cmd_new(len);
39  (*out)->cmd = ntohs(get_uint16(hdr));
40  (*out)->len = len;
42  buf_get_bytes(buf, (*out)->body, len);
43  return 1;
44 }
void buf_drain(buf_t *buf, size_t n)
Definition: buffers.c:330
size_t buf_datalen(const buf_t *buf)
Definition: buffers.c:394
void buf_peek(const buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:610
int buf_get_bytes(buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:637
Header file for buffers.c.
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:42
Header for ext_orport.c.
Master header file for Tor-specific functionality.
#define EXT_OR_CMD_HEADER_SIZE
Definition: proto_ext_or.c:19
int fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out)
Definition: proto_ext_or.c:27
Header for proto_ext_or.c.
ext_or_cmd_t * ext_or_cmd_new(uint16_t len)
Definition: ext_orport.c:36