Browse code

rtpproxy: implemented rpc commands rtpproxy.enable and rtpproxy.list

- removed mi commands

Daniel-Constantin Mierla authored on 03/01/2017 11:17:45
Showing 3 changed files
... ...
@@ -1,5 +1,5 @@
1 1
 #
2
-# rtpproxy example module makefile
2
+# rtpproxy module makefile
3 3
 #
4 4
 #
5 5
 # WARNING: do not run this directly, it should be run by the master Makefile
... ...
@@ -12,7 +12,6 @@ LIBS=
12 12
 DEFS+=-DKAMAILIO_MOD_INTERFACE
13 13
 
14 14
 SERLIBPATH=../../lib
15
-SER_LIBS+=$(SERLIBPATH)/kmi/kmi
16 15
 SER_LIBS+=$(SERLIBPATH)/srdb1/srdb1
17 16
 include ../../Makefile.modules
18 17
 
... ...
@@ -857,9 +857,9 @@ start_recording();
857 857
 	</section>
858 858
 
859 859
 	<section>
860
-		<title><acronym>MI</acronym> Commands</title>
861
-		<section>
862
-			<title><function moreinfo="none">nh_enable_rtpp</function></title>
860
+		<title>RPC Commands</title>
861
+		<section id="rtpproxy.r.enable">
862
+			<title>rtpproxy.enable</title>
863 863
 			<para>
864 864
 			Enables a rtp proxy if parameter value is greater than 0.
865 865
 			Disables it if a zero value is given.
... ...
@@ -877,17 +877,17 @@ start_recording();
877 877
 			</para>
878 878
 			<example>
879 879
 			<title>
880
-			<function moreinfo="none">nh_enable_rtpp</function> usage</title>
880
+			<function moreinfo="none">rtpproxy.enable</function> usage</title>
881 881
 			<programlisting format="linespecific">
882 882
 ...
883
-$ &ctltool; fifo nh_enable_rtpp udp:192.168.2.133:8081 0
883
+$ &kamcmd; rtpproxy.enable udp:192.168.2.133:8081 0
884 884
 ...
885 885
 			</programlisting>
886 886
 			</example>
887 887
 		</section>
888 888
 
889
-			<section>
890
-			<title><function moreinfo="none">nh_show_rtpp</function></title>
889
+			<section id="rtpproxy.r.list">
890
+			<title><function moreinfo="none">rtpproxy.list</function></title>
891 891
 			<para>
892 892
 			Displays all the rtp proxies and their information: set and
893 893
 			status (disabled or not, weight and recheck_ticks).
... ...
@@ -897,10 +897,10 @@ $ &ctltool; fifo nh_enable_rtpp udp:192.168.2.133:8081 0
897 897
 			</para>
898 898
 			<example>
899 899
 			<title>
900
-				<function moreinfo="none">nh_show_rtpp</function> usage</title>
900
+				<function moreinfo="none">rtpproxy.list</function> usage</title>
901 901
 			<programlisting format="linespecific">
902 902
 ...
903
-$ &ctltool; fifo nh_show_rtpp
903
+$ &kamcmd; rtpproxy.list
904 904
 ...
905 905
 			</programlisting>
906 906
 			</example>
... ...
@@ -908,4 +908,3 @@ $ &ctltool; fifo nh_show_rtpp
908 908
 	</section>
909 909
 
910 910
 </chapter>
911
-
... ...
@@ -63,7 +63,8 @@
63 63
 #include "../../core/ut.h"
64 64
 #include "../../core/pt.h"
65 65
 #include "../../core/timer_proc.h"
66
-#include "../../lib/kmi/mi.h"
66
+#include "../../core/rpc.h"
67
+#include "../../core/rpc_lookup.h"
67 68
 #include "../../core/pvar.h"
68 69
 #include "../../core/lvalue.h"
69 70
 #include "../../core/msg_translator.h"
... ...
@@ -98,30 +99,10 @@ MODULE_VERSION
98 99
 #define DEFAULT_RTPP_SET_ID		0
99 100
 static str DEFAULT_RTPP_SET_ID_STR = str_init("0");
100 101
 
