Skip to content

Commit

Permalink
tm: Add functionality to execute a route, whenever TM creates a reque…
Browse files Browse the repository at this point in the history
…st or reply
  • Loading branch information
carstenbock committed Mar 7, 2024
1 parent 7d9227b commit 7f5a976
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
53 changes: 53 additions & 0 deletions modules/tm/doc/tm_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,59 @@ modparam("tm", "cluster_param", "tid")
...
# disable auto-cancel handling
modparam("tm", "cluster_auto_cancel", no)
...
</programlisting>
</example>
</section>

<section id="param_local_request_route" xreflabel="local_request_route">
<title><varname>local_request_route</varname> (string)</title>
<para>
This parameter points to a route, which is executed whenever TM creates
a new request (e.g., through the B2B modules or through MI).
</para>
<para>
The route is executed with the generated message by TM, incorporating all
modifications.
</para>
<example>
<title>Set the <varname>local_request_route</varname> parameter</title>
<programlisting format="linespecific">
...
# Execute the route "local_request_route" upon sending a request
modparam("tm", "local_request_route", "tm_local_request")

route[tm_local_request] {
if (is_method("INVITE") && $rb(application/sdp) && !has_totag()) {
$avp(sdp_request) := $rb(application/sdp);
}
}
...
</programlisting>
</example>
</section>

<section id="param_local_reply_route" xreflabel="local_reply_route">
<title><varname>local_reply_route</varname> (string)</title>
<para>
This parameter points to a reply-route, which is executed whenever TM creates
a reply (e.g., through the B2B modules or through MI).
</para>

<example>
<title>Set the <varname>local_reply_route</varname> parameter</title>
<programlisting format="linespecific">
...
# Execute the route "tm_local_reply" upon sending a request
modparam("tm", "local_reply_route", "tm_local_reply")

onreply_route[tm_local_reply] {
if (is_method("BYE")) {
$var(rc) = rest_get("http://localhost/qos/delete",
$var(recv_body), $var(recv_ct), $var(rcode));
}
}

...
</programlisting>
</example>
Expand Down
18 changes: 17 additions & 1 deletion modules/tm/t_reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "../../dprint.h"
#include "../../config.h"
#include "../../parser/parser_f.h"
#include "../../parser/msg_parser.h"
#include "../../ut.h"
#include "../../timer.h"
#include "../../error.h"
Expand Down Expand Up @@ -119,7 +120,7 @@ static struct script_route_ref *goto_on_reply = NULL;
/* currently processed branch */
extern int _tm_branch_index;


static struct sip_msg dummy_msg;

/* returns the picked branch */
int t_get_picked_branch(void)
Expand Down Expand Up @@ -443,6 +444,21 @@ static int _reply_light( struct cell *trans, char* buf, unsigned int len,
if(trans->uas.request && trans->uas.request->flags&tcp_no_new_conn_rplflag)
tcp_no_new_conn = 1;

if(ref_script_route_is_valid(tm_local_reply)) {
LM_DBG("Found Local-Reply Route...\n");
LM_DBG("Message:\n-----------------\n%.*s\n--------------------\n", len, buf);
memset(&dummy_msg, 0, sizeof(struct sip_msg));
dummy_msg.buf = buf;
dummy_msg.len = len;

if (parse_msg(buf, len, &dummy_msg) == 0) {
LM_DBG("Parsed Message, executing Local-Reply Route with Message...\n");
run_top_route(sroutes->onreply[tm_local_reply->idx], &dummy_msg);
}
free_sip_msg(&dummy_msg);
}


if ( SEND_PR_BUFFER( rb, buf, len )==0 ) {
LM_DBG("reply sent out. buf=%p: %.9s..., "
"shmem=%p: %.9s\n", buf, buf, rb->buffer.s, rb->buffer.s );
Expand Down
1 change: 1 addition & 0 deletions modules/tm/t_reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

extern int restart_fr_on_each_reply;
extern int onreply_avp_mode;
extern struct script_route_ref *tm_local_reply;

/* reply processing status */
enum rps {
Expand Down
32 changes: 32 additions & 0 deletions modules/tm/tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ struct sip_msg* tm_pv_context_reply(struct sip_msg* msg);
int fr_timeout;
int fr_inv_timeout;

static char* tm_local_reply_route;
struct script_route_ref *tm_local_reply = NULL;

static char* tm_local_request_route;
struct script_route_ref *tm_local_request = NULL;

#define TM_CANCEL_BRANCH_ALL (1<<0)
#define TM_CANCEL_BRANCH_OTHERS (1<<1)

Expand Down Expand Up @@ -340,6 +346,10 @@ static const param_export_t params[]={
&tm_cluster_param.s },
{ "cluster_auto_cancel", INT_PARAM,
&tm_repl_auto_cancel },
{ "local_reply_route", STR_PARAM,
&tm_local_reply_route },
{ "local_request_route", STR_PARAM,
&tm_local_request_route },
{0,0,0}
};

Expand Down Expand Up @@ -947,6 +957,28 @@ static int mod_init(void)
LM_WARN("running without cluster support for transactions!\n");
}

if (tm_local_reply_route)
{
tm_local_reply = ref_script_route_by_name( tm_local_reply_route,
sroutes->onreply, ONREPLY_RT_NO, ONREPLY_ROUTE, 0);
if (!ref_script_route_is_valid(tm_local_reply))
{
LM_ERR("route <%s> does not exist\n",tm_local_reply_route);
return -1;
}
}

if (tm_local_request_route)
{
tm_local_request = ref_script_route_by_name( tm_local_request_route,
sroutes->request, RT_NO, REQUEST_ROUTE, 0);
if (!ref_script_route_is_valid(tm_local_request))
{
LM_ERR("route <%s> does not exist\n",tm_local_request_route);
return -1;
}
}

return 0;
}

Expand Down
15 changes: 14 additions & 1 deletion modules/tm/uac.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int pass_provisional_replies = 0;

/* T holder for the last local transaction */
struct cell** last_localT;

static struct sip_msg dummy_msg;

/*
* Initialize UAC
Expand Down Expand Up @@ -553,6 +553,19 @@ int t_uac(str* method, str* headers, str* body, dlg_t* dialog,
request->buffer.len = buf_len;
}

if (ref_script_route_is_valid(tm_local_request)) {
LM_DBG("Found Local-Request Route...\n");
memset(&dummy_msg, 0, sizeof(struct sip_msg));
dummy_msg.buf = request->buffer.s;
dummy_msg.len = request->buffer.len;

if (parse_msg(request->buffer.s, request->buffer.len, &dummy_msg) == 0) {
LM_DBG("Parsed Message, executing Local-Reply Route with Message...\n");
run_top_route(sroutes->request[tm_local_request->idx], &dummy_msg);
}
free_sip_msg(&dummy_msg);
}

/* for DNS based failover, copy the DNS proxy into transaction
* NOTE: while running the local route, the DNS proxy may be set
* if a different one is needed (due destination change), so
Expand Down
2 changes: 1 addition & 1 deletion modules/tm/uac.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/* Pass provisional replies to fifo applications */
extern int pass_provisional_replies;

extern struct script_route_ref *tm_local_request;

/*
* Function prototypes
Expand Down

0 comments on commit 7f5a976

Please sign in to comment.