diff --git a/AGENTS.md b/AGENTS.md index 36f0854..720f6fe 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -41,7 +41,7 @@ PipeWire. The verified device/carrier combinations are in the README. | `project.cpp` | The canonical build description, for [Crafter Build](https://forgejo.catcrafts.net/Catcrafts/Crafter.Build). Chooses the product by `--product=`. | | `interfaces/Imsd.cppm` | The primary module interface of `imsd-core`: `export import`s the nine partitions below. | | `interfaces/Imsd-Util.cppm` | `imsd::util` — SIP tokens, IPv6 host/port formatting, the RFC 5952 §4 IPv6 text (`CanonicalIp6`/`CanonicalAddr`; an IPv4-mapped address spelled in hex stays hex, where §5 and `inet_ntop` write it dotted) and the comma-separated address-list parser (`ParseAddressList`), hex/base64, MD5 (RFC 1321), the fixed protected ports `PortUc`/`PortUs` (45061/45062). | -| `interfaces/Imsd-Sip.cppm` | `imsd::sip` — SIP as a text layer plus TCP framing: header extraction (from the header section only: a read ends at the first empty line after the start line, empty lines before it skipped; a header is asked for by its long name and found under that or its compact form — RFC 3261 7.3.3: the RFC's ten, and `o` for Event and `x` for Session-Expires — with or without white space before the colon; `HeaderUri`: the party a header names; `Body`, moved here out of `CallMachine`), status codes, the granted lifetime, `ResponseOf` transaction matching. Not a full RFC 3261 parser, deliberately. | +| `interfaces/Imsd-Sip.cppm` | `imsd::sip` — SIP as a text layer plus TCP framing: header extraction (from the header section only: a read ends at the first empty line after the start line, empty lines before it skipped; a header is asked for by its long name and found under that or its compact form — RFC 3261 7.3.3: the RFC's ten, and `o` for Event and `x` for Session-Expires — with or without white space before the colon; `HeaderUri`: the party a header names; `Body`, moved here out of `CallMachine`), status codes, the granted lifetime, `ResponseOf` transaction matching (the Call-ID, and the CSeq as its number and its method, whatever spaces or tabs part them and whatever zeros stand before the number). Not a full RFC 3261 parser, deliberately. | | `interfaces/Imsd-Sdp.cppm` | `imsd::sdp` — SDP offer/answer: the AMR-WB + AMR + telephone-event offer (optional IR.92 preconditions; `OfferedTelephoneEventPt` names its telephone-event numbers), the parser the media plane needs (the codec, its payload type and mode, and the telephone-event payload type the m-line lists at that codec's clock), `BuildAnswer` for the terminating side, the `CODECS` override. | | `interfaces/Imsd-Sms.cppm` | `imsd::sms` — the short message codecs SMS over IP rides on (TS 24.341 carries the bytes a 2G/3G phone puts in an RP-DATA, in a SIP MESSAGE body): the GSM 7-bit default alphabet with its extension table and the septet packing, UCS-2 (TS 23.038); the SM-TL PDUs (TS 23.040: SMS-SUBMIT built, one per segment with the concatenation header when the text needs more than one; SMS-DELIVER, SMS-STATUS-REPORT and SMS-SUBMIT-REPORT parsed; the SMS-DELIVER-REPORT that acknowledges a delivery; the user-data header — concatenation, application ports — and the `Reassembler` of a concatenated message); the SM-RL messages (TS 24.011: RP-DATA, RP-ACK, RP-ERROR in both directions, the cause values); EF_SMSP (TS 31.102 §4.2.27), where the (U)SIM names the service centre, and qmicli's hex dump of it. Byte-level only: no SIP, no clock, no I/O. | | `interfaces/Imsd-Aka.cppm` | `imsd::aka` — USIM AKA glue: the AKAv1-MD5 digest (RFC 3310), IMPI/IMPU/realm from IMSI + MCC/MNC, parsers for `qmicli --uim-get-card-status` and `mmcli` output, the AUTHENTICATE APDU and its response. | @@ -304,7 +304,12 @@ What the suites pin: in a quoted string no end of it, the comma behind the closing quote its end) —, white space before the colon (RFC 3261 7.3.1's four spellings), - `ResponseOf`, refused finals with reason headers, + `ResponseOf` (the transaction's own 100 Trying; a final whose CSeq parts + its number and its method by two spaces, by a tab or by both, or has + zeros before its number: the + transaction's; another number, another method, a method in other letter + case, a CSeq without one of the two: not), refused finals with reason + headers, reginfo documents, `HeaderUri` and `Body`. A change that flips one of those tests is a behaviour change, not a cleanup. - `Sdp` — the offer's exact bytes, the parser (the telephone-event payload diff --git a/interfaces/Imsd-Sip.cppm b/interfaces/Imsd-Sip.cppm index 006c56b..c91a332 100644 --- a/interfaces/Imsd-Sip.cppm +++ b/interfaces/Imsd-Sip.cppm @@ -136,6 +136,21 @@ namespace imsd::sip { if (ec != std::errc{}) return std::nullopt; return v; } + + // A CSeq value as its sequence number and its method (RFC 3261 25.1: + // 1*DIGIT LWS Method): the digits the trimmed value begins with, less + // the zeros before the number, the spaces or tabs after them, then the + // method to the value's end. nullopt where no space or tab follows the + // digits. + std::optional> CSeqParts(std::string_view v) { + v = Trim(v); + const std::size_t digits = v.find_first_not_of("0123456789"); + const std::size_t method = v.find_first_not_of(" \t", digits); + if (method == digits) return std::nullopt; + std::string_view number = v.substr(0, digits); + while (number.starts_with('0')) number.remove_prefix(1); + return std::pair{number, v.substr(method)}; + } } export namespace imsd::sip { @@ -178,9 +193,16 @@ export namespace imsd::sip { // True for a response of the transaction (callid, cseq) — the reply a // wait for that request may judge. A request, another transaction's - // response or anything else a socket delivers is not. + // response or anything else a socket delivers is not. Two CSeqs are + // equal "if the sequence number and the request method are identical" + // (RFC 3261 20.16): they are compared so, whatever spaces or tabs part + // the two and whatever zeros stand before the number. bool ResponseOf(std::string_view msg, std::string_view callid, std::string_view cseq) { - return Status(msg).has_value() && Header(msg, "Call-ID") == callid && Header(msg, "CSeq") == cseq; + if (!Status(msg).has_value() || Header(msg, "Call-ID") != callid) return false; + const auto theirs = Header(msg, "CSeq"); + if (!theirs) return false; + const auto a = CSeqParts(*theirs), b = CSeqParts(cseq); + return a && b && *a == *b; } // Retry-After of a response (RFC 3261 20.33), in seconds; nullopt when diff --git a/tests/Sip/main.cpp b/tests/Sip/main.cpp index 77fc8b8..62ce946 100644 --- a/tests/Sip/main.cpp +++ b/tests/Sip/main.cpp @@ -355,6 +355,25 @@ int main() { Check(!ResponseOf("NOTIFY sip:x SIP/2.0\r\nCall-ID: 12345@host\r\nCSeq: 2 REGISTER\r\n\r\n", "12345@host", "2 REGISTER"), "a request is never a response"); Check(!ResponseOf("", "12345@host", "2 REGISTER"), "nothing is not it"); Check(ResponseOf("SIP/2.0 202 Accepted\r\nv: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKx\r\ni: 12345@host\r\nCSeq: 1 MESSAGE\r\nl: 0\r\n\r\n", "12345@host", "1 MESSAGE"), "a final whose Call-ID comes as `i:` is the transaction's"); + // a provisional of the transaction is its response too: both REGISTER waits start over on one + Check(ResponseOf("SIP/2.0 100 Trying\r\nCall-ID: 12345@host\r\nCSeq: 2 REGISTER\r\nContent-Length: 0\r\n\r\n", "12345@host", "2 REGISTER"), "the transaction's 100 Trying is its response"); + // the CSeq as its number and its method (RFC 3261 20.16: equal "if the sequence number and the request method are identical"; 25.1: 1*DIGIT LWS Method), whatever spaces or tabs part them + auto finalWith = [](std::string_view cseq) { return std::format("SIP/2.0 202 Accepted\r\nCall-ID: 12345@host\r\nCSeq: {}\r\nContent-Length: 0\r\n\r\n", cseq); }; + Check(ResponseOf(finalWith("1 MESSAGE"), "12345@host", "1 MESSAGE"), "two spaces between the CSeq's number and its method: the transaction's"); + Check(ResponseOf(finalWith("1\tMESSAGE"), "12345@host", "1 MESSAGE"), "a tab between them: the transaction's"); + Check(ResponseOf(finalWith("1 \t MESSAGE"), "12345@host", "1 MESSAGE"), "spaces and a tab between them: the transaction's"); + Check(ResponseOf(finalWith("01 MESSAGE"), "12345@host", "1 MESSAGE") && ResponseOf(finalWith("1 MESSAGE"), "12345@host", "001 MESSAGE"), "zeros before the number do not make it another"); + Check(ResponseOf(finalWith("0 MESSAGE"), "12345@host", "00 MESSAGE") && !ResponseOf(finalWith("10 MESSAGE"), "12345@host", "1 MESSAGE"), "zero is zero however long, and a zero after the number counts"); + Check(ResponseOf(finalWith("1 MESSAGE"), "12345@host", " 1 MESSAGE "), "white space around the CSeq asked for is no part of it"); + Check(!ResponseOf(finalWith("11 MESSAGE"), "12345@host", "1 MESSAGE") && !ResponseOf(finalWith("1 MESSAGE"), "12345@host", "11 MESSAGE"), "another number that begins alike is not it"); + Check(!ResponseOf(finalWith("1 MESSAGES"), "12345@host", "1 MESSAGE") && !ResponseOf(finalWith("1 MESSAG"), "12345@host", "1 MESSAGE"), "another method that begins alike is not it"); + Check(!ResponseOf(finalWith("1 message"), "12345@host", "1 MESSAGE"), "a method is a case-sensitive token"); + Check(!ResponseOf(finalWith("1MESSAGE"), "12345@host", "1 MESSAGE"), "no white space between number and method: not a CSeq"); + Check(!ResponseOf(finalWith("1 "), "12345@host", "1 MESSAGE") && !ResponseOf(finalWith("1"), "12345@host", "1 MESSAGE"), "a CSeq without a method is not it"); + Check(!ResponseOf(finalWith("MESSAGE"), "12345@host", "1 MESSAGE") && !ResponseOf(finalWith(" MESSAGE"), "12345@host", "1 MESSAGE"), "a CSeq without a number is not it"); + Check(!ResponseOf("SIP/2.0 202 Accepted\r\nCall-ID: 12345@host\r\nContent-Length: 0\r\n\r\n", "12345@host", "1 MESSAGE"), "a final without a CSeq is not it"); + Check(!ResponseOf(finalWith("1 MESSAGE"), "12345@host", "MESSAGE"), "asked with a CSeq of another shape: nothing is it"); + Check(!ResponseOf(finalWith("MESSAGE"), "12345@host", "MESSAGE") && !ResponseOf(finalWith("1MESSAGE"), "12345@host", "1MESSAGE"), "two CSeqs of another shape are no match, alike or not"); } // ---- RegInfoContactState: our binding in a reg-event NOTIFY (RFC 3680)