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) 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/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; 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); 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.