Browse code

nathelper: add optional set_contact_alias([trim]) parameter

Emmanuel Schmidbauer authored on 03/07/2020 18:00:57
Showing 2 changed files
... ...
@@ -842,7 +842,7 @@ if(is_rfc1918("$rd")) {
842 842
 
843 843
 	<section id="nathelper.set_contact_alias">
844 844
 		<title>
845
-		<function moreinfo="none">set_contact_alias()</function>
845
+		<function moreinfo="none">set_contact_alias([trim])</function>
846 846
 		</title>
847 847
 		<para>
848 848
 		Adds an <quote>;alias=ip~port~transport</quote> parameter to the
... ...
@@ -850,6 +850,16 @@ if(is_rfc1918("$rd")) {
850 850
 		The new contact URI is immediately visible to other modules in the
851 851
 		way the <function>fix_nated_contact()</function> does it.
852 852
 		</para>
853
+		<para>Meaning of parameters:</para>
854
+		<itemizedlist>
855
+			<listitem>
856
+				<para>
857
+					<emphasis>trim</emphasis> - by default, set_contact_alias() will not detect and trim an
858
+					already existing alias parameter. If this optional parameter is set to "1", set_contact_alias()
859
+					will trim the existing alias before adding a new one.
860
+				</para>
861
+			</listitem>
862
+		</itemizedlist>
853 863
 		<para>
854 864
 		This function can be used from
855 865
 		REQUEST_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE, and FAILURE_ROUTE.
... ...
@@ -110,6 +110,7 @@ static int fix_nated_contact_f(struct sip_msg *, char *, char *);
110 110
 static int add_contact_alias_0_f(struct sip_msg *, char *, char *);
111 111
 static int add_contact_alias_3_f(struct sip_msg *, char *, char *, char *);
112 112
 static int set_contact_alias_f(struct sip_msg *msg, char *str1, char *str2);
113
+static int w_set_contact_alias_f(struct sip_msg *msg, char *str1, char *str2);
113 114
 static int handle_ruri_alias_f(struct sip_msg *, char *, char *);
114 115
 static int pv_get_rr_count_f(struct sip_msg *, pv_param_t *, pv_value_t *);
115 116
 static int pv_get_rr_top_count_f(struct sip_msg *, pv_param_t *, pv_value_t *);
... ...
@@ -212,6 +213,8 @@ static cmd_export_t cmds[] = {
212 213
 	{"set_contact_alias",  (cmd_function)set_contact_alias_f,  0,
213 214
 		0, 0,
214 215
 		REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
216
+	{"set_contact_alias",  (cmd_function)w_set_contact_alias_f, 1,
217
+		fixup_int_1, 0, REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
215 218
 	{"handle_ruri_alias",  (cmd_function)handle_ruri_alias_f,    0,
216 219
 		0, 0,
217 220
 		REQUEST_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
... ...
@@ -704,10 +707,12 @@ static int fix_nated_contact_f(struct sip_msg *msg, char *str1, char *str2)
704 707
  * Replaces ip:port pair in the Contact: field with the source address
705 708
  * of the packet.
706 709
  */
707
-static int set_contact_alias(struct sip_msg *msg)
710
+static int set_contact_alias(struct sip_msg *msg, int trim)
708 711
 {
709 712
 	char nbuf[MAX_URI_SIZE];
713
+	char cbuf[MAX_URI_SIZE];
710 714
 	str nuri;
715
+	str curi;
711 716
 	int br;
712 717
 
713 718
 	int offset, len;
... ...
@@ -718,16 +723,24 @@ static int set_contact_alias(struct sip_msg *msg)
718 723
 
719 724
 	nuri.s = nbuf;
720 725
 	nuri.len = MAX_URI_SIZE;
726
+	curi.s = cbuf;
727
+	curi.len = MAX_URI_SIZE;
721 728
 	if(get_contact_uri(msg, &uri, &c) == -1)
722 729
 		return -1;
723 730
 	if((c->uri.s < msg->buf) || (c->uri.s > (msg->buf + msg->len))) {
724 731
 		LM_ERR("you can't update contact twice, check your config!\n");
725 732
 		return -1;
726 733
 	}
727
-
728
-	if(uri_add_rcv_alias(msg, &c->uri, &nuri) < 0) {
729
-		LM_DBG("cannot add the alias parameter\n");
730
-		return -1;
734
+	if(trim > 0 && uri_trim_rcv_alias(&c->uri, &curi) > 0) {
735
+		if(uri_add_rcv_alias(msg, &curi, &nuri) < 0) {
736
+			LM_DBG("cannot add the alias parameter\n");
737
+			return -1;
738
+		}
739
+	} else {
740
+		if(uri_add_rcv_alias(msg, &c->uri, &nuri) < 0) {
741
+			LM_DBG("cannot add the alias parameter\n");
742
+			return -1;
743
+		}
731 744
 	}
732 745
 
733 746
 	br = 1;
... ...
@@ -767,9 +780,31 @@ static int set_contact_alias(struct sip_msg *msg)
767 780
 	return 1;
768 781
 }
769 782
 
783
+static int ki_set_contact_alias(struct sip_msg *msg)
784
+{
785
+	return set_contact_alias(msg, 0);
786
+}
787
+
788
+static int ki_set_contact_alias_trim(struct sip_msg *msg)
789
+{
790
+	return set_contact_alias(msg, 1);
791
+}
792
+
770 793
 static int set_contact_alias_f(struct sip_msg *msg, char *str1, char *str2)
771 794
 {
772
-	return set_contact_alias(msg);
795
+	return set_contact_alias(msg, 0);
796
+}
797
+
798
+static int w_set_contact_alias_f(struct sip_msg *msg, char *str1, char *str2)
799
+{
800
+	int i = 0;
801
+	if(str1) {
802
+		if(get_int_fparam(&i, msg, (fparam_t *)str1) < 0)
803
+			return -1;
804
+	}
805
+	if(i > 1)
806
+		i = 1;
807
+	return set_contact_alias(msg, i);
773 808
 }
774 809
 
775 810
 #define SALIAS ";alias="
... ...
@@ -2689,7 +2724,12 @@ static sr_kemi_t sr_kemi_nathelper_exports[] = {
2689 2724
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2690 2725
 	},
2691 2726
 	{ str_init("nathelper"), str_init("set_contact_alias"),
2692
-		SR_KEMIP_INT, set_contact_alias,
2727
+		SR_KEMIP_INT, ki_set_contact_alias,
2728
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2729
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2730
+	},
2731
+	{ str_init("nathelper"), str_init("set_contact_alias_trim"),
2732
+		SR_KEMIP_INT, ki_set_contact_alias_trim,
2693 2733
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2694 2734
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2695 2735
 	},