diff --git a/AGENTS.md b/AGENTS.md index d860264..b07eb7e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -485,8 +485,8 @@ Rules: and `detached` not counted, `connecting` with `unknown` counted), its other states, ModemManager not answering and its second site (a refresh unanswered over a reconnected flow) not at all, and `tests/Engine` pin - nothing of it; the REGISTER wait's diverting arms — a datagram skipped on - the init port, a response to another transaction ignored, a provisional + nothing of it; the REGISTER wait's diverting arms — a response to + another transaction ignored, a provisional restarting the wait, a request (an INVITE among them) dispatched and answered under the wait; an incoming call answered from a session's dialer since the polkit gate — imsd-dialerd's `Accept` by the active @@ -638,19 +638,19 @@ Rules: same-second retransmission was answered again and absorbed, the delivery report answered 202. No capture of the benches on hardware holds a journal line of one of these arms (each arm's line is searched in them in the - fp6 journal's `2026-11-19-imsd-1.2.0-r32-cut` capture, `searches/arms.out`): a refused final to a submission's MESSAGE, an RP-ERROR this + fp6 journal's `2026-11-19-imsd-1.2.0-r32-cut` capture, `searches/arms.out`, and for the phone's + visits of 2026-11-20 to 2026-11-22 in its `2026-11-20-issues-2-5` + capture, `phone/arms.out` and `phone-form6/arms.out`): a refused final to a submission's MESSAGE, an RP-ERROR this code sends (the cause-96 unusable-TPDU path; an RP-ERROR that came in was reached: `smsc-override-2026-10-18-0217`, 02:19:41, RP cause 28 + TP-FCS - 0xc1, through `OnReport`), the final's wait and the report's - running out, a dead flow under a `Send`, a status report (one requested + 0xc1, through `OnReport`), the final's wait running out, the report's wait ending a message as `failed`, a dead flow under a `Send`, a status report (one requested at 01:33 with TP-SRR; none came in the following 11 minutes — the unfiltered journal of 01:33:30–01:45:00, `r23-phone-journal-2026-10-17-precise-and-modem-sms`; whether KPN's SC sends them over this path is unknown), 8-bit data (an MMS notification), a message while SMS over IP is off (the 480), a submit report In-Reply-To no MESSAGE of ours in flight (the 488) or one without - In-Reply-To for no message in flight (the 200), a page-mode MESSAGE, a - body that is no RP message, a delivery that names no gateway, a text + In-Reply-To for no message in flight (the 200), a page-mode MESSAGE, a body that is no RP message, a text past 255 segments, an EF_SMSP record 1 that cannot be read or holds no service centre, or one whose service centre is not an international number, a @@ -709,6 +709,35 @@ Rules: them was returned by the flow; at the commit before, the cut-short request was ignored as they were. Not in that bench: a 400 the flow cannot send. +- The daemon reads SIP from a socket in four places — the client flow, + the server ports' datagrams, their connections, the init port — and + each goes through a framer of `Imsd:Sip`. The two datagram reads 1.2.1 put behind `DatagramMessage` ran on the workstation's loopback + (2026-11-22, the fp6 journal's `2026-11-20-issues-2-5` capture). The + init port (`init-port`: the daemon in dev mode, its card and modem + reads answered by stand-ins, a stand-in P-CSCF answering the + unprotected REGISTER): a datagram of no bytes and one of line + breaks alone passed over, a 401 cut short of its Content-Length ignored + with its `UDP: datagram truncated … on the init port` line, and the 401 + after each taken; at the commit before, the empty datagram ended the + wait (`no 401 to unprotected REGISTER`) and the cut-short 401 was taken + for the reply. A request sent there, cut short or whole, drew a line + and no answer at either commit. The server ports (`server-ports`: + `ServerPorts` built around a bench `main`): a request with bytes after + its body handed on without them; one whose body is shorter than its + Content-Length answered 400 on either port, its Via, From, Call-ID and + CSeq echoed and its To tagged — the Content-Length of each on one line; a response, an ACK and a CANCEL cut + short so, and a request without an end of headers, ignored with their + lines; a datagram of line breaks alone passed over, and one of no bytes + with the request behind it handed on in the same pass; at the commit + before, those requests were handed on as they came, and the datagram of no + bytes ended the pass, the request behind it handed on in the next. On + the dev phone (2026-11-22, the capture's `phone-form6`, the binary of + 1.2.1) the 401 of a fresh registration after a reboot + came through the init port's read, and the submit report and the + delivery of each of two texts through the server ports' as datagrams (two per + text by a packet counter, none over TCP: `checks.log`); a MESSAGE the + phone sent to its own server port with a Content-Length larger than + its body was answered 400 (`inject-bench.log`). - `imsd-pcscf` ran on the dev phone (KPN) on 2026-10-11 against the live ims bearer: the `--mux 1 --profile 2 -v` sequence, a plain run, and `OutOfCall` on an unbound mux (the fp6 journal's `2026-10-11-pcscf-pco-probe` capture, @@ -1618,18 +1647,27 @@ found under its long name or, where it has one, its compact form (RFC 3261 7.3.3: a `c:` line is a `Content-Type`, an `i:` line a Call-ID, a `v:` line a Via), with or without white space before the colon; of a value folded onto further lines the first line is read (7.3.1 allows the -fold). A request that reaches the client flow in a datagram with fewer body -bytes than the Content-Length read from it says is answered 400 (RFC 3261 18.3: the element "SHOULD generate a 400 (Bad -Request) response"; the journal has `UDP: datagram truncated (…) — -answered 400`). Such a request is known by its first line, which ends with -the SIP version where a status line begins with it (7.1, 7.2), whatever -the version's letter case: a datagram cut short so that begins with a -status line, or with no line of SIP, is ignored, its line ending `— -ignored`. imsd keeps nothing of a request it answers so: a To tag -it adds is made from the request and a secret of the run, the same for the -same request (`BuildBadRequest`), and an ACK or a CANCEL cut short so draws -nothing — what RFC 3261 8.2.7 has an element do that answers without -state. +fold). A datagram on a server port or on the init port is +framed as one on the client flow is (RFC 3261 18.3): bytes past the +Content-Length read from it are discarded, and one that has no end of headers is +ignored with a line (`UDP: … on server socket — ignored`, `UDP: … +on the init port, from port — ignored`); one of no bytes or of line +breaks alone is passed over without a line, and so is one of other white +space alone on a server port. A request that reaches the client flow or a +server port in a datagram with fewer body bytes than the Content-Length +read from it says is answered 400 (RFC 3261 +18.3: the element "SHOULD generate a 400 (Bad Request) response"; the +journal has `UDP: datagram truncated (…) — answered 400`, with the +server port named before the dash). Such a request is known by its first +line, which ends with the SIP version where a status line begins with +it (7.1, 7.2), whatever the version's letter case: a datagram cut short +so that begins with a status line, or with no line of SIP, is ignored, +its line ending `— ignored`, and so is a request cut short so on the init +port, which answers no request. imsd keeps nothing of a request it +answers so: a To tag it adds is made from the request and a secret of +the run, the same for the same request (`BuildBadRequest`), and an ACK +or a CANCEL cut short so draws nothing — what RFC 3261 8.2.7 has an element +do that answers without state. - **A retransmitted MESSAGE** (the same Call-ID, CSeq and Via branch — the value of the first `branch=` of its first Via row; a MESSAGE with none diff --git a/implementations/main.cpp b/implementations/main.cpp index 2c64d82..a5b5a33 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -341,6 +341,11 @@ socklen_t MakeAddr(std::string_view ip, int port, sockaddr_storage& ss) { return sizeof(sockaddr_in); } +// The port a socket address names. +int PortOf(const sockaddr_storage& ss) { + return ss.ss_family == AF_INET6 ? ntohs(reinterpret_cast(&ss)->sin6_port) : ntohs(reinterpret_cast(&ss)->sin_port); +} + // ---- SIP over the protected TCP flow -------------------------------------- enum class Transport { Tcp, Udp }; constexpr std::string_view TransportName(Transport t) { return t == Transport::Udp ? "UDP" : "TCP"; } @@ -624,17 +629,35 @@ public: char buf[65535]; sockaddr_storage src; socklen_t sl = sizeof src; ssize_t n = recvfrom(us, buf, sizeof buf, 0, reinterpret_cast(&src), &sl); - if (n <= 0) break; + if (n < 0) break; std::string_view sv(buf, static_cast(n)); bool blank = sv.find_first_not_of("\r\n \t") == std::string_view::npos; if (blank) continue; - std::string msg(sv); + // One datagram = one message, framed as on the client flow + // (RFC 3261 18.3): bytes past its Content-Length are + // discarded; a request whose body is shorter than its + // Content-Length is answered 400 (CutShortRequest says + // which), the way any answer goes from here (below); + // whatever else DatagramMessage refuses is ignored with a + // line. One of white space alone, or of no bytes, was + // passed over above. + std::string why; + auto framed = imsd::sip::DatagramMessage(sv, &why); + bool cutShort = false; + if (!framed) { + framed = imsd::sip::CutShortRequest(sv); + cutShort = framed.has_value(); + } + if (!framed) { + if (!why.empty()) Log(std::format("UDP: {} on server socket {} — ignored", why, us == udpUc_ ? PortUc : PortUs)); + continue; + } + std::string msg = std::move(*framed); if (imsd::sip::Status(msg).has_value()) { // A response here means the P-CSCF answered to the Via port // instead of the source port of our request (rport) — it // never reaches the client flow. Say so. - int sp = src.ss_family == AF_INET6 ? ntohs(reinterpret_cast(&src)->sin6_port) : ntohs(reinterpret_cast(&src)->sin_port); - Log(std::format("UDP response on server socket {} from port {} ignored: {}", us == udpUc_ ? PortUc : PortUs, sp, sv.substr(0, sv.find("\r\n")))); + Log(std::format("UDP response on server socket {} from port {} ignored: {}", us == udpUc_ ? PortUc : PortUs, PortOf(src), std::string_view(msg).substr(0, msg.find("\r\n")))); continue; } if (!imsd::sip::Status(msg).has_value()) { // request only @@ -658,9 +681,15 @@ public: htons(static_cast(*vp)); if (udpUc_ >= 0) rs = udpUc_; } - out.push_back({std::move(msg), [rs, s, sll](const std::string& r) mutable { + auto reply = [rs, s, sll](const std::string& r) mutable { sendto(rs, r.data(), r.size(), 0, reinterpret_cast(&s), sll); - }}); + }; + if (cutShort) { + Log(std::format("UDP: {} on server socket {} — answered 400", why, us == udpUc_ ? PortUc : PortUs)); + reply(imsd::msg::BuildBadRequest(msg, BadRequestSecret())); + continue; + } + out.push_back({std::move(msg), std::move(reply)}); } } } @@ -2069,9 +2098,12 @@ private: // The final of the unprotected REGISTER (callid, CSeq 1) on the init // socket, within 8 s of the send or of its last provisional. The socket // is bound to the init port, not connected: what else it delivers — a - // late final of an earlier attempt, a stray datagram — is logged with - // its source port and skipped, never judged as the reply. Closes the - // socket; throws when nothing answers. + // late final of an earlier attempt, a stray datagram — is skipped, + // never judged as the reply, and logged with its source port unless it + // holds nothing but line breaks. A datagram is framed as on the other + // sockets (RFC 3261 18.3; DatagramMessage), but a request gets no + // answer here, whole or cut short: the socket is that REGISTER's own. + // Closes the socket; throws when nothing answers. std::string AwaitInitialFinal(int us, const std::string& callid) { double deadline = Mono() + 8.0; char buf[65535]; @@ -2085,10 +2117,16 @@ private: setsockopt(us, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv); sockaddr_storage src; socklen_t sl = sizeof src; ssize_t n = recvfrom(us, buf, sizeof buf, 0, reinterpret_cast(&src), &sl); - if (n <= 0) break; - std::string m(buf, static_cast(n)); + if (n < 0) break; + const int sp = PortOf(src); + std::string why; + auto framed = imsd::sip::DatagramMessage(std::string_view(buf, static_cast(n)), &why); + if (!framed) { + if (!why.empty()) Log(std::format("UDP: {} on the init port, from port {} — ignored", why, sp)); + continue; + } + std::string m = std::move(*framed); if (!imsd::sip::ResponseOf(m, callid, "1 REGISTER")) { - int sp = src.ss_family == AF_INET6 ? ntohs(reinterpret_cast(&src)->sin6_port) : ntohs(reinterpret_cast(&src)->sin_port); std::string_view sv = m; Log(std::format("ignoring a datagram on the init port from port {} that is not the unprotected REGISTER's reply: {}", sp, sv.substr(0, sv.find("\r\n")))); continue;