From: David Goulet Date: Thu, 5 Oct 2023 14:49:59 +0000 (-0400) Subject: prop359: Add relay msg basics. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5435b814f3fead79773b01c2fc64154b466f3ee8;p=thirdparty%2Ftor.git prop359: Add relay msg basics. --- diff --git a/src/core/or/include.am b/src/core/or/include.am index 904fcc137b..b4a1ce6653 100644 --- a/src/core/or/include.am +++ b/src/core/or/include.am @@ -107,6 +107,8 @@ noinst_HEADERS += \ src/core/or/relay.h \ src/core/or/relay_cell.h \ src/core/or/relay_crypto_st.h \ + src/core/or/relay_msg.h \ + src/core/or/relay_msg_st.h \ src/core/or/scheduler.h \ src/core/or/sendme.h \ src/core/or/congestion_control_flow.h \ diff --git a/src/core/or/relay_msg.c b/src/core/or/relay_msg.c new file mode 100644 index 0000000000..fa25019c5e --- /dev/null +++ b/src/core/or/relay_msg.c @@ -0,0 +1,44 @@ +/* Copyright (c) 2023, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file relay_msg.c + * \brief XXX: Write a brief introduction to this module. + **/ + +#define RELAY_MSG_PRIVATE + +#include "core/or/relay_msg.h" + +/* + * Public API + */ + +/** Called just before the consensus is changed with the given networkstatus_t + * object. */ +void +relay_msg_consensus_has_changed(const networkstatus_t *ns) +{ + relay_msg_enabled = get_param_enabled(ns); +} + +/** Free the given relay message. */ +void +relay_msg_free_(relay_msg_t *msg) +{ + if (!msg) { + return; + } + tor_free(msg->body); + tor_free(msg); +} + +/** Clear a relay message as in free its content and reset all fields to 0. + * This is useful for stack allocated memory. */ +void +relay_msg_clear(relay_msg_t *msg) +{ + tor_assert(msg); + tor_free(msg->body); + memset(msg, 0, sizeof(*msg)); +} diff --git a/src/core/or/relay_msg.h b/src/core/or/relay_msg.h new file mode 100644 index 0000000000..4dc6c109f7 --- /dev/null +++ b/src/core/or/relay_msg.h @@ -0,0 +1,27 @@ +/* Copyright (c) 2023, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file relay_msg.h + * \brief Header file for relay_msg.c. + **/ + +#ifndef TOR_RELAY_MSG_H +#define TOR_RELAY_MSG_H + +#include "core/or/or.h" + +#include "core/or/relay_msg_st.h" + +/* Relay message */ +void relay_msg_free_(relay_msg_t *msg); +void relay_msg_clear(relay_msg_t *msg); + +#define relay_msg_free(msg) \ + FREE_AND_NULL(relay_msg_t, relay_msg_free_, (msg)) + +#ifdef RELAY_MSG_PRIVATE + +#endif /* RELAY_MSG_PRIVATE */ + +#endif /* TOR_RELAY_MSG_H */ diff --git a/src/core/or/relay_msg_st.h b/src/core/or/relay_msg_st.h new file mode 100644 index 0000000000..4cdaa75d6d --- /dev/null +++ b/src/core/or/relay_msg_st.h @@ -0,0 +1,39 @@ +/* Copyright (c) 2023, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * @file relay_msg_st.h + * @brief A relay message which contains a relay command and parameters, + * if any, that is from a relay cell. + **/ + +#ifndef TOR_RELAY_MSG_ST_H +#define TOR_RELAY_MSG_ST_H + +#include "core/or/or.h" + +/** A relay message object which contains pointers to the header and payload. + * + * One acquires a relay message through the use of an iterator. Once you get a + * reference, the getters MUST be used to access data. + * + * This CAN NOT be made opaque so to avoid heap allocation in the fast path. */ +typedef struct relay_msg_t { + /* Relay cell protocol version of this message. */ + uint8_t relay_cell_proto; + /* Relay command of a message. */ + uint8_t command; + /* Length of payload. */ + uint16_t length; + /* Optional routing header: stream ID of a message or 0. */ + streamid_t stream_id; + /* Indicate if this is a message from a relay early cell. */ + bool is_relay_early; + /* Message body of a relay message. */ + // TODO #41051: This is an owned copy of the body. + // It might be better to turn this into a non-owned pointer into + // the cell body, if we can, to save a copy. + uint8_t *body; +} relay_msg_t; + +#endif /* !defined(TOR_RELAY_MSG_ST_H) */