aboutsummaryrefslogtreecommitdiff
path: root/src/core/or/circuitlist.c
diff options
context:
space:
mode:
authorTaylor Yu <catalyst@torproject.org>2018-12-16 17:01:25 -0600
committerTaylor Yu <catalyst@torproject.org>2018-12-20 18:46:17 -0600
commita0b4fa1f167f9b013ee1a8326e325ec3f97e4700 (patch)
tree23af519fc5dcd46d597776dea0ec9556aa9e3eab /src/core/or/circuitlist.c
parent271b50f54abac7af44e3e54589ff965d3cdac816 (diff)
downloadtor-a0b4fa1f167f9b013ee1a8326e325ec3f97e4700.tar.gz
tor-a0b4fa1f167f9b013ee1a8326e325ec3f97e4700.zip
Add origin circuit event pubsub system
Add a publish-subscribe subsystem to publish messages about changes to origin circuits. Functions in circuitbuild.c and circuitlist.c publish messages to this subsystem. Move circuit event constants out of control.h so that subscribers don't have to include all of control.h to take actions based on messages they receive. Part of ticket 27167.
Diffstat (limited to 'src/core/or/circuitlist.c')
-rw-r--r--src/core/or/circuitlist.c57
1 files changed, 56 insertions, 1 deletions
diff --git a/src/core/or/circuitlist.c b/src/core/or/circuitlist.c
index 0aa21000a1..c4b5f7ee3e 100644
--- a/src/core/or/circuitlist.c
+++ b/src/core/or/circuitlist.c
@@ -51,6 +51,7 @@
* logic, which was originally circuit-focused.
**/
#define CIRCUITLIST_PRIVATE
+#define OCIRC_EVENT_PRIVATE
#include "lib/cc/torint.h" /* TOR_PRIuSZ */
#include "core/or/or.h"
@@ -96,6 +97,9 @@
#include "lib/compress/compress_zstd.h"
#include "lib/buf/buffers.h"
+#define OCIRC_EVENT_PRIVATE
+#include "core/or/ocirc_event.h"
+
#include "ht.h"
#include "core/or/cpath_build_state_st.h"
@@ -481,6 +485,56 @@ circuit_set_n_circid_chan(circuit_t *circ, circid_t id,
}
}
+/**
+ * Helper function to publish a message about events on an origin circuit
+ *
+ * Publishes a message to subscribers of origin circuit events, and
+ * sends the control event.
+ **/
+int
+circuit_event_status(origin_circuit_t *circ, circuit_status_event_t tp,
+ int reason_code)
+{
+ ocirc_event_msg_t msg;
+
+ tor_assert(circ);
+
+ msg.type = OCIRC_MSGTYPE_CEVENT;
+ msg.u.cevent.gid = circ->global_identifier;
+ msg.u.cevent.evtype = tp;
+ msg.u.cevent.reason = reason_code;
+ msg.u.cevent.onehop = circ->build_state->onehop_tunnel;
+
+ ocirc_event_publish(&msg);
+ return control_event_circuit_status(circ, tp, reason_code);
+}
+
+/**
+ * Helper function to publish a state change message
+ *
+ * circuit_set_state() calls this to notify subscribers about a change
+ * of the state of an origin circuit.
+ **/
+static void
+circuit_state_publish(const circuit_t *circ)
+{
+ ocirc_event_msg_t msg;
+ const origin_circuit_t *ocirc;
+
+ if (!CIRCUIT_IS_ORIGIN(circ))
+ return;
+ ocirc = CONST_TO_ORIGIN_CIRCUIT(circ);
+ /* Only inbound OR circuits can be in this state, not origin circuits. */
+ tor_assert(circ->state != CIRCUIT_STATE_ONIONSKIN_PENDING);
+
+ msg.type = OCIRC_MSGTYPE_STATE;
+ msg.u.state.gid = ocirc->global_identifier;
+ msg.u.state.state = circ->state;
+ msg.u.state.onehop = ocirc->build_state->onehop_tunnel;
+
+ ocirc_event_publish(&msg);
+}
+
/** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
* it from lists as appropriate. */
void
@@ -510,6 +564,7 @@ circuit_set_state(circuit_t *circ, uint8_t state)
if (state == CIRCUIT_STATE_GUARD_WAIT || state == CIRCUIT_STATE_OPEN)
tor_assert(!circ->n_chan_create_cell);
circ->state = state;
+ circuit_state_publish(circ);
}
/** Append to <b>out</b> all circuits in state CHAN_WAIT waiting for
@@ -2270,7 +2325,7 @@ circuit_about_to_free(circuit_t *circ)
smartlist_remove(circuits_pending_other_guards, circ);
}
if (CIRCUIT_IS_ORIGIN(circ)) {
- control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
+ circuit_event_status(TO_ORIGIN_CIRCUIT(circ),
(circ->state == CIRCUIT_STATE_OPEN ||
circ->state == CIRCUIT_STATE_GUARD_WAIT) ?
CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,