101
-#define MI_SET_NATPING_STATE		"nh_enable_ping"
102
-#define MI_DEFAULT_NATPING_STATE	1
103
-
104
-#define MI_ENABLE_RTP_PROXY			"nh_enable_rtpp"
105
-#define MI_MIN_RECHECK_TICKS		0
106
-#define MI_MAX_RECHECK_TICKS		(unsigned int)-1
107
-
108
-#define MI_SHOW_RTP_PROXIES			"nh_show_rtpp"
109
-
110
-#define MI_RTP_PROXY_NOT_FOUND		"RTP proxy not found"
111
-#define MI_RTP_PROXY_NOT_FOUND_LEN	(sizeof(MI_RTP_PROXY_NOT_FOUND)-1)
112
-#define MI_PING_DISABLED			"NATping disabled from script"
113
-#define MI_PING_DISABLED_LEN		(sizeof(MI_PING_DISABLED)-1)
114
-#define MI_SET						"set"
115
-#define MI_SET_LEN					(sizeof(MI_SET)-1)
116
-#define MI_INDEX					"index"
117
-#define MI_INDEX_LEN				(sizeof(MI_INDEX)-1)
118
-#define MI_DISABLED					"disabled"
119
-#define MI_DISABLED_LEN				(sizeof(MI_DISABLED)-1)
120
-#define MI_WEIGHT					"weight"
121
-#define MI_WEIGHT_LEN				(sizeof(MI_WEIGHT)-1)
122
-#define MI_RECHECK_TICKS			"recheck_ticks"
123
-#define MI_RECHECK_T_LEN			(sizeof(MI_RECHECK_TICKS)-1)
102
+#define RPC_DEFAULT_NATPING_STATE	1
124 103
 
104
+#define RPC_MIN_RECHECK_TICKS		0
105
+#define RPC_MAX_RECHECK_TICKS		(unsigned int)-1
125 106
 
126 107
 
127 108
 /* Supported version of the RTP proxy command protocol */
... ...
@@ -166,13 +147,6 @@ static void mod_destroy(void);
166 147
 /* Pseudo-Variables */
167 148
 static int pv_get_rtpstat_f(struct sip_msg *, pv_param_t *, pv_value_t *);
168 149
 
169
-/*mi commands*/
170
-static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
171
-		void* param );
172
-static struct mi_root* mi_show_rtpproxies(struct mi_root* cmd_tree,
173
-		void* param);
174
-
175
-
176 150
 static int rtpproxy_disable_tout = 60;
177 151
 static int rtpproxy_retr = 5;
178 152
 static int rtpproxy_tout = 1;
... ...
@@ -298,20 +272,13 @@ static param_export_t params[] = {
298 272
 	{0, 0, 0}
299 273
 };
300 274
 
