From: Victor Seva <vseva@sipwise.com>
Date: Tue, 30 Jun 2020 09:27:58 +0200
Subject: pua_dialoginfo: use lock when use_puburi_avps is set

---
 src/modules/pua_dialoginfo/pua_dialoginfo.c | 21 ++++++++++++++++-----
 src/modules/pua_dialoginfo/pua_dialoginfo.h |  2 ++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/modules/pua_dialoginfo/pua_dialoginfo.c b/src/modules/pua_dialoginfo/pua_dialoginfo.c
index c70ea02..f027f63 100644
--- a/src/modules/pua_dialoginfo/pua_dialoginfo.c
+++ b/src/modules/pua_dialoginfo/pua_dialoginfo.c
@@ -338,6 +338,7 @@ __dialog_sendpublish(struct dlg_cell *dlg, int type, struct dlg_cb_params *_para
 	if(use_pubruri_avps && (refresh_pubruri_avps_flag > -1
 		|| (request->flags & (1<<refresh_pubruri_avps_flag))))
 	{
+		lock_get(&dlginfo->lock);
 		refresh_pubruri_avps(dlginfo, &uri);
 	}
 
@@ -494,6 +495,11 @@ __dialog_sendpublish(struct dlg_cell *dlg, int type, struct dlg_cb_params *_para
 						send_publish_flag==-1?1:0);
 			}
 	}
+	if(use_pubruri_avps && (refresh_pubruri_avps_flag > -1
+		|| (request->flags & (1<<refresh_pubruri_avps_flag))))
+	{
+		lock_release(&dlginfo->lock);
+	}
 }
 
 /*
@@ -566,7 +572,11 @@ struct dlginfo_cell* get_dialog_data(struct dlg_cell *dlg, int type)
 		return NULL;
 	}
 	memset( dlginfo, 0, len);
-
+	if(use_pubruri_avps && lock_init(&dlginfo->lock) == 0) {
+		LM_ERR("cannot init the lock\n");
+		free_dlginfo_cell(dlginfo);
+		return NULL;
+	}
 	/* copy from dlg structure to dlginfo structure */
 	dlginfo->lifetime     = override_lifetime ? override_lifetime : dlg->lifetime;
 	dlginfo->from_uri.s   = (char*)dlginfo + sizeof(struct dlginfo_cell);
@@ -714,25 +724,28 @@ __dialog_created(struct dlg_cell *dlg, int type, struct dlg_cb_params *_params)
 	dlginfo=get_dialog_data(dlg, type);
 	if(dlginfo==NULL)
 		return;
-
 	if (disable_caller_publish_flag == -1 || !(request && (request->flags
 					& (1<<disable_caller_publish_flag))))
 	{
+		if(use_pubruri_avps) lock_get(&dlginfo->lock);
 		dialog_publish_multi("Trying", dlginfo->pubruris_caller,
 				&(dlg->from_uri),
 				(include_req_uri)?&(dlg->req_uri):&(dlg->to_uri),
 				&(dlg->callid), 1, dlginfo->lifetime,
 				0, 0, 0, 0, (send_publish_flag==-1)?1:0);
+		if(use_pubruri_avps) lock_release(&dlginfo->lock);
 	}
 
 	if (callee_trying && (disable_callee_publish_flag == -1 || !(request
 					&& (request->flags & (1<<disable_callee_publish_flag)))))
 	{
+		if(use_pubruri_avps) lock_get(&dlginfo->lock);
 		dialog_publish_multi("Trying", dlginfo->pubruris_callee,
 				(include_req_uri)?&(dlg->req_uri):&(dlg->to_uri),
 				&(dlg->from_uri),
 				&(dlg->callid), 0, dlginfo->lifetime,
 				0, 0, 0, 0, (send_publish_flag==-1)?1:0);
+		if(use_pubruri_avps) lock_release(&dlginfo->lock);
 	}
 }
 
@@ -856,9 +869,7 @@ void free_dlginfo_cell(void *param) {
 	free_str_list_all(cell->pubruris_caller);
 	free_str_list_all(cell->pubruris_callee);
 
-	/*if (cell->to_tag) {
-		shm_free(cell->to_tag);
-	}*/
+	if(use_pubruri_avps) lock_destroy(cell->lock);
 	shm_free(param);
 }
 
diff --git a/src/modules/pua_dialoginfo/pua_dialoginfo.h b/src/modules/pua_dialoginfo/pua_dialoginfo.h
index 01fc876..8289669 100644
--- a/src/modules/pua_dialoginfo/pua_dialoginfo.h
+++ b/src/modules/pua_dialoginfo/pua_dialoginfo.h
@@ -23,6 +23,7 @@
 
 #ifndef _PUA_DLGINFO_H
 #define _PUA_DLGINFO_H
+#include "../../core/locking.h"
 #include "../pua/pua_bind.h"
 
 extern send_publish_t pua_send_publish;
@@ -35,6 +36,7 @@ void dialog_publish_multi(char *state, struct str_list* ruris, str *entity, str
  * dlg_cell during the callback (as this could create a race condition
  * if the dlg_cell gets meanwhile deleted) */
 struct dlginfo_cell {
+	gen_lock_t lock;
 	str from_uri;
 	str to_uri;
 	str callid;
