Browse code

registrar: added function reg_from_user(ultable, uri, mode)

- return true if the message came from the user identified uri parameter
by matching agains its location records

Daniel-Constantin Mierla authored on 20/05/2022 11:17:24
Showing 3 changed files
... ...
@@ -79,6 +79,8 @@ static int w_registered3(struct sip_msg* _m, char* _d, char* _uri, char* _flags)
79 79
 static int w_registered4(struct sip_msg* _m, char* _d, char* _uri, char* _flags,
80 80
 		char* _actionflags);
81 81
 static int w_reg_send_reply(sip_msg_t* _m, char* _p1, char* _p2);
82
+static int w_reg_from_user(sip_msg_t* _m, char* _ptname, char* _puri,
83
+		char *_pmode);
82 84
 
83 85
 /*! \brief Fixup functions */
84 86
 static int domain_fixup(void** param, int param_no);
... ...
@@ -210,6 +212,8 @@ static cmd_export_t cmds[] = {
210 212
 			REQUEST_ROUTE | FAILURE_ROUTE },
211 213
 	{"lookup_xavp",  (cmd_function)w_lookup_xavp,  4,  fixup_spve_all,
212 214
 			fixup_free_spve_null, ANY_ROUTE },
215
+	{"reg_from_user",  (cmd_function)w_reg_from_user,  3,  fixup_ssi,
216
+			fixup_free_ssi, ANY_ROUTE },
213 217
 	{"reg_send_reply",   (cmd_function)w_reg_send_reply,  0, 0, 0,
214 218
 			REQUEST_ROUTE | FAILURE_ROUTE },
