Browse code

uac: use get_dlg_varstatus() to check if var exists

Daniel-Constantin Mierla authored on 26/09/2022 13:58:15
Showing 1 changed files
... ...
@@ -391,7 +391,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
391 391
 
392 392
 	if (dlg) {
393 393
 		dlgvar_names = (uac_flag==FL_USE_UAC_FROM)?from_dlgvar:to_dlgvar;
394
-		if(dlg_api.get_dlg_varref(dlg, &dlgvar_names[0])) {
394
+		if(dlg_api.get_dlg_varstatus(dlg, &dlgvar_names[0])) {
395 395
 
396 396
 			LM_INFO("Already called uac_replace for this dialog\n");
397 397
 			/* delete the from_new dlg var */
Browse code

uac: use dlg api functions get_dlg_varval() and get_dlg_varref()

Daniel-Constantin Mierla authored on 23/09/2022 11:59:36
Showing 1 changed files
... ...
@@ -391,7 +391,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
391 391
 
392 392
 	if (dlg) {
393 393
 		dlgvar_names = (uac_flag==FL_USE_UAC_FROM)?from_dlgvar:to_dlgvar;
394
-		if(dlg_api.get_dlg_var(dlg, &dlgvar_names[0])) {
394
+		if(dlg_api.get_dlg_varref(dlg, &dlgvar_names[0])) {
395 395
 
396 396
 			LM_INFO("Already called uac_replace for this dialog\n");
397 397
 			/* delete the from_new dlg var */
... ...
@@ -873,8 +873,8 @@ static void replace_callback(struct dlg_cell *dlg, int type,
873 873
 	struct hdr_field *hdr;
874 874
 	struct to_body *body;
875 875
 	str old_uri;
876
-	str* new_uri;
877
-	str* new_display;
876
+	str new_uri = {0};
877
+	str new_display = {0};
878 878
 	str buf = STR_NULL;
879 879
 	char *p;
880 880
 	unsigned int uac_flag;
... ...
@@ -927,20 +927,22 @@ static void replace_callback(struct dlg_cell *dlg, int type,
927 927
 				" with the original uri\n");
928 928
 	}
929 929
 
930
-	if ((new_uri = dlg_api.get_dlg_var(dlg, &dlgvar_names[dlgvar_index])) == 0) {
930
+	dlg_api.get_dlg_varval(dlg, &dlgvar_names[dlgvar_index], &new_uri);
931
+	if (new_uri.s == NULL) {
931 932
 		LM_DBG("<%.*s> param not found\n", dlgvar_names[dlgvar_index].len,
932 933
 				dlgvar_names[dlgvar_index].s);
933 934
 		return;
934 935
 	}
935
-	if ((new_display = dlg_api.get_dlg_var(dlg, &dlgvar_names[dlgvar_dpindex])) == 0) {
936
+	dlg_api.get_dlg_varval(dlg, &dlgvar_names[dlgvar_dpindex], &new_display);
937
+	if (new_display.s == NULL) {
936 938
 		LM_DBG("<%.*s> param not found\n", dlgvar_names[dlgvar_dpindex].len,
937 939
 				dlgvar_names[dlgvar_dpindex].s);
938 940
 		return;
939 941
 	}
940 942
 
941 943
 	LM_DBG("Replace [%.*s %.*s] with [%.*s %.*s]\n", body->display.len, body->display.s,
942
-			old_uri.len, old_uri.s, new_display->len, new_display->s,
943
-			new_uri->len, new_uri->s);
944
+			old_uri.len, old_uri.s, new_display.len, new_display.s,
945
+			new_uri.len, new_uri.s);
944 946
 
945 947
 	/* deal with display name */
946 948
 	l = 0;
... ...
@@ -955,17 +957,17 @@ static void replace_callback(struct dlg_cell *dlg, int type,
955 957
 			return;
956 958
 		}
957 959
 	}
958
-	if (new_display->s && new_display->len > 0) {
960
+	if (new_display.s && new_display.len > 0) {
959 961
 		LM_DBG("inserting display [%.*s]\n",
960
-				new_display->len, new_display->s);
962
+				new_display.len, new_display.s);
961 963
 		/* add the new display exactly over the deleted one */
962
-		buf.s = pkg_malloc(new_display->len + 2);
964
+		buf.s = pkg_malloc(new_display.len + 2);
963 965
 		if (buf.s==0) {
964 966
 			PKG_MEM_ERROR;
965 967
 			return;
966 968
 		}
967
-		memcpy( buf.s, new_display->s, new_display->len);
968
-		buf.len = new_display->len;
969
+		memcpy(buf.s, new_display.s, new_display.len);
970
+		buf.len = new_display.len;
969 971
 		if (l==0 && (l=get_display_anchor(msg, hdr, body, &buf)) == 0) {
970 972
 			LM_ERR("failed to insert anchor\n");
971 973
 			pkg_free(buf.s);
... ...
@@ -979,12 +981,12 @@ static void replace_callback(struct dlg_cell *dlg, int type,
979 981
 	}
980 982
 
981 983
 	/* uri update - duplicate the decoded value */
982
-	p = pkg_malloc( new_uri->len);
984
+	p = pkg_malloc(new_uri.len);
983 985
 	if (!p) {
984 986
 		PKG_MEM_ERROR;
985 987
 		return;
986 988
 	}
987
-	memcpy(p, new_uri->s, new_uri->len);
989
+	memcpy(p, new_uri.s, new_uri.len);
988 990
 
989 991
 	/* build del/add lumps */
990 992
 	l = del_lump(msg, old_uri.s-msg->buf, old_uri.len, 0);
... ...
@@ -994,7 +996,7 @@ static void replace_callback(struct dlg_cell *dlg, int type,
994 996
 		return;
995 997
 	}
996 998
 
997
-	if (insert_new_lump_after( l, p, new_uri->len, 0)==0) {
999
+	if (insert_new_lump_after(l, p, new_uri.len, 0)==0) {
998 1000
 		LM_ERR("insert new lump failed\n");
999 1001
 		pkg_free(p);
1000 1002
 		return;
Browse code

uac: do not free chunks already inserted as lumps

- in case of errors happening later, the pointers are linked in
sip_msg_t and will be freed there, otherwise will result in a double
free

Daniel-Constantin Mierla authored on 06/04/2021 10:17:35
Showing 1 changed files
... ...
@@ -875,7 +875,7 @@ static void replace_callback(struct dlg_cell *dlg, int type,
875 875
 	str old_uri;
876 876
 	str* new_uri;
877 877
 	str* new_display;
878
-	str buf;
878
+	str buf = STR_NULL;
879 879
 	char *p;
880 880
 	unsigned int uac_flag;
881 881
 	int dlgvar_index = 0;
... ...
@@ -968,11 +968,13 @@ static void replace_callback(struct dlg_cell *dlg, int type,
968 968
 		buf.len = new_display->len;
969 969
 		if (l==0 && (l=get_display_anchor(msg, hdr, body, &buf)) == 0) {
970 970
 			LM_ERR("failed to insert anchor\n");
971
-			goto free1;
971
+			pkg_free(buf.s);
972
+			return;
972 973
 		}
973 974
 		if (insert_new_lump_after(l, buf.s, buf.len, 0) == 0) {
974 975
 			LM_ERR("insert new display lump failed\n");
975
-			goto free1;
976
+			pkg_free(buf.s);
977
+			return;
976 978
 		}
977 979
 	}
978 980
 
... ...
@@ -980,20 +982,22 @@ static void replace_callback(struct dlg_cell *dlg, int type,
980 982
 	p = pkg_malloc( new_uri->len);
981 983
 	if (!p) {
982 984
 		PKG_MEM_ERROR;
983
-		goto free1;
985
+		return;
984 986
 	}
985
-	memcpy( p, new_uri->s, new_uri->len);
987
+	memcpy(p, new_uri->s, new_uri->len);
986 988
 
987 989
 	/* build del/add lumps */
988
-	l = del_lump( msg, old_uri.s-msg->buf, old_uri.len, 0);
990
+	l = del_lump(msg, old_uri.s-msg->buf, old_uri.len, 0);
989 991
 	if (l==0) {
990 992
 		LM_ERR("del lump failed\n");
991
-		goto free2;
993
+		pkg_free(p);
994
+		return;
992 995
 	}
993 996
 
994 997
 	if (insert_new_lump_after( l, p, new_uri->len, 0)==0) {
995 998
 		LM_ERR("insert new lump failed\n");
996
-		goto free2;
999
+		pkg_free(p);
1000
+		return;
997 1001
 	}
998 1002
 
999 1003
 	/* register tm callback to change replies,
... ...
@@ -1007,12 +1011,6 @@ static void replace_callback(struct dlg_cell *dlg, int type,
1007 1011
 	msg->msg_flags |= uac_flag;
1008 1012
 
1009 1013
 	return;
1010
-
1011
-free2:
1012
-	pkg_free(p);
1013
-
1014
-free1:
1015
-	pkg_free(buf.s);
1016 1014
 }
1017 1015
 
1018 1016
 
Browse code

uac: restore first display name then uri with dialog callback

- same as for rr callback, otherwise the new header can be malformed:
the case of initial INVITE with From URI having no angle brackets and goes
out with display name and angle brackets, the ACK for 200ok results with
From broken when forwarded

Daniel-Constantin Mierla authored on 07/09/2020 17:35:04
Showing 1 changed files
... ...
@@ -942,26 +942,6 @@ static void replace_callback(struct dlg_cell *dlg, int type,
942 942
 			old_uri.len, old_uri.s, new_display->len, new_display->s,
943 943
 			new_uri->len, new_uri->s);
944 944
 
945
-	/* duplicate the decoded value */
946
-	p = pkg_malloc( new_uri->len);
947
-	if (!p) {
948
-		PKG_MEM_ERROR;
949
-		return;
950
-	}
951
-	memcpy( p, new_uri->s, new_uri->len);
952
-
953
-	/* build del/add lumps */
954
-	l = del_lump( msg, old_uri.s-msg->buf, old_uri.len, 0);
955
-	if (l==0) {
956
-		LM_ERR("del lump failed\n");
957
-		goto free;
958
-	}
959
-
960
-	if (insert_new_lump_after( l, p, new_uri->len, 0)==0) {
961
-		LM_ERR("insert new lump failed\n");
962
-		goto free;
963
-	}
964
-
965 945
 	/* deal with display name */
966 946
 	l = 0;
967 947
 	/* first remove the existing display */
... ...
@@ -972,8 +952,8 @@ static void replace_callback(struct dlg_cell *dlg, int type,
972 952
 		l = del_lump(msg, body->display.s-msg->buf, body->display.len, 0);
973 953
 		if (l==0) {
974 954
 			LM_ERR("display del lump failed\n");
975
-			goto free;
976
-			}
955
+			return;
956
+		}
977 957
 	}
978 958
 	if (new_display->s && new_display->len > 0) {
979 959
 		LM_DBG("inserting display [%.*s]\n",
... ...
@@ -982,20 +962,40 @@ static void replace_callback(struct dlg_cell *dlg, int type,
982 962
 		buf.s = pkg_malloc(new_display->len + 2);
983 963
 		if (buf.s==0) {
984 964
 			PKG_MEM_ERROR;
985
-			goto free;
965
+			return;
986 966
 		}
987 967
 		memcpy( buf.s, new_display->s, new_display->len);
988 968
 		buf.len = new_display->len;
989 969
 		if (l==0 && (l=get_display_anchor(msg, hdr, body, &buf)) == 0) {
990 970
 			LM_ERR("failed to insert anchor\n");
991
-			goto free2;
971
+			goto free1;
992 972
 		}
993 973
 		if (insert_new_lump_after(l, buf.s, buf.len, 0) == 0) {
994 974
 			LM_ERR("insert new display lump failed\n");
995
-			goto free2;
975
+			goto free1;
996 976
 		}
997 977
 	}
998 978
 
979
+	/* uri update - duplicate the decoded value */
980
+	p = pkg_malloc( new_uri->len);
981
+	if (!p) {
982
+		PKG_MEM_ERROR;
983
+		goto free1;
984
+	}
985
+	memcpy( p, new_uri->s, new_uri->len);
986
+
987
+	/* build del/add lumps */
988
+	l = del_lump( msg, old_uri.s-msg->buf, old_uri.len, 0);
989
+	if (l==0) {
990
+		LM_ERR("del lump failed\n");
991
+		goto free2;
992
+	}
993
+
994
+	if (insert_new_lump_after( l, p, new_uri->len, 0)==0) {
995
+		LM_ERR("insert new lump failed\n");
996
+		goto free2;
997
+	}
998
+
999 999
 	/* register tm callback to change replies,
1000 1000
 	 * but only if not registered earlier */
1001 1001
 	if (!(msg->msg_flags & (FL_USE_UAC_FROM|FL_USE_UAC_TO)) &&
... ...
@@ -1009,10 +1009,10 @@ static void replace_callback(struct dlg_cell *dlg, int type,
1009 1009
 	return;
1010 1010
 
1011 1011
 free2:
1012
-	pkg_free(buf.s);
1013
-
1014
-free:
1015 1012
 	pkg_free(p);
1013
+
1014
+free1:
1015
+	pkg_free(buf.s);
1016 1016
 }
1017 1017
 
1018 1018
 
Browse code

uac: convert to memory logging helper

Henning Westerholt authored on 13/02/2020 21:45:36
Showing 1 changed files
... ...
@@ -217,7 +217,7 @@ static inline struct lump* get_display_anchor(struct sip_msg *msg,
217 217
 	}
218 218
 	p1 = (char*)pkg_malloc(1);
219 219
 	if (p1==0) {
220
-		LM_ERR("no more pkg mem \n");
220
+		PKG_MEM_ERROR;
221 221
 		return 0;
222 222
 	}
223 223
 	*p1 = '>';
... ...
@@ -333,7 +333,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
333 333
 			buf.s = pkg_malloc( display->len + 2 );
334 334
 			if (buf.s==0)
335 335
 			{
336
-				LM_ERR("no more pkg mem\n");
336
+				PKG_MEM_ERROR;
337 337
 				goto error;
338 338
 			}
339 339
 			memcpy( buf.s, display->s, display->len);
... ...
@@ -370,7 +370,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
370 370
 	p = pkg_malloc( uri->len);
371 371
 	if (p==0)
372 372
 	{
373
-		LM_ERR("no more pkg mem\n");
373
+		PKG_MEM_ERROR;
374 374
 		goto error;
375 375
 	}
376 376
 	memcpy( p, uri->s, uri->len);
... ...
@@ -510,7 +510,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
510 510
 		param.s = (char*)pkg_malloc(param.len);
511 511
 		if (param.s==0)
512 512
 		{
513
-			LM_ERR("no more pkg mem\n");
513
+			PKG_MEM_ERROR;
514 514
 			goto error;
515 515
 		}
516 516
 		p = param.s;
... ...
@@ -594,7 +594,7 @@ int restore_uri( struct sip_msg *msg, str *rr_param, str* restore_avp,
594 594
 	add_to_rr.s = pkg_malloc(bsize);
595 595
 	if ( add_to_rr.s==0 ) {
596 596
 		add_to_rr.len = 0;
597
-		LM_ERR("no more pkg mem\n");
597
+		PKG_MEM_ERROR;
598 598
 		goto failed;
599 599
 	}
600 600
 	add_to_rr.len = snprintf(add_to_rr.s, bsize, ";%.*s=%.*s",
... ...
@@ -696,7 +696,7 @@ int restore_uri( struct sip_msg *msg, str *rr_param, str* restore_avp,
696 696
 	/* duplicate the decoded value */
697 697
 	p = pkg_malloc( new_uri.len);
698 698
 	if (p==0) {
699
-		LM_ERR("no more pkg mem\n");
699
+		PKG_MEM_ERROR;
700 700
 		goto failed;
701 701
 	}
702 702
 	memcpy( p, new_uri.s, new_uri.len);
... ...
@@ -776,7 +776,7 @@ static inline int restore_uri_reply(struct sip_msg *rpl,
776 776
 
777 777
 	new_val.s = pkg_malloc( len );
778 778
 	if (new_val.s==0) {
779
-		LM_ERR("no more pkg mem\n");
779
+		PKG_MEM_ERROR;
780 780
 		return -1;
781 781
 	}
782 782
 	memcpy( new_val.s, p, len);
... ...
@@ -945,7 +945,7 @@ static void replace_callback(struct dlg_cell *dlg, int type,
945 945
 	/* duplicate the decoded value */
946 946
 	p = pkg_malloc( new_uri->len);
947 947
 	if (!p) {
948
-		LM_ERR("no more pkg mem\n");
948
+		PKG_MEM_ERROR;
949 949
 		return;
950 950
 	}
951 951
 	memcpy( p, new_uri->s, new_uri->len);
... ...
@@ -981,7 +981,7 @@ static void replace_callback(struct dlg_cell *dlg, int type,
981 981
 		/* add the new display exactly over the deleted one */
982 982
 		buf.s = pkg_malloc(new_display->len + 2);
983 983
 		if (buf.s==0) {
984
-			LM_ERR("no more pkg mem\n");
984
+			PKG_MEM_ERROR;
985 985
 			goto free;
986 986
 		}
987 987
 		memcpy( buf.s, new_display->s, new_display->len);
Browse code

uac: use snprintf() instead of sprintf()

Daniel-Constantin Mierla authored on 18/11/2019 17:05:19
Showing 1 changed files
... ...
@@ -568,6 +568,7 @@ int restore_uri( struct sip_msg *msg, str *rr_param, str* restore_avp,
568 568
 	int i;
569 569
 	int_str avp_value;
570 570
 	int flag;
571
+	int bsize;
571 572
 
572 573
 	/* we should process only sequential request, but since we are looking
573 574
 	 * for Route param, the test is not really required -bogdan */
... ...
@@ -589,15 +590,20 @@ int restore_uri( struct sip_msg *msg, str *rr_param, str* restore_avp,
589 590
 		goto failed;
590 591
 	}
591 592
 
592
-	add_to_rr.s = pkg_malloc(3+rr_param->len+param_val.len);
593
+	bsize = 3+rr_param->len+param_val.len;
594
+	add_to_rr.s = pkg_malloc(bsize);
593 595
 	if ( add_to_rr.s==0 ) {
594 596
 		add_to_rr.len = 0;
595 597
 		LM_ERR("no more pkg mem\n");
596 598
 		goto failed;
597 599
 	}
598
-	add_to_rr.len = sprintf(add_to_rr.s, ";%.*s=%.*s",
600
+	add_to_rr.len = snprintf(add_to_rr.s, bsize, ";%.*s=%.*s",
599 601
 			rr_param->len, rr_param->s, param_val.len, param_val.s);
600 602
 
603
+	if(add_to_rr.len<0 || add_to_rr.len>=bsize) {
604
+		LM_ERR("printing rr param failed\n");
605
+		goto failed;
606
+	}
601 607
 	if ( uac_rrb.add_rr_param(msg, &add_to_rr)!=0 ) {
602 608
 		LM_ERR("add rr param failed\n");
603 609
 		goto failed;
Browse code

uac: debug messages when restoring from/to headers

Daniel-Constantin Mierla authored on 05/08/2019 16:03:47
Showing 1 changed files
... ...
@@ -622,6 +622,7 @@ int restore_uri( struct sip_msg *msg, str *rr_param, str* restore_avp,
622 622
 		}
623 623
 		old_body = (struct to_body*) msg->to->parsed;
624 624
 		flag = FL_USE_UAC_TO;
625
+		LM_DBG("replacing in To header\n");
625 626
 	} else {
626 627
 		/* replace the FROM URI */
627 628
 		if ( parse_from_header(msg)<0 ) {
... ...
@@ -630,6 +631,7 @@ int restore_uri( struct sip_msg *msg, str *rr_param, str* restore_avp,
630 631
 		}
631 632
 		old_body = (struct to_body*) msg->from->parsed;
632 633
 		flag = FL_USE_UAC_FROM;
634
+		LM_DBG("replacing in From header\n");
633 635
 	}
634 636
 
635 637
 	if(restore_avp->s) {
Browse code

uac: bug fix for uac module uac_replace* functions, register callbacks after start

- bug fix for uac module uac_replace* functions, register callbacks after restart
- only applies to the dialog tracking mode of the module
- the uac module was not using the available dialog callbacks for a proxy (re-)start
- because of this the uac module was not able to properly re-write in-dialog messages
like BYEs after a proxy restart
- As we don't have access to the uac_flag at the dialog on load callback, we just
install a callback for both from and to rewriting cases. If only one of the
functions is used in the cfg hen the uac module will obviously not find
database variables for the other case, and will log an error. This is of
course also a bit inefficient, but as it only applies to this (re-)start
case it does not matter.

Henning Westerholt authored on 13/04/2019 17:52:03
Showing 1 changed files
... ...
@@ -55,7 +55,7 @@ extern int_str restore_from_avp_name;
55 55
 extern unsigned short restore_to_avp_type;
56 56
 extern int_str restore_to_avp_name;
57 57
 
58
-extern struct dlg_binds dlg_api;
58
+struct dlg_binds dlg_api;
59 59
 static str from_dlgvar[] = {str_init("_uac_fu"), str_init("_uac_funew"), str_init("_uac_fdp"), str_init("_uac_fdpnew")};
60 60
 static str to_dlgvar[] = {str_init("_uac_to"), str_init("_uac_tonew"), str_init("_uac_tdp"), str_init("_uac_tdpnew")};
61 61
 
... ...
@@ -1006,3 +1006,76 @@ free2:
1006 1006
 free:
1007 1007
 	pkg_free(p);
1008 1008
 }
1009
+
1010
+
1011
+/* helper function to avoid code duplication */
1012
+static inline int uac_load_callback_helper(struct dlg_cell* dialog, unsigned int uac_flag) {
1013
+
1014
+	if( dlg_api.register_dlgcb(dialog, DLGCB_REQ_WITHIN,
1015
+			(void*)(unsigned long)replace_callback, (void*)(unsigned long)uac_flag, 0) != 0) {
1016
+		LM_ERR("can't register create dialog REQ_WITHIN callback\n");
1017
+		return -1;
1018
+	}
1019
+
1020
+	if( dlg_api.register_dlgcb(dialog, DLGCB_CONFIRMED,
1021
+			(void*)(unsigned long)replace_callback, (void*)(unsigned long)uac_flag, 0) != 0) {
1022
+		LM_ERR("can't register create dialog CONFIRM callback\n");
1023
+		return -1;
1024
+	}
1025
+
1026
+	if( dlg_api.register_dlgcb(dialog, DLGCB_TERMINATED,
1027
+			(void*)(unsigned long)replace_callback, (void*)(unsigned long)uac_flag, 0) != 0) {
1028
+		LM_ERR("can't register create dialog TERMINATED callback\n");
1029
+		return -1;
1030
+	}
1031
+	return 0;
1032
+}
1033
+
1034
+
1035
+/* callback for loading a dialog from database */
1036
+static void uac_on_load_callback(struct dlg_cell* dialog, int type, struct dlg_cb_params* params) {
1037
+
1038
+	if(!dialog) {
1039
+		LM_ERR("invalid values\n!");
1040
+		return;
1041
+	}
1042
+
1043
+	/* Note:
1044
+	 * We don't have a way to access the real uac flags from the uac_replace_*
1045
+	 * method call at this point in time anymore. Therefore we just install a
1046
+	 * callback for both FROM and TO replace cases. This might be a bit
1047
+	 * inefficient in cases where only one of the functions is used. But as
1048
+	 * this applies only e.g. to a proxy restart with runnning dialogs, it
1049
+	 * does not matter. The replace_callback function will just not find a
1050
+	 * an entry in the dialog variables table and log an error.
1051
+	 */
1052
+	if(uac_load_callback_helper(dialog, FL_USE_UAC_FROM) != 0) {
1053
+		LM_ERR("can't register create callbacks for UAC FROM\n");
1054
+		return;
1055
+	}
1056
+	if(uac_load_callback_helper(dialog, FL_USE_UAC_TO) != 0) {
1057
+		LM_ERR("can't register create callbacks for UAC TO\n");
1058
+		return;
1059
+	}
1060
+
1061
+	LM_DBG("dialog '%p' loaded and callbacks registered\n", dialog);
1062
+}
1063
+
1064
+
1065
+/* initialization of all necessary callbacks to track a dialog */
1066
+int uac_init_dlg(void) {
1067
+
1068
+	memset(&dlg_api, 0, sizeof(struct dlg_binds));
1069
+
1070
+	if( load_dlg_api(&dlg_api) != 0) {
1071
+		LM_ERR("can't load dialog API\n");
1072
+		return -1;
1073
+	}
1074
+
1075
+	if( dlg_api.register_dlgcb( 0, DLGCB_LOADED, uac_on_load_callback, 0, 0) != 0) {
1076
+		LM_ERR("can't register on load callback\n");
1077
+		return -1;
1078
+	}
1079
+	LM_DBG("loaded dialog API and registered on load callback\n");
1080
+	return 0;
1081
+}
Browse code

uac: add missing functionality to track also display names in dialog variables

- add missing functionality to also track display names in dialog variables after
usage of uac_replace_from/uac_replace_to
- the module was working correctly with signalisation tracked with TM callbacks,
but not for dialog tracking
- four new dialog variables are inserted in dialog_vars: _uac_tdp, _uac_tdpnew,
_uac_fdp, _uac_fdpnew
- fix a potential memory leak in one replace error path

Henning Westerholt authored on 18/03/2019 19:06:42
Showing 1 changed files
... ...
@@ -56,8 +56,8 @@ extern unsigned short restore_to_avp_type;
56 56
 extern int_str restore_to_avp_name;
57 57
 
58 58
 extern struct dlg_binds dlg_api;
59
-static str from_dlgvar[] = {str_init("_uac_fu"), str_init("_uac_funew")};
60
-static str to_dlgvar[] = {str_init("_uac_to"), str_init("_uac_tonew") };
59
+static str from_dlgvar[] = {str_init("_uac_fu"), str_init("_uac_funew"), str_init("_uac_fdp"), str_init("_uac_fdpnew")};
60
+static str to_dlgvar[] = {str_init("_uac_to"), str_init("_uac_tonew"), str_init("_uac_tdp"), str_init("_uac_tdpnew")};
61 61
 
62 62
 static char enc_table64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
63 63
 		"abcdefghijklmnopqrstuvwxyz0123456789+/";
... ...
@@ -257,6 +257,8 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
257 257
 	int_str avp_value;
258 258
 	struct dlg_cell* dlg = 0;
259 259
 	str * dlgvar_names;
260
+	str display_tmp;
261
+	str undefined_display = str_init("");
260 262
 
261 263
 	uac_flag = (hdr==msg->from)?FL_USE_UAC_FROM:FL_USE_UAC_TO;
262 264
 	if(get_route_type()==REQUEST_ROUTE) {
... ...
@@ -339,6 +341,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
339 341
 			if (l==0 && (l=get_display_anchor(msg,hdr,body,&buf))==0)
340 342
 			{
341 343
 				LM_ERR("failed to insert anchor\n");
344
+				pkg_free(buf.s);
342 345
 				goto error;
343 346
 			}
344 347
 			if (insert_new_lump_after( l, buf.s, buf.len, 0)==0)
... ...
@@ -403,7 +406,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
403 406
 		}
404 407
 		else {
405 408
 			/* the first time uac_replace is called for this dialog */
406
-
409
+			/* store old URI value */
407 410
 			if (dlg_api.set_dlg_var(dlg, &dlgvar_names[0], &body->uri) < 0) {
408 411
 				LM_ERR("cannot store value\n");
409 412
 				dlg_api.release_dlg(dlg);
... ...
@@ -422,6 +425,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
422 425
 				goto error;
423 426
 			}
424 427
 		}
428
+		/* store new URI value */
425 429
 		if (dlg_api.set_dlg_var(dlg, &dlgvar_names[1], uri) < 0) {
426 430
 			LM_ERR("cannot store new uri value\n");
427 431
 			dlg_api.release_dlg(dlg);
... ...
@@ -429,6 +433,35 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
429 433
 		}
430 434
 		LM_DBG("Stored <%.*s> var in dialog with value %.*s\n",
431 435
 				dlgvar_names[1].len, dlgvar_names[1].s, uri->len, uri->s);
436
+
437
+		/* store the display name as well */
438
+		if (body->display.s && body->display.len >0) {
439
+			display_tmp = body->display;
440
+		} else {
441
+			display_tmp = undefined_display;
442
+		}
443
+		if (dlg_api.set_dlg_var(dlg, &dlgvar_names[2], &display_tmp) < 0) {
444
+			LM_ERR("cannot store display value\n");
445
+			dlg_api.release_dlg(dlg);
446
+			goto error;
447
+		}
448
+		LM_DBG("Stored <%.*s> var in dialog with value %.*s\n",
449
+				dlgvar_names[1].len, dlgvar_names[2].s, display_tmp.len, display_tmp.s);
450
+
451
+		if (display && display->s && display->len > 0) {
452
+			display_tmp.s=display->s;
453
+			display_tmp.len=display->len;
454
+		} else {
455
+			display_tmp = undefined_display;
456
+		}
457
+		if (dlg_api.set_dlg_var(dlg, &dlgvar_names[3], &display_tmp) < 0) {
458
+			LM_ERR("cannot store new display value\n");
459
+			dlg_api.release_dlg(dlg);
460
+			goto error;
461
+		}
462
+		LM_DBG("Stored <%.*s> var in dialog with value %.*s\n",
463
+				dlgvar_names[1].len, dlgvar_names[3].s, display_tmp.len, display_tmp.s);
464
+
432 465
 		dlg_api.release_dlg(dlg);
433 466
 	} else {
434 467
 		if (!uac_rrb.append_fromtag) {
... ...
@@ -741,7 +774,6 @@ static inline int restore_uri_reply(struct sip_msg *rpl,
741 774
 	memcpy( new_val.s, p, len);
742 775
 	new_val.len = len;
743 776
 
744
-
745 777
 	body = (struct to_body*)rpl_hdr->parsed;
746 778
 
747 779
 	/* catch whitespace characters after uri */
... ...
@@ -802,7 +834,6 @@ void restore_uris_reply(struct cell* t, int type, struct tmcb_params *p)
802 834
 		}
803 835
 
804 836
 	}
805
-
806 837
 	if (req->msg_flags & FL_USE_UAC_TO ) {
807 838
 
808 839
 		/* parse TO in reply */
... ...
@@ -831,18 +862,22 @@ static void replace_callback(struct dlg_cell *dlg, int type,
831 862
 {
832 863
 	struct lump* l;
833 864
 	struct sip_msg *msg;
865
+	struct hdr_field *hdr;
866
+	struct to_body *body;
834 867
 	str old_uri;
835 868
 	str* new_uri;
869
+	str* new_display;
870
+	str buf;
836 871
 	char *p;
837 872
 	unsigned int uac_flag;
838 873
 	int dlgvar_index = 0;
874
+	int dlgvar_dpindex = 0;
839 875
 	str* dlgvar_names;
840 876
 
841 877
 	if (!dlg || !_params || _params->direction == DLG_DIR_NONE || !_params->req)
842 878
 		return;
843 879
 
844 880
 	uac_flag = (unsigned int)(unsigned long)*(_params->param);
845
-
846 881
 	msg = _params->req;
847 882
 	if(msg->msg_flags & uac_flag)
848 883
 		return;
... ...
@@ -859,6 +894,8 @@ static void replace_callback(struct dlg_cell *dlg, int type,
859 894
 			return;
860 895
 		}
861 896
 		old_uri = ((struct to_body*)msg->to->parsed)->uri;
897
+		hdr = (struct hdr_field*)msg->to;
898
+		body = ((struct to_body*)msg->to->parsed);
862 899
 	} else {
863 900
 		/* replace the FROM URI */
864 901
 		if ( parse_from_header(msg)<0 ) {
... ...
@@ -866,13 +903,18 @@ static void replace_callback(struct dlg_cell *dlg, int type,
866 903
 			return;
867 904
 		}
868 905
 		old_uri = ((struct to_body*)msg->from->parsed)->uri;
906
+		hdr = (struct hdr_field*)msg->from;
907
+		body = (struct to_body*)msg->from->parsed;
869 908
 	}
870 909
 
871 910
 	if (_params->direction == DLG_DIR_DOWNSTREAM) {
872 911
 		dlgvar_index = 1;
912
+		dlgvar_dpindex = 3;
873 913
 		LM_DBG("DOWNSTREAM direction detected - replacing uri"
874 914
 				" with the new uri\n");
875 915
 	} else {
916
+		dlgvar_index = 0;
917
+		dlgvar_dpindex = 2;
876 918
 		LM_DBG("UPSTREAM direction detected - replacing uri"
877 919
 				" with the original uri\n");
878 920
 	}
... ...
@@ -882,8 +924,14 @@ static void replace_callback(struct dlg_cell *dlg, int type,
882 924
 				dlgvar_names[dlgvar_index].s);
883 925
 		return;
884 926
 	}
927
+	if ((new_display = dlg_api.get_dlg_var(dlg, &dlgvar_names[dlgvar_dpindex])) == 0) {
928
+		LM_DBG("<%.*s> param not found\n", dlgvar_names[dlgvar_dpindex].len,
929
+				dlgvar_names[dlgvar_dpindex].s);
930
+		return;
931
+	}
885 932
 
886
-	LM_DBG("Replace [%.*s] with [%.*s]\n", old_uri.len, old_uri.s,
933
+	LM_DBG("Replace [%.*s %.*s] with [%.*s %.*s]\n", body->display.len, body->display.s,
934
+			old_uri.len, old_uri.s, new_display->len, new_display->s,
887 935
 			new_uri->len, new_uri->s);
888 936
 
889 937
 	/* duplicate the decoded value */
... ...
@@ -906,6 +954,40 @@ static void replace_callback(struct dlg_cell *dlg, int type,
906 954
 		goto free;
907 955
 	}
908 956
 
957
+	/* deal with display name */
958
+	l = 0;
959
+	/* first remove the existing display */
960
+	if (body->display.s && body->display.len > 0) {
961
+		LM_DBG("removing display [%.*s]\n",
962
+				body->display.len, body->display.s);
963
+		/* build del lump */
964
+		l = del_lump(msg, body->display.s-msg->buf, body->display.len, 0);
965
+		if (l==0) {
966
+			LM_ERR("display del lump failed\n");
967
+			goto free;
968
+			}
969
+	}
970
+	if (new_display->s && new_display->len > 0) {
971
+		LM_DBG("inserting display [%.*s]\n",
972
+				new_display->len, new_display->s);
973
+		/* add the new display exactly over the deleted one */
974
+		buf.s = pkg_malloc(new_display->len + 2);
975
+		if (buf.s==0) {
976
+			LM_ERR("no more pkg mem\n");
977
+			goto free;
978
+		}
979
+		memcpy( buf.s, new_display->s, new_display->len);
980
+		buf.len = new_display->len;
981
+		if (l==0 && (l=get_display_anchor(msg, hdr, body, &buf)) == 0) {
982
+			LM_ERR("failed to insert anchor\n");
983
+			goto free2;
984
+		}
985
+		if (insert_new_lump_after(l, buf.s, buf.len, 0) == 0) {
986
+			LM_ERR("insert new display lump failed\n");
987
+			goto free2;
988
+		}
989
+	}
990
+
909 991
 	/* register tm callback to change replies,
910 992
 	 * but only if not registered earlier */
911 993
 	if (!(msg->msg_flags & (FL_USE_UAC_FROM|FL_USE_UAC_TO)) &&
... ...
@@ -918,6 +1000,9 @@ static void replace_callback(struct dlg_cell *dlg, int type,
918 1000
 
919 1001
 	return;
920 1002
 
1003
+free2:
1004
+	pkg_free(buf.s);
1005
+
921 1006
 free:
922 1007
 	pkg_free(p);
923 1008
 }
Browse code

uac: add debugging information also for display name removal case

Henning Westerholt authored on 15/03/2019 23:25:11
Showing 1 changed files
... ...
@@ -326,6 +326,7 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
326 326
 		/* some new display to set? */
327 327
 		if (display->len)
328 328
 		{
329
+			LM_DBG("adding new display [%.*s]\n", display->len, display->s);
329 330
 			/* add the new display exactly over the deleted one */
330 331
 			buf.s = pkg_malloc( display->len + 2 );
331 332
 			if (buf.s==0)
Browse code

uac: small spelling fix in debug output

Henning Westerholt authored on 15/03/2019 17:12:22
Showing 1 changed files
... ...
@@ -882,7 +882,7 @@ static void replace_callback(struct dlg_cell *dlg, int type,
882 882
 		return;
883 883
 	}
884 884
 
885
-	LM_DBG("Replace [%.*s] eith [%.*s]\n", old_uri.len, old_uri.s,
885
+	LM_DBG("Replace [%.*s] with [%.*s]\n", old_uri.len, old_uri.s,
886 886
 			new_uri->len, new_uri->s);
887 887
 
888 888
 	/* duplicate the decoded value */
Browse code

uac: explicit name of the function called many times when updating from/to

Daniel-Constantin Mierla authored on 11/05/2017 14:15:21
Showing 1 changed files
... ...
@@ -262,7 +262,8 @@ int replace_uri( struct sip_msg *msg, str *display, str *uri,
262 262
 	if(get_route_type()==REQUEST_ROUTE) {
263 263
 		if(msg->msg_flags & uac_flag)
264 264
 		{
265
-			LM_ERR("Called uac_replace multiple times on the message\n");
265
+			LM_ERR("called uac_replace_%s() multiple times on the message\n",
266
+					(hdr==msg->from)?"from":"to");
266 267
 			return -1;
267 268
 		}
268 269
 	}
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -28,10 +28,10 @@
28 28
 
29 29
 #include <ctype.h>
30 30
 
31
-#include "../../parser/parse_from.h"
32
-#include "../../mem/mem.h"
33
-#include "../../data_lump.h"
34
-#include "../../route.h"
31
+#include "../../core/parser/parse_from.h"
32
+#include "../../core/mem/mem.h"
33
+#include "../../core/data_lump.h"
34
+#include "../../core/route.h"
35 35
 #include "../../modules/tm/h_table.h"
36 36
 #include "../../modules/tm/tm_load.h"
37 37
 #include "../rr/api.h"
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,921 @@
1
+/*
2
+ * Copyright (C) 2005 Voice Sistem SRL
3
+ *
4
+ * This file is part of Kamailio, a free SIP server.
5
+ *
6
+ * UAC Kamailio-module is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU General Public License
8
+ * as published by the Free Software Foundation; either version 2
9
+ * of the License, or (at your option) any later version.
10
+ *
11
+ * UAC Kamailio-module is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License
17
+ * along with this program; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
+ *
20
+ */
21
+
22
+/*!
23
+ * \file
24
+ * \brief Kamailio uac :: header replacement/retrieval functions
25
+ * \ingroup uac
26
+ * Module: \ref uac
27
+ */
28
+
29
+#include <ctype.h>
30
+
31
+#include "../../parser/parse_from.h"
32
+#include "../../mem/mem.h"
33
+#include "../../data_lump.h"
34
+#include "../../route.h"
35
+#include "../../modules/tm/h_table.h"
36
+#include "../../modules/tm/tm_load.h"
37
+#include "../rr/api.h"
38
+#include "../dialog/dlg_load.h"
39
+#include "../dialog/dlg_hash.h"
40
+
41
+
42
+#include "replace.h"
43
+
44
+extern str uac_passwd;
45
+extern int restore_mode;
46
+extern str rr_from_param;
47
+extern str rr_to_param;
48
+extern struct tm_binds uac_tmb;
49
+extern struct rr_binds uac_rrb;
50
+
51
+extern str restore_from_avp;
52
+extern str restore_to_avp;
53
+extern unsigned short restore_from_avp_type;
54
+extern int_str restore_from_avp_name;
55
+extern unsigned short restore_to_avp_type;
56
+extern int_str restore_to_avp_name;
57
+
58
+extern struct dlg_binds dlg_api;
59
+static str from_dlgvar[] = {str_init("_uac_fu"), str_init("_uac_funew")};
60
+static str to_dlgvar[] = {str_init("_uac_to"), str_init("_uac_tonew") };
61
+
62
+static char enc_table64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
63
+		"abcdefghijklmnopqrstuvwxyz0123456789+/";
64
+
65
+static int dec_table64[256];
66
+
67
+static void restore_uris_reply(struct cell* t, int type, struct tmcb_params *p);
68
+
69
+static void replace_callback(struct dlg_cell *dlg, int type,
70
+		struct dlg_cb_params *_params);
71
+
72
+#define text3B64_len(_l)   ( ( ((_l)+2)/3 ) << 2 )
73
+
74
+/* The reply, were the From-Line was replaced. */
75
+
76
+void init_from_replacer(void)
77
+{
78
+	int i;
79
+
80
+	for( i=0 ; i<256 ; i++)
81
+		dec_table64[i] = -1;
82
+	for ( i=0 ; i<64; i++)
83
+		dec_table64[(unsigned char)enc_table64[i]] = i;
84
+	}
85
+
86
+
87
+static inline int encode_uri( str *src, str *dst )
88
+{
89
+	static char buf[text3B64_len(MAX_URI_SIZE)];
90
+	int  idx;
91
+	int  left;
92
+	int  block;
93
+	int  i,r;
94
+	char *p;
95
+
96
+	dst->len = text3B64_len( src->len );
97
+	dst->s = buf;
98
+	if (dst->len>text3B64_len(MAX_URI_SIZE))
99
+	{
100
+		LM_ERR("uri too long\n");
101
+		return -1;
102
+	}
103
+
104
+	for ( idx=0, p=buf ; idx<src->len ; idx+=3)
105
+	{
106
+		left = src->len - idx -1 ;
107
+		left = (left>1? 2 : left);
108
+
109
+		/* Collect 1 to 3 bytes to encode */
110
+		block = 0;
111
+		for ( i=0,r= 16 ; i<=left ; i++,r-=8 )
112
+		{
113
+			block += ((unsigned char)src->s[idx+i]) << r;
114
+		}
115
+
116
+		/* Encode into 2-4 chars appending '=' if not enough data left.*/
117
+		*(p++) = enc_table64[(block >> 18) & 0x3f];
118
+		*(p++) = enc_table64[(block >> 12) & 0x3f];
119
+		*(p++) = left > 0 ? enc_table64[(block >> 6) & 0x3f] : '-';
120
+		*(p++) = left > 1 ? enc_table64[block & 0x3f] : '-';
121
+	}
122
+
123
+	return 0;
124
+}
125
+
126
+
127
+static inline int decode_uri( str *src , str *dst)
128
+{
129
+	static char buf[MAX_URI_SIZE];
130
+	int block;
131
+	int n;
132
+	int idx;
133
+	int end;
134
+	int i,j;
135
+	signed char c;
136
+
137
+	/* sanity checks */
138
+	if (!src) {
139
+		LM_ERR("NULL src\n");
140
+		return -1;
141
+	}
142
+
143
+	if (!dst) {
144
+		LM_ERR("NULL dst\n");
145
+		return -1;
146
+	}
147
+
148
+	if (!src->s || src->len == 0) {
149
+		LM_ERR("empty src\n");
150
+		return -1;
151
+	}
152
+
153
+	/* Count '-' at end and disregard them */
154
+	for( n=0,i=src->len-1; src->s[i]=='-'; i--)
155
+		n++;
156
+
157
+	dst->len = ((src->len * 6) >> 3) - n;
158
+	dst->s = buf;
159
+	if (dst->len>MAX_URI_SIZE)
160
+	{
161
+		LM_ERR("uri too long\n");
162
+		return -1;
163
+	}
164
+
165
+	end = src->len - n;