From 2f15ec965f62b1af1961d9b937422107cbed0e6d Mon Sep 17 00:00:00 2001
From: Wenpeng Song <wenpeng.song@iopsys.eu>
Date: Mon, 21 Nov 2022 10:14:58 +0000
Subject: [PATCH] Set CallLog.{i}.X_BT-COM_SIPIPAddress to the proxy address
 other than the local WAN IP

(cherry picked from commit 39421f0ad6c4a88db7b64d12a5696e7605ca3784)
---
 res/res_pjsip_session.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index f5cd037e43..6af6b8a8dc 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -4125,7 +4125,10 @@ end:
 
 		ast_channel_SIPSessionID_set(invite->session->channel, value);
 		ast_channel_sessionId_set(invite->session->channel, local->origin.id);
-		ast_channel_sipIpAddress_set(invite->session->channel, pj_strbuf(&local->origin.addr), pj_strlen(&local->origin.addr));
+		/* Log the IP that the INVITE came from as CallLog.{i}.X_Vendor_SIPIPAddress */
+		const char *addr_buf;
+		addr_buf = pj_sockaddr_print(&invite->rdata->pkt_info.src_addr, buffer, sizeof(buffer), 0);
+		ast_channel_sipIpAddress_set(invite->session->channel, addr_buf, strlen(addr_buf));
 	}
 
 	SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(invite->session));
@@ -5059,6 +5062,15 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
 		break;
 	}
 
+	if (inv->state == PJSIP_INV_STATE_CALLING && tsx->state == PJSIP_TSX_STATE_CALLING ) {
+		/* Log the IP that the INVITE been sent as CallLog.{i}.X_Vendor_SIPIPAddress */
+		const char *addr_buf;
+		char tempaddr[AST_SOCKADDR_BUFLEN];
+		addr_buf = pj_sockaddr_print(&tsx->addr, tempaddr, sizeof(tempaddr), 0);
+		ast_debug(3, "%s: INVITE sent to IP: %s \n", ast_sip_session_get_name(session), addr_buf );
+		ast_channel_sipIpAddress_set(session->channel, addr_buf, strlen(addr_buf));
+	}
+
 	if (AST_LIST_EMPTY(&session->delayed_requests)) {
 		/* No delayed request pending, so just return */
 		SCOPE_EXIT_RTN("Nothing delayed\n");
@@ -5371,7 +5383,6 @@ static struct pjmedia_sdp_session *create_local_sdp(pjsip_inv_session *inv, stru
 end:
 	if (session && session->channel) {
 		ast_channel_sessionId_set(session->channel, local->origin.id);
-		ast_channel_sipIpAddress_set(session->channel, pj_strbuf(&local->origin.addr), pj_strlen(&local->origin.addr));
 	}
 
 	SCOPE_EXIT_RTN_VALUE(local, "%s\n", ast_sip_session_get_name(session));
-- 
GitLab