Tor  0.4.7.0-alpha-dev
pubsub_publish.c
Go to the documentation of this file.
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 pubsub_publish.c
9  * @brief Header for functions to publish using a pub_binding_t.
10  **/
11 
12 #define PUBSUB_PRIVATE
13 #define DISPATCH_PRIVATE
14 #include "orconfig.h"
15 
16 #include "lib/dispatch/dispatch.h"
18 
21 
22 #include "lib/malloc/malloc.h"
23 #include "lib/log/util_bug.h"
24 
25 #include <string.h>
26 
27 /**
28  * Publish a message from the publication binding <b>pub</b> using the
29  * auxiliary data <b>auxdata</b>.
30  *
31  * Return 0 on success, -1 on failure.
32  **/
33 int
35 {
36  dispatch_t *d = pub->dispatch_ptr;
37  if (BUG(! d)) {
38  /* Tried to publish a message before the dispatcher was configured. */
39  /* (Without a dispatcher, we don't know how to free auxdata.) */
40  return -1;
41  }
42 
43  if (BUG(pub->msg_template.type >= d->n_types)) {
44  /* The type associated with this message is not known to the dispatcher. */
45  /* (Without a correct type, we don't know how to free auxdata.) */
46  return -1;
47  }
48 
49  if (BUG(pub->msg_template.msg >= d->n_msgs) ||
50  BUG(pub->msg_template.channel >= d->n_queues)) {
51  /* The message ID or channel ID was out of bounds. */
52  // LCOV_EXCL_START
53  d->typefns[pub->msg_template.type].free_fn(auxdata);
54  return -1;
55  // LCOV_EXCL_STOP
56  }
57 
58  if (! d->table[pub->msg_template.msg]) {
59  /* Fast path: nobody wants this data. */
60 
61  // XXXX Faster path: we could store this in the pub_binding_t.
62  d->typefns[pub->msg_template.type].free_fn(auxdata);
63  return 0;
64  }
65 
66  /* Construct the message object */
67  msg_t *m = tor_malloc(sizeof(msg_t));
68  memcpy(m, &pub->msg_template, sizeof(msg_t));
69  m->aux_data__ = auxdata;
70 
71  return dispatch_send_msg_unchecked(d, m);
72 }
Low-level APIs for message-passing system.
struct dispatch_t dispatch_t
Definition: dispatch.h:53
int dispatch_send_msg_unchecked(dispatch_t *d, msg_t *m)
private structures used for the dispatcher module
Headers for util_malloc.c.
Declaration of pub_binding_t.
int pubsub_pub_(const pub_binding_t *pub, msg_aux_data_t auxdata)
Header for pubsub_publish.c.
Definition: msgtypes.h:50
msg_type_id_t type
Definition: msgtypes.h:57
msg_aux_data_t aux_data__
Definition: msgtypes.h:60
struct dispatch_t * dispatch_ptr
Macros to manage assertions, fatal and non-fatal.