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,
Browse code

core: kemi - exported KSR.to_UDP()

Daniel-Constantin Mierla authored on 23/06/2021 16:39:35
Showing 1 changed files
... ...
@@ -2042,6 +2042,11 @@ static sr_kemi_t _sr_kemi_core[] = {
2042 2042
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2043 2043
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2044 2044
 	},
2045
+	{ str_init(""), str_init("to_UDP"),
2046
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_udp,
2047
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2048
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2049
+	},
2045 2050
 	{ str_init(""), str_init("to_TCP"),
2046 2051
 		SR_KEMIP_BOOL, sr_kemi_core_to_proto_tcp,
2047 2052
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

core: kemi - added to_TCP/TLS/SCTP/WS/WSS/WSX()

Daniel-Constantin Mierla authored on 15/06/2021 07:16:58
Showing 1 changed files
... ...
@@ -1586,6 +1586,72 @@ static int sr_kemi_core_to_proto_udp(sip_msg_t *msg)
1586 1586
 	return (proto == PROTO_UDP)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1587 1587
 }
1588 1588
 
1589
+/**
1590
+ *
1591
+ */
1592
+static int sr_kemi_core_to_proto_tcp(sip_msg_t *msg)
1593
+{
1594
+	int proto;
1595
+
1596
+	proto = sr_kemi_core_to_proto_helper(msg);
1597
+	return (proto == PROTO_TCP)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1598
+}
1599
+
1600
+/**
1601
+ *
1602
+ */
1603
+static int sr_kemi_core_to_proto_tls(sip_msg_t *msg)
1604
+{
1605
+	int proto;
1606
+
1607
+	proto = sr_kemi_core_to_proto_helper(msg);
1608
+	return (proto == PROTO_TLS)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1609
+}
1610
+
1611
+/**
1612
+ *
1613
+ */
1614
+static int sr_kemi_core_to_proto_sctp(sip_msg_t *msg)
1615
+{
1616
+	int proto;
1617
+
1618
+	proto = sr_kemi_core_to_proto_helper(msg);
1619
+	return (proto == PROTO_SCTP)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1620
+}
1621
+
1622
+/**
1623
+ *
1624
+ */
1625
+static int sr_kemi_core_to_proto_ws(sip_msg_t *msg)
1626
+{
1627
+	int proto;
1628
+
1629
+	proto = sr_kemi_core_to_proto_helper(msg);
1630
+	return (proto == PROTO_WS)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1631
+}
1632
+
1633
+/**
1634
+ *
1635
+ */
1636
+static int sr_kemi_core_to_proto_wss(sip_msg_t *msg)
1637
+{
1638
+	int proto;
1639
+
1640
+	proto = sr_kemi_core_to_proto_helper(msg);
1641
+	return (proto == PROTO_WSS)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1642
+}
1643
+
1644
+/**
1645
+ *
1646
+ */
1647
+static int sr_kemi_core_to_proto_wsx(sip_msg_t *msg)
1648
+{
1649
+	int proto;
1650
+
1651
+	proto = sr_kemi_core_to_proto_helper(msg);
1652
+	if (proto == PROTO_WSS) { return SR_KEMI_TRUE; }
1653
+	return (proto == PROTO_WS)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1654
+}
1589 1655
 
