Browse code

carrierroute: exported cr_user_carrier() to kemi

Daniel-Constantin Mierla authored on 30/09/2022 09:35:53
Showing 3 changed files
... ...
@@ -39,6 +39,7 @@
39 39
 #include "../../core/str.h"
40 40
 #include "../../core/mem/mem.h"
41 41
 #include "../../core/ut.h" /* for user2uid() */
42
+#include "../../core/kemi.h"
42 43
 #include "carrierroute.h"
43 44
 #include "cr_fixup.h"
44 45
 #include "cr_map.h"
... ...
@@ -270,3 +271,28 @@ static void mod_destroy(void) {
270 271
 	}
271 272
 	destroy_route_data();
272 273
 }
274
+
275
+
276
+/**
277
+ *
278
+ */
279
+/* clang-format off */
280
+static sr_kemi_t sr_kemi_carrierroute_exports[] = {
281
+	{ str_init("carrierroute"), str_init("cr_user_carrier"),
282
+		SR_KEMIP_INT, ki_cr_load_user_carrier,
283
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
284
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
285
+	},
286
+
287
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
288
+};
289
+/* clang-format on */
290
+
291
+/**
292
+ *
293
+ */
294
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
295
+{
296
+	sr_kemi_modules_add(sr_kemi_carrierroute_exports);
297
+	return 0;
298
+}
... ...
@@ -722,6 +722,48 @@ unlock_and_out:
722 722
 }
723 723
 
724 724
 
725
+/**
726
+ *
727
+ */
728
+int ki_cr_load_user_carrier_helper(struct sip_msg *_msg,
729
+		str *user, str *domain, pv_spec_t *dvar) {
730
+	pv_value_t val = {0};
731
+
732
+	/* get carrier id */
733
+	if ((val.ri = load_user_carrier(user, domain)) < 0) {
734
+		LM_ERR("error in load user carrier");
735
+		return -1;
736
+	} else {
737
+		/* set var */
738
+		val.flags = PV_VAL_INT|PV_TYPE_INT;
739
+		if(dvar->setf(_msg, &dvar->pvp, (int)EQ_T, &val)<0) {
740
+			LM_ERR("failed setting dst var\n");
741
+			return -1;
742
+		}
743
+	}
744
+	return 1;
745
+}
746
+
747
+/**
748
+ *
749
+ */
750
+int ki_cr_load_user_carrier(struct sip_msg *_msg,
751
+		str *user, str *domain, str *dstvar) {
752
+	pv_spec_t *dst;
753
+
754
+	dst = pv_cache_get(dstvar);
755
+	if(dst==NULL) {
756
+		LM_ERR("failed to get pv spec for: %.*s\n", dstvar->len, dstvar->s);
757
+		return -1;
758
+	}
759
+	if(dst->setf==NULL) {
760
+		LM_ERR("target pv is not writable: %.*s\n", dstvar->len, dstvar->s);
761
+		return -1;
762
+	}
763
+
764
+	return ki_cr_load_user_carrier_helper(_msg, user, domain, dst);
765
+}
766
+
725 767
 /**
726 768
  * Loads user carrier from subscriber table and stores it in an AVP.
727 769
  *
... ...
@@ -735,8 +777,6 @@ unlock_and_out:
735 777
 int cr_load_user_carrier(struct sip_msg * _msg,
736 778
 		char *_user, char *_domain, char *_dstvar) {
737 779
 	str user, domain;
738
-	pv_spec_t *dst;
739
-	pv_value_t val = {0};
740 780
 
741 781
 	if (fixup_get_svalue(_msg, (gparam_t*)_user, &user)<0) {
742 782
 		LM_ERR("cannot print the user\n");
... ...
@@ -747,22 +787,10 @@ int cr_load_user_carrier(struct sip_msg * _msg,
747 787
 		LM_ERR("cannot print the domain\n");
748 788
 		return -1;
749 789
 	}
750
-	/* get carrier id */
751
-	if ((val.ri = load_user_carrier(&user, &domain)) < 0) {
752
-		LM_ERR("error in load user carrier");
753
-		return -1;
754
-	} else {
755
-		/* set var */
756
-		dst = (pv_spec_t *)_dstvar;
757
-		val.flags = PV_VAL_INT|PV_TYPE_INT;
758
-		if(dst->setf(_msg, &dst->pvp, (int)EQ_T, &val)<0) {
759
-			LM_ERR("failed setting dst var\n");
760
-			return -1;
761
-		}
762
-	}
763
-	return 1;
764
-}
765 790
 
791
+	return ki_cr_load_user_carrier_helper(_msg, &user, &domain,
792
+			(pv_spec_t*)_dstvar);
793
+}
766 794
 
767 795
 /**
768 796
  * rewrites the request URI of msg after determining the
... ...
@@ -49,6 +49,9 @@ int cr_load_user_carrier(struct sip_msg * _msg, char *_user,
49 49
 		char *_domain, char *_dstavp);
50 50
 
51 51
 
52
+int ki_cr_load_user_carrier(struct sip_msg *_msg,
53
+		str *user, str *domain, str *dstvar);
54
+
52 55
 /**
53 56
  * rewrites the request URI of msg after determining the
54 57
  * new destination URI