215 219
 	{"bind_registrar",  (cmd_function)bind_registrar,  0,
... ...
@@ -560,6 +564,29 @@ static int w_lookup_xavp(sip_msg_t* _m, char* _ptname, char* _puri,
560 564
 	return ki_lookup_xavp(_m, &vtname, &vuri, &vrxname, &vcxname);
561 565
 }
562 566
 
567
+static int w_reg_from_user(sip_msg_t* _m, char* _ptname, char* _puri,
568
+		char *_pmode)
569
+{
570
+	str vtname = STR_NULL;
571
+	str vuri = STR_NULL;
572
+	int vmode = 0;
573
+
574
+	if(fixup_get_svalue(_m, (gparam_t*)_ptname, &vtname) != 0) {
575
+		LM_ERR("failed to get location table parameter\n");
576
+		return -1;
577
+	}
578
+	if(fixup_get_svalue(_m, (gparam_t*)_puri, &vuri) != 0) {
579
+		LM_ERR("failed to get uri parameter\n");
580
+		return -1;
581
+	}
582
+	if(fixup_get_ivalue(_m, (gparam_t*)_pmode, &vmode) != 0) {
583
+		LM_ERR("failed to get mode parameter\n");
584
+		return -1;
585
+	}
586
+
587
+	return ki_reg_from_user(_m, &vtname, &vuri, vmode);
588
+}
589
+
563 590
 static int w_registered(struct sip_msg* _m, char* _d, char* _uri)
564 591
 {
565 592
 	str uri = {0};
... ...
@@ -984,7 +1011,7 @@ static sr_kemi_t sr_kemi_registrar_exports[] = {
984 1011
 	},
985 1012
 	{ str_init("registrar"), str_init("lookup_xavp"),
986 1013
 		SR_KEMIP_INT, ki_lookup_xavp,
987
-		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_STR,
1014
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
988 1015
 			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
989 1016
 	},
990 1017
 	{ str_init("registrar"), str_init("registered"),
... ...
@@ -1037,6 +1064,11 @@ static sr_kemi_t sr_kemi_registrar_exports[] = {
1037 1064
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1038 1065
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1039 1066
 	},
1067
+	{ str_init("registrar"), str_init("reg_from_user"),
1068
+		SR_KEMIP_INT, ki_reg_from_user,
1069
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_INT,
1070
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1071
+	},
1040 1072
 	{ str_init("registrar"), str_init("reg_send_reply"),
1041 1073
 		SR_KEMIP_INT, ki_reg_send_reply,
1042 1074
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
... ...
@@ -33,6 +33,7 @@
33 33
 #include "../../core/mod_fix.h"
34 34
 #include "../../core/route.h"
35 35
 #include "../../core/action.h"
36
+#include "../../core/resolve.h"
36 37
 #include "../../core/fmsg.h"
37 38
 #include "../../core/kemi.h"
38 39
 #include "../../core/receive.h"
... ...
@@ -994,3 +995,88 @@ error:
994 995
 	}
995 996
 	return -1;
996 997
 }
998
+
999
+#define REG_FROM_USER_MPORT (1)
1000
+#define REG_FROM_USER_MPROTO (1<<1)
1001
+/**
1002
+ *
1003
+ */
1004
+int ki_reg_from_user(sip_msg_t* msg, str *utname, str* uri, int vmode)
1005
+{
1006
+	udomain_t* dt;
1007
+	urecord_t* r;
1008
+	ucontact_t* ptr;
1009
+	str aor = {0, 0};
1010
+	sip_uri_t rcvuri;
1011
+	int res = -1;
1012
+	int ret = -1;
1013
+	ip_addr_t ipb;
1014
+
1015
+	if (extract_aor(uri, &aor, NULL) < 0) {
1016
+		LM_ERR("failed to extract Address Of Record\n");
1017
+		return -1;
1018
+	}
1019
+
1020
+	if(ul.get_udomain(utname->s, &dt)<0) {
1021
+		LM_ERR("usrloc domain [%s] not found\n", utname->s);
1022
+		return -1;
1023
+	}
1024
+
1025
+	/* copy contacts */
1026
+	ul.lock_udomain(dt, &aor);
1027
+	res = ul.get_urecord(dt, &aor, &r);
1028
+	if (res > 0) {
1029
+		LM_DBG("'%.*s' not found in usrloc\n", aor.len, ZSW(aor.s));
1030
+		ul.unlock_udomain(dt, &aor);
1031
+		return -1;
1032
+	}
1033
+
1034
+	for(ptr = r->contacts; ptr; ptr = ptr->next) {
1035
+		if(ptr->received.s!=NULL) {
1036
+			if (parse_uri(ptr->received.s, ptr->received.len, &rcvuri) < 0) {
1037
+				LM_ERR("failed to parse rcv uri [%.*s]\n",
1038
+						ptr->received.len, ptr->received.s);
1039
+				goto error;
1040
+			}
1041
+		} else {
1042
+			if (parse_uri(ptr->c.s, ptr->c.len, &rcvuri) < 0) {
1043
+				LM_ERR("failed to parse contact uri [%.*s]\n",
1044
+						ptr->received.len, ptr->received.s);
1045
+				goto error;
1046
+			}
1047
+		}
1048
+		if(str2ipxbuf(&rcvuri.host, &ipb) < 0) {
1049
+			LM_WARN("failed to convert ip [%.*s] - ignoring\n",
1050
+					rcvuri.host.len, rcvuri.host.s);
1051
+			continue;
1052
+		}
1053
+		if(!ip_addr_cmp(&msg->rcv.src_ip, &ipb)) {
1054
+			continue;
1055
+		}
1056
+		if(vmode & REG_FROM_USER_MPORT) {
1057
+			if(msg->rcv.src_port!=rcvuri.port_no) {
1058
+				continue;
1059
+			}
1060
+		}
1061
+		if(vmode & REG_FROM_USER_MPROTO) {
1062
+			if(msg->rcv.proto!=rcvuri.proto) {
1063
+				continue;
1064
+			}
1065
+		}
1066
+		LM_DBG("matched contact record with source address [%.*s]\n",
1067
+				rcvuri.host.len, rcvuri.host.s);
1068
+		ret = 1;
1069
+		break;
1070
+	}
1071
+	ul.release_urecord(r);
1072
+	ul.unlock_udomain(dt, &aor);
1073
+
1074
+	return ret;
1075
+
1076
+error:
1077
+	ul.release_urecord(r);
1078
+	ul.unlock_udomain(dt, &aor);
1079
+	return -1;
1080
+
1081
+}
1082
+
... ...
@@ -55,5 +55,6 @@ sr_kemi_xval_t* ki_reg_ulc_cget(sip_msg_t* msg, str* rid, str* attr, int idx);
55 55
 
56 56
 int ki_lookup_xavp(sip_msg_t* msg, str *utname, str* uri,
57 57
 		str* rxname, str *cxname);
58
+int ki_reg_from_user(sip_msg_t* msg, str *utname, str* uri, int vmode);
58 59
 
59 60
 #endif