aboutsummaryrefslogtreecommitdiff
path: root/src/core/or/ocirc_event.c
blob: fa16459175c705f87143b91a0a486ffc5ba26768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

/**
 * \file ocirc_event.c
 * \brief Publish state change messages for origin circuits
 *
 * Implements a basic publish-subscribe framework for messages about
 * the state of origin circuits.  The publisher calls the subscriber
 * callback functions synchronously.
 *
 * Although the synchronous calls might not simplify the call graph,
 * this approach improves data isolation because the publisher doesn't
 * need knowledge about the internals of subscribing subsystems.  It
 * also avoids race conditions that might occur in asynchronous
 * frameworks.
 **/

#include "core/or/or.h"

#define OCIRC_EVENT_PRIVATE

#include "core/or/cpath_build_state_st.h"
#include "core/or/ocirc_event.h"
#include "core/or/or_sys.h"
#include "core/or/origin_circuit_st.h"
#include "lib/subsys/subsys.h"

DECLARE_PUBLISH(ocirc_state);
DECLARE_PUBLISH(ocirc_chan);
DECLARE_PUBLISH(ocirc_cevent);

static void
ocirc_event_free(msg_aux_data_t u)
{
  tor_free_(u.ptr);
}

static char *
ocirc_state_fmt(msg_aux_data_t u)
{
  ocirc_state_msg_t *msg = (ocirc_state_msg_t *)u.ptr;
  char *s = NULL;

  tor_asprintf(&s, "<gid=%"PRIu32" state=%d onehop=%d>",
               msg->gid, msg->state, msg->onehop);
  return s;
}

static char *
ocirc_chan_fmt(msg_aux_data_t u)
{
  ocirc_chan_msg_t *msg = (ocirc_chan_msg_t *)u.ptr;
  char *s = NULL;

  tor_asprintf(&s, "<gid=%"PRIu32" chan=%"PRIu64" onehop=%d>",
               msg->gid, msg->chan, msg->onehop);
  return s;
}

static char *
ocirc_cevent_fmt(msg_aux_data_t u)
{
  ocirc_cevent_msg_t *msg = (ocirc_cevent_msg_t *)u.ptr;
  char *s = NULL;

  tor_asprintf(&s, "<gid=%"PRIu32" evtype=%d reason=%d onehop=%d>",
               msg->gid, msg->evtype, msg->reason, msg->onehop);
  return s;
}

static dispatch_typefns_t ocirc_state_fns = {
  .free_fn = ocirc_event_free,
  .fmt_fn = ocirc_state_fmt,
};

static dispatch_typefns_t ocirc_chan_fns = {
  .free_fn = ocirc_event_free,
  .fmt_fn = ocirc_chan_fmt,
};

static dispatch_typefns_t ocirc_cevent_fns = {
  .free_fn = ocirc_event_free,
  .fmt_fn = ocirc_cevent_fmt,
};

int
ocirc_add_pubsub(struct pubsub_connector_t *connector)
{
  if (DISPATCH_REGISTER_TYPE(connector, ocirc_state, &ocirc_state_fns))
    return -1;
  if (DISPATCH_REGISTER_TYPE(connector, ocirc_chan, &ocirc_chan_fns))
    return -1;
  if (DISPATCH_REGISTER_TYPE(connector, ocirc_cevent, &ocirc_cevent_fns))
    return -1;
  if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_state))
    return -1;
  if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_chan))
    return -1;
  if (DISPATCH_ADD_PUB(connector, ocirc, ocirc_cevent))
    return -1;
  return 0;
}

void
ocirc_state_publish(ocirc_state_msg_t *msg)
{
  PUBLISH(ocirc_state, msg);
}

void
ocirc_chan_publish(ocirc_chan_msg_t *msg)
{
  PUBLISH(ocirc_chan, msg);
}

void
ocirc_cevent_publish(ocirc_cevent_msg_t *msg)
{
  PUBLISH(ocirc_cevent, msg);
}