From: Daniel-Constantin Mierla <miconda@gmail.com>
Date: Thu, 25 Aug 2022 14:51:23 +0200
Subject: [PATCH] tm: new inter-module API function t_find(...)

- combines get_t() with t_check_msg(...) to get the transaction,
  returning also if it was referenced or not
---
 src/modules/tm/t_lookup.c | 22 ++++++++++++++++++++++
 src/modules/tm/t_lookup.h |  3 +++
 src/modules/tm/tm_load.c  |  1 +
 src/modules/tm/tm_load.h  |  1 +
 4 files changed, 27 insertions(+)

diff --git a/src/modules/tm/t_lookup.c b/src/modules/tm/t_lookup.c
index bc3dd5e..09063c1 100644
--- a/src/modules/tm/t_lookup.c
+++ b/src/modules/tm/t_lookup.c
@@ -145,6 +145,28 @@ int get_t_branch()
 	return T_branch;
 }
 
+/**
+ * return the transaction by combining get() and t_check_msg()
+ * - if T is not set, checks the transactions table for msg, and if found,
+ *   sets T and *branch as well as *vref=1 to signal that T was ref'ed
+ */
+struct cell* t_find(struct sip_msg *msg, int *branch, int *vref)
+{
+	if(vref) {
+		*vref = 0;
+	}
+	if(T != NULL && T != T_UNDEFINED) {
+		return T;
+	}
+	t_check_msg(msg, branch);
+	if(T != NULL && T != T_UNDEFINED) {
+		if(vref) {
+			*vref = 1;
+		}
+	}
+	return T;
+}
+
 static inline int parse_dlg( struct sip_msg *msg )
 {
 	if (parse_headers(msg, HDR_FROM_F | HDR_CSEQ_F | HDR_TO_F, 0)==-1) {
diff --git a/src/modules/tm/t_lookup.h b/src/modules/tm/t_lookup.h
index 1f6596a..de65be8 100644
--- a/src/modules/tm/t_lookup.h
+++ b/src/modules/tm/t_lookup.h
@@ -68,6 +68,9 @@ int t_check_msg(struct sip_msg* , int *branch );
 typedef struct cell * (*tgett_f)(void);
 struct cell *get_t(void);
 
+typedef struct cell* (*tfind_f)(struct sip_msg*, int*, int*);
+struct cell* t_find(struct sip_msg *msg, int *branch, int *vref);
+
 typedef int (*tgett_branch_f)(void);
 int get_t_branch(void);
 
diff --git a/src/modules/tm/tm_load.c b/src/modules/tm/tm_load.c
index 8635b90..c9f39b5 100644
--- a/src/modules/tm/tm_load.c
+++ b/src/modules/tm/tm_load.c
@@ -92,6 +92,7 @@ int load_tm( struct tm_binds *tmb)
 	tmb->free_dlg = free_dlg;
 	tmb->print_dlg = print_dlg;
 	tmb->t_gett = get_t;
+	tmb->t_find = t_find;
 	tmb->t_gett_branch = get_t_branch;
 	tmb->t_sett = set_t;
 	tmb->calculate_hooks = w_calculate_hooks;
diff --git a/src/modules/tm/tm_load.h b/src/modules/tm/tm_load.h
index 4695a8f..1f97061 100644
--- a/src/modules/tm/tm_load.h
+++ b/src/modules/tm/tm_load.h
@@ -71,6 +71,7 @@ struct tm_binds {
 	free_dlg_f         free_dlg;
 	print_dlg_f        print_dlg;
 	tgett_f            t_gett;
+	tfind_f            t_find;
 	tgett_branch_f     t_gett_branch;
 	tsett_f            t_sett;
 	calculate_hooks_f  calculate_hooks;
