]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
prop359: Add relay msg basics.
authorDavid Goulet <dgoulet@torproject.org>
Thu, 5 Oct 2023 14:49:59 +0000 (10:49 -0400)
committerNick Mathewson <nickm@torproject.org>
Mon, 5 May 2025 17:06:14 +0000 (13:06 -0400)
src/core/or/include.am
src/core/or/relay_msg.c [new file with mode: 0644]
src/core/or/relay_msg.h [new file with mode: 0644]
src/core/or/relay_msg_st.h [new file with mode: 0644]

index 904fcc137bc38de5681d4308a013c96bc3d65431..b4a1ce66537bb92832c35c20ac40fe7f799ad4dc 100644 (file)
@@ -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 (file)
index 0000000..fa25019
--- /dev/null
@@ -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 (file)
index 0000000..4dc6c10
--- /dev/null
@@ -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 (file)
index 0000000..4cdaa75
--- /dev/null
@@ -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) */