Browse code

mtree: Re-instate error log messages in RPC reload removed in an earlier commit

Olle E. Johansson authored on 04/01/2022 09:47:28
Showing 1 changed files
... ...
@@ -937,6 +937,7 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
937 937
 		if(mt_load_db_trees()!=0)
938 938
 		{
939 939
 			rpc->fault(c, 500, "Can not reload Mtrees from database.");
940
+			LM_ERR("RPC failed: cannot reload mtrees from database\n");
940 941
 			return;
941 942
 		}
942 943
 		rpc->rpl_printf(c, "Ok. Mtrees reloaded.");
... ...
@@ -945,6 +946,7 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
945 946
 	if(!mt_defined_trees())
946 947
 	{
947 948
 		rpc->fault(c, 500, "No Mtrees defined.");
949
+		LM_ERR("RPC failed: No Mtrees defined\n");
948 950
 		return;
949 951
 	}
950 952
 
... ...
@@ -971,6 +973,7 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
971 973
 			if(mt_load_db(pt)!=0)
972 974
 			{
973 975
 				rpc->fault(c, 500, "Mtree Reload Failed");
976
+				LM_ERR("RPC failed: cannot reload mtrees from database\n");
974 977
 				return;
975 978
 			}
976 979
 			treeloaded = 1;
... ...
@@ -978,7 +981,7 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
978 981
 		pt = pt->next;
979 982
 	}
980 983
 	if(treeloaded == 0) {
981
-		rpc->fault(c, 500, "Can not find specified Mtree");
984
+		rpc->fault(c, 500, "Can not find specified Mtree");	
982 985
 	}
983 986
 	rpc->rpl_printf(c, "Ok. Mtree reloaded.");
984 987
 	return;
Browse code

mtree: Add RPC response on successful reload, re-organise function rpc_mtree_reload

Olle E. Johansson authored on 03/01/2022 13:53:05
Showing 1 changed files
... ...
@@ -929,61 +929,59 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
929 929
 {
930 930
 	str tname = {0, 0};
931 931
 	m_tree_t *pt = NULL;
932
-	int treloaded = 0;
932
+	int treeloaded = 0;
933 933
 
934 934
 	if(db_table.len>0)
935 935
 	{
936 936
 		/* re-loading all information from database */
937 937
 		if(mt_load_db_trees()!=0)
938 938
 		{
939
-			LM_ERR("cannot re-load mtrees from database\n");
940
-			goto error;
941
-		}
942
-	} else {
943
-		if(!mt_defined_trees())
944
-		{
945
-			LM_ERR("empty mtree list\n");
946
-			goto error;
939
+			rpc->fault(c, 500, "Can not reload Mtrees from database.");
940
+			return;
947 941
 		}
942
+		rpc->rpl_printf(c, "Ok. Mtrees reloaded.");
943
+		return;
944
+	} 
945
+	if(!mt_defined_trees())
946
+	{
947
+		rpc->fault(c, 500, "No Mtrees defined.");
948
+		return;
949
+	}
948 950
 
949
-		/* read tree name */
950
-		if (rpc->scan(c, "S", &tname) != 1) {
951
+	/* read tree name */
952
+	if (rpc->scan(c, "S", &tname) != 1) {
953
+		tname.s = 0;
954
+		tname.len = 0;
955
+	} else {
956
+		if(*tname.s=='.') {
951 957
 			tname.s = 0;
952 958
 			tname.len = 0;
953
-		} else {
954
-			if(*tname.s=='.') {
955
-				tname.s = 0;
956
-				tname.len = 0;
957
-			}
958 959
 		}
960
+	}
959 961
 
960
-		pt = mt_get_first_tree();
962
+	pt = mt_get_first_tree();
961 963
 
962
-		while(pt!=NULL)
964
+	while(pt!=NULL)
965
+	{
966
+		if(tname.s==NULL
967
+				|| (tname.s!=NULL && pt->tname.len>=tname.len
968
+					&& strncmp(pt->tname.s, tname.s, tname.len)==0))
963 969
 		{
964
-			if(tname.s==NULL
965
-					|| (tname.s!=NULL && pt->tname.len>=tname.len
966
-						&& strncmp(pt->tname.s, tname.s, tname.len)==0))
970
+			/* re-loading table from database */
971
+			if(mt_load_db(pt)!=0)
967 972
 			{
968
-				/* re-loading table from database */
969
-				if(mt_load_db(pt)!=0)
970
-				{
971
-					LM_ERR("cannot re-load mtree from database\n");
972
-					goto error;
973
-				}
974
-				treloaded = 1;
973
+				rpc->fault(c, 500, "Mtree Reload Failed");
974
+				return;
975 975
 			}
976
-			pt = pt->next;
977
-		}
978
-		if(treloaded == 0) {
979
-			rpc->fault(c, 500, "No Mtree Name Matching");
976
+			treeloaded = 1;
980 977
 		}
978
+		pt = pt->next;
981 979
 	}
982
-
980
+	if(treeloaded == 0) {
981
+		rpc->fault(c, 500, "Can not find specified Mtree");
982
+	}
983
+	rpc->rpl_printf(c, "Ok. Mtree reloaded.");
983 984
 	return;
984
-
985
-error:
986
-	rpc->fault(c, 500, "Mtree Reload Failed");
987 985
 }
988 986
 
989 987
 static const char* rpc_mtree_reload_doc[2] = {
Browse code

mtree: check if pv_count var is r/w

- reported by GH #2750

Daniel-Constantin Mierla authored on 28/05/2021 11:54:08
Showing 1 changed files
... ...
@@ -203,7 +203,7 @@ static int mod_init(void)
203 203
 	}
204 204
 
205 205
 	if(pv_parse_spec(&count_param, &pv_count) < 0
206
-			|| !(pv_is_w(&pv_weight)))
206
+			|| !(pv_is_w(&pv_count)))
207 207
 	{
208 208
 		LM_ERR("cannot parse count pv or pv is read-only\n");
209 209
 		return -1;
Browse code

mtree: Fix some typos, adjust some formatting

Olle E. Johansson authored on 28/05/2021 07:38:31
Showing 1 changed files
... ...
@@ -175,37 +175,37 @@ static int mod_init(void)
175 175
 		return -1;
176 176
 	}
