pubsub_publish.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * @file pubsub_publish.c
  8. * @brief Header for functions to publish using a pub_binding_t.
  9. **/
  10. #define PUBSUB_PRIVATE
  11. #define DISPATCH_PRIVATE
  12. #include "orconfig.h"
  13. #include "lib/dispatch/dispatch.h"
  14. #include "lib/dispatch/dispatch_st.h"
  15. #include "lib/pubsub/pub_binding_st.h"
  16. #include "lib/pubsub/pubsub_publish.h"
  17. #include "lib/malloc/malloc.h"
  18. #include "lib/log/util_bug.h"
  19. #include <string.h>
  20. /**
  21. * Publish a message from the publication binding <b>pub</b> using the
  22. * auxiliary data <b>auxdata</b>.
  23. *
  24. * Return 0 on success, -1 on failure.
  25. **/
  26. int
  27. pubsub_pub_(const pub_binding_t *pub, msg_aux_data_t auxdata)
  28. {
  29. dispatch_t *d = pub->dispatch_ptr;
  30. if (BUG(! d)) {
  31. /* Tried to publish a message before the dispatcher was configured. */
  32. /* (Without a dispatcher, we don't know how to free auxdata.) */
  33. return -1;
  34. }
  35. if (BUG(pub->msg_template.type >= d->n_types)) {
  36. /* The type associated with this message is not known to the dispatcher. */
  37. /* (Without a correct type, we don't know how to free auxdata.) */
  38. return -1;
  39. }
  40. if (BUG(pub->msg_template.msg >= d->n_msgs) ||
  41. BUG(pub->msg_template.channel >= d->n_queues)) {
  42. /* The message ID or channel ID was out of bounds. */
  43. // LCOV_EXCL_START
  44. d->typefns[pub->msg_template.type].free_fn(auxdata);
  45. return -1;
  46. // LCOV_EXCL_STOP
  47. }
  48. if (! d->table[pub->msg_template.msg]) {
  49. /* Fast path: nobody wants this data. */
  50. // XXXX Faster path: we could store this in the pub_binding_t.
  51. d->typefns[pub->msg_template.type].free_fn(auxdata);
  52. return 0;
  53. }
  54. /* Construct the message object */
  55. msg_t *m = tor_malloc(sizeof(msg_t));
  56. memcpy(m, &pub->msg_template, sizeof(msg_t));
  57. m->aux_data__ = auxdata;
  58. return dispatch_send_msg_unchecked(d, m);
  59. }