Skip to content

Commit 870f322

Browse files
authored
Merge pull request #2937 from MayamaTakeshi/t_reply_by_callid
Adding tm function t_reply_by_callid
2 parents e09eb58 + 6338168 commit 870f322

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

modules/tm/README

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ tm Module
7272

7373
1.4.21. t_flush_flags()
7474
1.4.22. t_anycast_replicate()
75+
1.4.23 t_reply_by_callid()
7576

7677
1.5. Exported Pseudo-Variables
7778

@@ -180,6 +181,7 @@ tm Module
180181
1.42. t_write_req/unix usage
181182
1.43. t_flush_flags usage
182183
1.44. t_anycast_replicate usage
184+
1.45. t_reply_by_callid usage
183185

184186
Chapter 1. Admin Guide
185187

@@ -1381,6 +1383,58 @@ if (is_method("ACK|CANCEL") && !t_check_trans()) {
13811383
}
13821384
...
13831385

1386+
1.4.23. t_reply_by_callid(code, reason_phrase, [callid], [cseq])
1387+
1388+
This function is used to send a reply to
1389+
an existing INVITE transaction.
1390+
The usual use case is when opensips is used as an UAS
1391+
and when an INVITE is receveid, it is "parked" locally on
1392+
opensips by replying to it with
1393+
t_reply(180, "Ringing" or t_reply(183, "Session Progress")
1394+
and later we need to handle CANCEL or BYE for it and send
1395+
'487 Request Terminated' to the original INVITE transaction.
1396+
1397+
The callid and cseq used to identify the transaction
1398+
will be obtained from the current messsage being processed.
1399+
But they can be passed explicitly so that for example we can
1400+
handle a BYE where the cseq must be the cseq
1401+
of the INVITE minus one.
1402+
1403+
This function can be used from REQUEST_ROUTE.
1404+
1405+
Example 1.45. t_reply_by_callid usage
1406+
...
1407+
1408+
route{
1409+
if($rU == "LOCAL_PARK") {
1410+
if(is_method("INVITE")) {
1411+
$T_fr_timeout = 10;
1412+
$T_fr_inv_timeout = 10;
1413+
append_to_reply("Contact: sip:LOCAL_PARK@$socket_in(ip):$socket_in(port)\r\n");
1414+
t_reply(180, "Ringing");
1415+
t_wait_for_new_branches();
1416+
} else if(is_method("CANCEL")) {
1417+
if(!t_reply_by_callid(487, "Request Terminated")) {
1418+
sl_send_reply(481, "Call Leg/Transaction Does Not Exist");
1419+
} else {
1420+
sl_send_reply(200, "OK");
1421+
}
1422+
} else if(is_method("BYE")) {
1423+
$var(prev_cseq) = ($(cs{s.int}) - 1);
1424+
if(!t_reply_by_callid(487, "Request Terminated", , $var(prev_cseq))) {
1425+
sl_send_reply(481, "Call Leg/Transaction Does Not Exist");
1426+
} else {
1427+
sl_send_reply(200, "OK");
1428+
}
1429+
} else if(is_method("ACK")) {
1430+
t_relay();
1431+
}
1432+
exit;
1433+
}
1434+
}
1435+
1436+
...
1437+
13841438
1.5. Exported Pseudo-Variables
13851439

13861440
Exported variables are listed in the next sections.

modules/tm/tm.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static int w_t_new_request(struct sip_msg* msg, str *method,
130130
static int t_wait_for_new_branches(struct sip_msg* msg,
131131
unsigned int* br_to_wait);
132132
static int w_t_wait_no_more_branches(struct sip_msg* msg);
133+
static int t_reply_by_callid(struct sip_msg* msg, unsigned int* code, str* text, str* callid, str* cseq);
133134

134135
struct sip_msg* tm_pv_context_request(struct sip_msg* msg);
135136
struct sip_msg* tm_pv_context_reply(struct sip_msg* msg);
@@ -282,6 +283,12 @@ static const cmd_export_t cmds[]={
282283
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
283284
{"t_anycast_replicate", (cmd_function)tm_anycast_replicate, {{0,0,0}},
284285
REQUEST_ROUTE},
286+
{"t_reply_by_callid", (cmd_function)t_reply_by_callid, {
287+
{CMD_PARAM_INT, fixup_reply_code, 0},
288+
{CMD_PARAM_STR, 0, 0},
289+
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
290+
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
291+
REQUEST_ROUTE},
285292
{"load_tm", (cmd_function)load_tm, {{0,0,0}}, 0},
286293
{0,0,{{0,0,0}},0}
287294
};
@@ -1645,6 +1652,33 @@ static int w_t_wait_no_more_branches(struct sip_msg* msg)
16451652
return 1;
16461653
}
16471654

1655+
1656+
static int t_reply_by_callid(struct sip_msg* msg, unsigned int* code, str* text, str* callid, str* cseq_number)
1657+
{
1658+
struct cell *trans;
1659+
int n;
1660+
1661+
if (!callid && msg->callid==NULL && ((parse_headers(msg, HDR_CALLID_F, 0) ==-1) || (msg->callid==NULL)) ) {
1662+
/* could not get callid */
1663+
return -2;
1664+
}
1665+
1666+
if (!cseq_number && !msg->cseq && ((parse_headers(msg, HDR_CSEQ_F, 0) == -1) || !msg->cseq)) {
1667+
/* could not get cseq */
1668+
return -3;
1669+
}
1670+
1671+
if(t_lookup_callid( &trans, callid ? *callid : msg->callid->body, cseq_number ? *cseq_number : get_cseq(msg)->number) < 0) {
1672+
/* transaction not found */
1673+
return -4;
1674+
}
1675+
1676+
n = t_reply_with_body( trans, *code, text, 0, 0, 0);
1677+
1678+
return n;
1679+
}
1680+
1681+
16481682
/******************** pseudo-variable functions *************************/
16491683

16501684
static int pv_get_tm_branch_idx(struct sip_msg *msg, pv_param_t *param,

0 commit comments

Comments
 (0)