proto_ext_or.c (1432B)
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" 14 #include "feature/relay/ext_orport.h" 15 #include "core/proto/proto_ext_or.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 27 fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out) 28 { 29 char hdr[EXT_OR_CMD_HEADER_SIZE]; 30 uint16_t len; 31 32 if (buf_datalen(buf) < EXT_OR_CMD_HEADER_SIZE) 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; 41 buf_drain(buf, EXT_OR_CMD_HEADER_SIZE); 42 buf_get_bytes(buf, (*out)->body, len); 43 return 1; 44 }