Procházet zdrojové kódy

nathelper(k): added the select for rewriting the contact

- exsting in the ser flavour version
Daniel-Constantin Mierla před 12 roky
rodič
revize
b2ef844bfd

+ 33 - 8
modules_k/nathelper/README

@@ -78,6 +78,10 @@ Ovidiu Sas
 
 
               7.1. nh_enable_ping
               7.1. nh_enable_ping
 
 
+        8. Selects
+
+              8.1. @nathelper.rewrite_contact[n]
+
    2. Frequently Asked Questions
    2. Frequently Asked Questions
 
 
    List of Examples
    List of Examples
@@ -102,6 +106,7 @@ Ovidiu Sas
    1.18. $rr_count usage
    1.18. $rr_count usage
    1.19. $rr_top_count usage
    1.19. $rr_top_count usage
    1.20. nh_enable_ping usage
    1.20. nh_enable_ping usage
+   1.21. @nathelper.rewrite_contact usage
 
 
 Chapter 1. Admin Guide
 Chapter 1. Admin Guide
 
 
@@ -148,6 +153,10 @@ Chapter 1. Admin Guide
 
 
         7.1. nh_enable_ping
         7.1. nh_enable_ping
 
 
+   8. Selects
+
+        8.1. @nathelper.rewrite_contact[n]
+
 1. Overview
 1. Overview
 
 
    This is a module to help with NAT traversal and reuse of tcp
    This is a module to help with NAT traversal and reuse of tcp
@@ -396,7 +405,7 @@ modparam("nathelper", "keepalive_timeout", 120)
    5.7. add_contact_alias([ip_addr, port, proto])
    5.7. add_contact_alias([ip_addr, port, proto])
    5.8. handle_ruri_alias()
    5.8. handle_ruri_alias()
 
 
-5.1.  fix_nated_contact()
+5.1. fix_nated_contact()
 
 
    Rewrites Contact HF to contain request's source address:port.
    Rewrites Contact HF to contain request's source address:port.
 
 
