From 7580649f71440e6d7d2373c410d5ec1a2433a6d9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 06:14:09 +0000 Subject: [PATCH 1/4] trans_layer: set the reply type in send_sf_error_reply() The sip_msg built for the stateful error reply never had its type set, so ~sip_msg did not delete the sip_reply it holds and every 400/420/483 rejection from sip_msg2am_request leaked it. Fixes #654 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019dXbSvpJc3UDsdCNVCxpZr --- core/sip/trans_layer.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/sip/trans_layer.cpp b/core/sip/trans_layer.cpp index 8547d1254..7a41392fc 100644 --- a/core/sip/trans_layer.cpp +++ b/core/sip/trans_layer.cpp @@ -684,6 +684,7 @@ int _trans_layer::send_sf_error_reply(const trans_ticket* tt, const sip_msg* req cstring to_tag(to_tag_buf,SL_TOTAG_LEN); sip_msg reply; + reply.type = SIP_REPLY; reply.u.reply = new sip_reply(reply_code,reason); char* c = (char*)hdrs.s; From 90307e0cc72aba5a5594d1d50c5da0d2abbdbf84 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 06:14:09 +0000 Subject: [PATCH 2/4] sip parser: parse the CSeq method after whitespace, fix param fall-through parse_cseq() returned as soon as whitespace or a line fold followed the method token, without calling parse_method(), and the sip_cseq constructor did not initialise 'method'. A request with a trailing space in CSeq could then fail the CSeq/request method check. All paths now go through parse_method(), 'method' is initialised, and the line-fold path no longer counts the CRLF as part of the method. In _parse_gen_params() the VP_PNAME_EQU case fell through into VP_PNAME_EQU_SWS, so 'tag = abc' was parsed with the value '= abc'. Fixes #655 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019dXbSvpJc3UDsdCNVCxpZr --- core/sip/parse_common.cpp | 1 + core/sip/parse_cseq.cpp | 8 +++++--- core/sip/parse_cseq.h | 3 ++- core/tests/test_parser.cpp | 39 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/core/sip/parse_common.cpp b/core/sip/parse_common.cpp index 2cc5213a9..a80eebbd6 100644 --- a/core/sip/parse_common.cpp +++ b/core/sip/parse_common.cpp @@ -189,6 +189,7 @@ static int _parse_gen_params(list* params, const char** c, DBG("'=' expected\n"); return MALFORMED_SIP_MSG; } + break; case VP_PNAME_EQU_SWS: switch(**c){ diff --git a/core/sip/parse_cseq.cpp b/core/sip/parse_cseq.cpp index da67be976..18aaf1ef4 100644 --- a/core/sip/parse_cseq.cpp +++ b/core/sip/parse_cseq.cpp @@ -104,7 +104,7 @@ int parse_cseq(sip_cseq* cseq, const char* beg, int len) case SP: case HTAB: cseq->method_str.set(beg,c-beg); - return 0; + goto method; } break; @@ -117,8 +117,8 @@ int parse_cseq(sip_cseq* cseq, const char* beg, int len) cseq->num_str.set(beg,c-(st==ST_CRLF?2:1)-beg); break; case C_METHOD: - cseq->method_str.set(beg,c-beg); - return 0; + cseq->method_str.set(beg,c-(st==ST_CRLF?2:1)-beg); + goto method; } st = saved_st; break; @@ -130,6 +130,8 @@ int parse_cseq(sip_cseq* cseq, const char* beg, int len) } cseq->method_str.set(beg,c-beg); + + method: if(parse_method(&cseq->method, cseq->method_str.s, cseq->method_str.len) < 0){ DBG("Cseq method parsing failed\n"); diff --git a/core/sip/parse_cseq.h b/core/sip/parse_cseq.h index 05e9bddac..a14f814a9 100644 --- a/core/sip/parse_cseq.h +++ b/core/sip/parse_cseq.h @@ -43,7 +43,8 @@ struct sip_cseq: public sip_parsed_hdr sip_cseq() : sip_parsed_hdr(), - num(0) + num(0), + method(0) {} }; diff --git a/core/tests/test_parser.cpp b/core/tests/test_parser.cpp index 753fdaceb..a45bb9b19 100644 --- a/core/tests/test_parser.cpp +++ b/core/tests/test_parser.cpp @@ -230,6 +230,45 @@ FCTMF_SUITE_BGN(test_parser) { } FCT_TEST_END(); + // A method followed by whitespace used to return before the method was + // parsed, leaving sip_cseq::method uninitialised (issue #655). + FCT_TEST_BGN(cseq_method_trailing_whitespace) { + const char *values[] = {"1 INVITE ", "1 INVITE\t", "1 INVITE \r\n", "1 INVITE\r\n "}; + for (size_t i = 0; i < sizeof(values) / sizeof(values[0]); i++) { + sip_cseq cseq; + cseq.method = -1; + int rc = parse_cseq(&cseq, values[i], strlen(values[i])); + fct_xchk(rc == 0 && cseq.method == sip_request::INVITE && cseq.method_str.len == 6, + "CSeq '%s': rc=%d method=%d method_len=%d", values[i], rc, cseq.method, cseq.method_str.len); + } + } + FCT_TEST_END(); + + FCT_TEST_BGN(cseq_method_is_initialised) { + sip_cseq cseq; + fct_chk_eq_int(cseq.method, sip_request::OTHER_METHOD); + } + FCT_TEST_END(); + + // Whitespace between a parameter name and '=' used to fall through into + // the value state, so "tag = abc" got the value "= abc" (issue #655). + FCT_TEST_BGN(gen_params_whitespace_before_equals) { + const char *value = "tag = abc;branch =z9hG4bK776"; + const char *c = value; + list params; + int rc = parse_gen_params(¶ms, &c, strlen(value), '\0'); + fct_req(rc == 0); + fct_req(params.size() == 2); + sip_avp *tag = params.front(); + sip_avp *branch = params.back(); + fct_chk(string(tag->name.s, tag->name.len) == "tag"); + fct_chk(string(tag->value.s, tag->value.len) == "abc"); + fct_chk(string(branch->name.s, branch->name.len) == "branch"); + fct_chk(string(branch->value.s, branch->value.len) == "z9hG4bK776"); + free_gen_params(¶ms); + } + FCT_TEST_END(); + // The CSeq number has to fit in 32 bits (RFC 3261 section 8.1.1.5); a // larger one must not wrap into a different, valid looking number. From 4a4b4c9c3b2c00dd67743c8c86bdd530b90d66c7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 06:14:09 +0000 Subject: [PATCH 3/4] AmSipDialog: count a pending INVITE only after the 100rel check Am100rel::onRequestIn() rejects with the static reply_error(), which does not go through onReplyTxed(), so an INVITE rejected there with 420/421 left pending_invites incremented and every later re-INVITE in the dialog was answered with 491. Fixes #656 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019dXbSvpJc3UDsdCNVCxpZr --- core/AmSipDialog.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/AmSipDialog.cpp b/core/AmSipDialog.cpp index 4334959a2..4cbfd0ade 100644 --- a/core/AmSipDialog.cpp +++ b/core/AmSipDialog.cpp @@ -120,12 +120,18 @@ bool AmSipDialog::onRxReqSanity(const AmSipRequest& req) + int2str(get_random() % 10) + CRLF); return false; } - - if (invite) - pending_invites++; } - return rel100.onRequestIn(req); + // The 100rel check may reject the request with a stateless reply that does + // not pass through onReplyTxed(), so only count the INVITE as pending once + // it has been accepted; otherwise pending_invites would never drop back. + if (!rel100.onRequestIn(req)) + return false; + + if (invite) + pending_invites++; + + return true; } bool AmSipDialog::onRxReqStatus(const AmSipRequest& req) From 3382dd2cb568f7f4cb765dff4fe49e416258f87f Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 06:14:09 +0000 Subject: [PATCH 4/4] trans_table: match a PRACK only against INVITE server transactions match_1xx_prack() only compared the RAck method with the stored request method, so a PRACK whose RAck named PRACK could match an earlier PRACK transaction and hit the assert in trans_layer::received_msg() in builds without NDEBUG. RFC 3262 reliable provisionals only exist for INVITE. Fixes #648 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019dXbSvpJc3UDsdCNVCxpZr --- core/sip/trans_table.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/sip/trans_table.cpp b/core/sip/trans_table.cpp index ed8024818..ce2b239b2 100644 --- a/core/sip/trans_table.cpp +++ b/core/sip/trans_table.cpp @@ -386,6 +386,13 @@ sip_trans* trans_bucket::match_1xx_prack(sip_msg* msg) } sip_trans* t = *it; + // RFC 3262: only provisional replies to an INVITE are sent + // reliably, so a PRACK can only match an INVITE server transaction. + if( (t->type != TT_UAS) || + (t->msg->u.request->method != sip_request::INVITE) ){ + continue; + } + /* first, check quickly if lenghts match (From tag, To tag, Call-ID) */ sip_from_to* from = dynamic_cast(msg->from->p);