Browse code

registrar: added lookup_xavp("ultable", "uri", "rxname", "cxname")

- lookup of uri and store record and contact attributes in xavps

Daniel-Constantin Mierla authored on 12/05/2022 16:18:14
Showing 3 changed files
... ...
@@ -70,6 +70,8 @@ static int w_save3(struct sip_msg* _m, char* _d, char* _cflags, char* _uri);
70 70
 static int w_lookup(struct sip_msg* _m, char* _d, char* _p2);
71 71
 static int w_lookup_to_dset(struct sip_msg* _m, char* _d, char* _p2);
72 72
 static int w_lookup_branches(struct sip_msg* _m, char* _d, char* _p2);
73
+static int w_lookup_xavp(sip_msg_t* _m, char* _ptname, char* _puri,
74
+		char *prxname, char *pcxname);
73 75
 static int w_registered(struct sip_msg* _m, char* _d, char* _uri);
74 76
 static int w_unregister(struct sip_msg* _m, char* _d, char* _uri);
75 77
 static int w_unregister2(struct sip_msg* _m, char* _d, char* _uri, char *_ruid);
... ...
@@ -206,6 +208,8 @@ static cmd_export_t cmds[] = {
206 208
 			REQUEST_ROUTE| FAILURE_ROUTE },
207 209
 	{"lookup_branches",  (cmd_function)w_lookup_branches, 1,  domain_uri_fixup, 0,
208 210
 			REQUEST_ROUTE | FAILURE_ROUTE },
211
+	{"lookup_xavp",  (cmd_function)w_lookup_xavp,  4,  fixup_spve_all,
212
+			fixup_free_spve_null, ANY_ROUTE },
209 213
 	{"reg_send_reply",   (cmd_function)w_reg_send_reply,  0, 0, 0,
210 214
 			REQUEST_ROUTE | FAILURE_ROUTE },
211 215
 	{"bind_registrar",  (cmd_function)bind_registrar,  0,
... ...
@@ -528,6 +532,34 @@ static int ki_lookup_branches(sip_msg_t* _m, str* _dtable)
528 532
 	return lookup_branches(_m, d);
529 533
 }
530 534
 
535
+static int w_lookup_xavp(sip_msg_t* _m, char* _ptname, char* _puri,
536
+		char *_prxname, char *_pcxname)
537
+{
538
+	str vtname = STR_NULL;
539
+	str vuri = STR_NULL;
540
+	str vrxname = STR_NULL;
541
+	str vcxname = STR_NULL;
542
+
543
+	if(fixup_get_svalue(_m, (gparam_t*)_ptname, &vtname) != 0) {
544
+		LM_ERR("failed to get location table parameter\n");
545
+		return -1;
546
+	}
547
+	if(fixup_get_svalue(_m, (gparam_t*)_puri, &vuri) != 0) {
548
+		LM_ERR("failed to get uri parameter\n");
549
+		return -1;
550
+	}
551
+	if(fixup_get_svalue(_m, (gparam_t*)_prxname, &vrxname) != 0) {
552
+		LM_ERR("failed to get record xavp parameter\n");
553
+		return -1;
554
+	}
555
+	if(fixup_get_svalue(_m, (gparam_t*)_pcxname, &vcxname) != 0) {
556
+		LM_ERR("failed to get contact xavp parameter\n");
557
+		return -1;
558
+	}
559
+
560
+	return ki_lookup_xavp(_m, &vtname, &vuri, &vrxname, &vcxname);
561
+}
562
+
531 563
 static int w_registered(struct sip_msg* _m, char* _d, char* _uri)
