From b16d00ff386d833bb51805c4901f4e62352cdfca Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Sat, 24 Dec 2016 08:34:02 +0100 Subject: [PATCH] mtree: removed mi commands --- src/modules/mtree/Makefile | 1 - src/modules/mtree/doc/mtree_admin.xml | 135 ++----- src/modules/mtree/mtree.c | 204 ----------- src/modules/mtree/mtree.h | 3 - src/modules/mtree/mtree_mod.c | 488 ++++++-------------------- 5 files changed, 144 insertions(+), 687 deletions(-) diff --git a/src/modules/mtree/Makefile b/src/modules/mtree/Makefile index b89770a5363..effe47895d9 100644 --- a/src/modules/mtree/Makefile +++ b/src/modules/mtree/Makefile @@ -10,6 +10,5 @@ DEFS+=-DKAMAILIO_MOD_INTERFACE SERLIBPATH=../../lib SER_LIBS+=$(SERLIBPATH)/srdb1/srdb1 -SER_LIBS+=$(SERLIBPATH)/kmi/kmi include ../../Makefile.modules diff --git a/src/modules/mtree/doc/mtree_admin.xml b/src/modules/mtree/doc/mtree_admin.xml index 4fc45b0d37b..ec6b2d1fa27 100644 --- a/src/modules/mtree/doc/mtree_admin.xml +++ b/src/modules/mtree/doc/mtree_admin.xml @@ -11,9 +11,9 @@ - + &adminguide; - +
Overview @@ -21,12 +21,12 @@ them in a named memory tree. Name of the tree is specified for each record or as module parameter. - + It exports to configuration file functions to match against in-memory trees and return the values (raw or precompiled) associated with matched prefixes. - + The maximum size of the prefix is limited internally to 63, database table definition may enforce lower maximum size. @@ -379,7 +379,7 @@ modparam("mtree", "mt_allow_duplicates", 1)
- +
Functions
@@ -407,117 +407,36 @@ mt_match("mytree", "$rU", "0");
- -
- -
- MI Commands -
- - <function moreinfo="none">mt_list</function> - - - List content of a tree. - - - Name: mt_list - - Parameters: - - _mtree_ : name of tree to list. - - - MI FIFO Command Format: - - - :mt_list:_reply_fifo_file_ - _mtname_ - _empty_line_ -
+ RPC Commands +
- <function moreinfo="none">mt_reload</function> + <function moreinfo="none">mtree.list</function> - Reload mtree from database. + List content of one or all trees. - Name: mt_mtree + Name: mtree.list Parameters: - _mtname_ - name of mem tree + _mtree_ : name of tree to list (optional). - - MI FIFO Command Format: - - - :mt_reload:_reply_fifo_file_ - _mtname_ - _empty_line_ - -
- -
- - <function moreinfo="none">mt_summary</function> - - - List usage summary for all trees. - - - Name: mt_summary - - Parameters: none. - - MI FIFO Command Format: - - - :mt_summary:_reply_fifo_file_ - _empty_line_ - -
- -
- - <function moreinfo="none">mt_match</function> - - - Match prefix value against mtree. - - - Name: mt_match - - Parameters: 3 - - - Name of mtree - - - Prefix to match - - - Mode of matching - - - - MI FIFO Command Format: - - - :mt_match:_reply_fifo_file_ - _mtree_ - _prefix_ - _mode_ - _empty_line_ - + + <function>mtree.list</function> rpc usage + +... +&kamcmd; mtree.list +&kamcmd; mtree.list mytree +... + +
-
-
- RPC Commands -
+
<function moreinfo="none">mtree.summary</function> @@ -529,8 +448,8 @@ mt_match("mytree", "$rU", "0"); _mtree_ - (optional) the name of the tree. -
-
+
+
<function moreinfo="none">mtree.reload</function> @@ -541,8 +460,8 @@ mt_match("mytree", "$rU", "0"); _mtree_ - name of mtree or empty string meaning all mtrees -
-
+
+
<function moreinfo="none">mtree.match</function> @@ -555,8 +474,8 @@ mt_match("mytree", "$rU", "0"); _prefix_ - match prefix _mode_ - matching mode -
-
+
+
diff --git a/src/modules/mtree/mtree.c b/src/modules/mtree/mtree.c index 7ff2af08793..3e09c8fe093 100644 --- a/src/modules/mtree/mtree.c +++ b/src/modules/mtree/mtree.c @@ -941,210 +941,6 @@ int mt_defined_trees(void) return 0; } -int mt_mi_add_tvalues(struct mi_node *rpl, m_tree_t *pt, str *tomatch) -{ - int l; - mt_node_t *itn; - mt_is_t *tvalues; - struct mi_attr* attr= NULL; - struct mi_node *node = NULL; - - if (pt == NULL || tomatch == NULL || tomatch->s == NULL) { - LM_ERR("bad parameters\n"); - return -1; - } - - l = 0; - itn = pt->head; - - while (itn != NULL && l < tomatch->len && l < MT_MAX_DEPTH) { - /* check validity */ - if(_mt_char_table[(unsigned int)tomatch->s[l]]==255) { - LM_ERR("invalid char at %d in [%.*s]\n", - l, tomatch->len, tomatch->s); - return -1; - } - tvalues = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues; - while (tvalues != NULL) { - node = add_mi_node_child(rpl, 0, "MT", 2, 0, 0); - if(node == NULL) - return -1; - if( add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5, - pt->tname.s, pt->tname.len) == NULL) - return -1; - if (add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7, - tomatch->s, l+1) == NULL) - return -1; - if (pt->type == MT_TREE_IVAL) { - attr = addf_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6, - "%d", tvalues->tvalue.n); - } else { /* pt->type == MT_TREE_SVAL */ - attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6, - tvalues->tvalue.s.s, tvalues->tvalue.s.len); - } - tvalues = tvalues->next; - if (attr == NULL) - return -1; - } - - itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child; - l++; - } - - if (node == NULL) return -1; - - return 0; -} - -int mt_mi_match_prefix(struct mi_node *rpl, m_tree_t *it, - str *tomatch, int mode) -{ - int l, len, n; - int i, j; - mt_node_t *itn; - is_t *tvalue; - mt_dw_t *dw; - int tprefix_len = 0; - - struct mi_node *node; - struct mi_attr* attr= NULL; - -#define MT_MAX_DST_LIST 64 - unsigned int tmp_list[2*(MT_MAX_DST_LIST+1)]; - - if(it==NULL || tomatch == NULL - || tomatch->s == NULL) - { - LM_ERR("bad parameters\n"); - return -1; - } - - l = len = 0; - n = 0; - if ((it->type==MT_TREE_SVAL) || (it->type==MT_TREE_IVAL)) { - if (mode == 2) - return mt_mi_add_tvalues(rpl, it, tomatch); - tvalue = mt_get_tvalue(it, tomatch, &tprefix_len); - if (tvalue == NULL) { - LM_DBG("no match for: %.*s\n", tomatch->len, tomatch->s); - return -1; - } - if (tvalue) { - node = add_mi_node_child(rpl, 0, "MT", 2, 0, 0); - if(node == NULL) - return -1; - if( add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5, - it->tname.s, it->tname.len) == NULL) - return -1; - if (add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7, - tomatch->s, tprefix_len) == NULL) - return -1; - if (it->type==MT_TREE_SVAL) { - attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6, - tvalue->s.s, tvalue->s.len); - } else { - attr = addf_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6, - "%d", tvalue->n); - } - if (attr == NULL) - return -1; - } - return 0; - } - - if(it->type!=MT_TREE_DW) - return -1; /* wrong tree type */ - - itn = it->head; - memset(tmp_list, 0, sizeof(unsigned int)*2*(MT_MAX_DST_LIST+1)); - - while(itn!=NULL && l < tomatch->len && l < MT_MAX_DEPTH) - { - /* check validity */ - if(_mt_char_table[(unsigned int)tomatch->s[l]]==255) - { - LM_ERR("invalid char at %d in [%.*s]\n", - l, tomatch->len, tomatch->s); - return -1; - } - - if(itn[_mt_char_table[(unsigned int)tomatch->s[l]]].tvalues!=NULL) - { - dw = (mt_dw_t*)itn[_mt_char_table[(unsigned int)tomatch->s[l]]].data; - while(dw) { - tmp_list[2*n]=dw->dstid; - tmp_list[2*n+1]=dw->weight; - n++; - if(n==MT_MAX_DST_LIST) - break; - dw = dw->next; - } - len = l+1; - } - if(n==MT_MAX_DST_LIST) - break; - - itn = itn[_mt_char_table[(unsigned int)tomatch->s[l]]].child; - l++; - } - - if(n==0) - return -1; /* no match */ - /* invalidate duplicated dstid, keeping longest match */ - for(i=(n-1); i>0; i--) - { - if (tmp_list[2*i]!=0) - { - for(j=0; j= 0; i--) - { - for (j = 1; j <= i; j++) - { - if (tmp_list[2*(j-1)+1] < tmp_list[2*j+1]) - { - tmp_list[2*MT_MAX_DST_LIST] = tmp_list[2*(j-1)]; - tmp_list[2*MT_MAX_DST_LIST+1] = tmp_list[2*(j-1)+1]; - tmp_list[2*(j-1)] = tmp_list[2*j]; - tmp_list[2*(j-1)+1] = tmp_list[2*j+1]; - tmp_list[2*j] = tmp_list[2*MT_MAX_DST_LIST]; - tmp_list[2*j+1] = tmp_list[2*MT_MAX_DST_LIST+1]; - } - } - } - - /* add as attributes */ - for(i=0; itname.s, it->tname.len) == NULL) - return -1; - if (add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7, - tomatch->s, len) == NULL) - return -1; - if(addf_mi_attr(node, MI_DUP_VALUE, "WEIGHT", 6, - "%d", (int)tmp_list[2*i+1]) == NULL) - return -1; - - if (addf_mi_attr(node, MI_DUP_VALUE, "DSTID", 5, - "%d", (int)tmp_list[2*i]) == NULL) - 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 42c8c7ed310..acc8ef0706a 100644 --- a/src/modules/mtree/mtree.h +++ b/src/modules/mtree/mtree.h @@ -27,7 +27,6 @@ #include "../../core/str.h" #include "../../core/parser/msg_parser.h" -#include "../../lib/kmi/mi.h" #include "../../core/rpc.h" #define MT_TREE_SVAL 0 @@ -112,8 +111,6 @@ int mt_init_list_head(void); m_tree_t *mt_add_tree(m_tree_t **dpt, str *tname, str *dbtable, str *cols, int type, int multi); -int mt_mi_match_prefix(struct mi_node *rpl, m_tree_t *pt, - str *tomatch, int mode); int mt_rpc_match_prefix(rpc_t* rpc, void* ctx, m_tree_t *pt, str *tomatch, int mode); #endif diff --git a/src/modules/mtree/mtree_mod.c b/src/modules/mtree/mtree_mod.c index 0197a9c8a56..16ac522103f 100644 --- a/src/modules/mtree/mtree_mod.c +++ b/src/modules/mtree/mtree_mod.c @@ -25,7 +25,6 @@ #include #include "../../lib/srdb1/db_op.h" -#include "../../lib/kmi/mi.h" #include "../../core/sr_module.h" #include "../../lib/srdb1/db.h" #include "../../core/mem/shm_mem.h" @@ -110,17 +109,11 @@ static int w_mt_match(struct sip_msg* msg, char* str1, char* str2, static int mod_init(void); static void mod_destroy(void); static int child_init(int rank); -static int mi_child_init(void); static int mtree_init_rpc(void); static int mt_match(sip_msg_t *msg, str *tname, str *tomatch, int mval); -static struct mi_root* mt_mi_reload(struct mi_root*, void* param); -static struct mi_root* mt_mi_list(struct mi_root*, void* param); -static struct mi_root* mt_mi_summary(struct mi_root*, void* param); -static struct mi_root* mt_mi_match(struct mi_root*, void* param); - static int mt_load_db(m_tree_t *pt); static int mt_load_db_trees(); @@ -150,22 +143,13 @@ static param_export_t params[]={ {0, 0, 0} }; -static mi_export_t mi_cmds[] = { - { "mt_reload", mt_mi_reload, 0, 0, mi_child_init }, - { "mt_list", mt_mi_list, 0, 0, 0 }, - { "mt_summary", mt_mi_summary, 0, 0, 0 }, - { "mt_match", mt_mi_match, 0, 0, 0 }, - { 0, 0, 0, 0, 0} -}; - - struct module_exports exports = { "mtree", DEFAULT_DLFLAGS, /* dlopen flags */ cmds, params, 0, - mi_cmds, /* exported MI functions */ + 0, /* exported MI functions */ 0, /* exported pseudo-variables */ 0, /* extra processes */ mod_init, /* module initialization function */ @@ -175,7 +159,6 @@ struct module_exports exports = { }; - /** * init module function */ @@ -183,11 +166,6 @@ static int mod_init(void) { m_tree_t *pt = NULL; - if(register_mi_mod(exports.name, mi_cmds)!=0) - { - LM_ERR("failed to register MI commands\n"); - return -1; - } if(mtree_init_rpc()!=0) { LM_ERR("failed to register RPC commands\n"); @@ -334,10 +312,7 @@ static int mod_init(void) return -1; } -/** - * mi and worker process initialization - */ -static int mi_child_init(void) +static int mt_child_init(void) { db_con = mt_dbf.init(&db_url); if(db_con==NULL) @@ -357,7 +332,7 @@ static int child_init(int rank) if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN) return 0; - if ( mi_child_init()!=0 ) + if ( mt_child_init()!=0 ) return -1; LM_DBG("#%d: database connection opened successfully\n", rank); @@ -846,260 +821,8 @@ static int mt_load_db_trees() return -1; } -/**************************** MI ***************************/ - -/** - * "mt_reload" syntax : - * \n - */ -static struct mi_root* mt_mi_reload(struct mi_root *cmd_tree, void *param) -{ - str tname = {0, 0}; - m_tree_t *pt; - struct mi_node* node = NULL; - - if(db_table.len>0) - { - /* re-loading all information from database */ - if(mt_load_db_trees()!=0) - { - LM_ERR("cannot re-load info from database\n"); - goto error; - } - } else { - if(!mt_defined_trees()) - { - LM_ERR("empty tree list\n"); - return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN); - } - - /* read tree name */ - node = cmd_tree->node.kids; - if(node != NULL) - { - tname = node->value; - if(tname.s == NULL || tname.len== 0) - return init_mi_tree( 404, "domain not found", 16); - - if(*tname.s=='.') { - tname.s = 0; - tname.len = 0; - } - } - - pt = mt_get_first_tree(); - - while(pt!=NULL) - { - if(tname.s==NULL - || (tname.s!=NULL && pt->tname.len>=tname.len - && strncmp(pt->tname.s, tname.s, tname.len)==0)) - { - /* re-loading table from database */ - if(mt_load_db(pt)!=0) - { - LM_ERR("cannot re-load info from database\n"); - goto error; - } - } - pt = pt->next; - } - } - - return init_mi_tree( 200, MI_OK_S, MI_OK_LEN); - -error: - return init_mi_tree( 500, "Failed to reload",16); -} - - -int mt_print_mi_node(m_tree_t *tree, mt_node_t *pt, struct mi_node* rpl, - char *code, int len) -{ - int i; - struct mi_node* node = NULL; - struct mi_attr* attr= NULL; - mt_is_t *tvalues; - str val; - - if(pt==NULL || len>=MT_MAX_DEPTH) - return 0; - - for(i=0; itname.s, tree->tname.len); - if(attr == NULL) - goto error; - attr = add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7, - code, len+1); - if(attr == NULL) - goto error; - - while (tvalues != NULL) { - if (tree->type == MT_TREE_IVAL) { - val.s = int2str(tvalues->tvalue.n, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6, - val.s, val.len); - } else { - attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6, - tvalues->tvalue.s.s, - tvalues->tvalue.s.len); - } - if(attr == NULL) - goto error; - tvalues = tvalues->next; - } - } - if(mt_print_mi_node(tree, pt[i].child, rpl, code, len+1)<0) - goto error; - } - return 0; -error: - return -1; -} - -/** - * "mt_list" syntax : - * tname - * - * - '.' (dot) means NULL value and will match anything - */ - -#define strpos(s,c) (strchr(s,c)-s) -struct mi_root* mt_mi_list(struct mi_root* cmd_tree, void* param) -{ - str tname = {0, 0}; - m_tree_t *pt; - struct mi_node* node = NULL; - struct mi_root* rpl_tree = NULL; - struct mi_node* rpl = NULL; - static char code_buf[MT_MAX_DEPTH+1]; - int len; - - if(!mt_defined_trees()) - { - LM_ERR("empty tree list\n"); - return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN); - } - - /* read tree name */ - node = cmd_tree->node.kids; - if(node != NULL) - { - tname = node->value; - if(tname.s == NULL || tname.len== 0) - return init_mi_tree( 404, "domain not found", 16); - - if(*tname.s=='.') { - tname.s = 0; - tname.len = 0; - } - } - - rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN); - if(rpl_tree == NULL) - return 0; - rpl = &rpl_tree->node; - - pt = mt_get_first_tree(); - - while(pt!=NULL) - { - if(tname.s==NULL || - (tname.s!=NULL && pt->tname.len>=tname.len && - strncmp(pt->tname.s, tname.s, tname.len)==0)) - { - len = 0; - if(mt_print_mi_node(pt, pt->head, rpl, code_buf, len)<0) - goto error; - } - pt = pt->next; - } - - return rpl_tree; - -error: - free_mi_tree(rpl_tree); - return 0; -} - -struct mi_root* mt_mi_summary(struct mi_root* cmd_tree, void* param) -{ - m_tree_t *pt; - struct mi_root* rpl_tree = NULL; - struct mi_node* node = NULL; - struct mi_attr* attr= NULL; - str val; - - if(!mt_defined_trees()) - { - LM_ERR("empty tree list\n"); - return init_mi_tree( 500, "No trees", 8); - } - - rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN); - if(rpl_tree == NULL) - return 0; - - pt = mt_get_first_tree(); - - while(pt!=NULL) - { - node = add_mi_node_child(&rpl_tree->node, 0, "MT", 2, 0, 0); - if(node == NULL) - goto error; - attr = add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5, - pt->tname.s, pt->tname.len); - if(attr == NULL) - goto error; - val.s = int2str(pt->type, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "TTYPE", 5, - val.s, val.len); - if(attr == NULL) - goto error; - val.s = int2str(pt->memsize, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "MEMSIZE", 7, - val.s, val.len); - if(attr == NULL) - goto error; - val.s = int2str(pt->nrnodes, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "NRNODES", 7, - val.s, val.len); - if(attr == NULL) - goto error; - val.s = int2str(pt->nritems, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "NRITEMS", 7, - val.s, val.len); - if(attr == NULL) - goto error; - val.s = int2str((int)pt->reload_count, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "RELOADCOUNT", 11, - val.s, val.len); - if(attr == NULL) - goto error; - val.s = int2str((int)pt->reload_time, &val.len); - attr = add_mi_attr(node, MI_DUP_VALUE, "RELOADTIME", 10, - val.s, val.len); - if(attr == NULL) - goto error; - - pt = pt->next; - } - - return rpl_tree; -error: - free_mi_tree(rpl_tree); - return 0; -} +/* RPC commands */ void rpc_mtree_summary(rpc_t* rpc, void* c) { str tname = {0, 0}; @@ -1315,123 +1038,146 @@ static const char* rpc_mtree_match_doc[6] = { }; -rpc_export_t mtree_rpc[] = { - {"mtree.summary", rpc_mtree_summary, rpc_mtree_summary_doc, RET_ARRAY}, - {"mtree.reload", rpc_mtree_reload, rpc_mtree_reload_doc, 0}, - {"mtree.match", rpc_mtree_match, rpc_mtree_match_doc, 0}, - {0, 0, 0, 0} -}; - -static int mtree_init_rpc(void) +int rpc_mtree_print_node(rpc_t* rpc, void* ctx, m_tree_t *tree, mt_node_t *pt, + char *code, int len) { - if (rpc_register_array(mtree_rpc) != 0) + int i; + struct mi_node* node = NULL; + struct mi_attr* attr= NULL; + mt_is_t *tvalues; + str val; + void* th = NULL; + void* ih = NULL; + + if(pt==NULL || len>=MT_MAX_DEPTH) + return 0; + + for(i=0; iadd(ctx, "{", &th) < 0) + { + rpc->fault(ctx, 500, "Internal error - node structure"); + return -1; + } + + val.s = code; + val.len = len+1; + if(rpc->struct_add(th, "SS[", + "tname", &tree->tname, + "tprefix", &val, + "tvalue", &ih)<0) + { + rpc->fault(ctx, 500, "Internal error - attribute fields"); + return -1; + } + + while (tvalues != NULL) { + if (tree->type == MT_TREE_IVAL) { + if(rpc->array_add(ih, "u", + (unsigned long)tvalues->tvalue.n)<0) { + rpc->fault(ctx, 500, "Internal error - int val"); + return -1; + } + } else { + if(rpc->array_add(ih, "S", &tvalues->tvalue.s)<0) { + rpc->fault(ctx, 500, "Internal error - str val"); + return -1; + } + } + tvalues = tvalues->next; + } + } + if(rpc_mtree_print_node(rpc, ctx, tree, pt[i].child, code, len+1)<0) + goto error; } return 0; +error: + return -1; } -struct mi_root* mt_mi_match(struct mi_root* cmd_tree, void* param) +/** + * "mtree.list" syntax : + * tname + * + * - '.' (dot) means NULL value and will match anything + */ +void rpc_mtree_list(rpc_t* rpc, void* ctx) { - m_tree_t *tr; - struct mi_root* rpl_tree = NULL; + str tname = {0, 0}; + m_tree_t *pt; struct mi_node* node = NULL; - - str tname, prefix, mode_param; - str bad_tname_param = STR_STATIC_INIT("Bad tname parameter"); - str bad_prefix_param = STR_STATIC_INIT("Bad prefix parameter"); - str bad_mode_param = STR_STATIC_INIT("Bad mode parameter"); - int mode; + struct mi_root* rpl_tree = NULL; + struct mi_node* rpl = NULL; + static char code_buf[MT_MAX_DEPTH+1]; + int len; if(!mt_defined_trees()) { - LM_ERR("empty tree list\n"); - return init_mi_tree( 500, "No trees", 8); + rpc->fault(ctx, 500, "Empty tree list."); + return; } - /* read tree name */ - node = cmd_tree->node.kids; - if(node != NULL) - { - tname = node->value; - if(tname.s == NULL || tname.len== 0) - return init_mi_tree( 400, bad_tname_param.s, bad_tname_param.len); - } - else - { - return init_mi_tree( 400, bad_tname_param.s, bad_tname_param.len); + if(rpc->scan(ctx, "*.S", &tname)!=1) { + tname.s = NULL; + tname.len = 0; } - /* read given prefix */ - node = node->next; - if(node != NULL) - { - prefix = node->value; - if(prefix.s == NULL || prefix.len== 0) - return init_mi_tree( 400, bad_prefix_param.s, bad_prefix_param.len); - } - else + pt = mt_get_first_tree(); + + while(pt!=NULL) { - return init_mi_tree( 400, bad_prefix_param.s, bad_prefix_param.len); + if(tname.s==NULL || + (tname.s!=NULL && pt->tname.len>=tname.len && + strncmp(pt->tname.s, tname.s, tname.len)==0)) + { + len = 0; + code_buf[0] = '\0'; + if(rpc_mtree_print_node(rpc, ctx, pt, pt->head, code_buf, len)<0) { + goto error; + } + } + pt = pt->next; } - /* read mode parameter (required) */ - node = node->next; - if (node != NULL) - { - mode_param = node->value; - if (mode_param.s == NULL || mode_param.len == 0 || - str2int(&mode_param, (unsigned int*)&mode)) - mode = -1; + return; - if (mode != 0 && mode != 2) - return init_mi_tree( 400, bad_mode_param.s, bad_mode_param.len); - } - else - { - return init_mi_tree( 400, bad_mode_param.s, bad_mode_param.len); - } +error: + LM_ERR("failed to build rpc response\n"); + return; +} -again: - lock_get( mt_lock ); - if (mt_reload_flag) { - lock_release( mt_lock ); - sleep_us(5); - goto again; - } - mt_tree_refcnt++; - lock_release( mt_lock ); +static const char* rpc_mtree_list_doc[6] = { + "List the content of one or all trees", + "Parameters:", + "tname - tree name (optional)", + 0 +}; - tr = mt_get_tree(&tname); - if(tr==NULL) - { - /* no tree with such name*/ - rpl_tree = init_mi_tree( 404, "Not found tree", 14); - goto error; - } - rpl_tree = init_mi_tree( 200, "OK", 2); - if (rpl_tree == NULL) - goto error; +rpc_export_t mtree_rpc[] = { + {"mtree.summary", rpc_mtree_summary, rpc_mtree_summary_doc, RET_ARRAY}, + {"mtree.reload", rpc_mtree_reload, rpc_mtree_reload_doc, 0}, + {"mtree.match", rpc_mtree_match, rpc_mtree_match_doc, 0}, + {"mtree.list", rpc_mtree_list, rpc_mtree_list_doc, RET_ARRAY}, + {0, 0, 0, 0} +}; - if(mt_mi_match_prefix(&rpl_tree->node, tr, &prefix, mode)<0) +static int mtree_init_rpc(void) +{ + if (rpc_register_array(mtree_rpc) != 0) { - LM_DBG("no prefix found in [%.*s] for [%.*s]\n", - tname.len, tname.s, - prefix.len, prefix.s); - free_mi_tree(rpl_tree); - rpl_tree = init_mi_tree( 404, "Not found tvalue", 16); + LM_ERR("failed to register RPC commands\n"); + return -1; } - -error: - lock_get( mt_lock ); - mt_tree_refcnt--; - lock_release( mt_lock ); - - return rpl_tree; + return 0; } + /** * */