@@ -408,7 +417,7 @@ modparam("nathelper", "keepalive_timeout", 120)
 if (search("User-Agent: Cisco ATA.*") {fix_nated_contact();};
 if (search("User-Agent: Cisco ATA.*") {fix_nated_contact();};
 ...
 ...
 
 
-5.2.  fix_nated_sdp(flags [, ip_address])
+5.2. fix_nated_sdp(flags [, ip_address])
 
 
    Alters the SDP information in orer to facilitate NAT traversal. What
    Alters the SDP information in orer to facilitate NAT traversal. What
    changes to be performed may be controled via the "flags" parameter.
    changes to be performed may be controled via the "flags" parameter.
@@ -436,7 +445,7 @@ if (search("User-Agent: Cisco ATA.*") {fix_nated_contact();};
 if (search("User-Agent: Cisco ATA.*") {fix_nated_sdp("3");};
 if (search("User-Agent: Cisco ATA.*") {fix_nated_sdp("3");};
 ...
 ...
 
 
-5.3.  add_rcv_param([flag]),
+5.3. add_rcv_param([flag]),
 
 
    Add received parameter to Contact header fields or Contact URI. The
    Add received parameter to Contact header fields or Contact URI. The
    parameter will contain URI created from the source IP, port, and
    parameter will contain URI created from the source IP, port, and
@@ -460,7 +469,7 @@ add_rcv_param(); # add the parameter to the Contact header
 add_rcv_param("1"); # add the parameter to the Contact URI
 add_rcv_param("1"); # add the parameter to the Contact URI
 ...
 ...
 
 
-5.4.  fix_nated_register()
+5.4. fix_nated_register()
 
 
    The function creates a URI consisting of the source IP, port, and
    The function creates a URI consisting of the source IP, port, and
    protocol and stores the URI in an Attribute-Value-Pair. The URI will be
    protocol and stores the URI in an Attribute-Value-Pair. The URI will be
@@ -478,7 +487,7 @@ add_rcv_param("1"); # add the parameter to the Contact URI
 fix_nated_register();
 fix_nated_register();
 ...
 ...
 
 
-5.5.  nat_uac_test(flags)
+5.5. nat_uac_test(flags)
 
 
    Tries to guess if client's request originated behind a nat. The
    Tries to guess if client's request originated behind a nat. The
    parameter determines what heuristics is used.
    parameter determines what heuristics is used.
@@ -501,14 +510,14 @@ fix_nated_register();
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
    FAILURE_ROUTE, BRANCH_ROUTE.
    FAILURE_ROUTE, BRANCH_ROUTE.
 
 
-5.6.  is_rfc1918(ip_address)
+5.6. is_rfc1918(ip_address)
 
 
    Determines if the address in the parameter is an rfc1918 address. The
    Determines if the address in the parameter is an rfc1918 address. The
    parameter allows pseudo-variables usage.
    parameter allows pseudo-variables usage.
 
 
    This function can be used from ANY_ROUTE.
    This function can be used from ANY_ROUTE.
 
 
-5.7.  add_contact_alias([ip_addr, port, proto])
+5.7. add_contact_alias([ip_addr, port, proto])
 
 
    Adds ;alias=ip~port~transport parameter to contact URI containing
    Adds ;alias=ip~port~transport parameter to contact URI containing
    either received ip, port, and transport protocol or those given as
    either received ip, port, and transport protocol or those given as
@@ -528,7 +537,7 @@ fix_nated_register();
     };
     };
 ...
 ...
 
 
-5.8.  handle_ruri_alias()
+5.8. handle_ruri_alias()
 
 
    Checks if Request URI has alias param and if so, removes it and sets
    Checks if Request URI has alias param and if so, removes it and sets
    $du based on its value. Note that this means that routing of request is
    $du based on its value. Note that this means that routing of request is
@@ -607,6 +616,22 @@ fix_nated_register();
 $ kamctl fifo nh_enable_ping 1
 $ kamctl fifo nh_enable_ping 1
 ...
 ...
 
 
+8. Selects
+
+   8.1. @nathelper.rewrite_contact[n]
+
+8.1. @nathelper.rewrite_contact[n]
+
+   Get n-th Contact value with IP:Port rewritten to source ip:port. N is
+   counted from 1. Only IP:port is rewritten, remaining part are left
+   unchanged. Full nameaddr is supported.
+
+   Example 1.21. @nathelper.rewrite_contact usage
+...
+$c = @nathelper.rewrite_contact[1];
+...
+$c2 = @nathelper.rewrite_contact[1].nameaddr.uri;
+
 Chapter 2. Frequently Asked Questions
 Chapter 2. Frequently Asked Questions
 
 
    2.1. What happend with "rtpproxy_disable" parameter?
    2.1. What happend with "rtpproxy_disable" parameter?

+ 22 - 0
modules_k/nathelper/doc/nathelper_admin.xml

@@ -736,5 +736,27 @@ $ &ctltool; fifo nh_enable_ping 1
 
 
 	</section>
 	</section>
 
 
+	<section>
+		<title>Selects</title>
+
+		<section id="nathelper.rewrite_contact">
+		<title>@nathelper.rewrite_contact[n]</title>
+		<para>
+			Get n-th Contact value with IP:Port rewritten to source ip:port. N is counted from 1.
+		    Only IP:port is rewritten, remaining part are left unchanged. Full nameaddr is supported.
+		</para>
+		<example>
+			<title>@nathelper.rewrite_contact usage</title>
+			<programlisting>
+...
+$c = @nathelper.rewrite_contact[1];
+...
+$c2 = @nathelper.rewrite_contact[1].nameaddr.uri;
+			</programlisting>
+		</example>
+		</section>
+
+	</section>
+
 </chapter>
 </chapter>
 
 

+ 80 - 0
modules_k/nathelper/nathelper.c

@@ -220,6 +220,7 @@
 #include "../../socket_info.h"
 #include "../../socket_info.h"
 #include "../../mod_fix.h"
 #include "../../mod_fix.h"
 #include "../../dset.h"
 #include "../../dset.h"
+#include "../../select.h"
 #include "../registrar/sip_msg.h"
 #include "../registrar/sip_msg.h"
 #include "../usrloc/usrloc.h"
 #include "../usrloc/usrloc.h"
 #include "nathelper.h"
 #include "nathelper.h"
@@ -439,6 +440,27 @@ struct module_exports exports = {
 };
 };
 
 
 
 
+static int
+sel_nathelper(str* res, select_t* s, struct sip_msg* msg)
+{
+
+	/* dummy */
+	return 0;
+}
+
+static int sel_rewrite_contact(str* res, select_t* s, struct sip_msg* msg);
+
+SELECT_F(select_any_nameaddr)
+
+select_row_t sel_declaration[] = {
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("nathelper"), sel_nathelper, SEL_PARAM_EXPECTED},
+	{ sel_nathelper, SEL_PARAM_STR, STR_STATIC_INIT("rewrite_contact"), sel_rewrite_contact, CONSUME_NEXT_INT },
+
+	{ sel_rewrite_contact, SEL_PARAM_STR, STR_STATIC_INIT("nameaddr"), select_any_nameaddr, NESTED | CONSUME_NEXT_STR},
+
+	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
+};
+
 static int
 static int
 fixup_fix_sdp(void** param, int param_no)
 fixup_fix_sdp(void** param, int param_no)
 {
 {
@@ -689,6 +711,8 @@ mod_init(void)
 		nets_1918[i].netaddr = ntohl(addr.s_addr) & nets_1918[i].mask;
 		nets_1918[i].netaddr = ntohl(addr.s_addr) & nets_1918[i].mask;
 	}
 	}
 
 
+	register_select_table(sel_declaration);
+
 	return 0;
 	return 0;
 }
 }
 
 
