pubsub_publish.c (2032B)
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" 17 #include "lib/dispatch/dispatch_st.h" 18 19 #include "lib/pubsub/pub_binding_st.h" 20 #include "lib/pubsub/pubsub_publish.h" 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 34 pubsub_pub_(const pub_binding_t *pub, msg_aux_data_t auxdata) 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 }