msgtypes.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 msgtypes.h
  8. * \brief Types used for messages in the dispatcher code.
  9. **/
  10. #ifndef TOR_DISPATCH_MSGTYPES_H
  11. #define TOR_DISPATCH_MSGTYPES_H
  12. #include <stdint.h>
  13. #include "ext/tor_queue.h"
  14. /**
  15. * These types are aliases for subsystems, channels, and message IDs.
  16. **/
  17. typedef uint16_t subsys_id_t;
  18. typedef uint16_t channel_id_t;
  19. typedef uint16_t message_id_t;
  20. /**
  21. * This identifies a C type that can be sent along with a message.
  22. **/
  23. typedef uint16_t msg_type_id_t;
  24. /**
  25. * An ID value returned for *_type_t when none exists.
  26. */
  27. #define ERROR_ID 65535
  28. /**
  29. * Auxiliary (untyped) data sent along with a message.
  30. *
  31. * We define this as a union of a pointer and a u64, so that the integer
  32. * types will have the same range across platforms.
  33. **/
  34. typedef union {
  35. void *ptr;
  36. uint64_t u64;
  37. } msg_aux_data_t;
  38. /**
  39. * Structure of a received message.
  40. **/
  41. typedef struct msg_t {
  42. TOR_SIMPLEQ_ENTRY(msg_t) next;
  43. subsys_id_t sender;
  44. channel_id_t channel;
  45. message_id_t msg;
  46. /** We could omit this field, since it is implicit in the message type, but
  47. * IMO let's leave it in for safety. */
  48. msg_type_id_t type;
  49. /** Untyped auxiliary data. You shouldn't have to mess with this
  50. * directly. */
  51. msg_aux_data_t aux_data__;
  52. } msg_t;
  53. /**
  54. * A function that a subscriber uses to receive a message.
  55. **/
  56. typedef void (*recv_fn_t)(const msg_t *m);
  57. /**
  58. * Table of functions to use for a given C type. Any omitted (NULL) functions
  59. * will be treated as no-ops.
  60. **/
  61. typedef struct dispatch_typefns_t {
  62. /** Release storage held for the auxiliary data of this type. */
  63. void (*free_fn)(msg_aux_data_t);
  64. /** Format and return a newly allocated string describing the contents
  65. * of this data element. */
  66. char *(*fmt_fn)(msg_aux_data_t);
  67. } dispatch_typefns_t;
  68. #endif