tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

dispatch_naming.c (2716B)


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