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 */
8 * @file pubsub_publish.c
9 * @brief Header for functions to publish using a pub_binding_t.
12 #define PUBSUB_PRIVATE
13 #define DISPATCH_PRIVATE
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"
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.
34 pubsub_pub_(const pub_binding_t
*pub
, msg_aux_data_t auxdata
)
36 dispatch_t
*d
= pub
->dispatch_ptr
;
38 /* Tried to publish a message before the dispatcher was configured. */
39 /* (Without a dispatcher, we don't know how to free auxdata.) */
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.) */
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. */
53 d
->typefns
[pub
->msg_template
.type
].free_fn(auxdata
);
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
);
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
);