1590 1656
 /**
1591 1657
  *
... ...
@@ -1976,8 +2042,33 @@ static sr_kemi_t _sr_kemi_core[] = {
1976 2042
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1977 2043
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1978 2044
 	},
1979
-	{ str_init(""), str_init("to_UDP"),
1980
-		SR_KEMIP_BOOL, sr_kemi_core_to_proto_udp,
2045
+	{ str_init(""), str_init("to_TCP"),
2046
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_tcp,
2047
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2048
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2049
+	},
2050
+	{ str_init(""), str_init("to_TLS"),
2051
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_tls,
2052
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2053
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2054
+	},
2055
+	{ str_init(""), str_init("to_SCTP"),
2056
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_sctp,
2057
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2058
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2059
+	},
2060
+	{ str_init(""), str_init("to_WS"),
2061
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_ws,
2062
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2063
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2064
+	},
2065
+	{ str_init(""), str_init("to_WSS"),
2066
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_wss,
2067
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
2068
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2069
+	},
2070
+	{ str_init(""), str_init("to_WSX"),
2071
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_wsx,
1981 2072
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1982 2073
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1983 2074
 	},
Browse code

core: kemi - added KSR.to_UDP() - return true if target is over UDP

- uses the R-URI/D-URI for requests (no DNS query done if transport is missing)
and 2nd Via for replies

Daniel-Constantin Mierla authored on 14/06/2021 11:22:35
Showing 1 changed files
... ...
@@ -1536,6 +1536,57 @@ static int sr_kemi_core_route(sip_msg_t *msg, str *route)
1536 1536
 	return ret;
1537 1537
 }
1538 1538
 
1539
+/**
1540
+ *
1541
+ */
1542
+static int sr_kemi_core_to_proto_helper(sip_msg_t *msg)
1543
+{
1544
+	sip_uri_t parsed_uri;
1545
+	str uri;
1546
+
1547
+	if(msg==NULL) {
1548
+		return -1;
1549
+	}
1550
+	if(msg->first_line.type == SIP_REPLY) {
1551
+		/* REPLY doesnt have r/d-uri - use second Via */
1552
+		if(parse_headers( msg, HDR_VIA2_F, 0)==-1) {
1553
+			LM_DBG("no 2nd via parsed\n");
1554
+			return -1;
1555
+		}
1556
+		if((msg->via2==0) || (msg->via2->error!=PARSE_OK)) {
1557
+			return -1;
1558
+		}
1559
+		return (int)msg->via2->proto;
1560
+	}
1561
+	if (msg->dst_uri.s != NULL && msg->dst_uri.len>0) {
1562
+		uri = msg->dst_uri;
1563
+	} else {
1564
+		if (msg->new_uri.s!=NULL && msg->new_uri.len>0)
1565
+		{
1566
+			uri = msg->new_uri;
1567
+		} else {
1568
+			uri = msg->first_line.u.request.uri;
1569
+		}
1570
+	}
1571
+	if(parse_uri(uri.s, uri.len, &parsed_uri)!=0) {
1572
+		LM_ERR("failed to parse nh uri [%.*s]\n", uri.len, uri.s);
1573
+		return -1;
1574
+	}
1575
+	return (int)parsed_uri.proto;
1576
+}
1577
+
1578
+/**
1579
+ *
1580
+ */
1581
+static int sr_kemi_core_to_proto_udp(sip_msg_t *msg)
1582
+{
1583
+	int proto;
1584
+
1585
+	proto = sr_kemi_core_to_proto_helper(msg);
1586
+	return (proto == PROTO_UDP)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1587
+}
1588
+
1589
+
1539 1590
 /**
1540 1591
  *
1541 1592
  */
... ...
@@ -1925,6 +1976,11 @@ static sr_kemi_t _sr_kemi_core[] = {
1925 1976
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1926 1977
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1927 1978
 	},
1979
+	{ str_init(""), str_init("to_UDP"),
1980
+		SR_KEMIP_BOOL, sr_kemi_core_to_proto_udp,
1981
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1982
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1983
+	},
1928 1984
 	{ str_init(""), str_init("is_src_port"),
1929 1985
 		SR_KEMIP_BOOL, sr_kemi_core_is_src_port,
1930 1986
 		{ SR_KEMIP_INT, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

core: kemi - added KSR.is_WSX() - return true of proto is WS or WSS

Daniel-Constantin Mierla authored on 09/06/2021 12:48:19
Showing 1 changed files
... ...
@@ -1155,6 +1155,18 @@ static int sr_kemi_core_is_proto_wss(sip_msg_t *msg)
1155 1155
 	return (msg->rcv.proto == PROTO_WSS)?SR_KEMI_TRUE:SR_KEMI_FALSE;
1156 1156
 }
1157 1157
 
1158
+/**
1159
+ *
1160
+ */
1161
+static int sr_kemi_core_is_proto_wsx(sip_msg_t *msg)
1162
+{
1163
+	if (msg->rcv.proto == PROTO_WSS) return SR_KEMI_TRUE;
1164
+	if (msg->rcv.proto == PROTO_WS) return SR_KEMI_TRUE;
1165
+
1166
+	return SR_KEMI_FALSE;
1167
+}
1168
+
1169
+
1158 1170
 /**
1159 1171
  *
1160 1172
  */
... ...
@@ -1888,6 +1900,11 @@ static sr_kemi_t _sr_kemi_core[] = {
1888 1900
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1889 1901
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1890 1902
 	},
1903
+	{ str_init(""), str_init("is_WSX"),
1904
+		SR_KEMIP_BOOL, sr_kemi_core_is_proto_wsx,
1905
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1906
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1907
+	},
1891 1908
 	{ str_init(""), str_init("is_SCTP"),
1892 1909
 		SR_KEMIP_BOOL, sr_kemi_core_is_proto_sctp,
1893 1910
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

core: kemi - check hdr type HDR_ERROR_T after parsing

Daniel-Constantin Mierla authored on 02/06/2021 16:57:53
Showing 1 changed files
... ...
@@ -1988,7 +1988,8 @@ static int sr_kemi_hdr_append_after(sip_msg_t *msg, str *txt, str *hname)
1988 1988
 	hbuf[hname->len] = ':';
1989 1989
 	hbuf[hname->len+1] = '\0';
1990 1990
 
1991
-	if (parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm)==0) {
1991
+	parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm);
1992
+	if(hfm.type==HDR_ERROR_T) {
1992 1993
 		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
1993 1994
 		return -1;
1994 1995
 	}
... ...
@@ -2056,7 +2057,8 @@ int sr_kemi_hdr_remove(sip_msg_t *msg, str *hname)
2056 2057
 	hbuf[hname->len] = ':';
2057 2058
 	hbuf[hname->len+1] = '\0';
2058 2059
 
2059
-	if (parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm)==0) {
2060
+	parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm);
2061
+	if(hfm.type==HDR_ERROR_T) {
2060 2062
 		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2061 2063
 		return -1;
2062 2064
 	}