177 177
 
178
-	if(pv_parse_spec(&value_param, &pv_value)<00
178
+	if(pv_parse_spec(&value_param, &pv_value) < 0
179 179
 			|| !(pv_is_w(&pv_value)))
180 180
 	{
181
-		LM_ERR("cannot parse value pv or is read only\n");
181
+		LM_ERR("cannot parse value pv or pv is read-only\n");
182 182
 		return -1;
183 183
 	}
184 184
 
185
-	if (pv_parse_spec(&values_param, &pv_values) <0
185
+	if (pv_parse_spec(&values_param, &pv_values) < 0
186 186
 			|| pv_values.type != PVT_AVP) {
187 187
 		LM_ERR("cannot parse values avp\n");
188 188
 		return -1;
189 189
 	}
190 190
 
191
-	if(pv_parse_spec(&dstid_param, &pv_dstid)<0
191
+	if(pv_parse_spec(&dstid_param, &pv_dstid) < 0
192 192
 			|| pv_dstid.type!=PVT_AVP)
193 193
 	{
194 194
 		LM_ERR("cannot parse dstid avp\n");
195 195
 		return -1;
196 196
 	}
197 197
 
198
-	if(pv_parse_spec(&weight_param, &pv_weight)<0
198
+	if(pv_parse_spec(&weight_param, &pv_weight) < 0
199 199
 			|| pv_weight.type!=PVT_AVP)
200 200
 	{
201
-		LM_ERR("cannot parse dstid avp\n");
201
+		LM_ERR("cannot parse weight avp\n");
202 202
 		return -1;
203 203
 	}
204 204
 
205
-	if(pv_parse_spec(&count_param, &pv_count)<0
205
+	if(pv_parse_spec(&count_param, &pv_count) < 0
206 206
 			|| !(pv_is_w(&pv_weight)))
207 207
 	{
208
-		LM_ERR("cannot parse count pv or is read-only\n");
208
+		LM_ERR("cannot parse count pv or pv is read-only\n");
209 209
 		return -1;
210 210
 	}
211 211
 
... ...
@@ -220,7 +220,7 @@ static int mod_init(void)
220 220
 	LM_DBG("mt_char_list=%s \n", mt_char_list.s);
221 221
 	mt_char_table_init();
222 222
 
223
-	/* binding to mysql module */
223
+	/* binding to database module */
224 224
 	if(db_bind_mod(&db_url, &mt_dbf))
225 225
 	{
226 226
 		LM_ERR("database module not found\n");
... ...
@@ -614,7 +614,7 @@ static int mt_load_db(m_tree_t *pt)
614 614
 		if(RES_ROWS(db_res)[0].values[0].type != DB1_STRING
615 615
 				|| RES_ROWS(db_res)[0].values[1].type != DB1_STRING)
616 616
 		{
617
-			LM_ERR("wrond column types in db table (%d / %d)\n",
617
+			LM_ERR("wrong column types in db table (%d / %d)\n",
618 618
 					RES_ROWS(db_res)[0].values[0].type,
619 619
 					RES_ROWS(db_res)[0].values[1].type);
620 620
 			goto error;
... ...
@@ -1029,7 +1029,7 @@ again:
1029 1029
 	if(tr==NULL)
1030 1030
 	{
1031 1031
 		/* no tree with such name*/
1032
-		rpc->fault(ctx, 404, "Not found tree");
1032
+		rpc->fault(ctx, 404, "Tree not found");
1033 1033
 		goto error;
1034 1034
 	}
1035 1035
 
... ...
@@ -1038,7 +1038,7 @@ again:
1038 1038
 		LM_DBG("no prefix found in [%.*s] for [%.*s]\n",
1039 1039
 				tname.len, tname.s,
1040 1040
 				tomatch.len, tomatch.s);
1041
-		rpc->fault(ctx, 404, "Not found");
1041
+		rpc->fault(ctx, 404, "Prefix not found");
1042 1042
 	}
1043 1043
 
1044 1044
 error:
Browse code

mtree: add clang-format directives to skip format on kemi_exports

format is important for generating docs by our python tool

Victor Seva authored on 09/12/2019 17:34:37
Showing 1 changed files
... ...
@@ -1196,6 +1196,7 @@ static int mtree_init_rpc(void)
1196 1196
 /**
1197 1197
  *
1198 1198
  */
1199
+/* clang-format off */
1199 1200
 static sr_kemi_t sr_kemi_mtree_exports[] = {
1200 1201
 	{ str_init("mtree"), str_init("mt_match"),
1201 1202
 		SR_KEMIP_INT, mt_match,
... ...
@@ -1205,7 +1206,7 @@ static sr_kemi_t sr_kemi_mtree_exports[] = {
1205 1206
 
1206 1207
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1207 1208
 };
1208
-
1209
+/* clang-format on */
1209 1210
 
1210 1211
 /**
1211 1212
  * load mtree module API
Browse code

mtree: updated mod_interface:

Markus Bönke authored on 28/09/2018 10:08:06 • Victor Seva committed on 28/09/2018 11:03:26
Showing 1 changed files
... ...
@@ -150,17 +150,15 @@ static param_export_t params[]={
150 150
 
151 151
 struct module_exports exports = {
152 152
 	"mtree",
153
-	DEFAULT_DLFLAGS, /* dlopen flags */
154
-	cmds,
155
-	params,
156
-	0,
157
-	0,              /* exported MI functions */
158
-	0,              /* exported pseudo-variables */
159
-	0,              /* extra processes */
160
-	mod_init,       /* module initialization function */
161
-	0,              /* response function */
162
-	mod_destroy,    /* destroy function */
163
-	child_init      /* per child init function */
153
+	DEFAULT_DLFLAGS,/* dlopen flags */
154
+	cmds,		/*·exported·functions·*/
155
+	params,		/*·exported·functions·*/
156
+	0,		/*·exported·RPC·methods·*/
157
+	0,		/* exported pseudo-variables */
158
+	0,		/* response·function */
159
+	mod_init,	/* module initialization function */
160
+	child_init,	/* per child init function */
161
+	mod_destroy	/* destroy function */
164 162
 };
165 163
 
166 164
 
Browse code

mtree: rpc reload without table name reloads all tables

- lost feature in rpc which was available in mi command

Daniel-Constantin Mierla authored on 21/08/2018 06:30:00
Showing 1 changed files
... ...
@@ -930,7 +930,8 @@ static const char* rpc_mtree_summary_doc[2] = {
930 930
 void rpc_mtree_reload(rpc_t* rpc, void* c)
931 931
 {
932 932
 	str tname = {0, 0};
933
-	m_tree_t *pt;
933
+	m_tree_t *pt = NULL;
934
+	int treloaded = 0;
934 935
 
935 936
 	if(db_table.len>0)
936 937
 	{
... ...
@@ -949,8 +950,13 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
949 950
 
950 951
 		/* read tree name */
951 952
 		if (rpc->scan(c, "S", &tname) != 1) {
952
-			rpc->fault(c, 500, "Failed to get table name parameter");
953
-			return;
953
+			tname.s = 0;
954
+			tname.len = 0;
955
+		} else {
956
+			if(*tname.s=='.') {
957
+				tname.s = 0;
958
+				tname.len = 0;
959
+			}
954 960
 		}
955 961
 
956 962
 		pt = mt_get_first_tree();
... ...
@@ -967,9 +973,13 @@ void rpc_mtree_reload(rpc_t* rpc, void* c)
967 973
 					LM_ERR("cannot re-load mtree from database\n");
968 974
 					goto error;
969 975
 				}
976
+				treloaded = 1;
970 977
 			}
971 978
 			pt = pt->next;
972 979
 		}
980
+		if(treloaded == 0) {
981
+			rpc->fault(c, 500, "No Mtree Name Matching");
982
+		}
973 983
 	}
974 984
 
975 985
 	return;
Browse code

mtree: access values before safety checks of variables

Daniel-Constantin Mierla authored on 04/07/2017 10:00:47
Showing 1 changed files
... ...
@@ -764,16 +764,20 @@ static int mt_load_db_trees()
764 764
 		{
765 765
 			/* check for NULL values ?!?! */
766 766
 			tname.s = (char*)(RES_ROWS(db_res)[i].values[0].val.string_val);
767
-			tname.len = strlen(tname.s);
768
-
769 767
 			tprefix.s = (char*)(RES_ROWS(db_res)[i].values[1].val.string_val);
770
-			tprefix.len = strlen(tprefix.s);
771
-
772 768
 			tvalue.s = (char*)(RES_ROWS(db_res)[i].values[2].val.string_val);
769
+
770
+			if(tprefix.s==NULL || tvalue.s==NULL || tname.s==NULL)
771
+			{
772
+				LM_ERR("Error - null fields in db\n");
773
+				continue;
774
+			}
775
+
776
+			tname.len = strlen(tname.s);
777
+			tprefix.len = strlen(tprefix.s);
773 778
 			tvalue.len = strlen(tvalue.s);
774 779
 
775
-			if(tprefix.s==NULL || tvalue.s==NULL || tname.s==NULL ||
776
-					tprefix.len<=0 || tvalue.len<=0 || tname.len<=0)
780
+			if(tname.len<=0 || tprefix.len<=0 || tvalue.len<=0)
777 781
 			{
778 782
 				LM_ERR("Error - bad values in db\n");
779 783
 				continue;
Browse code

modules/mtree: added mt_match API function

Juha Heinanen authored on 15/05/2017 13:40:28
Showing 1 changed files
... ...
@@ -42,6 +42,7 @@
42 42
 #include "../../core/kemi.h"
43 43
 
44 44
 #include "mtree.h"
45
+#include "api.h"
45 46
 
46 47
 MODULE_VERSION
47 48
 
... ...
@@ -112,6 +113,7 @@ static int  mod_init(void);
112 113
 static void mod_destroy(void);
113 114
 static int  child_init(int rank);
114 115
 static int mtree_init_rpc(void);
116
+static int bind_mtree(mtree_api_t* api);
115 117
 
116 118
 static int mt_match(sip_msg_t *msg, str *tname, str *tomatch,
117 119
 		int mval);
... ...
@@ -122,6 +124,7 @@ static int mt_load_db_trees();
122 124
 static cmd_export_t cmds[]={
123 125
 	{"mt_match", (cmd_function)w_mt_match, 3, fixup_mt_match,
124 126
 		0, REQUEST_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONREPLY_ROUTE},
127
+	{"bind_mtree", (cmd_function)bind_mtree, 0, 0, 0},
125 128
 	{0, 0, 0, 0, 0, 0}
126 129
 };
127 130
 
... ...
@@ -1191,6 +1194,22 @@ static sr_kemi_t sr_kemi_mtree_exports[] = {
1191 1194
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1192 1195
 };
1193 1196
 
1197
+
1198
+/**
1199
+ * load mtree module API
1200
+ */
1201
+static int bind_mtree(mtree_api_t* api)
1202
+{
1203
+	if (!api) {
1204
+		LM_ERR("Invalid parameter value\n");
1205
+		return -1;
1206
+	}
1207
+	api->mt_match = mt_match;
1208
+
1209
+	return 0;
1210
+}
1211
+
1212
+
1194 1213
 /**
1195 1214
  *
1196 1215
  */
Browse code

mtree: revert a0e4a9c18e and let db_table name not set by default

- it breaks some runtime rpc commands, reported by GH #1057
- reset it also if set by mistake when mtree param is used

Daniel-Constantin Mierla authored on 05/04/2017 07:34:07
Showing 1 changed files
... ...
@@ -74,7 +74,9 @@ CREATE TABLE mtrees (
74 74
 
75 75
 /** parameters */
76 76
 static str db_url = str_init(DEFAULT_DB_URL);
77
-static str db_table = str_init("mtrees");
77
+/* default name created by sql scripts is 'mtrees'
78
+ * - don't set it here with default value, only via config param */
79
+static str db_table = str_init("");
78 80
 static str tname_column   = str_init("tname");
79 81
 static str tprefix_column = str_init("tprefix");
80 82
 static str tvalue_column  = str_init("tvalue");
... ...
@@ -269,6 +271,9 @@ static int mod_init(void)
269 271
 			}
270 272
 			pt = pt->next;
271 273
 		}
274
+		/* reset db_table value */
275
+		db_table.s = "";
276
+		db_table.len = 0;
272 277
 	} else {
273 278
 		if(db_table.len<=0)
274 279
 		{
Browse code

mtree: removed some unnoticed mi variables

Daniel-Constantin Mierla authored on 04/01/2017 07:45:37
Showing 1 changed files
... ...
@@ -1042,8 +1042,6 @@ int rpc_mtree_print_node(rpc_t* rpc, void* ctx, m_tree_t *tree, mt_node_t *pt,
1042 1042
 		char *code, int len)
1043 1043
 {
1044 1044
 	int i;
1045
-	struct mi_node* node = NULL;
1046
-	struct mi_attr* attr= NULL;
1047 1045
 	mt_is_t *tvalues;
1048 1046
 	str val;
1049 1047
 	void* th = NULL;
... ...
@@ -1110,9 +1108,6 @@ void rpc_mtree_list(rpc_t* rpc, void* ctx)
1110 1108
 {
1111 1109
 	str tname = {0, 0};
1112 1110
 	m_tree_t *pt;
1113
-	struct mi_node* node = NULL;
1114
-	struct mi_root* rpl_tree = NULL;
1115
-	struct mi_node* rpl = NULL;
1116 1111
 	static char code_buf[MT_MAX_DEPTH+1];
1117 1112
 	int len;
1118 1113
 
Browse code

mtree: removed mi commands

Daniel-Constantin Mierla authored on 24/12/2016 07:34:02
Showing 1 changed files
... ...
@@ -25,7 +25,6 @@
25 25
 #include <time.h>
26 26
 
27 27
 #include "../../lib/srdb1/db_op.h"
28
-#include "../../lib/kmi/mi.h"
29 28
 #include "../../core/sr_module.h"
30 29
 #include "../../lib/srdb1/db.h"
31 30
 #include "../../core/mem/shm_mem.h"
... ...
@@ -110,17 +109,11 @@ static int w_mt_match(struct sip_msg* msg, char* str1, char* str2,
110 109
 static int  mod_init(void);
111 110
 static void mod_destroy(void);
112 111
 static int  child_init(int rank);
113
-static int  mi_child_init(void);
114 112
 static int mtree_init_rpc(void);
115 113
 
116 114
 static int mt_match(sip_msg_t *msg, str *tname, str *tomatch,
117 115
 		int mval);
118 116
 
119
-static struct mi_root* mt_mi_reload(struct mi_root*, void* param);
120
-static struct mi_root* mt_mi_list(struct mi_root*, void* param);
121
-static struct mi_root* mt_mi_summary(struct mi_root*, void* param);
122
-static struct mi_root* mt_mi_match(struct mi_root*, void* param);
123
-
124 117
 static int mt_load_db(m_tree_t *pt);
125 118
 static int mt_load_db_trees();
126 119
 
... ...
@@ -150,22 +143,13 @@ static param_export_t params[]={
150 143
 	{0, 0, 0}
151 144
 };
152 145
 
153
-static mi_export_t mi_cmds[] = {
154
-	{ "mt_reload",  mt_mi_reload,  0,  0,  mi_child_init },
155
-	{ "mt_list",    mt_mi_list,    0,  0,  0 },
156
-	{ "mt_summary", mt_mi_summary, 0,  0,  0 },
157
-	{ "mt_match", mt_mi_match, 0,  0,  0 },
158
-	{ 0, 0, 0, 0, 0}
159
-};
160
-
161
-
162 146
 struct module_exports exports = {
163 147
 	"mtree",
164 148
 	DEFAULT_DLFLAGS, /* dlopen flags */
165 149
 	cmds,
166 150
 	params,
167 151
 	0,
168
-	mi_cmds,        /* exported MI functions */
152
+	0,              /* exported MI functions */
169 153
 	0,              /* exported pseudo-variables */
170 154
 	0,              /* extra processes */
171 155
 	mod_init,       /* module initialization function */
... ...
@@ -175,7 +159,6 @@ struct module_exports exports = {
175 159
 };
176 160
 
177 161
 
178
-
179 162
 /**
180 163
  * init module function
181 164
  */
... ...
@@ -183,11 +166,6 @@ static int mod_init(void)
183 166
 {
184 167
 	m_tree_t *pt = NULL;
185 168
 
186
-	if(register_mi_mod(exports.name, mi_cmds)!=0)
187
-	{
188
-		LM_ERR("failed to register MI commands\n");
189
-		return -1;
190
-	}
191 169
 	if(mtree_init_rpc()!=0)
192 170
 	{
193 171
 		LM_ERR("failed to register RPC commands\n");
... ...
@@ -334,10 +312,7 @@ error1:
334 312
 	return -1;
335 313
 }
336 314
 
337
-/**
338
- * mi and worker process initialization
339
- */
340
-static int mi_child_init(void)
315
+static int mt_child_init(void)
341 316
 {
342 317
 	db_con = mt_dbf.init(&db_url);
343 318
 	if(db_con==NULL)
... ...
@@ -357,7 +332,7 @@ static int child_init(int rank)
357 332
 	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
358 333
 		return 0;
359 334
 
360
-	if ( mi_child_init()!=0 )
335
+	if ( mt_child_init()!=0 )
361 336
 		return -1;
362 337
 
363 338
 	LM_DBG("#%d: database connection opened successfully\n", rank);
... ...
@@ -846,260 +821,8 @@ error:
846 821
 	return -1;
847 822
 }
848 823
 
849
-/**************************** MI ***************************/
850
-
851
-/**
852
- * "mt_reload" syntax :
853
- * \n
854
- */
855
-static struct mi_root* mt_mi_reload(struct mi_root *cmd_tree, void *param)
856
-{
857
-	str tname = {0, 0};
858
-	m_tree_t *pt;
859
-	struct mi_node* node = NULL;
860
-
861
-	if(db_table.len>0)
862
-	{
863
-		/* re-loading all information from database */
864
-		if(mt_load_db_trees()!=0)
865
-		{
866
-			LM_ERR("cannot re-load info from database\n");
867
-			goto error;
868
-		}
869
-	} else {
870
-		if(!mt_defined_trees())
871
-		{
872
-			LM_ERR("empty tree list\n");
873
-			return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
874
-		}
875
-
876
-		/* read tree name */
877
-		node = cmd_tree->node.kids;
878
-		if(node != NULL)
879
-		{
880
-			tname = node->value;
881
-			if(tname.s == NULL || tname.len== 0)
882
-				return init_mi_tree( 404, "domain not found", 16);
883
-
884
-			if(*tname.s=='.') {
885
-				tname.s = 0;
886
-				tname.len = 0;
887
-			}
888
-		}
889
-
890
-		pt = mt_get_first_tree();
891
-
892
-		while(pt!=NULL)
893
-		{
894
-			if(tname.s==NULL
895
-					|| (tname.s!=NULL && pt->tname.len>=tname.len
896
-						&& strncmp(pt->tname.s, tname.s, tname.len)==0))
897
-			{
898
-				/* re-loading table from database */
899
-				if(mt_load_db(pt)!=0)
900
-				{
901
-					LM_ERR("cannot re-load info from database\n");
902
-					goto error;
903
-				}
904
-			}
905
-			pt = pt->next;
906
-		}
907
-	}
908
-
909
-	return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
910
-
911
-error:
912
-	return init_mi_tree( 500, "Failed to reload",16);
913
-}
914
-
915
-
916
-int mt_print_mi_node(m_tree_t *tree, mt_node_t *pt, struct mi_node* rpl,
917
-		char *code, int len)
918
-{
919
-	int i;
920
-	struct mi_node* node = NULL;
921
-	struct mi_attr* attr= NULL;
922
-	mt_is_t *tvalues;
923
-	str val;
924
-
925
-	if(pt==NULL || len>=MT_MAX_DEPTH)
926
-		return 0;
927
-
928
-	for(i=0; i<MT_NODE_SIZE; i++)
929
-	{
930
-		code[len]=mt_char_list.s[i];
931
-		tvalues = pt[i].tvalues;
932
-		if (tvalues != NULL)
933
-		{
934
-			node = add_mi_node_child(rpl, 0, "MT", 2, 0, 0);
935
-			if(node == NULL)
936
-				goto error;
937
-			attr = add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5,
938
-					tree->tname.s, tree->tname.len);
939
-			if(attr == NULL)
940
-				goto error;
941
-			attr = add_mi_attr(node, MI_DUP_VALUE, "TPREFIX", 7,
942
-					code, len+1);
943
-			if(attr == NULL)
944
-				goto error;
945
-
946
-			while (tvalues != NULL) {
947
-				if (tree->type == MT_TREE_IVAL) {
948
-					val.s = int2str(tvalues->tvalue.n, &val.len);
949
-					attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6,
950
-							val.s, val.len);
951
-				} else {
952
-					attr = add_mi_attr(node, MI_DUP_VALUE, "TVALUE", 6,
953
-							tvalues->tvalue.s.s,
954
-							tvalues->tvalue.s.len);
955
-				}
956
-				if(attr == NULL)
957
-					goto error;
958
-				tvalues = tvalues->next;
959
-			}
960
-		}
961
-		if(mt_print_mi_node(tree, pt[i].child, rpl, code, len+1)<0)
962
-			goto error;
963
-	}
964
-	return 0;
965
-error:
966
-	return -1;
967
-}
968
-
969
-/**
970
- * "mt_list" syntax :
971
- *    tname
972
- *
973
- * 	- '.' (dot) means NULL value and will match anything
974
- */
975
-
976
-#define strpos(s,c) (strchr(s,c)-s)
977
-struct mi_root* mt_mi_list(struct mi_root* cmd_tree, void* param)
978
-{
979
-	str tname = {0, 0};
980
-	m_tree_t *pt;
981
-	struct mi_node* node = NULL;
982
-	struct mi_root* rpl_tree = NULL;
983
-	struct mi_node* rpl = NULL;
984
-	static char code_buf[MT_MAX_DEPTH+1];
985
-	int len;
986
-
987
-	if(!mt_defined_trees())
988
-	{
989
-		LM_ERR("empty tree list\n");
990
-		return init_mi_tree( 500, MI_INTERNAL_ERR_S, MI_INTERNAL_ERR_LEN);
991
-	}
992
-
993
-	/* read tree name */
994
-	node = cmd_tree->node.kids;
995
-	if(node != NULL)
996
-	{
997
-		tname = node->value;
998
-		if(tname.s == NULL || tname.len== 0)
999
-			return init_mi_tree( 404, "domain not found", 16);
1000
-
1001
-		if(*tname.s=='.') {
1002
-			tname.s = 0;
1003
-			tname.len = 0;
1004
-		}
1005
-	}
1006
-
1007
-	rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
1008
-	if(rpl_tree == NULL)
1009
-		return 0;
1010
-	rpl = &rpl_tree->node;
1011
-
1012
-	pt = mt_get_first_tree();
1013
-
1014
-	while(pt!=NULL)
1015
-	{
1016
-		if(tname.s==NULL ||
1017
-				(tname.s!=NULL && pt->tname.len>=tname.len &&
1018
-					strncmp(pt->tname.s, tname.s, tname.len)==0))
1019
-		{
1020
-			len = 0;
1021
-			if(mt_print_mi_node(pt, pt->head, rpl, code_buf, len)<0)
1022
-				goto error;
1023
-		}
1024
-		pt = pt->next;
1025
-	}
1026
-
1027
-	return rpl_tree;
1028
-
1029
-error:
1030
-	free_mi_tree(rpl_tree);
1031
-	return 0;
1032
-}
1033
-
1034
-struct mi_root* mt_mi_summary(struct mi_root* cmd_tree, void* param)
1035
-{
1036
-	m_tree_t *pt;
1037
-	struct mi_root* rpl_tree = NULL;
1038
-	struct mi_node* node = NULL;
1039
-	struct mi_attr* attr= NULL;
1040
-	str val;
1041
-
1042
-	if(!mt_defined_trees())
1043
-	{
1044
-		LM_ERR("empty tree list\n");
1045
-		return init_mi_tree( 500, "No trees", 8);
1046
-	}
1047
-
1048
-	rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
1049
-	if(rpl_tree == NULL)
1050
-		return 0;
1051
-
1052
-	pt = mt_get_first_tree();
1053
-
1054
-	while(pt!=NULL)
1055
-	{
1056
-		node = add_mi_node_child(&rpl_tree->node, 0, "MT", 2, 0, 0);
1057
-		if(node == NULL)
1058
-			goto error;
1059
-		attr = add_mi_attr(node, MI_DUP_VALUE, "TNAME", 5,
1060
-				pt->tname.s, pt->tname.len);
1061
-		if(attr == NULL)
1062
-			goto error;
1063
-		val.s = int2str(pt->type, &val.len);
1064
-		attr = add_mi_attr(node, MI_DUP_VALUE, "TTYPE", 5,
1065
-				val.s, val.len);
1066
-		if(attr == NULL)
1067
-			goto error;
1068
-		val.s = int2str(pt->memsize, &val.len);
1069
-		attr = add_mi_attr(node, MI_DUP_VALUE, "MEMSIZE", 7,
1070
-				val.s, val.len);
1071
-		if(attr == NULL)
1072
-			goto error;
1073
-		val.s = int2str(pt->nrnodes, &val.len);
1074
-		attr = add_mi_attr(node, MI_DUP_VALUE, "NRNODES", 7,
1075
-				val.s, val.len);
1076
-		if(attr == NULL)
1077
-			goto error;
1078
-		val.s = int2str(pt->nritems, &val.len);
1079
-		attr = add_mi_attr(node, MI_DUP_VALUE, "NRITEMS", 7,
1080
-				val.s, val.len);
1081
-		if(attr == NULL)
1082
-			goto error;
1083
-		val.s = int2str((int)pt->reload_count, &val.len);
1084
-		attr = add_mi_attr(node, MI_DUP_VALUE, "RELOADCOUNT", 11,
1085
-				val.s, val.len);
1086
-		if(attr == NULL)
1087
-			goto error;
1088
-		val.s = int2str((int)pt->reload_time, &val.len);
1089
-		attr = add_mi_attr(node, MI_DUP_VALUE, "RELOADTIME", 10,
1090
-				val.s, val.len);
1091
-		if(attr == NULL)
1092
-			goto error;
1093
-
1094
-		pt = pt->next;
1095
-	}
1096
-
1097
-	return rpl_tree;
1098
-error:
1099
-	free_mi_tree(rpl_tree);
1100
-	return 0;
1101
-}
1102 824
 
825
+/* RPC commands */
1103 826
 void rpc_mtree_summary(rpc_t* rpc, void* c)
1104 827
 {
1105 828
 	str tname = {0, 0};
... ...
@@ -1315,123 +1038,146 @@ static const char* rpc_mtree_match_doc[6] = {
1315 1038
 };
1316 1039
 
1317 1040
 
1318
-rpc_export_t mtree_rpc[] = {
1319
-	{"mtree.summary", rpc_mtree_summary, rpc_mtree_summary_doc, RET_ARRAY},
1320
-	{"mtree.reload", rpc_mtree_reload, rpc_mtree_reload_doc, 0},
1321
-	{"mtree.match", rpc_mtree_match, rpc_mtree_match_doc, 0},
1322
-	{0, 0, 0, 0}
1323
-};
1324
-
1325
-static int mtree_init_rpc(void)
1041
+int rpc_mtree_print_node(rpc_t* rpc, void* ctx, m_tree_t *tree, mt_node_t *pt,
1042
+		char *code, int len)
1326 1043
 {
1327
-	if (rpc_register_array(mtree_rpc) != 0)
1044
+	int i;
1045
+	struct mi_node* node = NULL;
1046
+	struct mi_attr* attr= NULL;
1047
+	mt_is_t *tvalues;
1048
+	str val;
1049
+	void* th = NULL;
1050
+	void* ih = NULL;
1051
+
1052
+	if(pt==NULL || len>=MT_MAX_DEPTH)
1053
+		return 0;
1054
+
1055
+	for(i=0; i<MT_NODE_SIZE; i++)
1328 1056
 	{
1329
-		LM_ERR("failed to register RPC commands\n");
1330
-		return -1;
1057
+		code[len]=mt_char_list.s[i];
1058
+		tvalues = pt[i].tvalues;
1059
+		if (tvalues != NULL)
1060
+		{
1061
+			/* add structure node */
1062
+			if (rpc->add(ctx, "{", &th) < 0)
1063
+			{
1064
+				rpc->fault(ctx, 500, "Internal error - node structure");
1065
+				return -1;
1066
+			}
1067
+
1068
+			val.s = code;
1069
+			val.len = len+1;
1070
+			if(rpc->struct_add(th, "SS[",
1071
+				"tname",	&tree->tname,
1072
+				"tprefix", 	&val,
1073
+				"tvalue",	&ih)<0)
1074
+			{
1075
+				rpc->fault(ctx, 500, "Internal error - attribute fields");
1076
+				return -1;
1077
+			}
1078
+
1079
+			while (tvalues != NULL) {
1080
+				if (tree->type == MT_TREE_IVAL) {
1081
+					if(rpc->array_add(ih, "u",
1082
+								(unsigned long)tvalues->tvalue.n)<0) {
1083
+						rpc->fault(ctx, 500, "Internal error - int val");
1084
+						return -1;
1085
+					}
1086
+				} else {
1087
+					if(rpc->array_add(ih, "S", &tvalues->tvalue.s)<0) {
1088
+						rpc->fault(ctx, 500, "Internal error - str val");
1089
+						return -1;
1090
+					}
1091
+				}
1092
+				tvalues = tvalues->next;
1093
+			}
1094
+		}
1095
+		if(rpc_mtree_print_node(rpc, ctx, tree, pt[i].child, code, len+1)<0)
1096
+			goto error;
1331 1097
 	}
1332 1098
 	return 0;
1099
+error:
1100
+	return -1;
1333 1101
 }
1334 1102
 
1335
-struct mi_root* mt_mi_match(struct mi_root* cmd_tree, void* param)
1103
+/**
1104
+ * "mtree.list" syntax :
1105
+ *    tname
1106
+ *
1107
+ * 	- '.' (dot) means NULL value and will match anything
1108
+ */
1109
+void rpc_mtree_list(rpc_t* rpc, void* ctx)
1336 1110
 {
1337
-	m_tree_t *tr;
1338
-	struct mi_root* rpl_tree = NULL;
1111
+	str tname = {0, 0};
1112
+	m_tree_t *pt;
1339 1113
 	struct mi_node* node = NULL;
1340
-
1341
-	str tname, prefix, mode_param;
1342
-	str bad_tname_param = STR_STATIC_INIT("Bad tname parameter");
1343
-	str bad_prefix_param = STR_STATIC_INIT("Bad prefix parameter");
1344
-	str bad_mode_param = STR_STATIC_INIT("Bad mode parameter");
1345
-	int mode;
1114
+	struct mi_root* rpl_tree = NULL;
1115
+	struct mi_node* rpl = NULL;
1116
+	static char code_buf[MT_MAX_DEPTH+1];
1117
+	int len;
1346 1118
 
1347 1119
 	if(!mt_defined_trees())
1348 1120
 	{
1349
-		LM_ERR("empty tree list\n");
1350
-		return init_mi_tree( 500, "No trees", 8);
1121
+		rpc->fault(ctx, 500, "Empty tree list.");
1122
+		return;
1351 1123
 	}
1352 1124
 
1353
-	/* read tree name */
1354
-	node = cmd_tree->node.kids;
1355
-	if(node != NULL)
1356
-	{
1357
-		tname = node->value;
1358
-		if(tname.s == NULL || tname.len== 0)
1359
-			return init_mi_tree( 400, bad_tname_param.s, bad_tname_param.len);
1360
-	}
1361
-	else
1362
-	{
1363
-		return init_mi_tree( 400, bad_tname_param.s, bad_tname_param.len);
1125
+	if(rpc->scan(ctx, "*.S", &tname)!=1) {
1126
+		tname.s = NULL;
1127
+		tname.len = 0;
1364 1128
 	}
1365 1129
 
1366
-	/* read given prefix */
1367
-	node = node->next;
1368
-	if(node != NULL)
1369
-	{
1370
-		prefix = node->value;
1371
-		if(prefix.s == NULL || prefix.len== 0)
1372
-			return init_mi_tree( 400, bad_prefix_param.s, bad_prefix_param.len);
1373
-	}
1374