@@ -2292,3 +2316,59 @@ done:
 	/* let the core handle further the reply */
 	/* let the core handle further the reply */
 	return 1;
 	return 1;
 }
 }
+
+static int
+sel_rewrite_contact(str* res, select_t* s, struct sip_msg* msg)
+{
+	static char buf[500];
+	contact_t* c;
+	int n, def_port_fl, len;
+	char *cp;
+	str hostport;
+	struct sip_uri uri;
+
+	res->len = 0;
+	n = s->params[2].v.i;
+	if (n <= 0) {
+		LOG(L_ERR, "ERROR: rewrite_contact[%d]: zero or negative index not supported\n", n);
+		return -1;
+	}
+	c = 0;
+	do {
+		if (contact_iterator(&c, msg, c) < 0 || !c)
+			return -1;
+		n--;
+	} while (n > 0);
+
+	if (parse_uri(c->uri.s, c->uri.len, &uri) < 0 || uri.host.len <= 0) {
+		LOG(L_ERR, "rewrite_contact[%d]: Error while parsing Contact URI\n", s->params[2].v.i);
+		return -1;
+	}
+	len = c->len - uri.host.len;
+	if (uri.port.len > 0)
+		len -= uri.port.len;
+	def_port_fl = (msg->rcv.proto == PROTO_TLS && msg->rcv.src_port == SIPS_PORT) || (msg->rcv.proto != PROTO_TLS && msg->rcv.src_port == SIP_PORT);
+	if (!def_port_fl)
+		len += 1/*:*/+5/*port*/;
+	if (len > sizeof(buf)) {
+		LOG(L_ERR, "ERROR: rewrite_contact[%d]: contact too long\n", s->params[2].v.i);
+		return -1;
+	}
+	hostport = uri.host;
+	if (uri.port.len > 0)
+		hostport.len = uri.port.s + uri.port.len - uri.host.s;
+
+	res->s = buf;
+	res->len = hostport.s - c->name.s;
+	memcpy(buf, c->name.s, res->len);
+	cp = ip_addr2a(&msg->rcv.src_ip);
+	if (def_port_fl) {
+		res->len+= snprintf(buf+res->len, sizeof(buf)-res->len, "%s", cp);
+	} else {
+		res->len+= snprintf(buf+res->len, sizeof(buf)-res->len, "%s:%d", cp, msg->rcv.src_port);
+	}
+	memcpy(buf+res->len, hostport.s+hostport.len, c->len-(hostport.s+hostport.len-c->name.s));
+	res->len+= c->len-(hostport.s+hostport.len-c->name.s);
+
+	return 0;
+}