From b055b862e412ba5e71f026ada14c41e47e2d7cc2 Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Mon, 9 Aug 2021 17:43:15 +0200 Subject: [PATCH] New: NameValue struct and functions nv_find_by_name() / nv_find_by_value(). These were previously used by the config system as config_binary_flags_search() but can be useful in other areas as well. --- include/h.h | 4 ++++ include/struct.h | 9 ++++++++ src/conf.c | 60 ++++++++++++++---------------------------------- src/list.c | 34 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 43 deletions(-) diff --git a/include/h.h b/include/h.h index f47d601b2..5d2f719b7 100644 --- a/include/h.h +++ b/include/h.h @@ -1042,6 +1042,10 @@ extern void free_security_group(SecurityGroup *s); extern void set_security_group_defaults(void); extern int user_allowed_by_security_group(Client *client, SecurityGroup *s); extern int user_allowed_by_security_group_name(Client *client, char *secgroupname); +#define nv_find_by_name(stru, name) do_nv_find_by_name(stru, name, ARRAY_SIZEOF((stru))) +extern long do_nv_find_by_name(NameValue *table, char *cmd, int numelements); +#define nv_find_by_value(stru, value) do_nv_find_by_value(stru, value, ARRAY_SIZEOF((stru))) +extern char *do_nv_find_by_value(NameValue *table, long value, int numelements); extern void add_nvplist(NameValuePrioList **lst, int priority, char *name, char *value); extern void add_fmt_nvplist(NameValuePrioList **lst, int priority, char *name, FORMAT_STRING(const char *format), ...) __attribute__((format(printf,4,5))); extern NameValuePrioList *find_nvplist(NameValuePrioList *list, char *name); diff --git a/include/struct.h b/include/struct.h index 4a11d3068..54d900eaa 100644 --- a/include/struct.h +++ b/include/struct.h @@ -1186,6 +1186,15 @@ struct IRCCounts { /** The /LUSERS stats information */ extern MODVAR IRCCounts irccounts; +typedef struct NameValue NameValue; +/** Name and value list used in a static array, such as in conf.c */ +struct NameValue +{ + long value; + char *name; +}; + +/** Name and value list used in dynamic linked lists */ typedef struct NameValueList NameValueList; struct NameValueList { NameValueList *prev, *next; diff --git a/src/conf.c b/src/conf.c index 944190d15..e84fc8b18 100644 --- a/src/conf.c +++ b/src/conf.c @@ -31,13 +31,6 @@ struct ConfigCommand int (*testfunc)(ConfigFile *conf, ConfigEntry *ce); }; -typedef struct NameValue NameValue; -struct NameValue -{ - long flag; - char *name; -}; - /* Config commands */ @@ -2881,25 +2874,6 @@ int config_run() } -NameValue *config_binary_flags_search(NameValue *table, char *cmd, int size) { - int start = 0; - int stop = size-1; - int mid; - while (start <= stop) { - mid = (start+stop)/2; - - if (smycmp(cmd,table[mid].name) < 0) { - stop = mid-1; - } - else if (strcmp(cmd,table[mid].name) == 0) { - return &(table[mid]); - } - else - start = mid+1; - } - return NULL; -} - int config_test() { ConfigEntry *ce; @@ -4977,10 +4951,10 @@ int _conf_listen(ConfigFile *conf, ConfigEntry *ce) { for (cepp = cep->items; cepp; cepp = cepp->next) { - NameValue *ofp; - if ((ofp = config_binary_flags_search(_ListenerFlags, cepp->name, ARRAY_SIZEOF(_ListenerFlags)))) + long v; + if ((v = nv_find_by_name(_ListenerFlags, cepp->name))) { - tmpflags |= ofp->flag; + tmpflags |= v; } else { for (h = Hooks[HOOKTYPE_CONFIGRUN]; h; h = h->next) { @@ -5062,7 +5036,7 @@ int _conf_listen(ConfigFile *conf, ConfigEntry *ce) for (cepp = cep->items; cepp; cepp = cepp->next) { NameValue *ofp; - if (!config_binary_flags_search(_ListenerFlags, cepp->name, ARRAY_SIZEOF(_ListenerFlags))) + if (!nv_find_by_name(_ListenerFlags, cepp->name)) { for (h = Hooks[HOOKTYPE_CONFIGRUN_EX]; h; h = h->next) { @@ -5142,8 +5116,7 @@ int _conf_listen(ConfigFile *conf, ConfigEntry *ce) { for (cepp = cep->items; cepp; cepp = cepp->next) { - NameValue *ofp; - if (!config_binary_flags_search(_ListenerFlags, cepp->name, ARRAY_SIZEOF(_ListenerFlags))) + if (!nv_find_by_name(_ListenerFlags, cepp->name)) { for (h = Hooks[HOOKTYPE_CONFIGRUN_EX]; h; h = h->next) { @@ -5234,8 +5207,7 @@ int _test_listen(ConfigFile *conf, ConfigEntry *ce) has_options = 1; for (cepp = cep->items; cepp; cepp = cepp->next) { - NameValue *ofp; - if (!(ofp = config_binary_flags_search(_ListenerFlags, cepp->name, ARRAY_SIZEOF(_ListenerFlags)))) + if (!nv_find_by_name(_ListenerFlags, cepp->name)) { /* Check if a module knows about this listen::options::something */ int used_by_module = 0; @@ -6242,7 +6214,6 @@ int _conf_link(ConfigFile *conf, ConfigEntry *ce) { ConfigEntry *cep, *cepp, *ceppp; ConfigItem_link *link = NULL; - NameValue *ofp; link = safe_alloc(sizeof(ConfigItem_link)); safe_strdup(link->servername, ce->value); @@ -6275,8 +6246,9 @@ int _conf_link(ConfigFile *conf, ConfigEntry *ce) link->outgoing.options = 0; for (ceppp = cepp->items; ceppp; ceppp = ceppp->next) { - if ((ofp = config_binary_flags_search(_LinkFlags, ceppp->name, ARRAY_SIZEOF(_LinkFlags)))) - link->outgoing.options |= ofp->flag; + long v; + if ((v = nv_find_by_name(_LinkFlags, ceppp->name))) + link->outgoing.options |= v; } } else if (!strcmp(cepp->name, "ssl-options") || !strcmp(cepp->name, "tls-options")) @@ -6317,8 +6289,9 @@ int _conf_link(ConfigFile *conf, ConfigEntry *ce) link->options = 0; for (cepp = cep->items; cepp; cepp = cepp->next) { - if ((ofp = config_binary_flags_search(_LinkFlags, cepp->name, ARRAY_SIZEOF(_LinkFlags)))) - link->options |= ofp->flag; + long v; + if ((v = nv_find_by_name(_LinkFlags, cepp->name))) + link->options |= v; } } } @@ -7099,13 +7072,15 @@ void test_tlsblock(ConfigFile *conf, ConfigEntry *cep, int *totalerrors) else if (!strcmp(cepp->name, "options")) { for (ceppp = cepp->items; ceppp; ceppp = ceppp->next) - if (!config_binary_flags_search(_TLSFlags, ceppp->name, ARRAY_SIZEOF(_TLSFlags))) + { + if (!nv_find_by_name(_TLSFlags, ceppp->name)) { config_error("%s:%i: unknown SSL/TLS option '%s'", ceppp->file->filename, ceppp->line_number, ceppp->name); errors ++; } + } } else if (!strcmp(cepp->name, "sts-policy")) { @@ -7304,9 +7279,8 @@ void conf_tlsblock(ConfigFile *conf, ConfigEntry *cep, TLSOptions *tlsoptions) tlsoptions->options = 0; for (ceppp = cepp->items; ceppp; ceppp = ceppp->next) { - ofl = config_binary_flags_search(_TLSFlags, ceppp->name, ARRAY_SIZEOF(_TLSFlags)); - if (ofl) /* this should always be true */ - tlsoptions->options |= ofl->flag; + long v = nv_find_by_name(_TLSFlags, ceppp->name); + tlsoptions->options |= v; } } else if (!strcmp(cepp->name, "sts-policy")) diff --git a/src/list.c b/src/list.c index 06d8d8fb0..a12993316 100644 --- a/src/list.c +++ b/src/list.c @@ -613,3 +613,37 @@ void free_nvplist(NameValuePrioList *lst) safe_free(e); } } + +#define nv_find_by_name(stru, name) do_nv_find_by_name(stru, name, ARRAY_SIZEOF((stru))) + +long do_nv_find_by_name(NameValue *table, char *cmd, int numelements) +{ + int start = 0; + int stop = numelements-1; + int mid; + while (start <= stop) { + mid = (start+stop)/2; + + if (smycmp(cmd,table[mid].name) < 0) { + stop = mid-1; + } + else if (strcmp(cmd,table[mid].name) == 0) { + return table[mid].value; + } + else + start = mid+1; + } + return 0; +} + +#define nv_find_by_value(stru, value) do_nv_find_by_value(stru, value, ARRAY_SIZEOF((stru))) +char *do_nv_find_by_value(NameValue *table, long value, int numelements) +{ + int i; + + for (i=0; i < numelements; i++) + if (table[i].value == value) + return table[i].name; + + return NULL; +}