Add a naming system for IDs in dispatch.

This commit is contained in:
Nick Mathewson 2019-01-13 14:43:13 -05:00
parent e4d3098d4d
commit f5683d90be
4 changed files with 112 additions and 0 deletions

View file

@ -2,6 +2,7 @@ orconfig.h
ext/tor_queue.h
lib/cc/*.h
lib/container/*.h
lib/dispatch/*.h
lib/intmath/*.h

View file

@ -0,0 +1,63 @@
/* Copyright (c) 2001, Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "orconfig.h"
#include "lib/cc/compat_compiler.h"
#include "lib/dispatch/dispatch_naming.h"
#include "lib/dispatch/msgtypes.h"
#include "lib/container/namemap.h"
#include "lib/container/namemap_st.h"
#include "lib/log/util_bug.h"
#include "lib/log/log.h"
#include <stdlib.h>
/** Global namemap for message IDs. */
static namemap_t message_id_map = NAMEMAP_INIT();
/** Global namemap for subsystem IDs. */
static namemap_t subsys_id_map = NAMEMAP_INIT();
/** Global namemap for channel IDs. */
static namemap_t channel_id_map = NAMEMAP_INIT();
/** Global namemap for message type IDs. */
static namemap_t msg_type_id_map = NAMEMAP_INIT();
void
dispatch_naming_init(void)
{
}
/* Helper macro: declare functions to map IDs to and from names for a given
* type in a namemap_t.
*/
#define DECLARE_ID_MAP_FNS(type) \
type##_id_t \
get_##type##_id(const char *name) \
{ \
unsigned u = namemap_get_or_create_id(&type##_id_map, name); \
tor_assert(u != NAMEMAP_ERR); \
tor_assert(u != ERROR_ID); \
return (type##_id_t) u; \
} \
const char * \
get_##type##_id_name(type##_id_t id) \
{ \
return namemap_fmt_name(&type##_id_map, id); \
} \
size_t \
get_num_##type##_ids(void) \
{ \
return namemap_get_size(&type##_id_map); \
} \
EAT_SEMICOLON
DECLARE_ID_MAP_FNS(message);
DECLARE_ID_MAP_FNS(channel);
DECLARE_ID_MAP_FNS(subsys);
DECLARE_ID_MAP_FNS(msg_type);

View file

@ -0,0 +1,46 @@
/* Copyright (c) 2001, Matej Pfajfar.
* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#ifndef TOR_DISPATCH_NAMING_H
#define TOR_DISPATCH_NAMING_H
#include "lib/dispatch/msgtypes.h"
#include <stddef.h>
/**
* Return an existing channel ID by name, allocating the channel ID if
* if necessary. Returns ERROR_ID if we have run out of
* channels
*/
channel_id_t get_channel_id(const char *);
/**
* Return the name corresponding to a given channel ID.
**/
const char *get_channel_id_name(channel_id_t);
/**
* Return the total number of _named_ channel IDs.
**/
size_t get_num_channel_ids(void);
/* As above, but for messages. */
message_id_t get_message_id(const char *);
const char *get_message_id_name(message_id_t);
size_t get_num_message_ids(void);
/* As above, but for subsystems */
subsys_id_t get_subsys_id(const char *);
const char *get_subsys_id_name(subsys_id_t);
size_t get_num_subsys_ids(void);
/* As above, but for types. Note that types additionally must be
* "defined", if any message is to use them. */
msg_type_id_t get_msg_type_id(const char *);
const char *get_msg_type_id_name(msg_type_id_t);
size_t get_num_msg_type_ids(void);
void dispatch_naming_init(void);
#endif

View file

@ -8,6 +8,7 @@ endif
src_lib_libtor_dispatch_a_SOURCES = \
src/lib/dispatch/dispatch_cfg.c \
src/lib/dispatch/dispatch_core.c \
src/lib/dispatch/dispatch_naming.c \
src/lib/dispatch/dispatch_new.c
src_lib_libtor_dispatch_testing_a_SOURCES = \
@ -19,5 +20,6 @@ noinst_HEADERS += \
src/lib/dispatch/dispatch.h \
src/lib/dispatch/dispatch_cfg.h \
src/lib/dispatch/dispatch_cfg_st.h \
src/lib/dispatch/dispatch_naming.h \
src/lib/dispatch/dispatch_st.h \
src/lib/dispatch/msgtypes.h