301
-static mi_export_t mi_cmds[] = {
302
-	{MI_ENABLE_RTP_PROXY,     mi_enable_rtp_proxy,  0,                0, 0},
303
-	{MI_SHOW_RTP_PROXIES,     mi_show_rtpproxies,   MI_NO_INPUT_FLAG, 0, 0},
304
-	{ 0, 0, 0, 0, 0}
305
-};
306
-
307
-
308 275
 struct module_exports exports = {
309 276
 	"rtpproxy",
310 277
 	DEFAULT_DLFLAGS, /* dlopen flags */
311 278
 	cmds,
312 279
 	params,
313 280
 	0,           /* exported statistics */
314
-	mi_cmds,     /* exported MI functions */
281
+	0,           /* exported MI functions */
315 282
 	mod_pvs,     /* exported pseudo-variables */
316 283
 	0,           /* extra processes */
317 284
 	mod_init,
... ...
@@ -426,7 +393,7 @@ int insert_rtpp_node(struct rtpp_set *const rtpp_list, const str *const url, con
426 393
 	pnode->rn_umode = 0;
427 394
 	pnode->rn_disabled = disabled;
428 395
 	/* Permanently disable if marked as disabled */
429
-	pnode->rn_recheck_ticks = disabled ? MI_MAX_RECHECK_TICKS : 0;
396
+	pnode->rn_recheck_ticks = disabled ? RPC_MAX_RECHECK_TICKS : 0;
430 397
 	pnode->rn_url.s = (char*)(pnode + 1);
431 398
 	memcpy(pnode->rn_url.s, url->s, url->len);
432 399
 	pnode->rn_url.len = url->len;
... ...
@@ -605,51 +572,38 @@ static int fixup_set_id(void ** param, int param_no)
605 572
 	return 0;
606 573
 }
607 574
 
608
-static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
609
-		void* param )
610
-{	struct mi_node* node;
575
+static void  rtpproxy_rpc_enable(rpc_t* rpc, void* ctx)
576
+{
611 577
 	str rtpp_url;
612
-	unsigned int enable;
613
-	struct rtpp_set * rtpp_list;
614
-	struct rtpp_node * crt_rtpp;
578
+	int enable;
579
+	struct rtpp_set *rtpp_list;
580
+	struct rtpp_node *crt_rtpp;
615 581
 	int found;
616 582
 
617 583
 	found = 0;
584
+	enable = 0;
618 585
 
619 586
 	if(rtpp_set_list ==NULL)
620 587
 		goto end;
621 588
 
622
-	node = cmd_tree->node.kids;
623
-	if(node == NULL)
624
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
625
-
626
-	if(node->value.s == NULL || node->value.len ==0)
627
-		return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
628
-
629
-	rtpp_url = node->value;
630
-
631
-	node = node->next;
632
-	if(node == NULL)
633
-		return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN);
634
-
635
-	enable = 0;
636
-	if( strno2int( &node->value, &enable) <0)
637
-		goto error;
589
+	if (rpc->scan(ctx, "Sd", &rtpp_url, &enable) < 2) {
590
+		rpc->fault(ctx, 500, "Not enough parameters");
591
+		return;
592
+	}
638 593
 
639 594
 	for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
640
-			rtpp_list = rtpp_list->rset_next){
595
+			rtpp_list = rtpp_list->rset_next) {
641 596
 
642 597
 		for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
643
-				crt_rtpp = crt_rtpp->rn_next){
598
+				crt_rtpp = crt_rtpp->rn_next) {
644 599
 			/*found a matching rtpp*/
600
+			if(crt_rtpp->rn_url.len == rtpp_url.len) {
645 601
 
646
-			if(crt_rtpp->rn_url.len == rtpp_url.len){
647
-
648
-				if(strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0){
602
+				if(strncmp(crt_rtpp->rn_url.s, rtpp_url.s, rtpp_url.len) == 0) {
649 603
 					/*set the enabled/disabled status*/
650 604
 					found = 1;
651 605
 					crt_rtpp->rn_recheck_ticks =
652
-						enable? MI_MIN_RECHECK_TICKS : MI_MAX_RECHECK_TICKS;
606
+						enable? RPC_MIN_RECHECK_TICKS : RPC_MAX_RECHECK_TICKS;
653 607
 					crt_rtpp->rn_disabled = enable?0:1;
654 608
 				}
655 609
 			}
... ...
@@ -657,97 +611,74 @@ static struct mi_root* mi_enable_rtp_proxy(struct mi_root* cmd_tree,
657 611
 	}
658 612
 
659 613
 end:
660
-	if(found)
661
-		return init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
662
-	return init_mi_tree(404,MI_RTP_PROXY_NOT_FOUND,MI_RTP_PROXY_NOT_FOUND_LEN);
663
-error:
664
-	return init_mi_tree( 400, MI_BAD_PARM_S, MI_BAD_PARM_LEN);
614
+	if(!found) {
615
+		rpc->fault(ctx, 404, "RTPProxy not found");
616
+		return;
617
+	}
665 618
 }
666 619
 
667 620
 
668
-
669
-#define add_rtpp_node_int_info(_parent, _name, _name_len, _value, _child,\
670
-		_len, _string, _error)\
671
-do {\
672
-	(_string) = int2str((_value), &(_len));\
673
-	if((_string) == 0){\
674
-		LM_ERR("cannot convert int value\n");\
675
-		goto _error;\
676
-	}\
677
-	if(((_child) = add_mi_node_child((_parent), MI_DUP_VALUE, (_name), \
678
-					(_name_len), (_string), (_len))   ) == 0)\
679
-	goto _error;\
680
-}while(0);
681
-
682
-static struct mi_root* mi_show_rtpproxies(struct mi_root* cmd_tree,
683
-		void* param)
621
+static void  rtpproxy_rpc_list(rpc_t* rpc, void* ctx)
684 622
 {
685
-	struct mi_node* node, *crt_node, *child;
686
-	struct mi_root* root;
687
-	struct mi_attr * attr;
688
-	struct rtpp_set * rtpp_list;
689
-	struct rtpp_node * crt_rtpp;
690
-	char * string, *id;
691
-	int id_len, len;
623
+	struct rtpp_set *rtpp_list;
624
+	struct rtpp_node *crt_rtpp;
625
+	char *string, *id;
626
+	int id_len;
627
+	int len;
628
+	void *vh;
692 629
 
693 630
 	string = id = 0;
694 631
 
695
-	root = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
696
-	if (!root) {
697
-		LM_ERR("the MI tree cannot be initialized!\n");
698
-		return 0;
699
-	}
700
-
701 632
 	if(rtpp_set_list ==NULL)
702
-		return root;
703
-
704
-	node = &root->node;
633
+		return;
705 634
 
706 635
 	for(rtpp_list = rtpp_set_list->rset_first; rtpp_list != NULL;
707
-			rtpp_list = rtpp_list->rset_next){
636
+			rtpp_list = rtpp_list->rset_next) {
708 637
 
709 638
 		for(crt_rtpp = rtpp_list->rn_first; crt_rtpp != NULL;
710
-				crt_rtpp = crt_rtpp->rn_next){
639
+				crt_rtpp = crt_rtpp->rn_next) {
711 640
 
712
-			id =  int2str(rtpp_list->id_set, &id_len);
713
-			if(!id){
714
-				LM_ERR("cannot convert set id\n");
715
-				goto error;
641
+			if (rpc->add(ctx, "{", &vh) < 0) {
642
+				rpc->fault(ctx, 500, "Server error");
643
+				return;
716 644
 			}
645
+			rpc->struct_add(vh, "dSdddd",
646
+				"setid", rtpp_list->id_set,
647
+				"url", &crt_rtpp->rn_url,
648
+				"index", crt_rtpp->idx,
649
+				"disabled", crt_rtpp->rn_disabled,
650
+				"weight", crt_rtpp->rn_weight,
651
+				"recheck", crt_rtpp->rn_recheck_ticks);
652
+		}
653
+	}
654
+}
717 655
 
718
-			if(!(crt_node = add_mi_node_child(node, 0, crt_rtpp->rn_url.s,
719
-							crt_rtpp->rn_url.len, 0,0)) ) {
720
-				LM_ERR("cannot add the child node to the tree\n");
721
-				goto error;
722
-			}
656
+static const char* rtpproxy_rpc_enable_doc[2] = {
657
+	"Set state (enable/disable) for a rtp proxy.",
658
+	0
659
+};
723 660
 
724
-			LM_DBG("adding node name %s \n",crt_rtpp->rn_url.s );
661
+static const char* rtpproxy_rpc_list_doc[2] = {
662
+	"List rtp proxies.",
663
+	0
664
+};
725 665
 
726
-			if((attr = add_mi_attr(crt_node, MI_DUP_VALUE, MI_SET, MI_SET_LEN,
727
-							id, id_len))== 0){
728
-				LM_ERR("cannot add attributes to the node\n");
729
-				goto error;
730
-			}
666
+rpc_export_t rtpproxy_rpc[] = {
667
+	{"rtpproxy.list", rtpproxy_rpc_list, rtpproxy_rpc_list_doc, RET_ARRAY},
668
+	{"rtpproxy.enable", rtpproxy_rpc_enable, rtpproxy_rpc_enable_doc, 0},
669
+	{0, 0, 0, 0}
670
+};
731 671
 
732
-			add_rtpp_node_int_info(crt_node, MI_INDEX, MI_INDEX_LEN,
733
-					crt_rtpp->idx, child, len,string,error);
734
-			add_rtpp_node_int_info(crt_node, MI_DISABLED, MI_DISABLED_LEN,
735
-					crt_rtpp->rn_disabled, child, len,string,error);
736
-			add_rtpp_node_int_info(crt_node, MI_WEIGHT, MI_WEIGHT_LEN,
737
-					crt_rtpp->rn_weight,  child, len, string,error);
738
-			add_rtpp_node_int_info(crt_node, MI_RECHECK_TICKS,MI_RECHECK_T_LEN,
739
-					crt_rtpp->rn_recheck_ticks, child, len, string, error);
740
-		}
672
+static int rtpproxy_rpc_init(void)
673
+{
674
+	if (rpc_register_array(rtpproxy_rpc)!=0)
675
+	{
676
+		LM_ERR("failed to register RPC commands\n");
677
+		return -1;
741 678
 	}
742
-
743
-	return root;
744
-error:
745
-	if (root)
746
-		free_mi_tree(root);
747 679
 	return 0;
748 680
 }
749 681
 
750
-
751 682
 	static int
752 683
 mod_init(void)
753 684
 {
... ...
@@ -756,9 +687,9 @@ mod_init(void)
756 687
 	str s;
757 688
 	unsigned short avp_flags;
758 689
 
759
-	if(register_mi_mod(exports.name, mi_cmds)!=0)
690
+	if(rtpproxy_rpc_init()<0)
760 691
 	{
761
-		LM_ERR("failed to register MI commands\n");
692
+		LM_ERR("failed to register RPC commands\n");
762 693
 		return -1;
763 694
 	}
764 695
 
... ...
@@ -1424,7 +1355,7 @@ rtpp_test(struct rtpp_node *node, int isdisabled, int force)
1424 1355
 	char *cp;
1425 1356
 	struct iovec v[2] = {{NULL, 0}, {"V", 1}};
1426 1357
 
1427
-	if(node->rn_recheck_ticks == MI_MAX_RECHECK_TICKS){
1358
+	if(node->rn_recheck_ticks == RPC_MAX_RECHECK_TICKS){
1428 1359
 		LM_DBG("rtpp %s disabled for ever\n", node->rn_url.s);
1429 1360
 		return 1;
1430 1361
 	}