dispatch_new.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 dispatch_new.c
  8. * \brief Code to construct a dispatch_t from a dispatch_cfg_t.
  9. **/
  10. #define DISPATCH_PRIVATE
  11. #include "orconfig.h"
  12. #include "lib/dispatch/dispatch.h"
  13. #include "lib/dispatch/dispatch_st.h"
  14. #include "lib/dispatch/dispatch_cfg.h"
  15. #include "lib/dispatch/dispatch_cfg_st.h"
  16. #include "lib/cc/ctassert.h"
  17. #include "lib/intmath/cmp.h"
  18. #include "lib/malloc/malloc.h"
  19. #include "lib/log/util_bug.h"
  20. #include <string.h>
  21. /** Given a smartlist full of (possibly NULL) pointers to uint16_t values,
  22. * return the largest value, or dflt if the list is empty. */
  23. static int
  24. max_in_sl(const smartlist_t *sl, int dflt)
  25. {
  26. uint16_t *maxptr = NULL;
  27. SMARTLIST_FOREACH_BEGIN(sl, uint16_t *, u) {
  28. if (!maxptr)
  29. maxptr = u;
  30. else if (*u > *maxptr)
  31. maxptr = u;
  32. } SMARTLIST_FOREACH_END(u);
  33. return maxptr ? *maxptr : dflt;
  34. }
  35. /* The above function is only safe to call if we are sure that channel_id_t
  36. * and msg_type_id_t are really uint16_t. They should be so defined in
  37. * msgtypes.h, but let's be extra cautious.
  38. */
  39. CTASSERT(sizeof(uint16_t) == sizeof(msg_type_id_t));
  40. CTASSERT(sizeof(uint16_t) == sizeof(channel_id_t));
  41. /** Helper: Format an unformattable message auxiliary data item: just return a
  42. * copy of the string <>. */
  43. static char *
  44. type_fmt_nop(msg_aux_data_t arg)
  45. {
  46. (void)arg;
  47. return tor_strdup("<>");
  48. }
  49. /** Helper: Free an unfreeable message auxiliary data item: do nothing. */
  50. static void
  51. type_free_nop(msg_aux_data_t arg)
  52. {
  53. (void)arg;
  54. }
  55. /** Type functions to use when no type functions are provided. */
  56. static dispatch_typefns_t nop_typefns = {
  57. .free_fn = type_free_nop,
  58. .fmt_fn = type_fmt_nop
  59. };
  60. /**
  61. * Alert function to use when none is configured: do nothing.
  62. **/
  63. static void
  64. alert_fn_nop(dispatch_t *d, channel_id_t ch, void *arg)
  65. {
  66. (void)d;
  67. (void)ch;
  68. (void)arg;
  69. }
  70. /**
  71. * Given a list of recvfn_t, create and return a new dtbl_entry_t mapping
  72. * to each of those functions.
  73. **/
  74. static dtbl_entry_t *
  75. dtbl_entry_from_lst(smartlist_t *receivers)
  76. {
  77. if (!receivers)
  78. return NULL;
  79. size_t n_recv = smartlist_len(receivers);
  80. dtbl_entry_t *ent;
  81. ent = tor_malloc_zero(offsetof(dtbl_entry_t, rcv) +
  82. sizeof(dispatch_rcv_t) * n_recv);
  83. ent->n_fns = n_recv;
  84. SMARTLIST_FOREACH_BEGIN(receivers, const dispatch_rcv_t *, rcv) {
  85. memcpy(&ent->rcv[rcv_sl_idx], rcv, sizeof(*rcv));
  86. if (rcv->enabled) {
  87. ++ent->n_enabled;
  88. }
  89. } SMARTLIST_FOREACH_END(rcv);
  90. return ent;
  91. }
  92. /** Create and return a new dispatcher from a given dispatch_cfg_t. */
  93. dispatch_t *
  94. dispatch_new(const dispatch_cfg_t *cfg)
  95. {
  96. dispatch_t *d = tor_malloc_zero(sizeof(dispatch_t));
  97. /* Any message that has a type or a receiver counts towards our messages */
  98. const size_t n_msgs = MAX(smartlist_len(cfg->type_by_msg),
  99. smartlist_len(cfg->recv_by_msg)) + 1;
  100. /* Any channel that any message has counts towards the number of channels. */
  101. const size_t n_chans = (size_t) MAX(1, max_in_sl(cfg->chan_by_msg,0)) + 1;
  102. /* Any type that a message has, or that has functions, counts towards
  103. * the number of types. */
  104. const size_t n_types = (size_t) MAX(max_in_sl(cfg->type_by_msg,0),
  105. smartlist_len(cfg->fns_by_type)) + 1;
  106. d->n_msgs = n_msgs;
  107. d->n_queues = n_chans;
  108. d->n_types = n_types;
  109. /* Initialize the array of type-functions. */
  110. d->typefns = tor_calloc(n_types, sizeof(dispatch_typefns_t));
  111. for (size_t i = 0; i < n_types; ++i) {
  112. /* Default to no-op for everything... */
  113. memcpy(&d->typefns[i], &nop_typefns, sizeof(dispatch_typefns_t));
  114. }
  115. SMARTLIST_FOREACH_BEGIN(cfg->fns_by_type, dispatch_typefns_t *, fns) {
  116. /* Set the functions if they are provided. */
  117. if (fns) {
  118. if (fns->free_fn)
  119. d->typefns[fns_sl_idx].free_fn = fns->free_fn;
  120. if (fns->fmt_fn)
  121. d->typefns[fns_sl_idx].fmt_fn = fns->fmt_fn;
  122. }
  123. } SMARTLIST_FOREACH_END(fns);
  124. /* Initialize the message queues: one for each channel. */
  125. d->queues = tor_calloc(d->n_queues, sizeof(dqueue_t));
  126. for (size_t i = 0; i < d->n_queues; ++i) {
  127. TOR_SIMPLEQ_INIT(&d->queues[i].queue);
  128. d->queues[i].alert_fn = alert_fn_nop;
  129. }
  130. /* Build the dispatch tables mapping message IDs to receivers. */
  131. d->table = tor_calloc(d->n_msgs, sizeof(dtbl_entry_t *));
  132. SMARTLIST_FOREACH_BEGIN(cfg->recv_by_msg, smartlist_t *, rcv) {
  133. d->table[rcv_sl_idx] = dtbl_entry_from_lst(rcv);
  134. } SMARTLIST_FOREACH_END(rcv);
  135. /* Fill in the empty entries in the dispatch tables:
  136. * types and channels for each message. */
  137. SMARTLIST_FOREACH_BEGIN(cfg->type_by_msg, msg_type_id_t *, type) {
  138. if (d->table[type_sl_idx])
  139. d->table[type_sl_idx]->type = *type;
  140. } SMARTLIST_FOREACH_END(type);
  141. SMARTLIST_FOREACH_BEGIN(cfg->chan_by_msg, channel_id_t *, chan) {
  142. if (d->table[chan_sl_idx])
  143. d->table[chan_sl_idx]->channel = *chan;
  144. } SMARTLIST_FOREACH_END(chan);
  145. return d;
  146. }