Browse code

rtpengine: implement rpc commands and remove mi leftovers

Richard Fuchs authored on 04/01/2017 19:37:50
Showing 3 changed files
... ...
@@ -103,64 +103,12 @@ MODULE_VERSION
103 103
 #define HOSTNAME_SIZE				100
104 104
 
105 105
 #define DEFAULT_RTPP_SET_ID			0
106
-#define MI_SET_NATPING_STATE		"nh_enable_ping"
107
-#define MI_DEFAULT_NATPING_STATE	1
108
-
109
-#ifdef MI_REMOVED
110
-#define MI_ENABLE_RTP_PROXY			"nh_enable_rtpp"
111
-#define MI_SHOW_RTP_PROXIES			"nh_show_rtpp"
112
-#define MI_PING_RTP_PROXY			"nh_ping_rtpp"
113
-#define MI_SHOW_HASH_TOTAL			"nh_show_hash_total"
114
-#define MI_RELOAD_RTP_PROXY			"nh_reload_rtpp"
115
-
116
-#define MI_DB_NOT_FOUND				"RTP database not found"
117
-#define MI_DB_NOT_FOUND_LEN			(sizeof(MI_DB_NOT_FOUND)-1)
118
-#define MI_DB_ERR				"Error reloading from RTP database"
119
-#define MI_DB_ERR_LEN				(sizeof(MI_DB_ERR)-1)
120
-#define MI_DB_OK				"Success reloading from RTP database"
121
-#define MI_DB_OK_LEN				(sizeof(MI_DB_OK)-1)
122
-#define MI_RTP_PROXY_NOT_FOUND			"RTP proxy not found"
123
-#define MI_RTP_PROXY_NOT_FOUND_LEN		(sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
124
-#define MI_PING_DISABLED			"NAT ping disabled from script"
125
-#define MI_PING_DISABLED_LEN			(sizeof(MI_PING_DISABLED)-1)
126
-#define MI_DISABLED_PERMANENT			"1(permanent)"
127
-#define MI_DISABLED_PERMANENT_LEN		(sizeof(MI_DISABLED_PERMANENT)-1)
128
-#define MI_SET					"set"
129
-#define MI_SET_LEN				(sizeof(MI_SET)-1)
130
-#define MI_INDEX				"index"
131
-#define MI_INDEX_LEN				(sizeof(MI_INDEX)-1)
132
-#define MI_ENABLED				"enabled"
133
-#define MI_ENABLED_LEN				(sizeof(MI_ENABLED)-1)
134
-#define MI_DISABLED				"disabled"
135
-#define MI_DISABLED_LEN				(sizeof(MI_DISABLED)-1)
136
-#define MI_WEIGHT				"weight"
137
-#define MI_WEIGHT_LEN				(sizeof(MI_WEIGHT)-1)
138
-#define MI_RECHECK_TICKS			"recheck_ticks"
139
-#define MI_RECHECK_T_LEN			(sizeof(MI_RECHECK_TICKS)-1)
140
-
141
-#define MI_ERROR				"Error when adding rtpp node details"
142
-#define MI_ERROR_LEN				(sizeof(MI_ERROR)-1)
143
-#define MI_ALL					"all"
144
-#define MI_ALL_LEN				(sizeof(MI_ALL)-1)
145
-#define MI_ENABLE				"enable"
146
-#define MI_ENABLE_LEN				(sizeof(MI_ENABLE)-1)
147
-#define MI_DISABLE				"disable"
148
-#define MI_DISABLE_LEN				(sizeof(MI_DISABLE)-1)
149
-#define MI_PING					"ping"
150
-#define MI_PING_LEN				(sizeof(MI_PING)-1)
151
-#define MI_SUCCESS				"success"
152
-#define MI_SUCCESS_LEN				(sizeof(MI_SUCCESS)-1)
153
-#define MI_FAIL					"fail"
154
-#define MI_FAIL_LEN				(sizeof(MI_FAIL)-1)
155
-#define MI_HASH_ENTRIES				"entries"
156
-#define MI_HASH_ENTRIES_LEN			(sizeof(MI_HASH_ENTRIES)-1)
157
-#define MI_HASH_ENTRIES_FAIL			"Fail to get entry details"
158
-#define MI_HASH_ENTRIES_FAIL_LEN		(sizeof(MI_HASH_ENTRIES_FAIL)-1)
159
-
160
-#define MI_FOUND_ALL				2
161
-#define MI_FOUND_ONE				1
162
-#define MI_FOUND_NONE				0
163
-#endif
106
+
107
+enum {
108
+	RPC_FOUND_ALL = 2,
109
+	RPC_FOUND_ONE = 1,
110
+	RPC_FOUND_NONE = 0,
111
+};
164 112
 
165 113
 #define	CPORT					"22222"
166 114
 
... ...
@@ -211,22 +159,13 @@ static int get_ip_type(char *str_addr);
211 159
 static int get_ip_scope(char *str_addr); // useful for link-local ipv6
212 160
 static int bind_force_send_ip(int sock_idx);
213 161
 
162
+static int add_rtpp_node_info(void *ptrs, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list);
214 163
 static int rtpp_test_ping(struct rtpp_node *node);
215 164
 
216 165
 /* Pseudo-Variables */
217 166
 static int pv_get_rtpstat_f(struct sip_msg *, pv_param_t *, pv_value_t *);
218 167
 static int set_rtp_inst_pvar(struct sip_msg *msg, const str * const uri);
219 168
 
