Module: kamailio Branch: master Commit: 4cb373adf2e1c476a6efcbe38b8ccc6036bd3177 URL: https://github.com/kamailio/kamailio/commit/4cb373adf2e1c476a6efcbe38b8ccc6036bd3177
Author: Daniel-Constantin Mierla <[email protected]> Committer: Daniel-Constantin Mierla <[email protected]> Date: 2025-12-04T13:23:08+01:00 mtree: added item parameter - add item in a tree via modparam: modparam("mtree", "item", ":tname:tprefix:tvalue") - the first character of the modparam value is the separator between the fields --- Modified: src/modules/mtree/mtree.c Modified: src/modules/mtree/mtree.h Modified: src/modules/mtree/mtree_mod.c --- Diff: https://github.com/kamailio/kamailio/commit/4cb373adf2e1c476a6efcbe38b8ccc6036bd3177.diff Patch: https://github.com/kamailio/kamailio/commit/4cb373adf2e1c476a6efcbe38b8ccc6036bd3177.patch --- diff --git a/src/modules/mtree/mtree.c b/src/modules/mtree/mtree.c index 2bf0bff845f..5873d8b8294 100644 --- a/src/modules/mtree/mtree.c +++ b/src/modules/mtree/mtree.c @@ -32,6 +32,7 @@ #include "../../core/mem/shm_mem.h" #include "../../core/parser/parse_param.h" #include "../../core/ut.h" +#include "../../core/trim.h" #include "../../core/pvar.h" #include "../../core/lvalue.h" #include "../../core/shm_init.h" @@ -905,6 +906,79 @@ int mt_defined_trees(void) return 0; } +int mt_table_item(char *val) +{ + m_tree_t *mt = NULL; + str s = STR_NULL; + char *p = NULL; + str tname = STR_NULL; + str tprefix = STR_NULL; + str tvalue = STR_NULL; + + if(val == NULL) { + return -1; + } + + if(!shm_initialized()) { + LM_ERR("shm not initialized - cannot add items to mtree\n"); + return 0; + } + + s.s = val; + s.len = strlen(s.s); + if(s.len < 5) { + LM_ERR("parameter value is too small (%d / %.*s)\n", s.len, s.len, s.s); + return -1; + } + + LM_DBG("adding a new item [%.*s] (%d)\n", s.len, s.s, s.len); + + p = s.s + 1; + tname.s = p; + while(p < s.s + s.len) { + if(*p == s.s[0]) { + tname.len = (int)(p - tname.s); + break; + } + p++; + } + trim(&tname); + if(tname.len == 0) { + LM_ERR("invalid tname (%d / %.*s)\n", s.len, s.len, s.s); + return -1; + } + + mt = mt_get_tree(&tname); + if(mt == NULL) { + LM_ERR("mtree not found (%d / %.*s)\n", s.len, s.len, s.s); + return -1; + } + + p = p + 1; + tprefix.s = p; + while(p < s.s + s.len) { + if(*p == s.s[0]) { + tprefix.len = (int)(p - tprefix.s); + break; + } + p++; + } + if(tprefix.len == 0) { + LM_ERR("invalid tprefix (%d / %.*s)\n", s.len, s.len, s.s); + return -1; + } + + tvalue.s = p + 1; + tvalue.len = s.s + s.len - p; + + if(mt_add_to_tree(mt, &tprefix, &tvalue) < 0) { + LM_ERR("failed to add to mtree (%d / %.*s)\n", s.len, s.len, s.s); + return -1; + } + + return 0; +} + int mt_rpc_add_tvalues(rpc_t *rpc, void *ctx, m_tree_t *pt, str *tomatch) { int l; diff --git a/src/modules/mtree/mtree.h b/src/modules/mtree/mtree.h index 7705dbec6af..189c44a204f 100644 --- a/src/modules/mtree/mtree.h +++ b/src/modules/mtree/mtree.h @@ -108,6 +108,8 @@ int mt_table_spec(char *val); void mt_destroy_trees(void); int mt_defined_trees(void); +int mt_table_item(char *val); + m_tree_t *mt_swap_list_head(m_tree_t *ntree); int mt_init_list_head(void); m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable, str *cols, diff --git a/src/modules/mtree/mtree_mod.c b/src/modules/mtree/mtree_mod.c index cc3f68d4a7e..84925c08e53 100644 --- a/src/modules/mtree/mtree_mod.c +++ b/src/modules/mtree/mtree_mod.c @@ -107,6 +107,7 @@ static volatile int mt_tree_refcnt = 0; static volatile int mt_reload_flag = 0; int mt_param(modparam_t type, void *val); +int mt_item(modparam_t type, void *val); static int fixup_mt_match(void **param, int param_no); static int fixup_free_mt_match(void **param, int param_no); static int w_mt_match(struct sip_msg *msg, char *str1, char *str2, char *str3); @@ -133,6 +134,7 @@ static cmd_export_t cmds[] = { static param_export_t params[] = { {"mtree", PARAM_STRING | PARAM_USE_FUNC, (void *)mt_param}, + {"item", PARAM_STRING | PARAM_USE_FUNC, (void *)mt_item}, {"db_url", PARAM_STR, &db_url}, {"db_table", PARAM_STR, &db_table}, {"tname_column", PARAM_STR, &tname_column}, @@ -430,6 +432,16 @@ int mt_param(modparam_t type, void *val) return -1; } +int mt_item(modparam_t type, void *val) +{ + if(val == NULL) + goto error; + + return mt_table_item((char *)val); +error: + return -1; +} + static int mt_pack_values( m_tree_t *pt, db1_res_t *db_res, int row, int cols, str *tvalue) { _______________________________________________ Kamailio - Development Mailing List -- [email protected] To unsubscribe send an email to [email protected] Important: keep the mailing list in the recipients, do not reply only to the sender!
