tor  0.4.2.0-alpha-dev
proto_ext_or.c
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 
7 #include "core/or/or.h"
8 #include "lib/buf/buffers.h"
9 #include "feature/relay/ext_orport.h"
10 #include "core/proto/proto_ext_or.h"
11 
14 #define EXT_OR_CMD_HEADER_SIZE 4
15 
21 int
22 fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out)
23 {
24  char hdr[EXT_OR_CMD_HEADER_SIZE];
25  uint16_t len;
26 
27  if (buf_datalen(buf) < EXT_OR_CMD_HEADER_SIZE)
28  return 0;
29  buf_peek(buf, hdr, sizeof(hdr));
30  len = ntohs(get_uint16(hdr+2));
31  if (buf_datalen(buf) < (unsigned)len + EXT_OR_CMD_HEADER_SIZE)
32  return 0;
33  *out = ext_or_cmd_new(len);
34  (*out)->cmd = ntohs(get_uint16(hdr));
35  (*out)->len = len;
36  buf_drain(buf, EXT_OR_CMD_HEADER_SIZE);
37  buf_get_bytes(buf, (*out)->body, len);
38  return 1;
39 }
40 
void buf_peek(const buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:607
int buf_get_bytes(buf_t *buf, char *string, size_t string_len)
Definition: buffers.c:634
Master header file for Tor-specific functionality.
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:37
void buf_drain(buf_t *buf, size_t n)
Definition: buffers.c:328
ext_or_cmd_t * ext_or_cmd_new(uint16_t len)
Definition: ext_orport.c:36
Header file for buffers.c.