Skip to content

Commit 7f5a976

Browse files
committed
tm: Add functionality to execute a route, whenever TM creates a request or reply
1 parent 7d9227b commit 7f5a976

File tree

6 files changed

+118
-3
lines changed

6 files changed

+118
-3
lines changed

modules/tm/doc/tm_admin.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,59 @@ modparam("tm", "cluster_param", "tid")
856856
...
857857
# disable auto-cancel handling
858858
modparam("tm", "cluster_auto_cancel", no)
859+
...
860+
</programlisting>
861+
</example>
862+
</section>
863+
864+
<section id="param_local_request_route" xreflabel="local_request_route">
865+
<title><varname>local_request_route</varname> (string)</title>
866+
<para>
867+
This parameter points to a route, which is executed whenever TM creates
868+
a new request (e.g., through the B2B modules or through MI).
869+
</para>
870+
<para>
871+
The route is executed with the generated message by TM, incorporating all
872+
modifications.
873+
</para>
874+
<example>
875+
<title>Set the <varname>local_request_route</varname> parameter</title>
876+
<programlisting format="linespecific">
877+
...
878+
# Execute the route "local_request_route" upon sending a request
879+
modparam("tm", "local_request_route", "tm_local_request")
880+
881+
route[tm_local_request] {
882+
if (is_method("INVITE") && $rb(application/sdp) && !has_totag()) {
883+
$avp(sdp_request) := $rb(application/sdp);
884+
}
885+
}
886+
...
887+
</programlisting>
888+
</example>
889+
</section>
890+
891+
<section id="param_local_reply_route" xreflabel="local_reply_route">
892+
<title><varname>local_reply_route</varname> (string)</title>
893+
<para>
894+
This parameter points to a reply-route, which is executed whenever TM creates
895+
a reply (e.g., through the B2B modules or through MI).
896+
</para>
897+
898+
<example>
899+
<title>Set the <varname>local_reply_route</varname> parameter</title>
900+
<programlisting format="linespecific">
901+
...
902+
# Execute the route "tm_local_reply" upon sending a request
903+
modparam("tm", "local_reply_route", "tm_local_reply")
904+
905+
onreply_route[tm_local_reply] {
906+
if (is_method("BYE")) {
907+
$var(rc) = rest_get("http://localhost/qos/delete",
908+
$var(recv_body), $var(recv_ct), $var(rcode));
909+
}
910+
}
911+
859912
...
860913
</programlisting>
861914
</example>

modules/tm/t_reply.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "../../dprint.h"
6969
#include "../../config.h"
7070
#include "../../parser/parser_f.h"
71+
#include "../../parser/msg_parser.h"
7172
#include "../../ut.h"
7273
#include "../../timer.h"
7374
#include "../../error.h"
@@ -119,7 +120,7 @@ static struct script_route_ref *goto_on_reply = NULL;
119120
/* currently processed branch */
120121
extern int _tm_branch_index;
121122

122-
123+
static struct sip_msg dummy_msg;
123124

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

