Browse code

core: kemi - exported KSR.to_IPv4() and KSR.to_IPv6()

Daniel-Constantin Mierla authored on 05/07/2021 09:28:39
Showing 1 changed files
... ...
@@ -34,6 +34,7 @@
34 34
 #include "select_buf.h"
35 35
 #include "pvar.h"
36 36
 #include "trim.h"
37
+#include "resolve.h"
37 38
 #include "mem/shm.h"
38 39
 #include "parser/parse_uri.h"
39 40
 #include "parser/parse_from.h"
... ...
@@ -1653,6 +1654,88 @@ static int sr_kemi_core_to_proto_wsx(sip_msg_t *msg)
1653 1654
 	return (proto == PROTO_WS)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1654 1655
 }
1655 1656
 
1657
+/**
1658
+ *
1659
+ */
1660
+static int sr_kemi_core_to_af_helper(sip_msg_t *msg)
1661
+{
1662
+	sip_uri_t parsed_uri;
1663
+	str uri;
1664
+	str host;
1665
+
1666
+	if(msg==NULL) {
1667
+		return -1;
1668
+	}
1669
+	if(msg->first_line.type == SIP_REPLY) {
1670
+		/* REPLY doesnt have r/d-uri - use second Via */
1671
+		if(parse_headers( msg, HDR_VIA2_F, 0)==-1) {
1672
+			LM_DBG("no 2nd via parsed\n");
1673
+			return -1;
1674
+		}
1675
+		if((msg->via2==0) || (msg->via2->error!=PARSE_OK)) {
1676
+			return -1;
1677
+		}
1678
+		if(msg->via2->received) {
1679
+			LM_DBG("using 'received'\n");
1680
+			host = msg->via2->received->value;
1681
+		} else {
1682
+			LM_DBG("using via host\n");
1683
+			host = msg->via2->host;
1684
+		}
1685
+	} else {
1686
+		if (msg->dst_uri.s != NULL && msg->dst_uri.len>0) {
1687
+			uri = msg->dst_uri;
1688
+		} else {
1689
+			if (msg->new_uri.s!=NULL && msg->new_uri.len>0)
1690
+			{
1691
+				uri = msg->new_uri;
1692
+			} else {
1693
+				uri = msg->first_line.u.request.uri;
1694
+			}
1695
+		}
1696
+		if(parse_uri(uri.s, uri.len, &parsed_uri)!=0) {
1697
+			LM_ERR("failed to parse nh uri [%.*s]\n", uri.len, uri.s);
1698
+			return -1;
1699
+		}
1700
+		host = parsed_uri.host;
1701
+	}
1702
+
1703
+	if(host.len<=0) {
1704
+		return 0;
1705
+	}
1706
+	if(str2ip(&host)!=NULL) {
1707
+		return 4;
1708
+	}
1709
+	if(str2ip6(&host)!=NULL) {
1710
+		return 6;
1711
+	}
1712
+	return 0;
1713
+}
1714
+
1715
+/**
1716
+ *
1717
+ */
1718
+static int sr_kemi_core_to_af_ipv4(sip_msg_t *msg)
1719
+{
1720
+	int af;
1721
+
1722
+	af = sr_kemi_core_to_af_helper(msg);
1723
+	if (af == 4) { return SR_KEMI_TRUE; }
1724
+	return SR_KEMI_FALSE;
1725
+}
1726
+
1727
+/**
1728
+ *
1729
+ */
1730
+static int sr_kemi_core_to_af_ipv6(sip_msg_t *msg)
1731
+{
1732
+	int af;
1733
+
1734
+	af = sr_kemi_core_to_af_helper(msg);
1735
+	if (af == 6) { return SR_KEMI_TRUE; }
1736
+	return SR_KEMI_FALSE;
1737
+}
1738
+
1656 1739
 /**
1657 1740
  *
1658 1741
  */
... ...
@@ -2077,6 +2160,16 @@ static sr_kemi_t _sr_kemi_core[] = {
2077 2160
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2078 2161
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2079 2162
 	},
2163
+	{ str_init(""), str_init("to_IPv4"),
2164
+		SR_KEMIP_BOOL, sr_kemi_core_to_af_ipv4,
2165
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2166
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2167
+	},
2168
+	{ str_init(""), str_init("to_IPv6"),
2169
+		SR_KEMIP_BOOL, sr_kemi_core_to_af_ipv6,
2170
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2171
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2172
+	},
2080 2173
 	{ str_init(""), str_init("is_src_port"),
2081 2174
 		SR_KEMIP_BOOL, sr_kemi_core_is_src_port,
2082 2175
 		{ SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,