Skip to content

Commit

Permalink
sipsess: combine reinvite and update handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilianfridrich committed Jul 14, 2022
1 parent 4a1cb92 commit 93888f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 136 deletions.
59 changes: 7 additions & 52 deletions src/sipsess/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ static void prack_handler(struct sipsess_sock *sock, const struct sip_msg *msg)
}


static void reinvite_handler(struct sipsess_sock *sock,
static void target_refresh_handler(struct sipsess_sock *sock,
const struct sip_msg *msg)
{
struct sip *sip = sock->sip;
bool is_invite = true;
struct sipsess *sess;
struct mbuf *desc;
char m[256];
Expand All @@ -239,12 +240,13 @@ static void reinvite_handler(struct sipsess_sock *sock,
return;
}

is_invite = !pl_strcmp(&msg->met, "INVITE");
if (!sip_dialog_rseq_valid(sess->dlg, msg)) {
(void)sip_treply(NULL, sip, msg, 500, "Server Internal Error");
return;
}

if (sess->st || sess->awaiting_answer) {
if ((is_invite && sess->st) || sess->awaiting_answer) {
(void)sip_treplyf(NULL, NULL, sip, msg, false,
500, "Server Internal Error",
"Retry-After: 5\r\n"
Expand All @@ -253,7 +255,7 @@ static void reinvite_handler(struct sipsess_sock *sock,
return;
}

if (sess->req) {
if (is_invite && sess->req) {
(void)sip_treply(NULL, sip, msg, 491, "Request Pending");
return;
}
Expand All @@ -277,53 +279,6 @@ static void reinvite_handler(struct sipsess_sock *sock,
}


static void update_handler(struct sipsess_sock *sock,
const struct sip_msg *msg)
{
struct sip *sip = sock->sip;
struct sipsess *sess;
struct mbuf *desc;
char m[256];
int err;

sess = sipsess_find(sock, msg);
if (!sess || sess->terminated) {
(void)sip_treply(NULL, sip, msg, 481, "Call Does Not Exist");
return;
}

if (!sip_dialog_rseq_valid(sess->dlg, msg)) {
(void)sip_treply(NULL, sip, msg, 500, "Server Internal Error");
return;
}

if (sess->awaiting_answer) {
(void)sip_treplyf(NULL, NULL, sip, msg, false,
500, "Server Internal Error",
"Retry-After: 5\r\n"
"Content-Length: 0\r\n"
"\r\n");
return;
}

err = sess->offerh(&desc, msg, sess->arg);
if (err) {
(void)sip_reply(sip, msg, 488, str_error(err, m, sizeof(m)));
return;
}

(void)sip_dialog_update(sess->dlg, msg);
(void)sipsess_reply_2xx(sess, msg, 200, "OK", desc, NULL, NULL);

/* pending modifications considered outdated;
* sdp may have changed in above exchange */
sess->desc = mem_deref(sess->desc);
sess->modify_pending = false;
tmr_cancel(&sess->tmr);
mem_deref(desc);
}


static void invite_handler(struct sipsess_sock *sock,
const struct sip_msg *msg)
{
Expand All @@ -338,14 +293,14 @@ static bool request_handler(const struct sip_msg *msg, void *arg)
if (!pl_strcmp(&msg->met, "INVITE")) {

if (pl_isset(&msg->to.tag))
reinvite_handler(sock, msg);
target_refresh_handler(sock, msg);
else
invite_handler(sock, msg);

return true;
}
else if (!pl_strcmp(&msg->met, "UPDATE")) {
update_handler(sock, msg);
target_refresh_handler(sock, msg);
return true;
}
else if (!pl_strcmp(&msg->met, "ACK")) {
Expand Down
102 changes: 18 additions & 84 deletions src/sipsess/modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ static void tmr_handler(void *arg)
}


static void reinvite_resp_handler(int err, const struct sip_msg *msg,
static void target_refresh_resp_handler(int err, const struct sip_msg *msg,
void *arg)
{
struct sipsess *sess = arg;
const struct sip_hdr *hdr;
bool is_invite = true;
struct mbuf *desc = NULL;

if (err || sip_request_loops(&sess->ls, msg->scode))
if (!msg || err || sip_request_loops(&sess->ls, msg->scode))
goto out;

is_invite = !pl_strcmp(&msg->met, "INVITE");

if (msg->scode < 200) {
return;
}
Expand All @@ -45,88 +48,14 @@ static void reinvite_resp_handler(int err, const struct sip_msg *msg,

if (sess->sent_offer)
(void)sess->answerh(msg, sess->arg);
else {
else if (is_invite) {
sess->modify_pending = false;
(void)sess->offerh(&desc, msg, sess->arg);
}

(void)sipsess_ack(sess->sock, sess->dlg, msg->cseq.num,
sess->auth, sess->ctype, desc);
mem_deref(desc);
}
else {
if (sess->terminated)
goto out;

switch (msg->scode) {

case 401:
case 407:
err = sip_auth_authenticate(sess->auth, msg);
if (err) {
err = (err == EAUTH) ? 0 : err;
break;
}

err = sipsess_reinvite(sess, false);
if (err)
break;

return;

case 408:
case 481:
sipsess_terminate(sess, 0, msg);
return;

case 491:
tmr_start(&sess->tmr, sess->owner ? 3000 : 1000,
tmr_handler, sess);
return;

case 500:
hdr = sip_msg_hdr(msg, SIP_HDR_RETRY_AFTER);
if (!hdr)
break;

tmr_start(&sess->tmr, pl_u32(&hdr->val) * 1000,
tmr_handler, sess);
return;
}
}
out:
if (sess->terminated)
mem_deref(sess);
else if (err == ETIMEDOUT)
sipsess_terminate(sess, err, NULL);
else if (sess->modify_pending)
(void)sipsess_reinvite(sess, true);
else
sess->desc = mem_deref(sess->desc);
}


static void update_resp_handler(int err, const struct sip_msg *msg, void *arg)
{
struct sipsess *sess = arg;
const struct sip_hdr *hdr;
struct mbuf *desc = NULL;

if (err || sip_request_loops(&sess->ls, msg->scode))
goto out;

if (msg->scode < 200) {
return;
}
else if (msg->scode < 300) {
(void)sip_dialog_update(sess->dlg, msg);

if (sess->sent_offer)
(void)sess->answerh(msg, sess->arg);
else {
sess->modify_pending = false;
(void)sess->offerh(&desc, msg, sess->arg);
}
if (is_invite)
(void)sipsess_ack(sess->sock, sess->dlg, msg->cseq.num,
sess->auth, sess->ctype, desc);

mem_deref(desc);
}
Expand All @@ -144,7 +73,8 @@ static void update_resp_handler(int err, const struct sip_msg *msg, void *arg)
break;
}

err = sipsess_update(sess, false);
err = is_invite ? sipsess_reinvite(sess, false) :
sipsess_update(sess, false);
if (err)
break;

Expand Down Expand Up @@ -176,7 +106,11 @@ static void update_resp_handler(int err, const struct sip_msg *msg, void *arg)
else if (err == ETIMEDOUT)
sipsess_terminate(sess, err, NULL);
else if (sess->modify_pending)
(void)sipsess_update(sess, true);
if (is_invite)
(void)sipsess_reinvite(sess, true);
else
(void)sipsess_update(sess, true);

else
sess->desc = mem_deref(sess->desc);
}
Expand Down Expand Up @@ -210,7 +144,7 @@ int sipsess_reinvite(struct sipsess *sess, bool reset_ls)

return sip_drequestf(&sess->req, sess->sip, true, "INVITE",
sess->dlg, 0, sess->auth,
send_handler, reinvite_resp_handler, sess,
send_handler, target_refresh_resp_handler, sess,
"%s%s%s"
"Content-Length: %zu\r\n"
"\r\n"
Expand All @@ -234,7 +168,7 @@ int sipsess_update(struct sipsess *sess, bool reset_ls)

return sip_drequestf(&sess->req, sess->sip, true, "UPDATE",
sess->dlg, 0, sess->auth,
send_handler, update_resp_handler, sess,
send_handler, target_refresh_resp_handler, sess,
"%s%s%s"
"Content-Length: %zu\r\n"
"\r\n"
Expand Down

0 comments on commit 93888f9

Please sign in to comment.