scripts: Fix Python3 warnings
[tor.git] / src / lib / pubsub / pubsub_publish.c
blob5ea2988826f354f5374a63e4eea1a6b2132c72e7
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 */
7 /**
8 * @file pubsub_publish.c
9 * @brief Header for functions to publish using a pub_binding_t.
10 **/
12 #define PUBSUB_PRIVATE
13 #define DISPATCH_PRIVATE
14 #include "orconfig.h"
16 #include "lib/dispatch/dispatch.h"
17 #include "lib/dispatch/dispatch_st.h"
19 #include "lib/pubsub/pub_binding_st.h"
20 #include "lib/pubsub/pubsub_publish.h"
22 #include "lib/malloc/malloc.h"
23 #include "lib/log/util_bug.h"
25 #include <string.h>
27 /**
28 * Publish a message from the publication binding <b>pub</b> using the
29 * auxiliary data <b>auxdata</b>.
31 * Return 0 on success, -1 on failure.
32 **/
33 int
34 pubsub_pub_(const pub_binding_t *pub, msg_aux_data_t auxdata)
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;
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;
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
58 if (! d->table[pub->msg_template.msg]) {
59 /* Fast path: nobody wants this data. */
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;
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;
71 return dispatch_send_msg_unchecked(d, m);