From: Daniel-Constantin Mierla <miconda@gmail.com>
Date: Tue, 30 Aug 2022 10:53:15 +0200
Subject: [PATCH] tm: added t_unset() intermodule API function

- reset global t and branch without releasing the transaction
- useful for cases when global t needs to be set in callbacks and then
  reset before config execution is finished
---
 src/modules/tm/t_lookup.c | 13 +++++++++++++
 src/modules/tm/t_lookup.h |  3 +++
 src/modules/tm/tm_load.c  |  1 +
 src/modules/tm/tm_load.h  |  1 +
 4 files changed, 18 insertions(+)

diff --git a/src/modules/tm/t_lookup.c b/src/modules/tm/t_lookup.c
index 09063c1..4936c30 100644
--- a/src/modules/tm/t_lookup.c
+++ b/src/modules/tm/t_lookup.c
@@ -167,6 +167,19 @@ struct cell* t_find(struct sip_msg *msg, int *branch, int *vref)
 	return T;
 }
 
+/**
+ * unref and reset T globals
+ */
+void t_unset(void)
+{
+	if(T == NULL || T == T_UNDEFINED) {
+		return;
+	}
+
+	UNREF( T );
+	set_t(T_UNDEFINED, T_BR_UNDEFINED);
+}
+
 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 de65be8..c4b5404 100644
--- a/src/modules/tm/t_lookup.h
+++ b/src/modules/tm/t_lookup.h
@@ -71,6 +71,9 @@ 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 void (*tunset_f)(void);
+void t_unset(void);
+
 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 c9f39b5..d5fc6e7 100644
--- a/src/modules/tm/tm_load.c
+++ b/src/modules/tm/tm_load.c
@@ -93,6 +93,7 @@ int load_tm( struct tm_binds *tmb)
 	tmb->print_dlg = print_dlg;
 	tmb->t_gett = get_t;
 	tmb->t_find = t_find;
+	tmb->t_unset = t_unset;
 	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 1f97061..130d015 100644
--- a/src/modules/tm/tm_load.h
+++ b/src/modules/tm/tm_load.h
@@ -72,6 +72,7 @@ struct tm_binds {
 	print_dlg_f        print_dlg;
 	tgett_f            t_gett;
 	tfind_f            t_find;
+	tunset_f           t_unset;
 	tgett_branch_f     t_gett_branch;
 	tsett_f            t_sett;
 	calculate_hooks_f  calculate_hooks;
