imsd: what the client flow already holds is read before an SMS wait is judged

The run loop judged the wait of the message in flight — its MESSAGE's
final, its submit report — after draining the server ports and before
its read of the client flow. A final that had arrived in time and was
still unread at that pass counted as none: the segment was sent again —
before the repeat existed, the message failed — and the final then
found nothing to own (issue #3, the third of its three cases).

The pass now reads what the flow holds, without waiting, before it
judges: SipFlow::RecvReady returns a message that is whole in the frame
buffer or readable on the socket now. RecvMsg's read of the socket
moved into a helper both use (ReadOnce), and the handling of a message
read from the flow into OnFlowMessage, for the loop's two reads.

Tested on the workstation's loopback with the daemon's own SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
flow-read): over TCP and over UDP RecvReady returns a message the socket
holds without waiting and none when it holds none, and RecvMsg waits its
time and returns a message when one comes; over TCP RecvReady leaves a
message that is not whole yet. On the dev phone the binary of this
range's last commit judged a report's wait at that pass, 130.06 s after
the final, and sent the repeat (the capture's phone-form6,
visit-journal.txt); the pass has not run with a final waiting unread:
that takes a final timed to it.
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:13 +01:00
commit 1fa4d77fcd
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 111 additions and 57 deletions

View file

@ -151,7 +151,8 @@ status-report book's entries (made at a message's start, and only for one
that asked for a report: `StartNextSms`), the routing of a MESSAGE
final to the message in flight (`OnSmsResponse` — ahead of the reg-event
subscription's handler in the run loop, after it inside `AwaitFinal`), the
judgement of the final's wait and of the report's after the server ports' drain, the reassembler's
judgement of the final's wait and of the report's after the server ports' drain and the read of what
the client flow already holds, the reassembler's
30-minute age-out in the run loop, the retransmission check around
`OnSmsMessage`, the delivery reports' finals (`reportCallIds_`), a send
failure attributed by Call-ID, and the forwarding of a status report's
@ -763,7 +764,17 @@ Rules:
a 408, a 504 or an RP-ERROR; a repeat rejected as a duplicate, and one
ended in its turn; the first attempt's report after its repeat went
out.
`tests/Engine` pins the reducer for each.
`tests/Engine` pins the reducer for each. The read of the client flow before a wait is
judged: `SipFlow`'s two reads ran on the workstation's loopback, the
daemon's class built around a bench `main` (2026-11-20 to 2026-11-22,
the fp6 journal's `2026-11-20-issues-2-5` capture, `flow-read`: over TCP and
over UDP `RecvReady` returns a message the socket holds without
waiting and none when it holds none, and `RecvMsg` waits its time and
returns a message when one comes; over TCP `RecvReady` leaves a
message that is not whole yet; over UDP it passes a keepalive's CRLFs
over); the run loop around them judged a wait on the dev phone (the
report's, in the repeat's bench above), and has not run with a final
waiting unread at that pass.
- A request cut short of its Content-Length on the client flow ran on the
workstation's loopback (2026-11-22, the fp6 journal's
`2026-11-20-issues-2-5` capture, `cut-short`: `SipFlow` built around a
@ -1940,8 +1951,9 @@ do that answers without state.
reason — after a repeat the repeat's end and the first attempt's —, at
once: a report that still comes for the first attempt is then answered
and logged (Receiving, below) and changes nothing; a wait is judged at the run loop's passes,
after the server ports are read, so an answer read before that counts
however late. Not done of the clause's repeat: it cannot be switched off
after the server ports are read and what the client flow already holds
is (`SipFlow::RecvReady`, without waiting), so a final that
the flow held when it was read counts, however late. Not done of the clause's repeat: it cannot be switched off
("should be capable of being disabled through MMI"), and a failed
message is not kept for the user to send again under its TP-MR — a
`Send` of the same text is a new message with the next TP-MR, which the

View file

