From fb9f548b08ce71d3bfad1314831289e6ad650ca7 Mon Sep 17 00:00:00 2001
From: Wenpeng Song <wenpeng.song@iopsys.eu>
Date: Sat, 21 Oct 2023 14:24:13 +0000
Subject: [PATCH] Update SIPIPAddress for outgoing calls

---
 res/res_pjsip_session.c | 74 ++++++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 34 deletions(-)

diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 24cf437497..905819b848 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -2608,7 +2608,6 @@ void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_dat
 
 static pj_bool_t session_on_rx_request(pjsip_rx_data *rdata);
 static pj_bool_t session_on_rx_response(pjsip_rx_data *rdata);
-static void sipaddress_on_outgoing_inv_request(pjsip_tx_data *tdata); // check and set SIPIPAddress
 static pj_status_t session_on_tx_request(pjsip_tx_data *tdata); // early media
 static pj_status_t add_earlymedia_response_headers(pjsip_tx_data *tdata);
 static pj_status_t session_on_tx_response(pjsip_tx_data *tdata);
@@ -2624,6 +2623,44 @@ static pjsip_module session_module = {
 	.on_tx_response = add_earlymedia_response_headers,
 	.on_tx_response = session_on_tx_response,
 };
+static void log_sipipaddress_on_tx_msg(pjsip_tx_data *tdata); // check and set SIPIPAddress
+static pjsip_module log_sipipaddress_module = {
+	.name = { "log_sipipaddress_module", 23 },
+	.priority = 0,
+	.on_tx_request = log_sipipaddress_on_tx_msg,
+};
+/*!
+ * \internal
+ * Added for setting the CallLog.{i}.X_Vendor_SIPIPAddress of outgoing INVITE
+ */
+static void log_sipipaddress_on_tx_msg(pjsip_tx_data *tdata)
+{
+	SCOPE_ENTER(1, "log_sipipaddress_on_tx_msg, update SIPIPAddress\n");
+	pjsip_dialog *dlg = tdata ? pjsip_tdata_get_dlg(tdata) : NULL;
+	pjsip_inv_session *inv_session = dlg ? pjsip_dlg_get_inv_session(dlg) : NULL;
+	struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] : NULL);
+
+	if (inv_session && inv_session->invite_tsx && session && session->channel) {
+		ast_log(LOG_DEBUG, "%s: TSX State: %s  Inv State: %s\n", ast_sip_session_get_name(session),
+			pjsip_tsx_state_str(inv_session->invite_tsx->state), pjsip_inv_state_name(inv_session->state));
+
+		if (inv_session->state == PJSIP_INV_STATE_CALLING && inv_session->invite_tsx->state == PJSIP_TSX_STATE_CALLING
+			&& ast_strlen_zero(ast_channel_sipIpAddress(session->channel)) ) {
+			/* Log the IP that the INVITE been sent as CallLog.{i}.X_Vendor_SIPIPAddress */
+			/* With condition, tsx_state is calling && inv_state is calling && SIPIPAddress has no value */
+			const char *addr_buf;
+			char tempaddr[AST_SOCKADDR_BUFLEN];
+
+			addr_buf = pj_sockaddr_print(&tdata->tp_info.dst_addr, tempaddr, sizeof(tempaddr), 0);
+			ast_channel_sipIpAddress_set(session->channel, addr_buf, strlen(addr_buf));
+
+			ast_log(LOG_NOTICE, "%s:SIPIPAddress has been set to: %s\n",
+				ast_sip_session_get_name(session), ast_channel_sipIpAddress(session->channel));
+		}
+	}
+
+	SCOPE_EXIT_RTN();
+}
 
 /*! \brief Determine whether the SDP provided requires deferral of negotiating or not
  *
@@ -4451,38 +4488,6 @@ static void session_on_tsx_state(pjsip_transaction *tsx, pjsip_event *e)
 	SCOPE_EXIT_RTN();
 }
 
-/*!
- * \internal
- * Added for setting the CallLog.{i}.X_Vendor_SIPIPAddress of outgoing INVITE
- */
-static void sipaddress_on_outgoing_inv_request(pjsip_tx_data *tdata)
-{
-	SCOPE_ENTER(1, "sipaddress_on_outgoing_inv_request, update SIPIPAddress\n");
-	pjsip_dialog *dlg = tdata ? pjsip_tdata_get_dlg(tdata) : NULL;
-	pjsip_inv_session *inv_session = dlg ? pjsip_dlg_get_inv_session(dlg) : NULL;
-	struct ast_sip_session *session = (inv_session ? inv_session->mod_data[session_module.id] : NULL);
-
-	if (inv_session && inv_session->invite_tsx && session && session->channel) {
-		ast_debug(3, "%s: TSX State: %s  Inv State: %s\n", ast_sip_session_get_name(session),
-			pjsip_tsx_state_str(inv_session->invite_tsx->state), pjsip_inv_state_name(inv_session->state));
-
-		if (inv_session->state == PJSIP_INV_STATE_CALLING && inv_session->invite_tsx->state == PJSIP_TSX_STATE_CALLING
-			&& ast_strlen_zero(ast_channel_sipIpAddress(session->channel)) ) {
-			/* Log the IP that the INVITE been sent as CallLog.{i}.X_Vendor_SIPIPAddress */
-			/* With condition, tsx_state is calling && inv_state is calling && SIPIPAddress has no value */
-			const char *addr_buf;
-			char tempaddr[AST_SOCKADDR_BUFLEN];
-
-			addr_buf = pj_sockaddr_print(&tdata->tp_info.dst_addr, tempaddr, sizeof(tempaddr), 0);
-			ast_channel_sipIpAddress_set(session->channel, addr_buf, strlen(addr_buf));
-
-			ast_debug(3, "%s:SIPIPAddress has been set to: %s\n",
-				ast_sip_session_get_name(session), ast_channel_sipIpAddress(session->channel));
-		}
-	}
-	SCOPE_EXIT_RTN();
-}
-
 /*!
  * \internal
  * Added for debugging purposes
@@ -5116,7 +5121,6 @@ static void session_inv_on_tsx_state_changed(pjsip_inv_session *inv, pjsip_trans
 		 * can dig this data out again
 		 */
 		tsx->mod_data[id] = e->body.tsx_state.src.tdata->mod_data[id];
-		sipaddress_on_outgoing_inv_request(e->body.tsx_state.src.tdata); // check and set SIPIPAddress
 		break;
 	case PJSIP_EVENT_RX_MSG:
 		cb = ast_sip_mod_data_get(tsx->mod_data, id, MOD_DATA_ON_RESPONSE);
@@ -6505,6 +6509,7 @@ static int load_module(void)
 	}
 	ast_sip_register_service(&session_reinvite_module);
 	ast_sip_register_service(&outbound_invite_auth_module);
+	ast_sip_register_service(&log_sipipaddress_module);
 
 	ast_module_shutdown_ref(ast_module_info->self);
 #ifdef TEST_FRAMEWORK
@@ -6518,6 +6523,7 @@ static int unload_module(void)
 #ifdef TEST_FRAMEWORK
 	AST_TEST_UNREGISTER(test_resolve_refresh_media_states);
 #endif
+	ast_sip_unregister_service(&log_sipipaddress_module);
 	ast_sip_unregister_service(&outbound_invite_auth_module);
 	ast_sip_unregister_service(&session_reinvite_module);
 	ast_sip_unregister_service(&session_module);
-- 
GitLab