From: Victor Seva <vseva@sipwise.com>
Date: Thu, 3 Sep 2020 12:19:21 +0200
Subject: sca: add pai_avp parameter

Use the avp value when creating P-Asserted-Identity instead of
values from Headers. If no value, skip header creation.
---
 src/modules/sca/sca.c           | 27 +++++++++++++++++++++------
 src/modules/sca/sca.h           |  2 ++
 src/modules/sca/sca_call_info.c | 26 +++++++++++++++++++-------
 src/modules/sca/sca_util.h      |  2 ++
 4 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/src/modules/sca/sca.c b/src/modules/sca/sca.c
index c0042b9..bdcc2ab 100644
--- a/src/modules/sca/sca.c
+++ b/src/modules/sca/sca.c
@@ -70,6 +70,8 @@ unsigned short from_uri_avp_type;
 int_str from_uri_avp;
 unsigned short to_uri_avp_type;
 int_str to_uri_avp;
+unsigned short pai_avp_type;
+int_str pai_avp;
 
 /*
  * PROTOTYPES
@@ -159,6 +161,7 @@ int onhold_bflag = -1;
 str server_address = STR_NULL;
 str from_uri_avp_param = STR_NULL;
 str to_uri_avp_param = STR_NULL;
+str pai_avp_param = STR_NULL;
 
 static param_export_t params[] = {
 		{"outbound_proxy", PARAM_STR, &outbound_proxy},
@@ -174,6 +177,7 @@ static param_export_t params[] = {
 		{"server_address", PARAM_STR, &server_address},
 		{"from_uri_avp", PARAM_STR, &from_uri_avp_param},
 		{"to_uri_avp", PARAM_STR, &to_uri_avp_param},
+		{"pai_avp", PARAM_STR, &pai_avp_param},
 		{NULL, 0, NULL},
 };
 
@@ -349,7 +353,7 @@ static int sca_child_init(int rank)
 	return (0);
 }
 
-static int sca_process_avps(
+static int sca_process_avp(
 		str *avp_param, int_str *avp, unsigned short *avp_type)
 {
 	pv_spec_t *avp_spec;
@@ -375,6 +379,21 @@ static int sca_process_avps(
 	return 0;
 }
 
+static int sca_process_avps()
+{
+	if(sca_process_avp(&from_uri_avp_param, &from_uri_avp, &from_uri_avp_type)
+			< 0) {
+		return -1;
+	}
+	if(sca_process_avp(&to_uri_avp_param, &to_uri_avp, &to_uri_avp_type) < 0) {
+		return -1;
+	}
+	if(sca_process_avp(&pai_avp_param, &pai_avp, &pai_avp_type) < 0) {
+		return -1;
+	}
+	return 0;
+}
+
 static int sca_mod_init(void)
 {
 	sca = (sca_mod *)shm_malloc(sizeof(sca_mod));
@@ -441,11 +460,7 @@ static int sca_mod_init(void)
 	// timer process forks in sca_child_init, above.
 	register_dummy_timers(1);
 
-	if(sca_process_avps(&from_uri_avp_param, &from_uri_avp, &from_uri_avp_type)
-					< 0
-			|| sca_process_avps(
-					   &to_uri_avp_param, &to_uri_avp, &to_uri_avp_type)
-					   < 0) {
+	if(sca_process_avps() < 0) {
 		goto error;
 	}
 
diff --git a/src/modules/sca/sca.h b/src/modules/sca/sca.h
index 3d08921..44b354e 100644
--- a/src/modules/sca/sca.h
+++ b/src/modules/sca/sca.h
@@ -63,5 +63,7 @@ extern unsigned short from_uri_avp_type;
 extern int_str from_uri_avp;
 extern unsigned short to_uri_avp_type;
 extern int_str to_uri_avp;
+extern unsigned short pai_avp_type;
+extern int_str pai_avp;
 
 #endif // SCA_H
diff --git a/src/modules/sca/sca_call_info.c b/src/modules/sca/sca_call_info.c
index d01ab86..1517a35 100644
--- a/src/modules/sca/sca_call_info.c
+++ b/src/modules/sca/sca_call_info.c
@@ -1138,20 +1138,32 @@ static int sca_call_info_insert_asserted_identity(
 	str aor = STR_NULL;
 	str hdr = STR_NULL;
 	int len;
+	int aor_avp = 0;
 	int rc = -1;
 
+	if(sca_get_avp_value(pai_avp_type, pai_avp, &aor) < 0) {
+		if(pai_avp.s.len > 0) {
+			LM_DBG("$avp(%.*s) has no value, skip PAI\n", STR_FMT(&pai_avp.s));
+			rc = 1;
+			goto done;
+		}
+		if(sca_create_canonical_aor_for_ua(msg, &aor, ua_type) < 0) {
+			LM_ERR("sca_call_info_insert_asserted_identity: failed to create "
+				   "canonical AoR\n");
+			goto done;
+		}
+	} else {
+		aor_avp = 1;
+		LM_DBG("using $avp(%.*s)[%.*s] as PAI\n", STR_FMT(&pai_avp.s),
+				STR_FMT(&aor));
+	}
+
 	anchor = anchor_lump(msg, msg->eoh - msg->buf, 0, HDR_OTHER_T);
 	if(anchor == NULL) {
 		LM_ERR("Failed to anchor lump\n");
 		goto done;
 	}
 
-	if(sca_create_canonical_aor_for_ua(msg, &aor, ua_type) < 0) {
-		LM_ERR("sca_call_info_insert_asserted_identity: failed to create "
-			   "canonical AoR\n");
-		goto done;
-	}
-
 #define SCA_P_ASSERTED_IDENTITY_HDR_PREFIX "P-Asserted-Identity: "
 #define SCA_P_ASSERTED_IDENTITY_HDR_PREFIX_LEN strlen("P-Asserted-Identity: ")
 
@@ -1197,7 +1209,7 @@ static int sca_call_info_insert_asserted_identity(
 	rc = 1;
 
 done:
-	if(aor.s != NULL) {
+	if(aor_avp == 0 && aor.s != NULL) {
 		pkg_free(aor.s);
 	}
 	if(rc < 0 && hdr.s != NULL) {
diff --git a/src/modules/sca/sca_util.h b/src/modules/sca/sca_util.h
index 563dedc..823e51d 100644
--- a/src/modules/sca/sca_util.h
+++ b/src/modules/sca/sca_util.h
@@ -76,4 +76,6 @@ int sca_create_canonical_aor(sip_msg_t *, str *);
 // convenient call hold detection
 int sca_call_is_held(sip_msg_t *);
 
+int sca_get_avp_value(unsigned short, int_str, str *);
+
 #endif // SCA_UTIL_H
