Skip to content

Commit

Permalink
nathelper: use CSV named flags for script functions
Browse files Browse the repository at this point in the history
Translate integer flags to CSV named flags for fix_nated_sdp() and
nat_uac_test().
  • Loading branch information
rvlad-patrascu committed Apr 26, 2023
1 parent b836acc commit f188857
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
30 changes: 15 additions & 15 deletions modules/nathelper/doc/nathelper_admin.xml
Expand Up @@ -640,33 +640,33 @@ if (search("User-Agent: Cisco ATA.*") {
<para>Meaning of the parameters is as follows:</para>
<itemizedlist>
<listitem><para>
<emphasis>flags (int)</emphasis> - the value may be a bitwise OR of
<emphasis>flags (string)</emphasis> - the value may be a CSV of
the following flags:
</para>
<itemizedlist>
<listitem>
<para><emphasis>0x01</emphasis> - adds
<para><emphasis>add-dir-active</emphasis> - adds
<quote>a=direction:active</quote> SDP line;
</para>
</listitem>
<listitem>
<para><emphasis>0x02</emphasis> - rewrite media
<para><emphasis>rewrite-media-ip</emphasis> - rewrite media
&ip; address (c=) with source address of the message
or the provided IP address (the provided IP address takes
precedence over the source address).</para>
</listitem>
<listitem>
<para><emphasis>0x04</emphasis> - adds
<para><emphasis>add-no-rtpproxy</emphasis> - adds
<quote>a=nortpproxy:yes</quote> SDP line;</para>
</listitem>
<listitem>
<para><emphasis>0x08</emphasis> - rewrite IP from
<para><emphasis>rewrite-origin-ip</emphasis> - rewrite IP from
origin description (o=) with source address of the message
or the provided IP address (the provided IP address takes
precedence over the source address).</para>
</listitem>
<listitem>
<para><emphasis>0x10</emphasis> - force rewrite of
<para><emphasis>rewrite-null-ips</emphasis> - force rewrite of
null media IP and/or origin IP address.
Without this flag, null IPs are left untouched.</para>
</listitem>
Expand Down Expand Up @@ -770,40 +770,40 @@ fix_nated_register();
Tries to guess if client's request originated behind a nat.
The parameter determines what heuristics is used.
</para>
<para>Meaning of the <emphasis>flags (int)</emphasis> parameter
<para>Meaning of the <emphasis>flags (string)</emphasis> parameter
is as follows:</para>
<itemizedlist>
<listitem><para>
<emphasis>1</emphasis> - Contact header field is searched
<emphasis>ct</emphasis> - Contact header field is searched
for occurrence of RFC1918 / RFC6598 addresses.
</para></listitem>
<listitem><para>
<emphasis>2</emphasis> - the "received" test is used: address
<emphasis>si-via</emphasis> - the "received" test is used: address
in Via is compared against source IP address of signaling
</para></listitem>
<listitem><para>
<emphasis>4</emphasis> - Top Most VIA is searched
<emphasis>via</emphasis> - Top Most VIA is searched
for occurrence of RFC1918 / RFC6598 addresses
</para></listitem>
<listitem><para>
<emphasis>8</emphasis> - SDP is searched for occurrence of
<emphasis>sdp</emphasis> - SDP is searched for occurrence of
RFC1918 / RFC6598 addresses
</para></listitem>
<listitem><para>
<emphasis>16</emphasis> - test if the source port is different
<emphasis>sp-via</emphasis> - test if the source port is different
from the port in Via
</para></listitem>
<listitem><para>
<emphasis>32</emphasis> - address in Contact is compared against
<emphasis>si-ct</emphasis> - address in Contact is compared against
source IP address of signaling
</para></listitem>
<listitem><para>
<emphasis>64</emphasis> - Port in Contact is compared against
<emphasis>sp-ct</emphasis> - Port in Contact is compared against
source port of signaling
</para></listitem>
</itemizedlist>
<para>
All flags can be bitwise combined, the test returns true if any of
A CSV of the above flags can be provided, the test returns true if any of
the tests identified a NAT.
</para>
<para>
Expand Down
57 changes: 45 additions & 12 deletions modules/nathelper/nathelper.c
Expand Up @@ -110,7 +110,10 @@ static int sipping_latency_flag = -1; /* by the code imported by sip_pinger*/
#define STORE_BRANCH_CTID \
(sipping_flag && (rm_on_to_flag || sipping_latency_flag))

static int nat_uac_test_f(struct sip_msg* msg, int *tests);
static int fixup_flags_uac_test(void** param);
static int fixup_flags_sdp(void** param);

static int nat_uac_test_f(struct sip_msg* msg, void *tests);
static int fix_nated_contact_f(struct sip_msg* msg, str *params);
static int fix_nated_sdp_f(struct sip_msg* msg, int* level, str *ip,
str *new_sdp_lines);
Expand Down Expand Up @@ -221,12 +224,12 @@ static const cmd_export_t cmds[] = {
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, {0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"fix_nated_sdp", (cmd_function)fix_nated_sdp_f, {
{CMD_PARAM_INT,0,0},
{CMD_PARAM_STR,fixup_flags_sdp,0},
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, {0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"nat_uac_test", (cmd_function)nat_uac_test_f, {
{CMD_PARAM_INT,0,0}, {0,0,0}},
{CMD_PARAM_STR,fixup_flags_uac_test,0}, {0,0,0}},
REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
{"fix_nated_register", (cmd_function)fix_nated_register_f,
{{0,0,0}}, REQUEST_ROUTE},
Expand Down Expand Up @@ -848,52 +851,68 @@ contact_rport(struct sip_msg* msg)

}

static str nat_uac_test_flag_names[] =
{
str_init("private-contact"), /* NAT_UAC_TEST_C_1918 */
str_init("diff-ip-src-via"), /* NAT_UAC_TEST_V_RCVD */
str_init("private-via"), /* NAT_UAC_TEST_V_1918 */
str_init("private-sdp"), /* NAT_UAC_TEST_S_1918 */
str_init("diff-port-src-via"), /* NAT_UAC_TEST_RPORT */
str_init("diff-ip-src-contact"), /* NAT_UAC_TEST_C_RCVD */
str_init("diff-port-src-contact"), /* NAT_UAC_TEST_C_RPORT */
STR_NULL
};


static int fixup_flags_uac_test(void** param)
{
return fixup_named_flags(param, nat_uac_test_flag_names, NULL, NULL);
}

static int
nat_uac_test_f(struct sip_msg* msg, int *tests)
nat_uac_test_f(struct sip_msg* msg, void *flags)
{
unsigned int tests = (unsigned int)(unsigned long)flags;

/* return true if any of the NAT-UAC tests holds */

/* test if the source port is different from the port in Via */
if ((*tests & NAT_UAC_TEST_RPORT) &&
if ((tests & NAT_UAC_TEST_RPORT) &&
(msg->rcv.src_port!=(msg->via1->port?msg->via1->port:SIP_PORT)) ){
return 1;
}
/*
* test if source address of signaling is different from
* address advertised in Via
*/
if ((*tests & NAT_UAC_TEST_V_RCVD) && received_test(msg))
if ((tests & NAT_UAC_TEST_V_RCVD) && received_test(msg))
return 1;
/*
* test for occurrences of RFC1918 / RFC6598 addresses in Contact
* header field
*/
if ((*tests & NAT_UAC_TEST_C_1918) && (contact_1918(msg)>0))
if ((tests & NAT_UAC_TEST_C_1918) && (contact_1918(msg)>0))
return 1;
/*
* test for occurrences of RFC1918 / RFC6598 addresses in SDP body
*/
if ((*tests & NAT_UAC_TEST_S_1918) && sdp_1918(msg))
if ((tests & NAT_UAC_TEST_S_1918) && sdp_1918(msg))
return 1;
/*
* test for occurrences of RFC1918 / RFC6598 addresses top Via
*/
if ((*tests & NAT_UAC_TEST_V_1918) && via_1918(msg))
if ((tests & NAT_UAC_TEST_V_1918) && via_1918(msg))
return 1;
/*
* test if source address of signaling is different from
* address advertised in Contact
*/
if ((*tests & NAT_UAC_TEST_C_RCVD) && contact_rcv(msg))
if ((tests & NAT_UAC_TEST_C_RCVD) && contact_rcv(msg))
return 1;
/*
* test if source port of signaling is different from
* port advertised in Contact
*/
if ((*tests & NAT_UAC_TEST_C_RPORT) && contact_rport(msg))
if ((tests & NAT_UAC_TEST_C_RPORT) && contact_rport(msg))
return 1;

/* no test succeeded */
Expand Down Expand Up @@ -1101,6 +1120,20 @@ replace_sdp_ip(struct sip_msg* msg, str *org_body, char *line, str *ip, int forc
return 0;
}

static str fix_nated_sdp_flag_names[] =
{
str_init("add-dir-active"), /* ADD_ADIRECTION */
str_init("rewrite-media-ip"), /* FIX_MEDIP */
str_init("add-no-rtpproxy"), /* ADD_ANORTPPROXY */
str_init("rewrite-origin-ip"), /* FIX_ORGIP */
str_init("rewrite-null-ips"), /* FORCE_NULL_ADDR */
STR_NULL
};

static int fixup_flags_sdp(void** param)
{
return fixup_named_flags(param, fix_nated_sdp_flag_names, NULL, NULL);
}

static int
fix_nated_sdp_f(struct sip_msg* msg, int* level, str *ip, str *new_sdp_lines)
Expand Down

0 comments on commit f188857

Please sign in to comment.