220
-#ifdef MI_REMOVED
221
-static int add_rtpp_node_info(struct mi_node *node, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list);
222
-/*mi commands*/
223
-static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree, void* param);
224
-static struct mi_root* mi_show_rtp_proxy(struct mi_root* cmd_tree, void* param);
225
-static struct mi_root* mi_ping_rtp_proxy(struct mi_root* cmd_tree, void* param);
226
-static struct mi_root* mi_show_hash_total(struct mi_root* cmd_tree, void* param);
227
-static struct mi_root* mi_reload_rtp_proxy(struct mi_root* cmd_tree, void* param);
228
-#endif
229
-
230 169
 static int rtpengine_allow_op = 0;
231 170
 static struct rtpp_node **queried_nodes_ptr = NULL;
232 171
 static pid_t mypid;
... ...
@@ -356,17 +295,6 @@ static param_export_t params[] = {
356 295
 	{0, 0, 0}
357 296
 };
358 297
 
359
-#ifdef MI_REMOVED
360
-static mi_export_t mi_cmds[] = {
361
-	{MI_ENABLE_RTP_PROXY,     mi_enable_rtp_proxy,  0,  0,  0},
362
-	{MI_SHOW_RTP_PROXIES,     mi_show_rtp_proxy,    0,  0,  0},
363
-	{MI_PING_RTP_PROXY,       mi_ping_rtp_proxy,    0,  0,  0},
364
-	{MI_SHOW_HASH_TOTAL,      mi_show_hash_total,   0,  0,  0},
365
-	{MI_RELOAD_RTP_PROXY,     mi_reload_rtp_proxy,  0,  0,  0},
366
-	{ 0, 0, 0, 0, 0}
367
-};
368
-#endif
369
-
370 298
 struct module_exports exports = {
371 299
 	"rtpengine",
372 300
 	DEFAULT_DLFLAGS, /* dlopen flags */
... ...
@@ -404,7 +332,7 @@ static int is_queried_node(struct rtpp_node *node, struct rtpp_node **queried_no
404 332
 int rtpengine_delete_node(struct rtpp_node *rtpp_node)
405 333
 {
406 334
 	rtpp_node->rn_displayed = 0;
407
-	rtpp_node->rn_disabled = MI_MAX_RECHECK_TICKS;
335
+	rtpp_node->rn_disabled = RTPENGINE_MAX_RECHECK_TICKS;
408 336
 
409 337
 	return 1;
410 338
 }
... ...
@@ -795,7 +723,7 @@ int add_rtpengine_socks(struct rtpp_set * rtpp_list, char * rtpproxy,
795 723
 		lock_get(rtpp_no_lock);
796 724
 		pnode->idx = *rtpp_no;
797 725
 
798
-		if (ticks == MI_MAX_RECHECK_TICKS) {
726
+		if (ticks == RTPENGINE_MAX_RECHECK_TICKS) {
799 727
 			pnode->rn_recheck_ticks = ticks;
800 728
 		} else {
801 729
 			pnode->rn_recheck_ticks = ticks + get_ticks();
... ...
@@ -1056,598 +984,283 @@ error:
1056 984
 	return -1;
1057 985
 }
1058 986
 
1059
-#ifdef MI_REMOVED
1060
-static struct mi_root* mi_enable_rtp_proxy(struct mi_root *cmd_tree, void *param)
1061
-{
1062
-	struct mi_node *node, *crt_node;
1063
-	struct rtpp_set *rtpp_list;
1064
-	struct rtpp_node *crt_rtpp, *found_rtpp;
1065
-	struct mi_root *root = NULL;
1066
-	struct mi_attr *attr;
1067
-	unsigned int enable;
1068
-	int found, found_rtpp_disabled;
1069
-	str rtpp_url;
1070
-	str snode, sattr, svalue;
1071 987
 
1072
-	found = MI_FOUND_NONE;
1073
-	found_rtpp_disabled = 0;
1074
-	found_rtpp = NULL;
1075
-	enable = 0;
1076
-
1077
-	if (rtpp_set_list == NULL) {
1078
-		return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1079
-	}
988
+static void rtpengine_rpc_reload(rpc_t* rpc, void* ctx)
989
+{
990
+	unsigned int current_rtpp_no;
1080 991
 
1081
-	node = cmd_tree->node.kids;
1082
-	if (node == NULL) {
1083
-		return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
992
+	if (rtpp_db_url.s == NULL) {
993
+		// no database
994
+		rpc->fault(ctx, 500, "No Database URL");
995
+		return;
1084 996
 	}
1085 997
 
1086
-	if (node->value.s == NULL || node->value.len ==0) {
1087
-		return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
998
+	if (init_rtpproxy_db() < 0) {
999
+		// fail reloading from database
1000
+		rpc->fault(ctx, 500, "Failed reloading db");
1001
+		return;
1088 1002
 	}
1089 1003
 
1090
-	/* get proxy */
1091
-	rtpp_url = node->value;
1092
-
1093
-	node = node->next;
1094
-	if (node == NULL) {
1095
-		return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
1096
-	}
1004
+	lock_get(rtpp_no_lock);
1005
+	current_rtpp_no = *rtpp_no;
1006
+	lock_release(rtpp_no_lock);
1097 1007
 
1098
-	if (node->value.s == NULL || node->value.len ==0) {
1099
-		return init_mi_tree(400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
1008
+	if (rtpp_socks_size != current_rtpp_no) {
1009
+		build_rtpp_socks(current_rtpp_no);
1100 1010
 	}
1011
+}
1101 1012
 
1102
-	/* get value (enable/disable) */
1103
-	if(strno2int(&node->value, &enable) < 0) {
1104
-		goto error;
1105
-	}
1013
+static int rtpengine_rpc_iterate(rpc_t* rpc, void* ctx, const str *rtpp_url,
1014
+		int (*cb)(struct rtpp_node *, struct rtpp_set *, void *), void *data)
1015
+{
1016
+	struct rtpp_set *rtpp_list;
1017
+	struct rtpp_node *crt_rtpp;
1018
+	int found = RPC_FOUND_NONE, err = 0;
1019
+	int ret;
1106 1020
 
1107
-	node = node->next;
1108
-	if (node != NULL) {
1109
-		return init_mi_tree(400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
1021
+	if (!rtpp_set_list) {
1022
+		rpc->fault(ctx, 404, "Instance not found (no sets loaded)");
1023
+		return -1;
1110 1024
 	}
1111 1025
 
1112 1026
 	/* found a matching all - show all rtpp */
1113
-	if (strncmp(MI_ALL, rtpp_url.s, MI_ALL_LEN) == 0) {
1114
-		found = MI_FOUND_ALL;
1027
+	if (strncmp("all", rtpp_url->s, 3) == 0) {
1028
+		found = RPC_FOUND_ALL;
1115 1029
 	}
1116 1030
 
1117 1031
 	lock_get(rtpp_set_list->rset_head_lock);
1118
-	for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
1032
+	for (rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
1119 1033
 			rtpp_list = rtpp_list->rset_next) {
1120 1034
 
1121 1035
 		lock_get(rtpp_list->rset_lock);
1122
-		for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
1036
+		for (crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
1123 1037
 				crt_rtpp = crt_rtpp->rn_next) {
1124 1038
 
1125 1039
 			if (!crt_rtpp->rn_displayed) {
1126 1040
 				continue;
1127 1041
 			}
1128 1042
 
1129
-			/* found a matching rtpp - show it */
1130
-			if (found == MI_FOUND_ALL ||
1131
-			   (crt_rtpp->rn_url.len == rtpp_url.len &&
1132
-			   strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0)) {
1133
-
1134
-				/* do ping when try to enable the rtpp */
1135
-				if (enable) {
1136
-
1137
-					/* if ping success, enable the rtpp and reset ticks */
1138
-					if (rtpp_test_ping(crt_rtpp) == 0) {
1139
-						crt_rtpp->rn_disabled = 0;
1140
-						crt_rtpp->rn_recheck_ticks = MI_MIN_RECHECK_TICKS;
1141
-
1142
-					/* if ping fail, disable the rtpps but _not_ permanently*/
1143
-					} else {
1144
-						crt_rtpp->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout);
1145
-						crt_rtpp->rn_disabled = 1;
1146
-						found_rtpp_disabled = 1;
1147
-					}
1148
-
1149
-				/* do not ping when disable the rtpp; disable it permanenty */
1150
-				} else {
1151
-					crt_rtpp->rn_disabled = 1;
1152
-					crt_rtpp->rn_recheck_ticks = MI_MAX_RECHECK_TICKS;
1043
+			/* found a matching rtpp - ping it */
1044
+			if (found == RPC_FOUND_ALL ||
1045
+			   (crt_rtpp->rn_url.len == rtpp_url->len &&
1046
+			   strncmp(crt_rtpp->rn_url.s, rtpp_url->s, rtpp_url->len) == 0)) {
1047
+
1048
+				ret = cb(crt_rtpp, rtpp_list, data);
1049
+				if (ret) {
1050
+					err = 1;
1051
+					break;
1153 1052
 				}
1154 1053
 
1155
-				if (found == MI_FOUND_NONE) {
1156
-					found = MI_FOUND_ONE;
1157
-					found_rtpp = crt_rtpp;
1054
+				if (found == RPC_FOUND_NONE) {
1055
+					found = RPC_FOUND_ONE;
1158 1056
 				}
1159 1057
 			}
1160 1058
 		}
1161 1059
 		lock_release(rtpp_list->rset_lock);
1162
-	}
1163
-	lock_release(rtpp_set_list->rset_head_lock);
1164
-
1165
-	root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
1166
-	if (!root) {
1167
-		LM_ERR("the MI tree cannot be initialized!\n");
1168
-		return 0;
1169
-	}
1170
-	node = &root->node;
1171 1060
 
1172
-	switch (found) {
1173
-		case MI_FOUND_ALL:
1174
-			snode.s = MI_ALL;
1175
-			snode.len = MI_ALL_LEN;
1176
-			break;
1177
-		case MI_FOUND_ONE:
1178
-			snode.s = found_rtpp->rn_url.s;
1179
-			snode.len = found_rtpp->rn_url.len;
1061
+		if (err)
1180 1062
 			break;
1181
-		default:
1182
-			if (root) {
1183
-				free_mi_tree(root);
1184
-			}
1185
-			return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1186
-	}
1187
-
1188
-	svalue.s = MI_SUCCESS;
1189
-	svalue.len = MI_SUCCESS_LEN;
1190
-
1191
-	if (enable) {
1192
-		sattr.s = MI_ENABLE;
1193
-		sattr.len = MI_ENABLE_LEN;
1194
-
1195
-		if (found_rtpp_disabled) {
1196
-			svalue.s = MI_FAIL;
1197
-			svalue.len = MI_FAIL_LEN;
1198
-		}
1199
-	} else {
1200
-		sattr.s = MI_DISABLE;
1201
-		sattr.len = MI_DISABLE_LEN;
1202 1063
 	}
1064
+	lock_release(rtpp_set_list->rset_head_lock);
1203 1065
 
1204
-	if (!(crt_node = add_mi_node_child(node, 0, snode.s, snode.len, 0, 0))) {
1205
-		LM_ERR("cannot add the child node to the tree\n");
1206
-		goto error;
1207
-	}
1066
+	if (err)
1067
+		return -1;
1208 1068
 
1209
-	if ((attr = add_mi_attr(crt_node, MI_DUP_VALUE, sattr.s, sattr.len, svalue.s, svalue.len)) == 0) {
1210
-		LM_ERR("cannot add attributes to the node\n");
1211
-		goto error;
1069
+	if (found == RPC_FOUND_NONE) {
1070
+		rpc->fault(ctx, 404, "Instance not found");
1071
+		return -1;
1212 1072
 	}
1213 1073
 
1214
-
1215
-	return root;
1216
-
1217
-error:
1218
-	if (root) {
1219
-		free_mi_tree(root);
1220
-	}
1221
-	return init_mi_tree(404, MI_ERROR, MI_ERROR_LEN);
1074
+	return found;
1222 1075
 }
1223 1076
 
1224
-
1225
-
1226
-
1227
-#define add_rtpp_node_int_info(_parent, _name, _name_len, _value, _child,\
1228
-								_len, _string, _error)\
1229
-	do {\
1230
-		(_string) = int2str((_value), &(_len));\
1231
-		if((_string) == 0){\
1232
-			LM_ERR("cannot convert int value\n");\
1233
-				goto _error;\
1234
-		}\
1235
-		if(((_child) = add_mi_node_child((_parent), MI_DUP_VALUE, (_name), \
1236
-				(_name_len), (_string), (_len))) == 0)\
1237
-			goto _error;\
1238
-	}while(0);
1239
-
1240
-
1241
-static int add_rtpp_node_info (struct mi_node *node, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list)
1077
+static int add_rtpp_node_info (void *ptrsp, struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list)
1242 1078
 {
1243
-	int id_len, len;
1079
+	void *vh;
1080
+	void **ptrs = ptrsp;
1081
+	rpc_t *rpc;
1082
+	void *ctx;
1244 1083
 	int rtpp_ticks;
1245
-	struct mi_node *crt_node, *child;
1246
-	struct mi_attr *attr;
1247
-	char *string, *id;
1248
-
1249
-	string = id = 0;
1250
-
1251
-	id = int2str(rtpp_list->id_set, &id_len);
1252
-	if (!id) {
1253
-		LM_ERR("cannot convert set id\n");
1254
-		goto error;
1255
-	}
1256
-
1257
-	if (!(crt_node = add_mi_node_child(node, 0, crt_rtpp->rn_url.s, crt_rtpp->rn_url.len, 0,0))) {
1258
-		LM_ERR("cannot add the child node to the tree\n");
1259
-		goto error;
1260
-	}
1261 1084
 
1262
-	LM_DBG("adding node name %s \n",crt_rtpp->rn_url.s );
1085
+	rpc = ptrs[0];
1086
+	ctx = ptrs[1];
1263 1087
 
1264
-	if ((attr = add_mi_attr(crt_node, MI_DUP_VALUE, MI_SET, MI_SET_LEN, id, id_len)) == 0) {
1265
-		LM_ERR("cannot add attributes to the node\n");
1266
-		goto error;
1088
+	if (rpc->add(ctx, "{", &vh) < 0) {
1089
+		rpc->fault(ctx, 500, "Server error");
1090
+		return -1;
1267 1091
 	}
1268 1092
 
1269
-	add_rtpp_node_int_info(crt_node, MI_INDEX, MI_INDEX_LEN,
1270
-		crt_rtpp->idx, child, len, string, error);
1093
+	rpc->struct_add(vh, "Sddd",
1094
+			"url", &crt_rtpp->rn_url,
1095
+			"set", rtpp_list->id_set,
1096
+			"index", crt_rtpp->idx,
1097
+			"weight", crt_rtpp->rn_weight);
1271 1098
 
1272
-	if ((1 == crt_rtpp->rn_disabled ) && (crt_rtpp->rn_recheck_ticks == MI_MAX_RECHECK_TICKS)) {
1273
-		if (!(child = add_mi_node_child(crt_node, MI_DUP_VALUE, MI_DISABLED, MI_DISABLED_LEN,
1274
-		   MI_DISABLED_PERMANENT, MI_DISABLED_PERMANENT_LEN))) {
1275
-			LM_ERR("cannot add disabled (permanent) message\n");
1276
-			goto error;
1277
-		}
1099
+	if ((1 == crt_rtpp->rn_disabled ) && (crt_rtpp->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS)) {
1100
+		rpc->struct_add(vh, "s", "disabled", "1(permanent)");
1278 1101
 	} else {
1279
-		add_rtpp_node_int_info(crt_node, MI_DISABLED, MI_DISABLED_LEN,
1280
-			crt_rtpp->rn_disabled, child, len, string, error);
1102
+		rpc->struct_add(vh, "d", "disabled", crt_rtpp->rn_disabled);
1281 1103
 	}
1282 1104
 
1283
-	add_rtpp_node_int_info(crt_node, MI_WEIGHT, MI_WEIGHT_LEN,
1284
-		crt_rtpp->rn_weight, child, len, string, error);
1285
-
1286
-	if (crt_rtpp->rn_recheck_ticks == MI_MAX_RECHECK_TICKS) {
1287
-		if (!(child = add_mi_node_child(crt_node, MI_DUP_VALUE,
1288
-		   MI_RECHECK_TICKS, MI_RECHECK_T_LEN,
1289
-		   "N/A", sizeof("N/A") - 1))) {
1290
-			LM_ERR("cannot add MAX recheck_ticks value\n");
1291
-			goto error;
1292
-		}
1105
+	if (crt_rtpp->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) {
1106
+		rpc->struct_add(vh, "s", "recheck_ticks", "N/A");
1293 1107
 	} else {
1294 1108
 		rtpp_ticks = crt_rtpp->rn_recheck_ticks - get_ticks();
1295 1109
 		rtpp_ticks = rtpp_ticks < 0 ? 0 : rtpp_ticks;
1296
-		add_rtpp_node_int_info(crt_node, MI_RECHECK_TICKS, MI_RECHECK_T_LEN,
1297
-			rtpp_ticks, child, len, string, error);
1110
+		rpc->struct_add(vh, "d", "recheck_ticks", rtpp_ticks);
1298 1111
 	}
1299 1112
 
1300 1113
 	return 0;
1301
-
1302
-error:
1303
-	return -1;
1304 1114
 }
1305 1115
 
1306
-static struct mi_root* mi_show_rtp_proxy(struct mi_root* cmd_tree, void* param)
1116
+static int rtpengine_iter_cb_enable(struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *flagp)
1307 1117
 {
1308
-	struct mi_node *node;
1309
-	struct mi_root *root = NULL;
1310
-	struct rtpp_set *rtpp_list;
1311
-	struct rtpp_node *crt_rtpp;
1312
-	int found;
1313
-	str rtpp_url;
1314
-
1315
-	found = MI_FOUND_NONE;
1316
-
1317
-	if (rtpp_set_list == NULL) {
1318
-		return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1319
-	}
1320
-
1321
-	node = cmd_tree->node.kids;
1322
-	if (node == NULL) {
1323
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
1324
-	}
1325
-
1326
-	if (node->value.s == NULL || node->value.len ==0) {
1327
-		return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
1328
-	}
1329
-
1330
-	rtpp_url = node->value;
1331
-	if (strncmp(MI_ALL, rtpp_url.s, MI_ALL_LEN) != 0 && rtpp_set_list == NULL) {
1332
-		return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1333
-	}
1334
-
1335
-	node = node->next;
1336
-	if (node != NULL) {
1337
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
1338
-	}
1339
-
1340
-	root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
1341
-	if (!root) {
1342
-		LM_ERR("the MI tree cannot be initialized!\n");
1343
-		return 0;
1344
-	}
1118
+	int *flag = flagp;
1345 1119
 
1346
-	node = &root->node;
1120
+	/* do ping when try to enable the rtpp */
1121
+	if (*flag) {
1347 1122
 
1348
-	/* found a matching all - show all rtpp */
1349
-	if (strncmp(MI_ALL, rtpp_url.s, MI_ALL_LEN) == 0) {
1350
-		found = MI_FOUND_ALL;
1351
-	}
1352
-
1353
-	lock_get(rtpp_set_list->rset_head_lock);
1354
-	for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
1355
-			rtpp_list = rtpp_list->rset_next) {
1356
-
1357
-		lock_get(rtpp_list->rset_lock);
1358
-		for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
1359
-				crt_rtpp = crt_rtpp->rn_next) {
1360
-
1361
-			if (!crt_rtpp->rn_displayed) {
1362
-				continue;
1363
-			}
1364
-
1365
-			/* found a matching rtpp - show it */
1366
-			if (found == MI_FOUND_ALL ||
1367
-			   (crt_rtpp->rn_url.len == rtpp_url.len &&
1368
-			   strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0)) {
1369
-
1370
-				if (add_rtpp_node_info(node, crt_rtpp, rtpp_list) < 0) {
1371
-					lock_release(rtpp_list->rset_lock);
1372
-					lock_release(rtpp_set_list->rset_head_lock);
1373
-					goto error;
1374
-				}
1123
+		/* if ping success, enable the rtpp and reset ticks */
1124
+		if (rtpp_test_ping(crt_rtpp) == 0) {
1125
+			crt_rtpp->rn_disabled = 0;
1126
+			crt_rtpp->rn_recheck_ticks = RTPENGINE_MIN_RECHECK_TICKS;
1375 1127
 
1376
-				if (found == MI_FOUND_NONE) {
1377
-					found = MI_FOUND_ONE;
1378
-				}
1379
-			}
1128
+		/* if ping fail, disable the rtpps but _not_ permanently*/
1129
+		} else {
1130
+			crt_rtpp->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout);
1131
+			crt_rtpp->rn_disabled = 1;
1132
+			*flag = 2; /* return value to caller */
1380 1133
 		}
1381
-		lock_release(rtpp_list->rset_lock);
1382
-	}
1383
-	lock_release(rtpp_set_list->rset_head_lock);
1384 1134
 
1385
-	switch (found) {
1386
-		case MI_FOUND_ALL:
1387
-		case MI_FOUND_ONE:
1388
-			break;
1389
-		default:
1390
-			if (root) {
1391
-				free_mi_tree(root);
1392
-			}
1393
-			return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1135
+	/* do not ping when disable the rtpp; disable it permanenty */
1136
+	} else {
1137
+		crt_rtpp->rn_disabled = 1;
1138
+		crt_rtpp->rn_recheck_ticks = RTPENGINE_MAX_RECHECK_TICKS;
1394 1139
 	}
1395 1140
 
1396
-	return root;
1397
-
1398
-error:
1399
-	if (root) {
1400
-		free_mi_tree(root);
1401
-	}
1402
-	return init_mi_tree(404, MI_ERROR, MI_ERROR_LEN);
1141
+	return 0;
1403 1142
 }
1404 1143
 
1405
-static struct mi_root* mi_ping_rtp_proxy(struct mi_root* cmd_tree, void* param)
1144
+static void rtpengine_rpc_enable(rpc_t* rpc, void* ctx)
1406 1145
 {
1407
-	struct mi_node *node, *crt_node;
1408
-	struct mi_attr *attr;
1409
-	struct mi_root *root = NULL;
1410
-	struct rtpp_set *rtpp_list;
1411
-	struct rtpp_node *crt_rtpp, *found_rtpp;
1412
-	int found, found_rtpp_disabled;
1146
+	void *vh;
1413 1147
 	str rtpp_url;
1414
-	str snode, sattr, svalue;
1415
-
1416
-	found = 0;
1417
-	found_rtpp_disabled = 0;
1418
-	found_rtpp = NULL;
1419
-
1420
-	if (rtpp_set_list == NULL) {
1421
-		return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1422
-	}
1423
-
1424
-	node = cmd_tree->node.kids;
1425
-	if (node == NULL) {
1426
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
1427
-	}
1428
-
1429
-	if (node->value.s == NULL || node->value.len ==0) {
1430
-		return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
1431
-	}
1432
-
1433
-	rtpp_url = node->value;
1434
-
1435
-	node = node->next;
1436
-	if (node != NULL) {
1437
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
1438
-	}
1439
-
1440
-	/* found a matching all - ping all rtpp */
1441
-	if (strncmp(MI_ALL, rtpp_url.s, MI_ALL_LEN) == 0) {
1442
-		found = MI_FOUND_ALL;
1443
-	}
1444
-
1445
-	lock_get(rtpp_set_list->rset_head_lock);
1446
-	for (rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
1447
-			rtpp_list = rtpp_list->rset_next) {
1448
-
1449
-		lock_get(rtpp_list->rset_lock);
1450
-		for (crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
1451
-				crt_rtpp = crt_rtpp->rn_next) {
1452
-
1453
-			if (!crt_rtpp->rn_displayed) {
1454
-				continue;
1455
-			}
1456
-
1457
-			/* found a matching rtpp - ping it */
1458
-			if (found == MI_FOUND_ALL ||
1459
-			   (crt_rtpp->rn_url.len == rtpp_url.len &&
1460
-			   strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0)) {
1461
-
1462
-				/* if ping fail */
1463
-				if (rtpp_test_ping(crt_rtpp) < 0) {
1464
-					crt_rtpp->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout);
1465
-					found_rtpp_disabled = 1;
1466
-					crt_rtpp->rn_disabled = 1;
1467
-				}
1468
-
1469
-				if (found == MI_FOUND_NONE) {
1470
-					found = MI_FOUND_ONE;
1471
-					found_rtpp = crt_rtpp;
1472
-				}
1473
-			}
1474
-		}
1475
-		lock_release(rtpp_list->rset_lock);
1476
-	}
1477
-	lock_release(rtpp_set_list->rset_head_lock);
1478
-
1479
-	root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
1480
-	if (!root) {
1481
-		LM_ERR("the MI tree cannot be initialized!\n");
1482
-		return 0;
1483
-	}
1148
+	int flag, found;
1484 1149
 
1485
-	node = &root->node;
1486
-
1487
-	switch (found) {
1488
-		case MI_FOUND_ALL:
1489
-			snode.s = MI_ALL;
1490
-			snode.len = MI_ALL_LEN;
1491
-			break;
1492
-		case MI_FOUND_ONE:
1493
-			snode.s = found_rtpp->rn_url.s;
1494
-			snode.len = found_rtpp->rn_url.len;
1495
-			break;
1496
-		default:
1497
-			if (root) {
1498
-				free_mi_tree(root);
1499
-			}
1500
-			return init_mi_tree(404, MI_RTP_PROXY_NOT_FOUND, MI_RTP_PROXY_NOT_FOUND_LEN);
1150
+	if (rpc->scan(ctx, "Sd", &rtpp_url, &flag) < 2) {
1151
+		rpc->fault(ctx, 500, "Not enough parameters");
1152
+		return;
1501 1153
 	}
1502 1154
 
1503
-	sattr.s = MI_PING;
1504
-	sattr.len = MI_PING_LEN;
1505
-
1506
-	if (found_rtpp_disabled) {
1507
-		svalue.s = MI_FAIL;
1508
-		svalue.len = MI_FAIL_LEN;
1509
-	} else {
1510
-		svalue.s = MI_SUCCESS;
1511
-		svalue.len = MI_SUCCESS_LEN;
1512
-	}
1155
+	flag = flag ? 1 : 0; /* also used as a return value */
1513 1156
 
1514
-	if (!(crt_node = add_mi_node_child(node, 0, snode.s, snode.len, 0, 0))) {
1515
-		LM_ERR("cannot add the child node to the tree\n");
1516
-		goto error;
1517
-	}
1157
+	found = rtpengine_rpc_iterate(rpc, ctx, &rtpp_url, rtpengine_iter_cb_enable, &flag);
1158
+	if (found == -1)
1159
+		return;
1518 1160
 
1519
-	if ((attr = add_mi_attr(crt_node, MI_DUP_VALUE, sattr.s, sattr.len, svalue.s, svalue.len)) == 0) {
1520
-		LM_ERR("cannot add attributes to the node\n");
1521
-		goto error;
1161
+	if (rpc->add(ctx, "{", &vh) < 0) {
1162
+		rpc->fault(ctx, 500, "Server error");
1163
+		return;
1522 1164
 	}
1523 1165
 
1524
-	return root;
1166
+	rpc->struct_add(vh, "S", "url", &rtpp_url);
1525 1167
 
1526
-error:
1527
-	if (root) {
1528
-		free_mi_tree(root);
1529
-	}
1530
-	return init_mi_tree(404, MI_ERROR, MI_ERROR_LEN);
1168
+	if (flag == 0)
1169
+		rpc->struct_add(vh, "s", "status", "disable");
1170
+	else if (flag == 1)
1171
+		rpc->struct_add(vh, "s", "status", "enable");
1172
+	else
1173
+		rpc->struct_add(vh, "s", "status", "fail");
1531 1174
 }
1532 1175
 
1533
-
1534
-static struct mi_root* mi_show_hash_total(struct mi_root* cmd_tree, void* param)
1176
+static int rtpengine_iter_cb_show(struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *ptrsp)
1535 1177
 {
1536
-	struct mi_node *node, *crt_node;
1537
-	struct mi_attr *attr;
1538
-	struct mi_root *root = NULL;
1539
-	unsigned int total;
1540
-	str total_str;
1541
-
1542
-	// Init print tree
1543
-	root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
1544
-	if (!root) {
1545
-		LM_ERR("the MI tree cannot be initialized!\n");
1546
-		return 0;
1547
-	}
1548
-	node = &root->node;
1549
-
1550
-	// Create new node and add it to the roots's kids
1551
-	if (!(crt_node = add_mi_node_child(node, MI_DUP_NAME, "total", strlen("total"), 0, 0))) {
1552
-		LM_ERR("cannot add the child node to the tree\n");
1553
-		goto error;
1554
-	}
1555
-
1556
-	// Get total number of entries
1557
-	total = rtpengine_hash_table_total();
1558
-	total_str.s = int2str(total, &total_str.len);
1559
-
1560
-	// Add node attributes
1561
-	if ((attr = add_mi_attr(crt_node, MI_DUP_VALUE, MI_HASH_ENTRIES, MI_HASH_ENTRIES_LEN, total_str.s, total_str.len)) == 0) {
1562
-		LM_ERR("cannot add attributes to the node\n");
1563
-		goto error;
1564
-	}
1178
+	if (add_rtpp_node_info(ptrsp, crt_rtpp, rtpp_list) < 0)
1179
+		return -1;
1180
+	return 0;
1181
+}
1565 1182
 
1566
-	return root;
1183
+static void rtpengine_rpc_show(rpc_t* rpc, void* ctx)
1184
+{
1185
+	str rtpp_url;
1186
+	void *ptrs[2] = {rpc, ctx};
1567 1187
 
1568
-error:
1569
-	if (root) {
1570
-		free_mi_tree(root);
1188
+	if (rpc->scan(ctx, "S", &rtpp_url) < 1) {
1189
+		rpc->fault(ctx, 500, "Not enough parameters");
1190
+		return;
1571 1191
 	}
1572 1192
 
1573
-	return init_mi_tree(404, MI_HASH_ENTRIES_FAIL, MI_HASH_ENTRIES_FAIL_LEN);
1193
+	rtpengine_rpc_iterate(rpc, ctx, &rtpp_url, rtpengine_iter_cb_show, ptrs);
1574 1194
 }
1575 1195
 
1576
-static struct mi_root*
1577
-mi_reload_rtp_proxy(struct mi_root* cmd_tree, void* param)
1196
+static int rtpengine_iter_cb_ping(struct rtpp_node *crt_rtpp, struct rtpp_set *rtpp_list, void *data)
1578 1197
 {
1579
-	struct mi_root *root = NULL;
1580
-	unsigned int current_rtpp_no;
1581
-
1582
-	if (rtpp_db_url.s == NULL) {
1583
-		// no database
1584
-		root = init_mi_tree(404, MI_DB_NOT_FOUND, MI_DB_NOT_FOUND_LEN);
1585
-		if (!root) {
1586
-			LM_ERR("the MI tree cannot be initialized!\n");
1587
-			return 0;
1588
-		}
1589
-	} else {
1590
-		if (init_rtpproxy_db() < 0) {
1591
-			// fail reloading from database
1592
-			root = init_mi_tree(404, MI_DB_ERR, MI_DB_ERR_LEN);
1593
-			if (!root) {
1594
-				LM_ERR("the MI tree cannot be initialized!\n");
1595
-				return 0;
1596
-			}
1597
-		} else {
1598
-			lock_get(rtpp_no_lock);
1599
-			current_rtpp_no = *rtpp_no;
1600
-			lock_release(rtpp_no_lock);
1601
-
1602
-			if (rtpp_socks_size != current_rtpp_no) {
1603
-				build_rtpp_socks(current_rtpp_no);
1604
-			}
1198
+	int *found_rtpp_disabled = data;
1605 1199
 
1606
-			// success reloading from database
1607
-			root = init_mi_tree(200, MI_DB_OK, MI_DB_OK_LEN);
1608
-			if (!root) {
1609
-				LM_ERR("the MI tree cannot be initialized!\n");
1610
-				return 0;
1611
-			}
1612
-		}
1200
+	/* if ping fail */
1201
+	if (rtpp_test_ping(crt_rtpp) < 0) {
1202
+		crt_rtpp->rn_recheck_ticks = get_ticks() + cfg_get(rtpengine,rtpengine_cfg,rtpengine_disable_tout);
1203
+		*found_rtpp_disabled = 1;
1204
+		crt_rtpp->rn_disabled = 1;
1613 1205
 	}
1614 1206
 
1615
-	return root;
1207
+	return 0;
1616 1208
 }
1617
-#endif
1618 1209
 
1619
-static void  rtpengine_rpc_reload(rpc_t* rpc, void* ctx)
1210
+static void rtpengine_rpc_ping(rpc_t* rpc, void* ctx)
1620 1211
 {
1621
-	unsigned int current_rtpp_no;
1212
+	void *vh;
1213
+	int found;
1214
+	int found_rtpp_disabled = 0;
1215
+	str rtpp_url;
1622 1216
 
1623
-	if (rtpp_db_url.s == NULL) {
1624
-		// no database
1625
-		rpc->fault(ctx, 500, "No Database URL");
1217
+	if (rpc->scan(ctx, "S", &rtpp_url) < 1) {
1218
+		rpc->fault(ctx, 500, "Not enough parameters");
1626 1219
 		return;
1627 1220
 	}
1628 1221
 
1629
-	if (init_rtpproxy_db() < 0) {
1630
-		// fail reloading from database
1631
-		rpc->fault(ctx, 500, "Failed reloading db");
1222
+	found = rtpengine_rpc_iterate(rpc, ctx, &rtpp_url, rtpengine_iter_cb_ping, &found_rtpp_disabled);
1223
+	if (found == -1)
1224
+		return;
1225
+
1226
+	if (rpc->add(ctx, "{", &vh) < 0) {
1227
+		rpc->fault(ctx, 500, "Server error");
1632 1228
 		return;
1633 1229
 	}
1634 1230
 
1635
-	lock_get(rtpp_no_lock);
1636
-	current_rtpp_no = *rtpp_no;
1637
-	lock_release(rtpp_no_lock);
1231
+	rpc->struct_add(vh, "Ss",
1232
+			"url", &rtpp_url,
1233
+			"status", (found_rtpp_disabled ? "fail" : "success"));
1234
+}
1638 1235
 
1639
-	if (rtpp_socks_size != current_rtpp_no) {
1640
-		build_rtpp_socks(current_rtpp_no);
1641
-	}
1236
+static void rtpengine_rpc_get_hash_total(rpc_t* rpc, void* ctx)
1237
+{
1238
+	rpc->add(ctx, "u", rtpengine_hash_table_total());
1642 1239
 }
1643 1240
 
1241
+
1644 1242
 static const char* rtpengine_rpc_reload_doc[2] = {
1645
-	"Reload rtpengine proxies.",
1646
-	0
1243
+	"Reload rtpengine proxies from database", 0
1244
+};
1245
+static const char* rtpengine_rpc_ping_doc[2] = {
1246
+	"Ping an rtpengine instance", 0
1247
+};
1248
+static const char* rtpengine_rpc_show_doc[2] = {
1249
+	"Get details about an rtpengine instance", 0
1250
+};
1251
+static const char* rtpengine_rpc_enable_doc[2] = {
1252
+	"Enable or disable an rtpengine instance", 0
1253
+};
1254
+static const char* rtpengine_rpc_get_hash_total_doc[2] = {
1255
+	"Get total number of entries in hash table", 0
1647 1256
 };
1648 1257
 
1649 1258
 rpc_export_t rtpengine_rpc[] = {
1650 1259
 	{"rtpengine.reload", rtpengine_rpc_reload, rtpengine_rpc_reload_doc, 0},
1260
+	{"rtpengine.ping", rtpengine_rpc_ping, rtpengine_rpc_ping_doc, 0},
1261
+	{"rtpengine.show", rtpengine_rpc_show, rtpengine_rpc_show_doc, RET_ARRAY},
1262
+	{"rtpengine.enable", rtpengine_rpc_enable, rtpengine_rpc_enable_doc, 0},
1263
+	{"rtpengine.get_hash_total", rtpengine_rpc_get_hash_total, rtpengine_rpc_get_hash_total_doc, 0},
1651 1264
 	{0, 0, 0, 0}
1652 1265
 };
1653 1266
 
... ...
@@ -2560,7 +2173,7 @@ rtpp_test(struct rtpp_node *node, int isdisabled, int force)
2560 2173
 	char *cp;
2561 2174
 	int ret;
2562 2175
 
2563
-	if(node->rn_recheck_ticks == MI_MAX_RECHECK_TICKS){
2176
+	if(node->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS){
2564 2177
 		LM_DBG("rtpp %s disabled for ever\n", node->rn_url.s);
2565 2178
 		return 1;
2566 2179
 	}
... ...
@@ -2937,7 +2550,7 @@ select_rtpp_node(str callid, str viabranch, int do_test, struct rtpp_node **quer
2937 2550
 
2938 2551
 	// if proper configuration and node manually or timeout disabled, return it
2939 2552
 	if (rtpengine_allow_op) {
2940
-		if (node->rn_recheck_ticks == MI_MAX_RECHECK_TICKS) {
2553
+		if (node->rn_recheck_ticks == RTPENGINE_MAX_RECHECK_TICKS) {
2941 2554
 			LM_DBG("node=%.*s for calllen=%d callid=%.*s is disabled(permanent) (probably still UP)! Return it\n",
2942 2555
 				node->rn_url.len, node->rn_url.s, callid.len, callid.len, callid.s);
2943 2556
 		} else {
... ...
@@ -27,8 +27,8 @@
27 27
 #include "../../core/str.h"
28 28
 #include "../../core/locking.h"
29 29
 
30
-#define MI_MIN_RECHECK_TICKS		0
31
-#define MI_MAX_RECHECK_TICKS		((unsigned int)-1)
30
+#define RTPENGINE_MIN_RECHECK_TICKS		0
31
+#define RTPENGINE_MAX_RECHECK_TICKS		((unsigned int)-1)
32 32
 
33 33
 enum rtpe_operation {
34 34
         OP_OFFER = 1,
... ...
@@ -111,7 +111,7 @@ static int rtpp_load_db(void)
111 111
 		weight = VAL_INT(values+2);
112 112
 		disabled = VAL_INT(values+3);
113 113
 		if (disabled) {
114
-			ticks = MI_MAX_RECHECK_TICKS;
114
+			ticks = RTPENGINE_MAX_RECHECK_TICKS;
115 115
 		} else {
116 116
 			ticks = 0;
117 117
 		}