532 564
 {
533 565
 	str uri = {0};
... ...
@@ -950,6 +982,11 @@ static sr_kemi_t sr_kemi_registrar_exports[] = {
950 982
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
951 983
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
952 984
 	},
985
+	{ str_init("registrar"), str_init("lookup_xavp"),
986
+		SR_KEMIP_INT, ki_lookup_xavp,
987
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_STR,
988
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
989
+	},
953 990
 	{ str_init("registrar"), str_init("registered"),
954 991
 		SR_KEMIP_INT, regapi_registered,
955 992
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
... ...
@@ -863,4 +863,134 @@ void reg_ul_expired_contact(ucontact_t* ptr, int type, void* param)
863 863
 	return;
864 864
 error:
865 865
 	regpv_free_profile(rpp);
866
-	return; }
866
+	return;
867
+}
868
+
869
+int ki_lookup_xavp(sip_msg_t* msg, str *utname, str* uri,
870
+		str* rxname, str *cxname)
871
+{
872
+	udomain_t* dt;
873
+	urecord_t* r;
874
+	ucontact_t* ptr;
875
+	str aor = {0, 0};
876
+	sr_xavp_t *rxavp=NULL;
877
+	sr_xavp_t *cxavp=NULL;
878
+	sr_xavp_t *pxavp=NULL;
879
+	sr_xval_t nxval;
880
+	str fxname = {0, 0};
881
+	int res;
882
+	int n;
883
+
884
+	if (extract_aor(uri, &aor, NULL) < 0) {
885
+		LM_ERR("failed to extract Address Of Record\n");
886
+		return -1;
887
+	}
888
+
889
+	if(ul.get_udomain(utname->s, &dt)<0) {
890
+		LM_ERR("usrloc domain [%s] not found\n", utname->s);
891
+		return -1;
892
+	}
893
+
894
+	/* add record aor field */
895
+	memset(&nxval, 0, sizeof(sr_xval_t));
896
+	nxval.type = SR_XTYPE_STR;
897
+	nxval.v.s = aor;
898
+	STR_STATIC_SET(fxname, "aor");
899
+	if(xavp_add_value(&fxname, &nxval, &rxavp)==NULL) {
900
+		LM_ERR("failed to add xavp %.*s field\n", fxname.len, fxname.s);
901
+		return -1;
902
+	}
903
+
904
+	/* copy contacts */
905
+	ul.lock_udomain(dt, &aor);
906
+	res = ul.get_urecord(dt, &aor, &r);
907
+	if (res > 0) {
908
+		LM_DBG("'%.*s' not found in usrloc\n", aor.len, ZSW(aor.s));
909
+		ul.unlock_udomain(dt, &aor);
910
+		xavp_destroy_list(&rxavp);
911
+		return -1;
912
+	}
913
+
914
+	ptr = r->contacts;
915
+	n = 0;
916
+	while(ptr) {
917
+		/* add record uri field */
918
+		memset(&nxval, 0, sizeof(sr_xval_t));
919
+		nxval.type = SR_XTYPE_STR;
920
+		nxval.v.s = ptr->c;
921
+		STR_STATIC_SET(fxname, "uri");
922
+		if(xavp_add_value(&fxname, &nxval, &cxavp)==NULL) {
923
+			LM_ERR("failed to add xavp %.*s field\n", fxname.len, fxname.s);
924
+			goto error;
925
+		}
926
+		if(ptr->sock) {
927
+			memset(&nxval, 0, sizeof(sr_xval_t));
928
+			nxval.type = SR_XTYPE_STR;
929
+			nxval.v.s = ptr->sock->sock_str;
930
+			STR_STATIC_SET(fxname, "socket");
931
+			if(xavp_add_value(&fxname, &nxval, &cxavp)==NULL) {
932
+				LM_ERR("failed to add xavp %.*s field\n", fxname.len, fxname.s);
933
+				goto error;
934
+			}
935
+		}
936
+		if(ptr->received.s!=NULL) {
937
+			memset(&nxval, 0, sizeof(sr_xval_t));
938
+			nxval.type = SR_XTYPE_STR;
939
+			nxval.v.s = ptr->received;
940
+			STR_STATIC_SET(fxname, "socket");
941
+			if(xavp_add_value(&fxname, &nxval, &cxavp)==NULL) {
942
+				LM_ERR("failed to add xavp %.*s field\n", fxname.len, fxname.s);
943
+				goto error;
944
+			}
945
+		}
946
+		/* add cxavp in root list */
947
+		memset(&nxval, 0, sizeof(sr_xval_t));
948
+		nxval.type = SR_XTYPE_XAVP;
949
+		nxval.v.xavp = cxavp;
950
+		if((pxavp = xavp_add_value_after(cxname, &nxval, pxavp))==NULL) {
951
+			LM_ERR("cannot add dst xavp to root list\n");
952
+			goto error;
953
+		}
954
+		cxavp = NULL;
955
+		n++;
956
+		ptr = ptr->next;
957
+	}
958
+	ul.release_urecord(r);
959
+	ul.unlock_udomain(dt, &aor);
960
+
961
+	/* add record count field */
962
+	memset(&nxval, 0, sizeof(sr_xval_t));
963
+	nxval.type = SR_XTYPE_INT;
964
+	nxval.v.i = n;
965
+	STR_STATIC_SET(fxname, "count");
966
+	if(xavp_add_value(&fxname, &nxval, &rxavp)==NULL) {
967
+		LM_ERR("failed to add xavp %.*s field\n", fxname.len, fxname.s);
968
+		xavp_destroy_list(&rxavp);
969
+		return -1;
970
+	}
971
+
972
+	/* add rxavp in root list */
973
+	memset(&nxval, 0, sizeof(sr_xval_t));
974
+	nxval.type = SR_XTYPE_XAVP;
975
+	nxval.v.xavp = rxavp;
976
+	if((pxavp = xavp_add_value(rxname, &nxval, NULL))==NULL) {
977
+		LM_ERR("cannot add rxavp to root list\n");
978
+		xavp_destroy_list(&rxavp);
979
+		return -1;
980
+	}
981
+
982
+	LM_DBG("fetched <%d> contacts for <%.*s> in [%.*s]\n",
983
+			n, aor.len, aor.s, cxname->len, cxname->s);
984
+	return 1;
985
+
986
+error:
987
+	ul.release_urecord(r);
988
+	ul.unlock_udomain(dt, &aor);
989
+	if(cxavp!=NULL) {
990
+		xavp_destroy_list(&cxavp);
991
+	}
992
+	if(rxavp!=NULL) {
993
+		xavp_destroy_list(&rxavp);
994
+	}
995
+	return -1;
996
+}
... ...
@@ -53,4 +53,7 @@ void reg_ul_expired_contact(ucontact_t* c, int type, void* param);
53 53
 sr_kemi_xval_t* ki_reg_ulc_rget(sip_msg_t* msg, str* rid, str* attr);
54 54
 sr_kemi_xval_t* ki_reg_ulc_cget(sip_msg_t* msg, str* rid, str* attr, int idx);
55 55
 
56
+int ki_lookup_xavp(sip_msg_t* msg, str *utname, str* uri,
57
+		str* rxname, str *cxname);
58
+
56 59
 #endif