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

sip: add RFC 3311 support #425

Merged
merged 3 commits into from
Jul 16, 2022
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ legend:
* [RFC 2915](https://tools.ietf.org/html/rfc2915) - The Naming Authority Pointer (NAPTR) DNS Resource Record
* [RFC 3261](https://tools.ietf.org/html/rfc3261) - SIP: Session Initiation Protocol
* [RFC 3262](https://tools.ietf.org/html/rfc3262) - SIP Reliability of Provisional Responses
* [RFC 3311](https://tools.ietf.org/html/rfc3311) - The SIP UPDATE Method
* [RFC 3263](https://tools.ietf.org/html/rfc3263) - Locating SIP Servers
* [RFC 3264](https://tools.ietf.org/html/rfc3264) - An Offer/Answer Model with SDP
* [RFC 3265](https://tools.ietf.org/html/rfc3265) - SIP-Specific Event Notification
Expand Down
30 changes: 21 additions & 9 deletions src/sipsess/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,14 @@ 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;
bool got_offer;
struct sipsess *sess;
struct mbuf *desc;
struct mbuf *desc = NULL;
char m[256];
int err;

Expand All @@ -239,12 +241,15 @@ static void reinvite_handler(struct sipsess_sock *sock,
return;
}

is_invite = !pl_strcmp(&msg->met, "INVITE");
got_offer = (mbuf_get_left(msg->mb) > 0);

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,15 +258,18 @@ 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;
}

err = sess->offerh(&desc, msg, sess->arg);
if (err) {
(void)sip_reply(sip, msg, 488, str_error(err, m, sizeof(m)));
return;
if (got_offer || is_invite) {
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);
Expand Down Expand Up @@ -291,12 +299,16 @@ 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")) {
target_refresh_handler(sock, msg);
return true;
}
else if (!pl_strcmp(&msg->met, "ACK")) {
ack_handler(sock, msg);
return true;
Expand Down
60 changes: 49 additions & 11 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->cseq.met, "INVITE");

if (msg->scode < 200) {
return;
}
Expand All @@ -45,13 +48,15 @@ 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);
if (is_invite)
(void)sipsess_ack(sess->sock, sess->dlg, msg->cseq.num,
sess->auth, sess->ctype, desc);

mem_deref(desc);
}
else {
Expand All @@ -68,7 +73,8 @@ static void reinvite_resp_handler(int err, const struct sip_msg *msg,
break;
}

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

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

else
sess->desc = mem_deref(sess->desc);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Maybe code duplication with reinvite_resp_handler() can be avoided. Also it might be more readable with a few if for the differences.

Copy link
Contributor Author

@maximilianfridrich maximilianfridrich Jul 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combined in 2fb5cf3.

Expand Down Expand Up @@ -134,7 +144,31 @@ 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"
"%b",
sess->desc ? "Content-Type: " : "",
sess->desc ? sess->ctype : "",
sess->desc ? "\r\n" : "",
sess->desc ? mbuf_get_left(sess->desc) :(size_t)0,
sess->desc ? mbuf_buf(sess->desc) : NULL,
sess->desc ? mbuf_get_left(sess->desc):(size_t)0);
}


int sipsess_update(struct sipsess *sess, bool reset_ls)
{
sess->sent_offer = sess->desc ? true : false;
sess->modify_pending = false;

if (reset_ls)
sip_loopstate_reset(&sess->ls);

return sip_drequestf(&sess->req, sess->sip, true, "UPDATE",
sess->dlg, 0, sess->auth,
send_handler, target_refresh_resp_handler, sess,
"%s%s%s"
"Content-Length: %zu\r\n"
"\r\n"
Expand All @@ -158,16 +192,20 @@ int sipsess_reinvite(struct sipsess *sess, bool reset_ls)
*/
int sipsess_modify(struct sipsess *sess, struct mbuf *desc)
{
if (!sess || sess->st || sess->terminated)
if (!sess || (sess->st && sess->established) || sess->terminated)
return EINVAL;

mem_deref(sess->desc);
sess->desc = mem_ref(desc);

if (sess->req || sess->tmr.th || sess->replyl.head) {
if (sess->tmr.th || sess->replyl.head ||
(sess->req && sess->established)) {
sess->modify_pending = true;
return 0;
}

return sipsess_reinvite(sess, true);
if (sess->established)
return sipsess_reinvite(sess, true);
else
return sipsess_update(sess, true);
}
10 changes: 5 additions & 5 deletions src/sipsess/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ int sipsess_reply_2xx(struct sipsess *sess, const struct sip_msg *msg,
struct sipsess_reply *reply = NULL;
struct sip_contact contact;
int err = ENOMEM;
bool is_prack = !pl_strcmp(&msg->met, "PRACK");
bool non_invite = !pl_strcmp(&msg->met, "PRACK")
|| !pl_strcmp(&msg->met, "UPDATE");

if (!is_prack) {
if (!non_invite) {
reply = mem_zalloc(sizeof(*reply), destructor);
if (!reply)
goto out;
Expand All @@ -106,8 +107,7 @@ int sipsess_reply_2xx(struct sipsess *sess, const struct sip_msg *msg,
}

sip_contact_set(&contact, sess->cuser, &msg->dst, msg->tp);

err = sip_treplyf(is_prack ? NULL : &sess->st,
err = sip_treplyf(non_invite ? NULL : &sess->st,
reply ? &reply->mb : NULL, sess->sip,
msg, true, scode, reason,
"%H"
Expand Down Expand Up @@ -140,7 +140,7 @@ int sipsess_reply_2xx(struct sipsess *sess, const struct sip_msg *msg,

out:
if (err) {
if (!is_prack)
if (!non_invite)
sess->st = mem_deref(sess->st);

mem_deref(reply);
Expand Down
1 change: 1 addition & 0 deletions src/sipsess/sipsess.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ int sipsess_reply_1xx(struct sipsess *sess, const struct sip_msg *msg,
int sipsess_reply_ack(struct sipsess *sess, const struct sip_msg *msg,
bool *awaiting_answer);
int sipsess_reinvite(struct sipsess *sess, bool reset_ls);
int sipsess_update(struct sipsess *sess, bool reset_ls);
int sipsess_bye(struct sipsess *sess, bool reset_ls);
int sipsess_request_alloc(struct sipsess_request **reqp, struct sipsess *sess,
const char *ctype, struct mbuf *body,
Expand Down