Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tm function t_reply_by_callid #2937

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions modules/tm/README
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ tm Module

1.4.21. t_flush_flags()
1.4.22. t_anycast_replicate()
1.4.23 t_reply_by_callid()

1.5. Exported Pseudo-Variables

Expand Down Expand Up @@ -173,6 +174,7 @@ tm Module
1.42. t_write_req/unix usage
1.43. t_flush_flags usage
1.44. t_anycast_replicate usage
1.45. t_reply_by_callid usage

Chapter 1. Admin Guide

Expand Down Expand Up @@ -1372,6 +1374,58 @@ if (is_method("ACK|CANCEL") && !t_check_trans()) {
}
...

1.4.23. t_reply_by_callid(code, reason_phrase, [callid], [cseq])

This function is used to send a reply to
an existing INVITE transaction.
The usual use case is when opensips is used as an UAS
and when an INVITE is receveid, it is "parked" locally on
opensips by replying to it with
t_reply(180, "Ringing" or t_reply(183, "Session Progress")
and later we need to handle CANCEL or BYE for it and send
'487 Request Terminated' to the original INVITE transaction.

The callid and cseq used to identify the transaction
will be obtained from the current messsage being processed.
But they can be passed explicitly so that for example we can
handle a BYE where the cseq must be the cseq
of the INVITE minus one.

This function can be used from REQUEST_ROUTE.

Example 1.45. t_reply_by_callid usage
...

route{
if($rU == "LOCAL_PARK") {
if(is_method("INVITE")) {
$T_fr_timeout = 10;
$T_fr_inv_timeout = 10;
append_to_reply("Contact: sip:LOCAL_PARK@$socket_in(ip):$socket_in(port)\r\n");
t_reply(180, "Ringing");
t_wait_for_new_branches();
} else if(is_method("CANCEL")) {
if(!t_reply_by_callid(487, "Request Terminated")) {
sl_send_reply(481, "Call Leg/Transaction Does Not Exist");
} else {
sl_send_reply(200, "OK");
}
} else if(is_method("BYE")) {
$var(prev_cseq) = ($(cs{s.int}) - 1);
if(!t_reply_by_callid(487, "Request Terminated", , $var(prev_cseq))) {
sl_send_reply(481, "Call Leg/Transaction Does Not Exist");
} else {
sl_send_reply(200, "OK");
}
} else if(is_method("ACK")) {
t_relay();
}
exit;
}
}

...

1.5. Exported Pseudo-Variables

Exported variables are listed in the next sections.
Expand Down
34 changes: 34 additions & 0 deletions modules/tm/tm.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ static int w_t_new_request(struct sip_msg* msg, str *method,
static int t_wait_for_new_branches(struct sip_msg* msg,
unsigned int* br_to_wait);
static int w_t_wait_no_more_branches(struct sip_msg* msg);
static int t_reply_by_callid(struct sip_msg* msg, unsigned int* code, str* text, str* callid, str* cseq);

struct sip_msg* tm_pv_context_request(struct sip_msg* msg);
struct sip_msg* tm_pv_context_reply(struct sip_msg* msg);
Expand Down Expand Up @@ -272,6 +273,12 @@ static cmd_export_t cmds[]={
REQUEST_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE|FAILURE_ROUTE},
{"t_anycast_replicate", (cmd_function)tm_anycast_replicate, {{0,0,0}},
REQUEST_ROUTE},
{"t_reply_by_callid", (cmd_function)t_reply_by_callid, {
{CMD_PARAM_INT, fixup_reply_code, 0},
{CMD_PARAM_STR, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0},
{CMD_PARAM_STR | CMD_PARAM_OPT, 0, 0}, {0,0,0}},
REQUEST_ROUTE},
{"load_tm", (cmd_function)load_tm, {{0,0,0}}, 0},
{0,0,{{0,0,0}},0}
};
Expand Down Expand Up @@ -1609,6 +1616,33 @@ static int w_t_wait_no_more_branches(struct sip_msg* msg)
return 1;
}


static int t_reply_by_callid(struct sip_msg* msg, unsigned int* code, str* text, str* callid, str* cseq_number)
{
struct cell *trans;
int n;

if (!callid && msg->callid==NULL && ((parse_headers(msg, HDR_CALLID_F, 0) ==-1) || (msg->callid==NULL)) ) {
/* could not get callid */
return -2;
}

if (!cseq_number && !msg->cseq && ((parse_headers(msg, HDR_CSEQ_F, 0) == -1) || !msg->cseq)) {
/* could not get cseq */
return -3;
}

if(t_lookup_callid( &trans, callid ? *callid : msg->callid->body, cseq_number ? *cseq_number : get_cseq(msg)->number) < 0) {
/* transaction not found */
return -4;
}

n = t_reply_with_body( trans, *code, text, 0, 0, 0);

return n;
}


/******************** pseudo-variable functions *************************/

static int pv_get_tm_branch_idx(struct sip_msg *msg, pv_param_t *param,
Expand Down