@ -525,41 +525,22 @@ public:
pollfd p{fd_, POLLIN, 0};
int r = poll(&p, 1, ms);
if (r <= 0) { if (r == 0) return std::nullopt; if (errno == EINTR) continue; return std::nullopt; }
char buf[65535];
ssize_t n = recv(fd_, buf, sizeof buf, 0);
if (transport_ == Transport::Udp) {
// One datagram = one message (RFC 3261 18.3): never accumulated
// across datagrams. An empty datagram is not EOF. A recv error
// is the kernel relaying an ICMP for the inner (decrypted) UDP
// — port unreachable → ECONNREFUSED, prohibited/policy → EACCES
// — i.e. the peer answering "no"; an ICMP quoting the ESP
// packet itself never reaches us (esp6_err ignores all but
// PKT_TOOBIG), so UDP has no dead-flow signal beyond these and
// the refresh timeouts.
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) continue;
Log(std::format("UDP flow: recv error: {}", std::strerror(errno)));
alive_ = false; return std::nullopt;
}
if (n == 0) continue;
const std::string_view d(buf, static_cast<std::size_t>(n));
std::string why;
if (auto m = imsd::sip::DatagramMessage(d, &why)) return m;
if (!why.empty()) {
// A request cut short of its Content-Length is answered 400
// (RFC 3261 18.3; CutShortRequest says which); whatever else
// DatagramMessage refuses is ignored.
const auto head = imsd::sip::CutShortRequest(d);
Log(std::format("UDP: {} — {}", why, head ? "answered 400" : "ignored"));
if (head && !Send(imsd::msg::BuildBadRequest(*head, BadRequestSecret()))) return std::nullopt;
}
continue;
}
// EOF/RST is not a timeout: mark the flow dead so the engine's
// reconnect path notices within one loop turn instead of at the
// next (possibly 30-min-away) keepalive refresh.
if (n <= 0) { alive_ = false; return std::nullopt; }
fb_.Append(std::string_view(buf, static_cast<std::size_t>(n)));
std::optional<std::string> m;
if (!ReadOnce(m)) return std::nullopt;
if (m) return m;
}
}
// A message the flow already holds — whole in the frame buffer, or
// readable on the socket now — without waiting; none when it holds
// none whole.
std::optional<std::string> RecvReady() {
for (;;) {
if (auto m = fb_.TryExtract()) return m;
pollfd p{fd_, POLLIN, 0};
if (poll(&p, 1, 0) <= 0) return std::nullopt;
std::optional<std::string> m;
if (!ReadOnce(m)) return std::nullopt;
if (m) return m;
}
}
int Fd() const { return fd_; }
@ -569,6 +550,47 @@ public:
// read from it can be answered once it is closed.
void Close() { if (fd_ >= 0) { close(fd_); fd_ = -1; } alive_ = false; fb_ = {}; }
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
// when the flow is dead and the caller has nothing more to read.
bool ReadOnce(std::optional<std::string>& m) {
char buf[65535];
ssize_t n = recv(fd_, buf, sizeof buf, 0);
if (transport_ == Transport::Udp) {
// One datagram = one message (RFC 3261 18.3): never accumulated
// across datagrams. An empty datagram is not EOF. A recv error
// is the kernel relaying an ICMP for the inner (decrypted) UDP
// — port unreachable → ECONNREFUSED, prohibited/policy → EACCES
// — i.e. the peer answering "no"; an ICMP quoting the ESP
// packet itself never reaches us (esp6_err ignores all but
// PKT_TOOBIG), so UDP has no dead-flow signal beyond these and
// the refresh timeouts.
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return true;
Log(std::format("UDP flow: recv error: {}", std::strerror(errno)));
alive_ = false; return false;
}
if (n == 0) return true;
const std::string_view d(buf, static_cast<std::size_t>(n));
std::string why;
m = imsd::sip::DatagramMessage(d, &why);
if (!m && !why.empty()) {
// A request cut short of its Content-Length is answered 400
// (RFC 3261 18.3; CutShortRequest says which); whatever else
// DatagramMessage refuses is ignored.
const auto head = imsd::sip::CutShortRequest(d);
Log(std::format("UDP: {} — {}", why, head ? "answered 400" : "ignored"));
if (head && !Send(imsd::msg::BuildBadRequest(*head, BadRequestSecret()))) return false;
}
return true;
}
// EOF/RST is not a timeout: mark the flow dead so the engine's
// reconnect path notices within one loop turn instead of at the
// next (possibly 30-min-away) keepalive refresh.
if (n <= 0) { alive_ = false; return false; }
fb_.Append(std::string_view(buf, static_cast<std::size_t>(n)));
return true;
}
int fd_ = -1;
bool alive_ = false;
bool lastConnectLocal_ = false;
@ -1502,12 +1524,22 @@ public:
reply_ = in.reply;
DispatchRequest(in.msg);
}
// the SMS deadline (the final's wait, the report's) is judged right
// after the server ports were drained: a submit report that landed
// during a blocking re-REGISTER below must not count as a timeout
if (smsOut_ && smsOut_->Deadline() > 0 && Mono() > smsOut_->Deadline()) {
ExecuteSms(smsOut_->OnDeadline(Mono()));
AfterSms();
// the SMS deadline (the final's wait, the report's) is judged after
// the server ports were drained above — a submit report that landed
// during a blocking re-REGISTER below must not count as a timeout —
// and after what the client flow already holds is read here, without
// waiting: a final that came in time and was not yet read must not
// count as one either
if (SmsWaitOver()) {
while (SmsWaitOver()) {
auto pending = sip_.RecvReady();
if (!pending) break;
OnFlowMessage(*pending);
}
if (SmsWaitOver()) {
ExecuteSms(smsOut_->OnDeadline(Mono()));
AfterSms();
}
}
// a concatenated set that got no segment for §7.9's 30 min ages out
// on the loop's clock, whether or not another MESSAGE ever comes
@ -1522,18 +1554,7 @@ public:
Execute(call_->OnDeadline());
MaybeDropCall();
}
auto msg = sip_.RecvMsg(0.3);
if (!msg) continue;
auto st = imsd::sip::Status(*msg);
if (!st) { reply_ = [this](const std::string& r) { sip_.Send(r); }; DispatchRequest(*msg); continue; }
std::string cseq(imsd::sip::Header(*msg, "CSeq").value_or(""));
std::string callid(imsd::sip::Header(*msg, "Call-ID").value_or(""));
if (call_ && callid == call_->CallId() && cseq.ends_with("INVITE")) {
Execute(call_->OnInviteResponse(*msg));
MaybeDropCall();
} else if (!OnSmsResponse(*msg, *st)) {
OnSubscribeResponse(*msg, *st);
}
if (auto msg = sip_.RecvMsg(0.3)) OnFlowMessage(*msg);
}
// shutdown: release an active call, keep the SA/registration for resume
if (call_) { Execute(call_->OnHangup()); MaybeDropCall(); }
@ -1550,6 +1571,27 @@ private:
};
struct Stopping {}; // the engine was told to stop while a bring-up waited
// Whether the wait of the message in flight — its MESSAGE's final, its
// submit report — has run out by the clock.
bool SmsWaitOver() const { return smsOut_ && smsOut_->Deadline() > 0 && Mono() > smsOut_->Deadline(); }
// A message read from the client flow outside a REGISTER wait: a request
// is dispatched and answered on the flow; a response goes to the call
// whose INVITE it answers, else to the short message or the delivery
// report whose MESSAGE it answers, else to the reg-event subscription.
void OnFlowMessage(const std::string& msg) {
auto st = imsd::sip::Status(msg);
if (!st) { reply_ = [this](const std::string& r) { sip_.Send(r); }; DispatchRequest(msg); return; }
std::string cseq(imsd::sip::Header(msg, "CSeq").value_or(""));
std::string callid(imsd::sip::Header(msg, "Call-ID").value_or(""));
if (call_ && callid == call_->CallId() && cseq.ends_with("INVITE")) {
Execute(call_->OnInviteResponse(msg));
MaybeDropCall();
} else if (!OnSmsResponse(msg, *st)) {
OnSubscribeResponse(msg, *st);
}
}
// A bring-up the network deferred (a fresh-SA throttle) or refused: wait
// it out here, in process, on the recovery policy's schedule, then try
// the bring-up again. There is nothing to reconnect over meanwhile (the