From 0aea0dac7f18ff23e9dbc97d94561f84b7715420 Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Wed, 25 Nov 2026 10:24:07 +0100 Subject: [PATCH] imsd: when the client flow is found dead before any response to a call's INVITE, one reconnect and the INVITE again, else the call ends A flow that is dead in a way imsd has not seen yet, the peer having dropped the connection's state, shows itself at the next bytes sent on it, where the peer answers them with a reset. Where those bytes were an outgoing call's INVITE, imsd read the reset, marked the flow dead and did nothing more for the call: no reconnect runs while a call is up, so the attempt stayed in `dialing` until its setup deadline, 90 s, sent a CANCEL that could not go out and was given up 10 s later, where a call that is no emergency call ends (`setup deadline expired`); the flow was reconnected only once the call was gone. Against a stand-in network that forgets a flow after 20 s of silence, 1.2.3's call ended 100 s after the Dial. The engine's loop now takes such a call up (MaybeResendInvite). When the client flow is found dead under an outgoing attempt whose INVITE has had no response, a 100 included, and no CANCEL, the dead socket is closed without a FIN's exchange, the flow is reopened with one connect and the same INVITE is sent again, the setup deadline running from then. Once per attempt: an emergency call's fallback attempt has a resend of its own. The INVITE is the first one byte for byte, its branch included, so a server that did receive the first matches the second to its transaction (RFC 3261 17.2.3). No REGISTER goes first: the registration and its SAs stand, and TS 33.203 7.1 has a UE with no TCP connection towards the P-CSCF set one up before it sends a request. Where a reg-event NOTIFY has ended the binding nothing is reopened. Where nothing is reopened, the connect fails (one that gets no answer, after 12 s) or the flow dies again before a response, the call ends with `error`, an emergency call's first attempt too: there is no flow for its fallback. The recovery policy takes the flow up once the call is gone, as before. The close without a FIN's exchange is for a flow the peer closed: closed the usual way, such a socket stays in the kernel's table until the peer has acknowledged imsd's own FIN, and a connect from its port before that is refused. A reset goes out in the FIN's place there; after a reset or the kernel's own giving up the socket is closed already and nothing is sent; and the reopen runs on a flow already found dead only. The recovery's reconnect closes the usual way, as before, and connects once more 10 s after such a refusal (one is in a user's log on O2, 2026-09-24, in the second the flow was found dead). Not changed: under a call, a flow that died with no reset and no close reaching the phone is found dead only when the kernel gives up on unacknowledged bytes, 924.6 s or more after them by its default tcp_retries2, so an attempt whose own INVITE is the first such bytes gets no response and is cancelled at its setup deadline, as before; a flow found dead after a response to the INVITE is not reopened, as before; a Dial whose INVITE cannot be sent ends its call at once, as before. Tested: tests/Engine (which attempt is sent again, the same bytes, once per attempt, the deadline anew, the call's end where the flow is lost); the daemon of this commit and 1.2.3's on the workstation against a stand-in network (the fp6 journal's 2026-11-24-crlf-keepalive capture, bench-dial.sh): a flow the network forgot, reopened and the INVITE answered 486 as on a live flow, where 1.2.3's call ended after 100 s; a flow the network closed at the INVITE, with the acknowledgement of the daemon's close kept back; one that died on the daemon's side at the kernel's retransmission limit; a network that takes no new connection, one that resets the second INVITE's flow too and one that has ended the binding, where the call ended within a second; UDP. And on the dev phone on KPN, the binary of the range's tip: a call to voicemail answered and hung up, and one whose flow was made to die under the INVITE before the INVITE left the phone: reopened from the same port, the INVITE again 2.1 s after the death was found, answered 0.2 s later. By a call, no P-CSCF that had dropped a flow's state has been met, and none that had received the first INVITE before the flow died. --- AGENTS.md | 72 ++++++++++++++++++++++++++++++++++-- implementations/main.cpp | 36 ++++++++++++++++++ interfaces/Imsd-Engine.cppm | 58 ++++++++++++++++++++++++++--- tests/Engine/main.cpp | 74 +++++++++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+), 9 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 8b9105c..0b18a03 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -158,7 +158,13 @@ derived ones for a file that has none; `FreshRegister`'s restore after a failed attempt) — the commands, the answers' reading, the follow-up, the value of a file and the check of the identity are `Imsd:Aka`'s, with tests; the shell's part ran against -stand-ins on the workstation (§5). SMS over IP (1.2.0, §7.9) keeps in the +stand-ins on the workstation (§5). A call whose client flow is found dead +before any response to its INVITE (1.2.4, §7.6) has its decisions in +`CallMachine`, with tests: which attempt is sent again, with which bytes, +and when the call ends instead. The shell's part (`MaybeResendInvite`: the dead flow closed +without a FIN's exchange, the one connect, the policy's word on whether the +old registration may be reconnected over) ran against a stand-in network on +the workstation and on the dev phone (§5). SMS over IP (1.2.0, §7.9) keeps in the shell, among others, the service centre's source (`SMSC=` over EF_SMSP, `LoadSmsParameters`), the three counters (TP-MR, RP-MR, the concatenation reference), their seeding and the TP-MR file, the routing of an inbound MESSAGE @@ -414,7 +420,10 @@ What the suites pin: (reliable 18x/PRACK, 2xx ACK + media start, far-end hangup, CANCEL/answer races, busy/error finals, the terminating side's 100/180/200/486/487/488, G.711-only and AMR-NB gateway offers, ring timeout, BYE both ways, the - emergency chain, DTMF — sent to the leg on an active call that negotiated + emergency chain, an attempt's INVITE with no response and no CANCEL yet + sent again over a flow the shell reopened, byte for byte and once per + attempt, and its call ended where the flow is lost (an emergency call's + first attempt too), DTMF — sent to the leg on an active call that negotiated telephone-event, dropped with the reason before the answer, after the call, without the negotiation, for a character that is no tone or for a string past `DtmfMaxTones`; the receive payload type: our offer's on a @@ -948,6 +957,40 @@ Rules: phone the 401 of the fresh registration came from KPN, whose 401 of 2026-07-18 had its WWW-Authenticate over four lines (§13; the fresh one was not dumped). +- A call whose client flow is found dead before any response to its + INVITE (§7.6, 1.2.4). + The machine's side is pinned in `tests/Engine`; the shell's + (`MaybeResendInvite`) ran on the workstation against a stand-in network + (2026-11-24, the fp6 journal's `2026-11-24-crlf-keepalive` capture, + `bench/bench-dial.sh`: the daemon in dev mode, its card and modem reads + answered by stand-ins, a Dial put on its bus 25 s after the + registration, a network that answers a delivered INVITE 486 and can + drop a flow in several ways). On a flow the network had forgotten, + which it reset at the INVITE's bytes, the flow was reopened and the + INVITE delivered in the same second, the call ending `refused-or-busy` + as on a live flow; 1.2.3's call there ended `error` 100 s after the + Dial. The same outcome where the network closed the flow at the INVITE + and no acknowledgement of the daemon's own close reached it (what the + close without a FIN's exchange is for). Where the flow died on the + daemon's side at the kernel's retransmission limit, the first INVITE + never delivered, the second went out about 2 s after the death was + found and the call ended the same way. Where the network took no new + connection (it refused the reopen's connect), where it reset the + reopened flow at the second INVITE, and where a reg-event NOTIFY had + ended the binding before the reset, the call ended `error` within a + second of the Dial. Over UDP an ICMP port unreachable ended the flow: + the INVITE went out once more and the call ended `error` at the second + ICMP. On the dev phone (KPN, the same day, the capture's `phone3/`, the + binary of 1.2.4): a call to voicemail answered and hung up; and one + whose flow was made to die under the INVITE before the INVITE left the + phone (every segment of the flow dropped on the phone, five in all, and + the kernel's retransmission limit lowered for the moment; nothing of + the old connection's end went to the P-CSCF): the flow was reopened + from the same port, the INVITE went out again 2.1 s after the death was + found, and the call was answered 0.2 s after that. Not met by a call: + a P-CSCF that had dropped a flow's state, and one that had received + the first INVITE before the flow died; a reopen's connect that gets no + answer at all; the emergency chain on a network. - `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, @@ -1579,7 +1622,24 @@ sent, emit this D-Bus state change, arm this deadline). States and reasons are t default; `CODECS` restricts and orders; `PRECOND=1` adds IR.92 preconditions), reliable 18x with PRACK, 2xx ACK and media start, CANCEL before answer, BYE after. Busy and error finals end the call with - `refused-or-busy` or `error`. + `refused-or-busy` or `error`. Where the client flow is found dead after + an attempt's INVITE was sent and before any response to it (a 100 + included) or a CANCEL of it, the shell closes the dead socket without a + FIN's exchange, reopens the flow with one connect and the same INVITE + goes out again, once per attempt of the emergency chain, the setup + deadline running from then (`MaybeResendInvite`). No REGISTER goes + first: the registration and its SAs stand (where a reg-event NOTIFY has + ended the binding, §7.4, nothing is reopened), and TS 33.203 7.1 has a + UE that has no TCP connection towards the P-CSCF set one up before it + sends a request. Where nothing is reopened, the connect fails (one that + gets no answer, after 12 s) or the flow dies again before a response, + the call ends with `error`. Under a call, a flow that died with no + reset and no close reaching the phone is found dead only when the + kernel gives up on bytes sent on it that were never acknowledged + (924.6 s or more after them by its default `tcp_retries2` of 15): an + attempt whose own INVITE is the first such bytes gets no response and + is cancelled at its setup deadline, long before that. A flow found dead + after a response to the INVITE is not reopened. - Incoming: an INVITE on a protected-port listener is validated before it is announced to the UI; 100 and 180 (reliable when the caller requires 100rel); `Accept` sends the 200 with the SDP answer at the offer's own @@ -1592,7 +1652,8 @@ sent, emit this D-Bus state change, arm this deadline). States and reasons are t `EMERGENCY_NUMBERS`; SIM `EF_ECC` is not read yet) is tried as `urn:service:sos` first, with one fallback to a plain INVITE of the digits; a plain INVITE answered `380 Alternative Service` with an emergency body is - re-attempted as `urn:service:sos`. + re-attempted as `urn:service:sos`. An attempt whose client flow is lost + (above) ends the call there: no fallback follows it. - DTMF (`SendDtmf`): the tones go out as RFC 4733 telephone-event packets on the call's RTP stream, at the payload type the SDP negotiated for the codec's clock (`MediaLeg::dtmfPt`: the answer's for an outgoing call, the @@ -1779,6 +1840,9 @@ Lines the operator (and the scripts) look for, all prefixed `imsd: `: failed: … — the resume is given up (the engine's rule): the pair is removed`). - `audio session: …` at start and at each call's media start; `CODECS override active: …`. +- `call X: the client flow died before any response to the INVITE; reopening it` and, when the connect + succeeded, `call X: the INVITE again over the reopened flow` (§7.6); `call X ended: error — the client flow + died before any response to the INVITE` where the call ends instead. - `media pt=N octet=true|false -> [X]:P, telephone-event pt N|none` at a call's media start (`… pt N (theirs M)` when `DTMF_RX_PT` differs from the one we send with); `call X: DTMF '…' -> media leg (telephone-event pt diff --git a/implementations/main.cpp b/implementations/main.cpp index 296eb7c..34a8198 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -635,6 +635,17 @@ public: // What was read and not yet dispatched goes with the flow: nothing // read from it can be answered once it is closed. void Close() { if (fd_ >= 0) { close(fd_); fd_ = -1; } alive_ = false; fb_ = {}; } + // Close without the FIN's exchange: the kernel drops the connection at + // once (after the peer's FIN with a reset in our FIN's place; where a + // reset or its own giving up has ended it, it sends nothing), so the + // same local port can connect to the same peer again in the same + // breath. A socket closed with Close() after the peer's FIN stays in the + // kernel's table until the peer has acknowledged ours, and a connect from + // its port meanwhile is refused (EADDRNOTAVAIL). For a dead flow only. + void Abort() { + if (fd_ >= 0) { linger l{1, 0}; setsockopt(fd_, SOL_SOCKET, SO_LINGER, &l, sizeof l); } + Close(); + } private: // One read of the socket once poll(2) has named it: `m` is the message a // datagram held (UDP); on TCP the bytes go to the frame buffer. False @@ -1658,6 +1669,7 @@ public: MaybeRefresh(); MaybeReconnect(); MaybeResubscribe(); + MaybeResendInvite(); if (!call_ && sip_.Alive() && Mono() - lastKa_ > 30) { sip_.SendKeepalive(); lastKa_ = Mono(); } ReapMedia(); if (call_ && call_->State() != imsd::engine::CallState::Active && Mono() > deadline_) { @@ -2664,6 +2676,30 @@ private: else ReconnectCycle(); } + // A client flow found dead under an outgoing call whose INVITE has had + // no response and no CANCEL (CallMachine::Unanswered) is reopened once + // per attempt and the INVITE sent again: a flow the network had + // forgotten is reset at the first bytes sent on it, and those may be a + // call's. No REGISTER goes first: the registration and its SAs stand + // (not where the network has ended the binding: RegRecovery's + // Reconnectable, and then nothing is reopened), and TS 33.203 7.1 has a + // UE that has no TCP connection towards the P-CSCF set one up before it + // sends a request. One connect, which waits 12 s for an answer; where + // it fails, and where the flow dies again under the resent INVITE, the + // call ends (CallMachine::OnFlowLost), and the recovery policy takes + // the flow up when the call is gone (MaybeReconnect). + void MaybeResendInvite() { + if (!call_ || sip_.Alive() || !call_->Unanswered()) return; + bool ok = false; + if (recovery_.Reconnectable() && call_->ResendDue()) { + Log(std::format("call {}: the client flow died before any response to the INVITE; reopening it", call_->Uni())); + sip_.Abort(); + ok = sip_.Connect(local_, PortUc, pcscf_, portPs_, transport_, /*tries=*/1) && Execute(call_->OnFlowReopened()); + } + if (!ok && call_ && !call_->Terminated()) Execute(call_->OnFlowLost()); + MaybeDropCall(); + } + // The policy's next step, as the journal line: what, and when. std::string NextStep() { auto plan = recovery_.Next(Boot()); diff --git a/interfaces/Imsd-Engine.cppm b/interfaces/Imsd-Engine.cppm index 8774435..607b7bc 100644 --- a/interfaces/Imsd-Engine.cppm +++ b/interfaces/Imsd-Engine.cppm @@ -420,11 +420,49 @@ export namespace imsd::engine { offer.codecs = codecs_; std::string sdp = imsd::sdp::BuildOffer(offer); std::vector a; - a.push_back(Send(imsd::msg::BuildInvite(ctx_, d_, sdp, precond_))); + inviteOut_ = imsd::msg::BuildInvite(ctx_, d_, sdp, precond_); + a.push_back(Send(inviteOut_)); a.push_back(Deadline(InviteTimeout)); return a; } + // The current outgoing attempt has sent its INVITE and has had no + // response to it, not a 100 either, and no CANCEL has followed it. + // The shell asks when it finds the client flow dead under a call: a + // flow the network had forgotten is reset at the first bytes sent + // on it, and those may be this INVITE. + bool Unanswered() const { + return state_ == CallState::Dialing && !inviteOut_.empty() && !answered_ && !cancelled_; + } + // … and the attempt has not been sent again yet. + bool ResendDue() const { return Unanswered() && !resent_; } + + // The shell reopened the flow under an attempt whose resend is due: + // the same INVITE again, byte for byte (the same branch: RFC 3261 + // 17.2.3 has a server that did receive the first match this one to + // its transaction), and the setup deadline from now. Once per + // attempt. + std::vector OnFlowReopened() { + std::vector a; + if (!ResendDue()) return a; + resent_ = true; + a.push_back(LogAct(std::format("call {}: the INVITE again over the reopened flow", uni_))); + a.push_back(Send(inviteOut_)); + a.push_back(Deadline(InviteTimeout)); + return a; + } + + // The flow under an unanswered attempt is dead and stays so (it + // could not be reopened, or died again after the resend): the call + // ends here, an emergency call's first attempt too, with nothing to + // send a CANCEL on and no flow for a fallback attempt. + std::vector OnFlowLost() { + std::vector a; + if (!Unanswered()) return a; + Append(a, Finish("error", "the client flow died before any response to the INVITE")); + return a; + } + // UAS: answer the inbound INVITE with 100 Trying + 180 Ringing (made // reliable only when the caller Requires 100rel) and arm the ring // timeout. An offer we cannot serve — no audio line, or none of the @@ -499,6 +537,7 @@ export namespace imsd::engine { AbsorbDialog(msg); auto st = imsd::sip::Status(msg); if (!st) return a; + answered_ = true; int code = *st; if (code > 100 && code < 200) { if (state_ == CallState::Dialing) { @@ -761,6 +800,12 @@ export namespace imsd::engine { bool sosTried_ = false; bool plainTried_ = false; bool userCancelled_ = false; // CANCEL came from OnHangup, not a deadline + // The current outgoing attempt's INVITE as sent, whether any response + // to it has come, and whether it went out a second time over a + // reopened flow (OnFlowReopened). + std::string inviteOut_; + bool answered_ = false; + bool resent_ = false; std::set pracked_; std::string answerSdp_; std::optional dtmfPt_; // the running leg's telephone-event pt, set at media start @@ -781,15 +826,18 @@ export namespace imsd::engine { answerSdp_.clear(); cancelled_ = false; userCancelled_ = false; + answered_ = false; + resent_ = false; } // The emergency fallback chain — where the next attempt (if any) // points. `msg` may be empty (deadline paths). // - a sos INVITE that fails for any reason other than the user - // hanging up is re-attempted as a plain INVITE of the dialled - // digits: the pre-emergency-support behavior, so classification - // can only improve on the status quo, never block a call the old - // code would have placed. + // hanging up, its own send failing at the Dial (Fail) or the + // client flow lost under it (OnFlowLost: there is nothing to send + // on) is re-attempted as a plain INVITE of the dialled digits: the + // pre-emergency-support behavior, so classification can only improve + // on the status quo, never block a call the old code would have placed. // - a plain INVITE answered 380 Alternative Service whose body // names emergency (TS 24.229 5.1.6: the network classified a // number we didn't) is re-attempted as urn:service:sos. A 380 diff --git a/tests/Engine/main.cpp b/tests/Engine/main.cpp index 9429a13..4d1ab5f 100644 --- a/tests/Engine/main.cpp +++ b/tests/Engine/main.cpp @@ -470,6 +470,55 @@ int main() { Check(m.Terminated(), "terminated on second timeout"); } + // ---- the client flow dies before any response to the INVITE: the shell + // reopens it and the same INVITE goes out again, once per attempt + { + imsd::util::Rng rng(16); + CallMachine m(c, rng, "ims-call-6b", "1233", 50004, false); + Check(!m.Unanswered() && !m.ResendDue(), "no INVITE sent yet: nothing to send again"); + auto start = m.Start(); + const Action* inv = Sent(start, "INVITE "); + Check(m.Unanswered() && m.ResendDue(), "an INVITE with no response: a resend is due"); + auto again = m.OnFlowReopened(); + const Action* inv2 = Sent(again, "INVITE "); + Check(inv && inv2 && inv2->text == inv->text, "the resent INVITE is the first one, byte for byte"); + const Action* dl = Find(again, Action::Type::SetDeadline); + Check(dl && dl->seconds == imsd::engine::InviteTimeout, "the resend arms the setup deadline anew"); + Check(m.State() == CallState::Dialing && !m.Terminated(), "still dialing after the resend"); + Check(m.Unanswered() && !m.ResendDue(), "one resend per attempt"); + Check(m.OnFlowReopened().empty(), "a second reopen sends nothing"); + auto lost = m.OnFlowLost(); + const Action* t = Find(lost, Action::Type::State); + Check(t && t->reason == "error" && m.Terminated(), "a flow that dies again ends the call with error"); + Check(Has(lost, Action::Type::Deleted) && Sent(lost, "CANCEL ") == nullptr, "... deleted, and no CANCEL onto a dead flow"); + Check(!m.Unanswered() && m.OnFlowLost().empty() && m.OnFlowReopened().empty(), "an ended call has nothing more to end or to send"); + } + + // ---- ... not once a response has come (a 100 is one), the user has hung + // up or a deadline has cancelled the attempt, and never for an + // incoming call + { + imsd::util::Rng rng(17); + CallMachine m(c, rng, "ims-call-6c", "1233", 50004, false); + m.Start(); + m.OnInviteResponse(Final(m.CallId(), 100, "Trying")); + Check(!m.Unanswered() && !m.ResendDue(), "a 100 Trying is a response: no resend"); + Check(m.OnFlowReopened().empty() && m.OnFlowLost().empty() && !m.Terminated(), "... and the flow's death is left to the deadline"); + imsd::util::Rng rng2(18); + CallMachine n(c, rng2, "ims-call-6d", "1233", 50004, false); + n.Start(); + n.OnHangup(); + Check(!n.Unanswered() && n.OnFlowReopened().empty() && n.OnFlowLost().empty(), "an attempt the user cancelled is not sent again"); + imsd::util::Rng rng3(19); + CallMachine o(c, rng3, "ims-call-6e", "1233", 50004, false); + o.Start(); + o.OnDeadline(); // ring timeout: CANCEL + Check(!o.Unanswered() && o.OnFlowReopened().empty() && o.OnFlowLost().empty(), "nor one a deadline cancelled"); + imsd::util::Rng rng4(20); + CallMachine p(c, rng4, "ims-call-6f", IncomingInvite{MtInvite()}, 50004); + Check(!p.Unanswered() && p.OnFlowReopened().empty() && p.OnFlowLost().empty(), "an incoming call has no INVITE of ours to send again"); + } + // ================= incoming (terminating / UAS) ======================= // ---- full accepted incoming call: INVITE -> 100+180, Accept -> 200 with @@ -880,6 +929,31 @@ int main() { Check(m.State() == CallState::Active, "active on the fallback leg"); } + // ---- the emergency chain with a flow that dies: each attempt has a + // resend of its own + { + imsd::util::Rng rng(44); + CallMachine m(c, rng, "ims-call-16b", "112", 50004, false, true); + auto start = m.Start(); + auto again = m.OnFlowReopened(); + const Action* sos = Sent(start, "INVITE "); + const Action* sos2 = Sent(again, "INVITE "); + Check(sos && sos2 && sos2->text == sos->text && sos2->text.starts_with("INVITE urn:service:sos "), "the sos INVITE again over a reopened flow"); + auto a = m.OnInviteResponse(Final(m.CallId(), 500, "Server Internal Error")); + const Action* plain = Sent(a, "INVITE "); + Check(plain && plain->text.starts_with("INVITE sip:112;phone-context="), "the fallback follows a resent sos attempt as it follows a first one"); + Check(m.Unanswered() && m.ResendDue(), "the fallback attempt has a resend of its own"); + auto again2 = m.OnFlowReopened(); + const Action* plain2 = Sent(again2, "INVITE "); + Check(plain && plain2 && plain2->text == plain->text, "... of the fallback's INVITE"); + imsd::util::Rng rng2(45); + CallMachine n(c, rng2, "ims-call-16c", "112", 50004, false, true); + n.Start(); + auto lost = n.OnFlowLost(); + const Action* t = Find(lost, Action::Type::State); + Check(t && t->reason == "error" && n.Terminated() && Sent(lost, "INVITE ") == nullptr, "a sos attempt whose flow is lost ends there: no fallback INVITE onto a dead flow"); + } + // ---- chain exhausted: the fallback leg's final terminates, no 3rd try { imsd::util::Rng rng(45);