Browse code

nathelper: fix_nated_sdp added ignoring RFC3605-param if omitted

- doesn't throw an error while "a=rtcp" param is omitted (RFC1889 behavior) or address is omitted
- replace_sdp_ip() returns 0 on omitted IP-address
- added param can_omit to replace_sdp_ip()

fixes #2784 regression introduced by #2497

Dmitry Wagin authored on 15/06/2021 12:58:12 • Victor Seva committed on 18/11/2021 10:16:31
Showing 1 changed files
... ...
@@ -1777,13 +1777,12 @@ static int is_rfc1918_f(struct sip_msg *msg, char *str1, char *str2)
1777 1777
 #define AOLDMEDPRT_LEN (sizeof(AOLDMEDPRT) - 1)
1778 1778
 
1779 1779
 
1780
-/* replace ip addresses in SDP and return umber of replacements */
1780
+/* replace ip addresses in SDP and return number of replacements */
1781 1781
 static inline int replace_sdp_ip(
1782
-		struct sip_msg *msg, str *org_body, char *line, str *ip, int linelen)
1782
+		struct sip_msg *msg, str *org_body, char *line, str *ip, int linelen, int can_omit)
1783 1783
 {
1784 1784
 	str body1, oldip, newip;
1785 1785
 	str body = *org_body;
1786
-	unsigned hasreplaced = 0;
1787 1786
 	int pf, pf1 = 0;
1788 1787
 	str body2;
1789 1788
 	char *bodylimit = body.s + body.len;
... ...
@@ -1799,10 +1798,17 @@ static inline int replace_sdp_ip(
1799 1798
 	}
1800 1799
 	body1 = body;
1801 1800
 	for(;;) {
1802
-		if(nh_extract_mediaip(&body1, &oldip, &pf, line, linelen) == -1)
1801
+		ret = nh_extract_mediaip(&body1, &oldip, &pf, line, linelen);
1802
+		if(ret == 0)
1803 1803
 			break;
1804
-		if(pf != AF_INET) {
1805
-			LM_ERR("not an IPv4 address in '%s' SDP\n", line);
1804
+		if(ret == -1) {
1805
+			if(can_omit) {
1806
+				body2.s = body1.s + linelen;
1807
+				body2.len = bodylimit - body2.s;
1808
+				body1 = body2;
1809
+				continue;
1810
+			}
1811
+			LM_ERR("no `IP[4|6]' in `%s' field\n", line);
1806 1812
 			return -1;
1807 1813
 		}
1808 1814
 		if(!pf1)
... ...
@@ -1820,13 +1826,8 @@ static inline int replace_sdp_ip(
1820 1826
 			return -1;
1821 1827
 		}
1822 1828
 		count += ret;
1823
-		hasreplaced = 1;
1824 1829
 		body1 = body2;
1825 1830
 	}
1826
-	if(!hasreplaced && linelen>=6 && memcmp("a=rtcp", line, 6)!=0) {
1827
-		LM_ERR("can't extract '%s' IP from the SDP\n", line);
1828
-		return -1;
1829
-	}
1830 1831
 
1831 1832
 	return count;
1832 1833
 }
... ...
@@ -1905,15 +1906,14 @@ static int ki_fix_nated_sdp_ip(sip_msg_t *msg, int level, str *ip)
1905 1906
 	if(level & (FIX_MEDIP | FIX_ORGIP)) {
1906 1907
 
1907 1908
 		/* Iterate all a=rtcp and replace ips in them. rfc3605 */
1908
-		ret = replace_sdp_ip(msg, &body, "a=rtcp", (ip && ip->len>0) ? ip : 0, 6);
1909
+		ret = replace_sdp_ip(msg, &body, "a=rtcp", (ip && ip->len>0) ? ip : 0, 6, 1);
1909 1910
 		if(ret == -1)
1910
-			LM_DBG("a=rtcp parameter does not exist. nothing to do.\n");
1911
-		else 
1912
-			count += ret;
1911
+			return -1;
1912
+		count += ret;
1913 1913
 
1914 1914
 		if(level & FIX_MEDIP) {
1915 1915
 			/* Iterate all c= and replace ips in them. */
1916
-			ret = replace_sdp_ip(msg, &body, "c=", (ip && ip->len>0) ? ip : 0, 2);
1916
+			ret = replace_sdp_ip(msg, &body, "c=", (ip && ip->len>0) ? ip : 0, 2, 0);
1917 1917
 			if(ret == -1)
1918 1918
 				return -1;
1919 1919
 			count += ret;
... ...
@@ -1921,7 +1921,7 @@ static int ki_fix_nated_sdp_ip(sip_msg_t *msg, int level, str *ip)
1921 1921
 
1922 1922
 		if(level & FIX_ORGIP) {
1923 1923
 			/* Iterate all o= and replace ips in them. */
1924
-			ret = replace_sdp_ip(msg, &body, "o=",  (ip && ip->len>0) ? ip : 0, 2);
1924
+			ret = replace_sdp_ip(msg, &body, "o=",  (ip && ip->len>0) ? ip : 0, 2, 0);
1925 1925
 			if(ret == -1)
1926 1926
 				return -1;
1927 1927
 			count += ret;
... ...
@@ -1968,7 +1968,7 @@ static int nh_extract_mediaip(str *body, str *mediaip, int *pf, char *line,
1968 1968
 		cp = cp1 + linelen;
1969 1969
 	}
1970 1970
 	if(cp1 == NULL)
1971
-		return -1;
1971
+		return 0;
1972 1972
 
1973 1973
 	mediaip->s = cp1 + linelen;
1974 1974
 	mediaip->len =
... ...
@@ -2003,7 +2003,6 @@ static int nh_extract_mediaip(str *body, str *mediaip, int *pf, char *line,
2003 2003
 		cp = eat_space_end(cp + len, mediaip->s + mediaip->len);
2004 2004
 	}
2005 2005
 	if(nextisip != 2 || mediaip->len == 0) {
2006
-		LM_ERR("no `IP[4|6]' in `%s' field\n", line);
2007 2006
 		return -1;
2008 2007
 	}
2009 2008
 	return 1;