447+
if(ref_script_route_is_valid(tm_local_reply)) {
448+
LM_DBG("Found Local-Reply Route...\n");
449+
LM_DBG("Message:\n-----------------\n%.*s\n--------------------\n", len, buf);
450+
memset(&dummy_msg, 0, sizeof(struct sip_msg));
451+
dummy_msg.buf = buf;
452+
dummy_msg.len = len;
453+
454+
if (parse_msg(buf, len, &dummy_msg) == 0) {
455+
LM_DBG("Parsed Message, executing Local-Reply Route with Message...\n");
456+
run_top_route(sroutes->onreply[tm_local_reply->idx], &dummy_msg);
457+
}
458+
free_sip_msg(&dummy_msg);
459+
}
460+
461+
446462
if ( SEND_PR_BUFFER( rb, buf, len )==0 ) {
447463
LM_DBG("reply sent out. buf=%p: %.9s..., "
448464
"shmem=%p: %.9s\n", buf, buf, rb->buffer.s, rb->buffer.s );

modules/tm/t_reply.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
extern int restart_fr_on_each_reply;
3232
extern int onreply_avp_mode;
33+
extern struct script_route_ref *tm_local_reply;
3334

3435
/* reply processing status */
3536
enum rps {

modules/tm/tm.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ struct sip_msg* tm_pv_context_reply(struct sip_msg* msg);
141141
int fr_timeout;
142142
int fr_inv_timeout;
143143

144+
static char* tm_local_reply_route;
145+
struct script_route_ref *tm_local_reply = NULL;
146+
147+
static char* tm_local_request_route;
148+
struct script_route_ref *tm_local_request = NULL;
149+
144150
#define TM_CANCEL_BRANCH_ALL (1<<0)
145151
#define TM_CANCEL_BRANCH_OTHERS (1<<1)
146152

@@ -340,6 +346,10 @@ static const param_export_t params[]={
340346
&tm_cluster_param.s },
341347
{ "cluster_auto_cancel", INT_PARAM,
342348
&tm_repl_auto_cancel },
349+
{ "local_reply_route", STR_PARAM,
350+
&tm_local_reply_route },
351+
{ "local_request_route", STR_PARAM,
352+
&tm_local_request_route },
343353
{0,0,0}
344354
};
345355

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

960+
if (tm_local_reply_route)
961+
{
962+
tm_local_reply = ref_script_route_by_name( tm_local_reply_route,
963+
sroutes->onreply, ONREPLY_RT_NO, ONREPLY_ROUTE, 0);
964+
if (!ref_script_route_is_valid(tm_local_reply))
965+
{
966+
LM_ERR("route <%s> does not exist\n",tm_local_reply_route);
967+
return -1;
968+
}
969+
}
970+
971+
if (tm_local_request_route)
972+
{
973+
tm_local_request = ref_script_route_by_name( tm_local_request_route,
974+
sroutes->request, RT_NO, REQUEST_ROUTE, 0);
975+
if (!ref_script_route_is_valid(tm_local_request))
976+
{
977+
LM_ERR("route <%s> does not exist\n",tm_local_request_route);
978+
return -1;
979+
}
980+
}
981+
950982
return 0;
951983
}
952984

modules/tm/uac.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int pass_provisional_replies = 0;
7676

7777
/* T holder for the last local transaction */
7878
struct cell** last_localT;
79-
79+
static struct sip_msg dummy_msg;
8080

8181
/*
8282
* Initialize UAC
@@ -553,6 +553,19 @@ int t_uac(str* method, str* headers, str* body, dlg_t* dialog,
553553
request->buffer.len = buf_len;
554554
}
555555

556+
if (ref_script_route_is_valid(tm_local_request)) {
557+
LM_DBG("Found Local-Request Route...\n");
558+
memset(&dummy_msg, 0, sizeof(struct sip_msg));
559+
dummy_msg.buf = request->buffer.s;
560+
dummy_msg.len = request->buffer.len;
561+
562+
if (parse_msg(request->buffer.s, request->buffer.len, &dummy_msg) == 0) {
563+
LM_DBG("Parsed Message, executing Local-Reply Route with Message...\n");
564+
run_top_route(sroutes->request[tm_local_request->idx], &dummy_msg);
565+
}
566+
free_sip_msg(&dummy_msg);
567+
}
568+
556569
/* for DNS based failover, copy the DNS proxy into transaction
557570
* NOTE: while running the local route, the DNS proxy may be set
558571
* if a different one is needed (due destination change), so

modules/tm/uac.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/* Pass provisional replies to fifo applications */
3636
extern int pass_provisional_replies;
37-
37+
extern struct script_route_ref *tm_local_request;
3838

3939
/*
4040
* Function prototypes

0 commit comments

Comments
 (0)