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

(cherry picked from commit fa6038172c0095287096c7021ca5dadad728cfaa)

Dmitry Wagin authored on 15/06/2021 12:58:12 • Victor Seva committed on 18/11/2021 10:42:21
Showing 1 changed files
... ...
@@ -1609,13 +1609,12 @@ static int is_rfc1918_f(struct sip_msg *msg, char *str1, char *str2)
1609 1609
 #define AOLDMEDPRT_LEN (sizeof(AOLDMEDPRT) - 1)
1610 1610
 
1611 1611
 
1612
-/* replace ip addresses in SDP and return umber of replacements */
1612
+/* replace ip addresses in SDP and return number of replacements */
1613 1613
 static inline int replace_sdp_ip(
1614
-		struct sip_msg *msg, str *org_body, char *line, str *ip, int linelen)
1614
+		struct sip_msg *msg, str *org_body, char *line, str *ip, int linelen, int can_omit)
1615 1615
 {
1616 1616
 	str body1, oldip, newip;
1617 1617
 	str body = *org_body;
1618
-	unsigned hasreplaced = 0;
1619 1618
 	int pf, pf1 = 0;
1620 1619
 	str body2;
1621 1620
 	char *bodylimit = body.s + body.len;
... ...
@@ -1631,10 +1630,17 @@ static inline int replace_sdp_ip(
1631 1630
 	}
1632 1631
 	body1 = body;
1633 1632
 	for(;;) {
1634
-		if(nh_extract_mediaip(&body1, &oldip, &pf, line, linelen) == -1)
1633
+		ret = nh_extract_mediaip(&body1, &oldip, &pf, line, linelen);
1634
+		if(ret == 0)
1635 1635
 			break;
1636
-		if(pf != AF_INET) {
1637
-			LM_ERR("not an IPv4 address in '%s' SDP\n", line);
1636
+		if(ret == -1) {
1637
+			if(can_omit) {
1638
+				body2.s = body1.s + linelen;
1639
+				body2.len = bodylimit - body2.s;
1640
+				body1 = body2;
1641
+				continue;
1642
+			}
1643
+			LM_ERR("no `IP[4|6]' in `%s' field\n", line);
1638 1644
 			return -1;
1639 1645
 		}
1640 1646
 		if(!pf1)
... ...
@@ -1652,13 +1658,8 @@ static inline int replace_sdp_ip(
1652 1658
 			return -1;
1653 1659
 		}
1654 1660
 		count += ret;
1655
-		hasreplaced = 1;
1656 1661
 		body1 = body2;
1657 1662
 	}
1658
-	if(!hasreplaced) {
1659
-		LM_ERR("can't extract '%s' IP from the SDP\n", line);
1660
-		return -1;
1661
-	}
1662 1663
 
1663 1664
 	return count;
1664 1665
 }
... ...
@@ -1737,15 +1738,14 @@ static int ki_fix_nated_sdp_ip(sip_msg_t *msg, int level, str *ip)
1737 1738
 	if(level & (FIX_MEDIP | FIX_ORGIP)) {
1738 1739
 
1739 1740
 		/* Iterate all a=rtcp and replace ips in them. rfc3605 */
1740
-		ret = replace_sdp_ip(msg, &body, "a=rtcp", (ip && ip->len>0) ? ip : 0, 6);
1741
+		ret = replace_sdp_ip(msg, &body, "a=rtcp", (ip && ip->len>0) ? ip : 0, 6, 1);
1741 1742
 		if(ret == -1)
1742
-			LM_DBG("a=rtcp parameter does not exist. nothing to do.\n");
1743
-		else 
1744
-			count += ret;
1743
+			return -1;
1744
+		count += ret;
1745 1745
 
1746 1746
 		if(level & FIX_MEDIP) {
1747 1747
 			/* Iterate all c= and replace ips in them. */
1748
-			ret = replace_sdp_ip(msg, &body, "c=", (ip && ip->len>0) ? ip : 0, 2);
1748
+			ret = replace_sdp_ip(msg, &body, "c=", (ip && ip->len>0) ? ip : 0, 2, 0);
1749 1749
 			if(ret == -1)
1750 1750
 				return -1;
1751 1751
 			count += ret;
... ...
@@ -1753,7 +1753,7 @@ static int ki_fix_nated_sdp_ip(sip_msg_t *msg, int level, str *ip)
1753 1753
 
1754 1754
 		if(level & FIX_ORGIP) {
1755 1755
 			/* Iterate all o= and replace ips in them. */
1756
-			ret = replace_sdp_ip(msg, &body, "o=",  (ip && ip->len>0) ? ip : 0, 2);
1756
+			ret = replace_sdp_ip(msg, &body, "o=",  (ip && ip->len>0) ? ip : 0, 2, 0);
1757 1757
 			if(ret == -1)
1758 1758
 				return -1;
1759 1759
 			count += ret;
... ...
@@ -1800,7 +1800,7 @@ static int nh_extract_mediaip(str *body, str *mediaip, int *pf, char *line,
1800 1800
 		cp = cp1 + linelen;
1801 1801
 	}
1802 1802
 	if(cp1 == NULL)
1803
-		return -1;
1803
+		return 0;
1804 1804
 
1805 1805
 	mediaip->s = cp1 + linelen;
1806 1806
 	mediaip->len =
... ...
@@ -1835,7 +1835,6 @@ static int nh_extract_mediaip(str *body, str *mediaip, int *pf, char *line,
1835 1835
 		cp = eat_space_end(cp + len, mediaip->s + mediaip->len);
1836 1836
 	}
1837 1837
 	if(nextisip != 2 || mediaip->len == 0) {
1838
-		LM_ERR("no `IP[4|6]' in `%s' field\n", line);
1839 1838
 		return -1;
1840 1839
 	}
1841 1840
 	return 1;