Browse code

dialplan: export dp_translate to kemi

(cherry picked from commit 2f67daa9ce945cb57b8429101569203d4b891d1c)

Stefan Mititelu authored on 14/09/2022 10:19:05 • Daniel-Constantin Mierla committed on 27/09/2022 09:51:48
Showing 1 changed files
... ...
@@ -75,6 +75,10 @@ static int dp_reload_f(struct sip_msg* msg);
75 75
 static int w_dp_replace(sip_msg_t* msg, char* pid, char* psrc, char* pdst);
76 76
 static int w_dp_match(sip_msg_t* msg, char* pid, char* psrc);
77 77
 
78
+static int ki_dp_translate(sip_msg_t* msg, int id, str *input, str *output);
79
+static int ki_dp_translate_1_args(sip_msg_t* msg, int id);
80
+static int ki_dp_translate_3_args(sip_msg_t* msg, int id, str *input, str *output);
81
+
78 82
 int dp_replace_fixup(void** param, int param_no);
79 83
 int dp_replace_fixup_free(void** param, int param_no);
80 84
 
... ...
@@ -846,11 +850,97 @@ static sr_kemi_t sr_kemi_dialplan_exports[] = {
846 850
 		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_STR,
847 851
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
848 852
 	},
853
+	{ str_init("dialplan"), str_init("dp_transalte"),
854
+		SR_KEMIP_INT, ki_dp_translate_1_args,
855
+		{ SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
856
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
857
+	},
858
+	{ str_init("dialplan"), str_init("dp_transalte"),
859
+		SR_KEMIP_INT, ki_dp_translate_3_args,
860
+		{ SR_KEMIP_INT, SR_KEMIP_STR, SR_KEMIP_STR,
861
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
862
+	},
849 863
 
850 864
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
851 865
 };
852 866
 /* clang-format on */
853 867
 
868
+static int ki_dp_translate(sip_msg_t* msg, int id, str *input_spv, str *output_spv) {
869
+	str input, output;
870
+	dpl_id_p idp;
871
+	str attrs, *outattrs;
872
+	pv_spec_t *pvs_i = NULL, *pvs_o = NULL;
873
+
874
+	if (!msg)
875
+		return -1;
876
+
877
+	if (input_spv == NULL || input_spv->s == NULL || input_spv->len <= 0 ||
878
+	    output_spv == NULL || output_spv->s == NULL || output_spv->len <= 0) {
879
+		LM_ERR("invalid destination var name for input or output\n");
880
+		return -1;
881
+	}
882
+
883
+	if (input_spv == NULL && output_spv == NULL) {
884
+		pvs_i = pv_cache_get(&default_param_s);
885
+		pvs_o = pv_cache_get(&default_param_s);
886
+	} else {
887
+		pvs_i = pv_cache_get(input_spv);
888
+		pvs_o = pv_cache_get(output_spv);
889
+	}
890
+
891
+	if (pvs_i == NULL || pvs_o == NULL) {
892
+		LM_ERR("cannot get pv spec for input or output\n");
893
+		return -1;
894
+	}
895
+
896
+	if ((pvs_i->type!=PVT_AVP && pvs_i->type!=PVT_XAVP && pvs_i->type!=PVT_SCRIPTVAR &&
897
+	    pvs_i->type!=PVT_RURI && pvs_i->type!=PVT_RURI_USERNAME) ||
898
+	    (pvs_o->type!=PVT_AVP && pvs_o->type!=PVT_XAVP && pvs_o->type!=PVT_SCRIPTVAR &&
899
+	    pvs_o->type!=PVT_RURI && pvs_o->type!=PVT_RURI_USERNAME)) {
900
+		LM_ERR("type of pv error\n");
901
+		return -1;
902
+	}
903
+
904
+	if ((idp = select_dpid(id)) ==0 ){
905
+		LM_DBG("no information available for dpid %i\n", id);
906
+		return -2;
907
+	}
908
+
909
+	/* get the input */
910
+	if (dp_get_svalue(msg, pvs_i, &input)!=0){
911
+		LM_ERR("invalid param 2\n");
912
+		return -1;
913
+	}
914
+
915
+	LM_DBG("input is %.*s\n", input.len, input.s);
916
+
917
+	outattrs = (!attr_pvar)?NULL:&attrs;
918
+	if (dp_translate_helper(msg, &input, &output, idp, outattrs)!=0) {
919
+		LM_DBG("could not translate %.*s "
920
+				"with dpid %i\n", input.len, input.s, idp->dp_id);
921
+		return -1;
922
+	}
923
+	LM_DBG("input %.*s with dpid %i => output %.*s\n",
924
+			input.len, input.s, idp->dp_id, output.len, output.s);
925
+
926
+	/* set the output */
927
+	if (dp_update(msg, pvs_o, &output, outattrs) !=0){
928
+		LM_ERR("cannot set the output\n");
929
+		return -1;
930
+	}
931
+
932
+	return 1;
933
+
934
+}
935
+
936
+static int ki_dp_translate_1_args(sip_msg_t* msg, int id) {
937
+	return ki_dp_translate(msg, id, NULL, NULL);
938
+}
939
+
940
+static int ki_dp_translate_3_args(sip_msg_t* msg, int id, str *input, str *output) {
941
+	return ki_dp_translate(msg, id, input, output);
942
+}
943
+
854 944
 /**
855 945
  *
856 946
  */