dispatch_naming.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "orconfig.h"
  7. #include "lib/cc/compat_compiler.h"
  8. #include "lib/dispatch/dispatch_naming.h"
  9. #include "lib/dispatch/msgtypes.h"
  10. #include "lib/container/namemap.h"
  11. #include "lib/container/namemap_st.h"
  12. #include "lib/log/util_bug.h"
  13. #include "lib/log/log.h"
  14. #include <stdlib.h>
  15. /** Global namemap for message IDs. */
  16. static namemap_t message_id_map = NAMEMAP_INIT();
  17. /** Global namemap for subsystem IDs. */
  18. static namemap_t subsys_id_map = NAMEMAP_INIT();
  19. /** Global namemap for channel IDs. */
  20. static namemap_t channel_id_map = NAMEMAP_INIT();
  21. /** Global namemap for message type IDs. */
  22. static namemap_t msg_type_id_map = NAMEMAP_INIT();
  23. void
  24. dispatch_naming_init(void)
  25. {
  26. }
  27. /* Helper macro: declare functions to map IDs to and from names for a given
  28. * type in a namemap_t.
  29. */
  30. #define DECLARE_ID_MAP_FNS(type) \
  31. type##_id_t \
  32. get_##type##_id(const char *name) \
  33. { \
  34. unsigned u = namemap_get_or_create_id(&type##_id_map, name); \
  35. tor_assert(u != NAMEMAP_ERR); \
  36. tor_assert(u != ERROR_ID); \
  37. return (type##_id_t) u; \
  38. } \
  39. const char * \
  40. get_##type##_id_name(type##_id_t id) \
  41. { \
  42. return namemap_fmt_name(&type##_id_map, id); \
  43. } \
  44. size_t \
  45. get_num_##type##_ids(void) \
  46. { \
  47. return namemap_get_size(&type##_id_map); \
  48. } \
  49. EAT_SEMICOLON
  50. DECLARE_ID_MAP_FNS(message);
  51. DECLARE_ID_MAP_FNS(channel);
  52. DECLARE_ID_MAP_FNS(subsys);
  53. DECLARE_ID_MAP_FNS(msg_type);