... ...
@@ -2106,7 +2108,8 @@ static int sr_kemi_hdr_is_present(sip_msg_t *msg, str *hname)
2106 2108
 	hbuf[hname->len] = ':';
2107 2109
 	hbuf[hname->len+1] = '\0';
2108 2110
 
2109
-	if (parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm)==0) {
2111
+	parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm);
2112
+	if(hfm.type==HDR_ERROR_T) {
2110 2113
 		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2111 2114
 		return -1;
2112 2115
 	}
... ...
@@ -2188,7 +2191,8 @@ static int sr_kemi_hdr_insert_before(sip_msg_t *msg, str *txt, str *hname)
2188 2191
 	hbuf[hname->len] = ':';
2189 2192
 	hbuf[hname->len+1] = '\0';
2190 2193
 
2191
-	if (parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm)==0) {
2194
+	parse_hname2_short(hbuf, hbuf+hname->len+1, &hfm);
2195
+	if(hfm.type==HDR_ERROR_T) {
2192 2196
 		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2193 2197
 		return -1;
2194 2198
 	}
... ...
@@ -2305,7 +2309,8 @@ static sr_kemi_xval_t* sr_kemi_hdr_get_mode(sip_msg_t *msg, str *hname, int idx,
2305 2309
 		sr_kemi_xval_null(&_sr_kemi_xval, rmode);
2306 2310
 		return &_sr_kemi_xval;
2307 2311
 	}
2308
-	if (parse_hname2_str(hname, &shdr)==0) {
2312
+	parse_hname2_str(hname, &shdr);
2313
+	if(shdr.type==HDR_ERROR_T) {
2309 2314
 		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2310 2315
 		sr_kemi_xval_null(&_sr_kemi_xval, rmode);
2311 2316
 		return &_sr_kemi_xval;
... ...
@@ -2426,7 +2431,8 @@ static int sr_kemi_hdr_match_content(sip_msg_t *msg, str *hname, str *op,
2426 2431
 		return SR_KEMI_FALSE;
2427 2432
 	}
2428 2433
 
2429
-	if (parse_hname2_str(hname, &hfm)==0) {
2434
+	parse_hname2_str(hname, &hfm);
2435
+	if(hfm.type==HDR_ERROR_T) {
2430 2436
 		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2431 2437
 		return SR_KEMI_FALSE;
2432 2438
 	}
Browse code

core: kemi - exported KSR.pv.geti("pv")

- return integer value of the pv, trying to convert if its value is
string

Daniel-Constantin Mierla authored on 12/05/2021 06:15:33
Showing 1 changed files
... ...
@@ -2855,6 +2855,47 @@ static sr_kemi_xval_t* sr_kemi_pv_getvn (sip_msg_t *msg, str *pvn, int xival)
2855 2855
 	return sr_kemi_pv_get_valx (msg, pvn, NULL, xival, 1);
2856 2856
 }
2857 2857
 
2858
+/**
2859
+ *
2860
+ */
2861
+static int sr_kemi_pv_geti (sip_msg_t *msg, str *pvn)
2862
+{
2863
+	pv_spec_t *pvs;
2864
+	pv_value_t val;
2865
+	int vi;
2866
+
2867
+	LM_DBG("pv get: %.*s\n", pvn->len, pvn->s);
2868
+	vi = pv_locate_name(pvn);
2869
+	if(vi != pvn->len) {
2870
+		LM_WARN("invalid pv [%.*s] (%d/%d)\n", pvn->len, pvn->s, vi, pvn->len);
2871
+		return 0;
2872
+	}
2873
+	pvs = pv_cache_get(pvn);
2874
+	if(pvs==NULL) {
2875
+		LM_WARN("cannot get pv spec for [%.*s]\n", pvn->len, pvn->s);
2876
+		return 0;
2877
+	}
2878
+
2879
+	memset(&val, 0, sizeof(pv_value_t));
2880
+	if(pv_get_spec_value(msg, pvs, &val) != 0) {
2881
+		LM_WARN("unable to get pv value for [%.*s]\n", pvn->len, pvn->s);
2882
+		return 0;
2883
+	}
2884
+	if(val.flags&PV_VAL_NULL) {
2885
+		return 0;
2886
+	}
2887
+	if(val.flags&(PV_TYPE_INT|PV_VAL_INT)) {
2888
+		return val.ri;
2889
+	}
2890
+	if(val.ri!=0) {
2891
+		return val.ri;
2892
+	}
2893
+	vi = 0;
2894
+	str2sint(&val.rs, &vi);
2895
+
2896
+	return vi;
2897
+}
2898
+
2858 2899
 /**
2859 2900
  *
2860 2901
  */
... ...
@@ -3003,6 +3044,11 @@ static sr_kemi_t _sr_kemi_pv[] = {
3003 3044
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3004 3045
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3005 3046
 	},
3047
+	{ str_init("pv"), str_init("geti"),
3048
+		SR_KEMIP_INT, sr_kemi_pv_geti,
3049
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3050
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3051
+	},
3006 3052
 	{ str_init("pv"), str_init("getvn"),
3007 3053
 		SR_KEMIP_XVAL, sr_kemi_pv_getvn,
3008 3054
 		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_NONE,
Browse code

core: kemi - parse headers before insert operation

Daniel-Constantin Mierla authored on 28/04/2021 14:06:50
Showing 1 changed files
... ...
@@ -2143,6 +2143,11 @@ static int sr_kemi_hdr_insert(sip_msg_t *msg, str *txt)
2143 2143
 	if(txt==NULL || txt->s==NULL || msg==NULL)
2144 2144
 		return -1;
2145 2145
 
2146
+	if ((parse_headers(msg, HDR_EOH_F, 0) == -1) || (msg->headers == NULL)) {
2147
+		LM_ERR("error while parsing message\n");
2148
+		return -1;
2149
+	}
2150
+
2146 2151
 	LM_DBG("insert hf: %.*s\n", txt->len, txt->s);
2147 2152
 	hdr = (char*)pkg_malloc(txt->len);
2148 2153
 	if(hdr==NULL) {
... ...
@@ -2188,7 +2193,7 @@ static int sr_kemi_hdr_insert_before(sip_msg_t *msg, str *txt, str *hname)
2188 2193
 		return -1;
2189 2194
 	}
2190 2195
 
2191
-	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
2196
+	if ((parse_headers(msg, HDR_EOH_F, 0) == -1) || (msg->headers == NULL)) {
2192 2197
 		LM_ERR("error while parsing message\n");
2193 2198
 		return -1;
2194 2199
 	}
Browse code

core: kemi - KSR.is_method_in() uses E/e to match PRACK

Daniel-Constantin Mierla authored on 19/12/2020 14:34:05
Showing 1 changed files
... ...
@@ -852,6 +852,12 @@ static int sr_kemi_core_is_method_in(sip_msg_t *msg, str *vmethod)
852 852
 					return SR_KEMI_TRUE;
853 853
 				}
854 854
 			break;
855
+			case 'E':
856
+			case 'e':
857
+				if(imethod==METHOD_PRACK) {
858
+					return SR_KEMI_TRUE;
859
+				}
860
+			break;
855 861
 			case 'P':
856 862
 			case 'p':
857 863
 				if(imethod==METHOD_PUBLISH) {
Browse code

core: kemi - use parse_hname2_str() to avoid cloning header name

Daniel-Constantin Mierla authored on 23/10/2020 07:28:14
Showing 1 changed files
... ...
@@ -2275,8 +2275,6 @@ static int sr_kemi_hdr_append_to_reply(sip_msg_t *msg, str *txt)
2275 2275
 static sr_kemi_xval_t* sr_kemi_hdr_get_mode(sip_msg_t *msg, str *hname, int idx,
2276 2276
 		int rmode)
2277 2277
 {
2278
-	char hbuf[256];
2279
-	str s;
2280 2278
 	hdr_field_t shdr;
2281 2279
 	hdr_field_t *ihdr;
2282 2280
 #define SR_KEMI_VHDR_SIZE 256
... ...
@@ -2296,23 +2294,12 @@ static sr_kemi_xval_t* sr_kemi_hdr_get_mode(sip_msg_t *msg, str *hname, int idx,
2296 2294
 		sr_kemi_xval_null(&_sr_kemi_xval, rmode);
2297 2295
 		return &_sr_kemi_xval;
2298 2296
 	}
2299
-	if(hname->len>=252) {
2300
-		LM_ERR("header name too long\n");
2297
+	if (parse_hname2_str(hname, &shdr)==0) {
2298
+		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2301 2299
 		sr_kemi_xval_null(&_sr_kemi_xval, rmode);
2302 2300
 		return &_sr_kemi_xval;
2303 2301
 	}
2304 2302
 
2305
-	memcpy(hbuf, hname->s, hname->len);
2306
-	hbuf[hname->len] = ':';
2307
-	hbuf[hname->len+1] = '\0';
2308
-	s.s = hbuf;
2309
-	s.len = hname->len + 1;
2310
-
2311
-	if (parse_hname2_short(s.s, s.s + s.len, &shdr)==0) {
2312
-		LM_ERR("error parsing header name [%.*s]\n", s.len, s.s);
2313
-		sr_kemi_xval_null(&_sr_kemi_xval, rmode);
2314
-		return &_sr_kemi_xval;
2315
-	}
2316 2303
 	n = 0;
2317 2304
 	for (ihdr=msg->headers; ihdr; ihdr=ihdr->next) {
2318 2305
 		hmatch = 0;
Browse code

core: kemi - proper conditions for operators in sr_kemi_hdr_match_content()

Daniel-Constantin Mierla authored on 19/10/2020 10:59:15
Showing 1 changed files
... ...
@@ -2441,13 +2441,13 @@ static int sr_kemi_hdr_match_content(sip_msg_t *msg, str *hname, str *op,
2441 2441
 	if(op->len == 2) {
2442 2442
 		if(strncasecmp(op->s, "eq", 2) == 0) {
2443 2443
 			opval = 1;
2444
-		} if(strncasecmp(op->s, "ne", 2) == 0) {
2444
+		} else if(strncasecmp(op->s, "ne", 2) == 0) {
2445 2445
 			opval = 2;
2446
-		} if(strncasecmp(op->s, "sw", 2) == 0) {
2446
+		} else if(strncasecmp(op->s, "sw", 2) == 0) {
2447 2447
 			opval = 3;
2448
-		} if(strncasecmp(op->s, "in", 2) == 0) {
2448
+		} else if(strncasecmp(op->s, "in", 2) == 0) {
2449 2449
 			opval = 4;
2450
-		} if(strncasecmp(op->s, "re", 2) == 0) {
2450
+		} else if(strncasecmp(op->s, "re", 2) == 0) {
2451 2451
 			opval = 5;
2452 2452
 			LM_ERR("operator not implemented: %.*s\n", op->len, op->s);
2453 2453
 			return SR_KEMI_FALSE;
... ...
@@ -2468,7 +2468,7 @@ static int sr_kemi_hdr_match_content(sip_msg_t *msg, str *hname, str *op,
2468 2468
 		} else if(hidx->s[0]=='l' || hidx->s[0]=='L') {
2469 2469
 			/* last */
2470 2470
 			hidxval = 2;
2471
-		} else if(hidx->s[0]=='a' || hidx->s[0]=='a') {
2471
+		} else if(hidx->s[0]=='a' || hidx->s[0]=='A') {
2472 2472
 			/* all */
2473 2473
 			hidxval = 3;
2474 2474
 		} else if(hidx->s[0]=='o' || hidx->s[0]=='O') {
Browse code

core: kemi - new function hdr.match_content(hname, op, mval, hidx)

- return true/false based on matching header content

Daniel-Constantin Mierla authored on 06/10/2020 10:56:09
Showing 1 changed files
... ...
@@ -33,6 +33,7 @@
33 33
 #include "strutils.h"
34 34
 #include "select_buf.h"
35 35
 #include "pvar.h"
36
+#include "trim.h"
36 37
 #include "mem/shm.h"
37 38
 #include "parser/parse_uri.h"
38 39
 #include "parser/parse_from.h"
... ...
@@ -2409,6 +2410,165 @@ static sr_kemi_xval_t* sr_kemi_hdr_getw_idx(sip_msg_t *msg, str *hname, int idx)
2409 2410
 	return sr_kemi_hdr_get_mode(msg, hname, idx, SR_KEMI_XVAL_NULL_PRINT);
2410 2411
 }
2411 2412
 
2413
+/**
2414
+ *
2415
+ */
2416
+static int sr_kemi_hdr_match_content(sip_msg_t *msg, str *hname, str *op,
2417
+		str *mval, str *hidx)
2418
+{
2419
+	hdr_field_t *hf;
2420
+	hdr_field_t hfm;
2421
+	int opval = 0;
2422
+	int hidxval = 0;
2423
+	int matched = 0;
2424
+	int hnum = 0;
2425
+	str hbody = STR_NULL;
2426
+
2427
+	if(hname==NULL || hname->s==NULL || msg==NULL) {
2428
+		return SR_KEMI_FALSE;
2429
+	}
2430
+
2431
+	if (parse_hname2_str(hname, &hfm)==0) {
2432
+		LM_ERR("error parsing header name [%.*s]\n", hname->len, hname->s);
2433
+		return SR_KEMI_FALSE;
2434
+	}
2435
+
2436
+	if (parse_headers(msg, HDR_EOH_F, 0) == -1) {
2437
+		LM_ERR("error while parsing message\n");
2438
+		return SR_KEMI_FALSE;
2439
+	}
2440
+
2441
+	if(op->len == 2) {
2442
+		if(strncasecmp(op->s, "eq", 2) == 0) {
2443
+			opval = 1;
2444
+		} if(strncasecmp(op->s, "ne", 2) == 0) {
2445
+			opval = 2;
2446
+		} if(strncasecmp(op->s, "sw", 2) == 0) {
2447
+			opval = 3;
2448
+		} if(strncasecmp(op->s, "in", 2) == 0) {
2449
+			opval = 4;
2450
+		} if(strncasecmp(op->s, "re", 2) == 0) {
2451
+			opval = 5;
2452
+			LM_ERR("operator not implemented: %.*s\n", op->len, op->s);
2453
+			return SR_KEMI_FALSE;
2454
+		} else {
2455
+			LM_ERR("invalid operator: %.*s\n", op->len, op->s);
2456
+			return SR_KEMI_FALSE;
2457
+		}
2458
+	} else {
2459
+		LM_ERR("invalid operator: %.*s\n", op->len, op->s);
2460
+		return SR_KEMI_FALSE;
2461
+	}
2462
+
2463
+
2464
+	if(hidx->len >= 1) {
2465
+		if(hidx->s[0]=='f' || hidx->s[0]=='F') {
2466
+			/* first */
2467
+			hidxval = 1;
2468
+		} else if(hidx->s[0]=='l' || hidx->s[0]=='L') {
2469
+			/* last */
2470
+			hidxval = 2;
2471
+		} else if(hidx->s[0]=='a' || hidx->s[0]=='a') {
2472
+			/* all */
2473
+			hidxval = 3;
2474
+		} else if(hidx->s[0]=='o' || hidx->s[0]=='O') {
2475
+			/* one - at least one */
2476
+			hidxval = 4;
2477
+		} else {
2478
+			LM_ERR("invalid header index: %.*s\n", hidx->len, hidx->s);
2479
+			return SR_KEMI_FALSE;
2480
+		}
2481
+	} else {
2482
+		LM_ERR("invalid header index: %.*s\n", hidx->len, hidx->s);
2483
+		return SR_KEMI_FALSE;
2484
+	}
2485
+
2486
+	LM_DBG("searching hf: %.*s\n", hname->len, hname->s);
2487
+	for (hf=msg->headers; hf; hf=hf->next) {
2488
+		if (hfm.type!=HDR_OTHER_T && hfm.type!=HDR_ERROR_T) {
2489
+			if (hfm.type!=hf->type) {
2490
+				continue;
2491
+			}
2492
+		} else {
2493
+			if (hf->name.len!=hname->len) {
2494
+				continue;
2495
+			}
2496
+			if(strncasecmp(hf->name.s, hname->s, hname->len)!=0) {
2497
+				continue;
2498
+			}
2499
+		}
2500
+		hnum++;
2501
+		matched = 0;
2502
+		hbody = hf->body;
2503
+		trim(&hbody);
2504
+		switch(opval) {
2505
+			case 1:
2506
+			case 2:
2507
+				if(mval->len != hbody.len) {
2508
+					if(opval == 2) {
2509
+						/* ne */
2510
+						matched = 1;
2511
+					}
2512
+				} else {
2513
+					if(strncasecmp(mval->s, hbody.s, hbody.len) == 0) {
2514
+						if(opval == 1) {
2515
+							/* eq */
2516
+							matched = 1;
2517
+						}
2518
+					}
2519
+				}
2520
+				break;
2521
+			case 3:
2522
+				/* sw */
2523
+				if(hbody.len >= mval->len) {
2524
+					if(strncasecmp(hbody.s, mval->s, mval->len) == 0) {
2525
+						matched = 1;
2526
+					}
2527
+				}
2528
+				break;
2529
+			case 4:
2530
+				/* in */
2531
+				if(hbody.len >= mval->len) {
2532
+					if(str_casesearch(&hbody, mval) != NULL) {
2533
+						matched = 1;
2534
+					}
2535
+				}
2536
+				break;
2537
+			case 5:
2538
+				/* re */
2539
+				break;
2540
+		}
2541
+		if(hnum==1 && hidxval==1) {
2542
+			/* first */
2543
+			if(matched == 1) {
2544
+				return SR_KEMI_TRUE;
2545
+			} else {
2546
+				return SR_KEMI_FALSE;
2547
+			}
2548
+		}
2549
+		if(hidxval==3) {
2550
+			/* all */
2551
+			if(matched == 0) {
2552
+				return SR_KEMI_FALSE;
2553
+			}
2554
+		}
2555
+		if(hidxval==4) {
2556
+			/* one */
2557
+			if(matched == 1) {
2558
+				return SR_KEMI_TRUE;
2559
+			}
2560
+		}
2561
+	}
2562
+
2563
+	/* last - all */
2564
+	if(matched == 1) {
2565
+		return SR_KEMI_TRUE;
2566
+	} else {
2567
+		return SR_KEMI_FALSE;
2568
+	}
2569
+}
2570
+
2571
+
2412 2572
 /**
2413 2573
  *
2414 2574
  */
... ...
@@ -2488,6 +2648,11 @@ static sr_kemi_t _sr_kemi_hdr[] = {
2488 2648
 		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_NONE,
2489 2649
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2490 2650
 	},
2651
+	{ str_init("hdr"), str_init("match_content"),
2652
+		SR_KEMIP_BOOL, sr_kemi_hdr_match_content,
2653
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
2654
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
2655
+	},
2491 2656
 
2492 2657
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
2493 2658
 };
Browse code

core: kemi - exported core route function

Daniel-Constantin Mierla authored on 20/08/2020 14:16:56
Showing 1 changed files
... ...
@@ -1482,6 +1482,41 @@ static int sr_kemi_core_get_debug(sip_msg_t *msg)
1482 1482
 	return get_cfg_debug_level();
1483 1483
 }
1484 1484
 
1485
+/**
1486
+ *
1487
+ */
1488
+static int sr_kemi_core_route(sip_msg_t *msg, str *route)
1489
+{
1490
+	run_act_ctx_t tctx;
1491
+	run_act_ctx_t *pctx = NULL;
1492
+	int rtid = -1;
1493
+	int ret = 0;
1494
+
1495
+	if(route == NULL || route->s == NULL) {
1496
+		return -1;
1497
+	}
1498
+
1499
+	rtid = route_lookup(&main_rt, route->s);
1500
+	if (rtid < 0) {
1501
+		return -1;
1502
+	}
1503
+
1504
+	if(_sr_kemi_act_ctx != NULL) {
1505
+		pctx = _sr_kemi_act_ctx;
1506
+	} else {
1507
+		init_run_actions_ctx(&tctx);
1508
+		pctx = &tctx;
1509
+	}
1510
+
1511
+	ret=run_actions(pctx, main_rt.rlist[rtid], msg);
1512
+
1513
+	if (pctx->run_flags & EXIT_R_F) {
1514
+		return 0;
1515
+	}
1516
+
1517
+	return ret;
1518
+}
1519
+
1485 1520
 /**
1486 1521
  *
1487 1522
  */
... ...
@@ -1881,6 +1916,11 @@ static sr_kemi_t _sr_kemi_core[] = {
1881 1916
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1882 1917
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1883 1918
 	},
1919
+	{ str_init(""), str_init("route"),
1920
+		SR_KEMIP_INT, sr_kemi_core_route,
1921
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1922
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1923
+	},
1884 1924
 
1885 1925
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1886 1926
 };
Browse code

core: exposed kemi function to remove a header by name

Daniel-Constantin Mierla authored on 28/05/2020 07:26:43
Showing 1 changed files
... ...
@@ -1991,7 +1991,7 @@ static int sr_kemi_hdr_append_after(sip_msg_t *msg, str *txt, str *hname)
1991 1991
 /**
1992 1992
  *
1993 1993
  */
1994
-static int sr_kemi_hdr_remove(sip_msg_t *msg, str *hname)
1994
+int sr_kemi_hdr_remove(sip_msg_t *msg, str *hname)
1995 1995
 {
1996 1996
 	struct lump* anchor;
1997 1997
 	hdr_field_t *hf;
Browse code

core: kemi - functions to test http methods

Daniel-Constantin Mierla authored on 19/05/2020 12:23:01
Showing 1 changed files
... ...
@@ -1076,6 +1076,38 @@ static int sr_kemi_core_is_method_kdmq(sip_msg_t *msg)
1076 1076
 }
1077 1077
 
1078 1078
 
1079
+/**
1080
+ *
1081
+ */
1082
+static int sr_kemi_core_is_method_get(sip_msg_t *msg)
1083
+{
1084
+	return sr_kemi_core_is_method_type(msg, METHOD_GET);
1085
+}
1086
+
1087
+/**
1088
+ *
1089
+ */
1090
+static int sr_kemi_core_is_method_post(sip_msg_t *msg)
1091
+{
1092
+	return sr_kemi_core_is_method_type(msg, METHOD_POST);
1093
+}
1094
+
1095
+/**
1096
+ *
1097
+ */
1098
+static int sr_kemi_core_is_method_put(sip_msg_t *msg)
1099
+{
1100
+	return sr_kemi_core_is_method_type(msg, METHOD_PUT);
1101
+}
1102
+
1103
+/**
1104
+ *
1105
+ */
1106
+static int sr_kemi_core_is_method_delete(sip_msg_t *msg)
1107
+{
1108
+	return sr_kemi_core_is_method_type(msg, METHOD_DELETE);
1109
+}
1110
+
1079 1111
 /**
1080 1112
  *
1081 1113
  */
... ...
@@ -1769,6 +1801,26 @@ static sr_kemi_t _sr_kemi_core[] = {
1769 1801
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1770 1802
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1771 1803
 	},
1804
+	{ str_init(""), str_init("is_GET"),
1805
+		SR_KEMIP_BOOL, sr_kemi_core_is_method_get,
1806
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1807
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1808
+	},
1809
+	{ str_init(""), str_init("is_POST"),
1810
+		SR_KEMIP_BOOL, sr_kemi_core_is_method_post,
1811
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1812
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1813
+	},
1814
+	{ str_init(""), str_init("is_PUT"),
1815
+		SR_KEMIP_BOOL, sr_kemi_core_is_method_put,
1816
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1817
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1818
+	},
1819
+	{ str_init(""), str_init("is_DELETE"),
1820
+		SR_KEMIP_BOOL, sr_kemi_core_is_method_delete,
1821
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1822
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1823
+	},
1772 1824
 	{ str_init(""), str_init("is_UDP"),
1773 1825
 		SR_KEMIP_BOOL, sr_kemi_core_is_proto_udp,
1774 1826
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

core: kemi - added KSR.is_KDQM()

Daniel-Constantin Mierla authored on 18/05/2020 09:28:20
Showing 1 changed files
... ...
@@ -1067,6 +1067,15 @@ static int sr_kemi_core_is_method_message(sip_msg_t *msg)
1067 1067
 }
1068 1068
 
1069 1069
 
1070
+/**
1071
+ *
1072
+ */
1073
+static int sr_kemi_core_is_method_kdmq(sip_msg_t *msg)
1074
+{
1075
+	return sr_kemi_core_is_method_type(msg, METHOD_KDMQ);
1076
+}
1077
+
1078
+
1070 1079
 /**
1071 1080
  *
1072 1081
  */
... ...
@@ -1755,6 +1764,11 @@ static sr_kemi_t _sr_kemi_core[] = {
1755 1764
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1756 1765
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1757 1766
 	},
1767
+	{ str_init(""), str_init("is_KDMQ"),
1768
+		SR_KEMIP_BOOL, sr_kemi_core_is_method_kdmq,
1769
+		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
1770
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1771
+	},
1758 1772
 	{ str_init(""), str_init("is_UDP"),
1759 1773
 		SR_KEMIP_BOOL, sr_kemi_core_is_proto_udp,
1760 1774
 		{ SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

core: kemi - added missing is_MESSAGE()

- GH #2331

Daniel-Constantin Mierla authored on 18/05/2020 06:29:09
Showing 1 changed files
... ...
@@ -1057,6 +1057,16 @@ static int sr_kemi_core_is_method_prack(sip_msg_t *msg)
1057 1057
 	return sr_kemi_core_is_method_type(msg, METHOD_PRACK);