engine, imsd: a submit report for a MESSAGE no longer in flight that this run sent in the last hour is answered 200

TS 24.341 5.3.1.2 has a submit report answered 200 when its In-Reply-To
"indicates that the request corresponds to a short message submitted by
the SM-over-IP sender" and 488 when it does not, and its NOTE 3 has the
sender store the Call-ID for that. imsd knew only the Call-ID of the
segment in flight (OwnsReport): a report that came once its message had
ended — the 40 s wait run out — or once its segment was followed by the
next was answered 488 like a stranger's, and its line said only "no
MESSAGE of ours in flight" (issue #3, the first of its three cases).

RecentSubmissions keeps the Call-ID of every MESSAGE a submission goes
out in for an hour, with the message's id and the segment. A report
In-Reply-To one of them that the message in flight does not take is
answered 200, and the journal has `submit report (RP-ACK) In-Reply-To
…: sms <id>'s segment i/n, no longer in flight; 200`, an RP-ERROR's
with its cause. The 488 is left for an In-Reply-To that names no MESSAGE
of ours on record, and its line says so ("on record" for "in flight").
A report without In-Reply-To that no message in flight takes by its
RP-MR is answered 200 as before; its line ends `that no message in
flight owns; 200` where it ended `for no message in flight; 200`.

The bus is told nothing of a late report: the Messaging interface is
frozen with `failed` as a message's last word (AGENTS.md §8.4), so a
text the service centre took after its wait had run out stays `failed`
there; the journal's line is the record of it.

Tested by tests/Engine: the table (a Call-ID on record for 3600 s, the
first of a list that is ours, each segment's MESSAGE put on record as it
goes out) and OnSmsMessage (200 and the line for a report on a segment
that was followed by the next, on a message that was sent and, with an
RP-ERROR's cause, on one its wait had ended; 488 and its line for a
stranger's). On the dev phone, with the binary of this range's last
commit, no report came for a MESSAGE no longer in flight (the fp6
journal's 2026-11-20-issues-2-5 capture, phone-form6), so the 200 for
one and its line have run in the suite alone.
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:12 +01:00
commit ef04b473a7
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 203 additions and 94 deletions

View file

@ -35,7 +35,9 @@ MESSAGE outside a call's dialog leads to (the final for it, the message or
status report it delivered, the delivery report to send), with
RecentTransactions (a retransmitted request within 32 s gets its final
again and is not handled again; one whose first Via line has no
`branch=`, or an empty one, is never taken for a copy), and StatusReportBook with SameRecipient (a status
`branch=`, or an empty one, is never taken for a copy), RecentSubmissions (the
Call-IDs of the MESSAGEs our submissions went out in, an hour each: a submit
report for one no longer in flight is answered 200, not 488), and StatusReportBook with SameRecipient (a status
report's match to the message that asked for one, TP-MR and recipient, and
its word for the whole message), SmsWordOrder (the order of a message's
words) and the ids (RunToken, SentSmsId, ReceivedSmsId), and the received-message store's record codec
@ -1041,6 +1043,63 @@ export namespace imsd::engine {
std::map<std::uint8_t, std::shared_ptr<Message>> byMr_;
};
// The Call-IDs an In-Reply-To lists (RFC 3261 20.21: a callid, then
// more after commas), each without the white space around it.
std::vector<std::string_view> InReplyToCallIds(std::string_view inReplyTo) {
std::vector<std::string_view> ids;
for (auto part : std::views::split(inReplyTo, ','))
ids.push_back(imsd::sip::Trim(std::string_view(part.begin(), part.end())));
return ids;
}
// An RP-ERROR's refusal in words: its RP cause and, when its submit
// report carries one, the TP-FCS.
std::string RpErrorWords(const imsd::sms::Rp& rp) {
std::string words = std::format("RP cause {} ({})", rp.cause.value_or(0), imsd::sms::RpCauseText(rp.cause.value_or(0)));
if (auto r = imsd::sms::ParseSubmitReport(rp.tpdu, true); r && r->failureCause)
words += std::format(", TP-FCS 0x{:02x} ({})", *r->failureCause, imsd::sms::FailureCauseText(*r->failureCause));
return words;
}
// The MESSAGEs our submissions went out in, by Call-ID. TS 24.341
// 5.3.1.2 has a submit report answered 200 when its In-Reply-To
// "indicates that the request corresponds to a short message submitted
// by the SM-over-IP sender" and 488 when it does not, and its NOTE 3
// has the sender store the Call-ID for that. The message in flight
// knows its own MESSAGE (SmsOutbound); this table keeps every one for
// an hour after it went out, so a report that comes once its message
// has ended — its wait run out, say — or once its segment was followed
// by the next is still known for ours: answered 200 and logged with
// the message's id. Nothing else follows from it: a message that has
// ended has had its word on the bus, and `failed` is its last (§8.4);
// one whose next segment is in flight goes on as it was. The table is
// this run's.
class RecentSubmissions {
public:
static constexpr double Keep = 3600.0;
struct Entry { std::string id; std::size_t segment = 0, segments = 0; double at = 0; };
// the MESSAGE `callid` carried segment `segment` of `segments` of the message `id`
void Sent(std::string_view callid, std::string_view id, std::size_t segment, std::size_t segments, double now) {
Purge(now);
byCallId_[std::string(callid)] = Entry{std::string(id), segment, segments, now};
}
// The MESSAGE of ours an In-Reply-To names — of the Call-IDs it lists, the first on record.
std::optional<Entry> Named(std::string_view inReplyTo, double now) {
Purge(now);
for (std::string_view callid : InReplyToCallIds(inReplyTo))
if (auto it = byCallId_.find(callid); it != byCallId_.end()) return it->second;
return std::nullopt;
}
std::size_t Size() const { return byCallId_.size(); }
private:
void Purge(double now) {
std::erase_if(byCallId_, [&](const auto& e) { return now - e.second.at > Keep; });
}
std::map<std::string, Entry, std::less<>> byCallId_;
};
// The waits (TS 24.011 10: TR1M; TS 24.341 5.3.1.1 NOTE 1 has it
// exceed SIP timer F): a MESSAGE's final within 32 s
// of the MESSAGE or of its latest provisional, which starts the wait
@ -1131,8 +1190,9 @@ export namespace imsd::engine {
};
enum class Phase { Idle, Sending, AwaitingReport, Done };
SmsOutbound(const imsd::msg::Context& ctx, imsd::util::Rng& rng, std::string id, Params p)
: ctx_(ctx), rng_(rng), id_(std::move(id)), p_(std::move(p)) {}
// `submitted`: where each MESSAGE this message goes out in is put on record
SmsOutbound(const imsd::msg::Context& ctx, imsd::util::Rng& rng, RecentSubmissions& submitted, std::string id, Params p)
: ctx_(ctx), rng_(rng), submitted_(submitted), id_(std::move(id)), p_(std::move(p)) {}
const std::string& Id() const { return id_; }
// the segment in flight's MESSAGE (empty before the first went out)
@ -1199,11 +1259,8 @@ export namespace imsd::engine {
// order is its own) is taken as well.
bool OwnsReport(std::string_view inReplyTo, std::optional<std::uint8_t> rpMr) const {
if (phase_ != Phase::AwaitingReport && phase_ != Phase::Sending) return false;
if (!inReplyTo.empty()) { // a list of Call-IDs (RFC 3261 20.21): ours among them
for (auto part : std::views::split(inReplyTo, ','))
if (imsd::sip::Trim(std::string_view(part.begin(), part.end())) == callid_) return true;
return false;
}
if (!inReplyTo.empty()) // a list of Call-IDs (RFC 3261 20.21): ours among them
return std::ranges::contains(InReplyToCallIds(inReplyTo), std::string_view(callid_));
return rpMr && *rpMr == CurrentRpMr();
}
@ -1216,10 +1273,7 @@ export namespace imsd::engine {
if (phase_ != Phase::AwaitingReport && phase_ != Phase::Sending) return out;
if (rp.type == imsd::sms::RpType::ErrorMt) {
phase_ = Phase::Done;
std::string why = std::format("segment {}/{} refused by the SC: RP cause {} ({})", seg_ + 1, tpdus_.size(), rp.cause.value_or(0), imsd::sms::RpCauseText(rp.cause.value_or(0)));
if (auto r = imsd::sms::ParseSubmitReport(rp.tpdu, true); r && r->failureCause)
why += std::format(", TP-FCS 0x{:02x} ({})", *r->failureCause, imsd::sms::FailureCauseText(*r->failureCause));
out.push_back(Fail(why));
out.push_back(Fail(std::format("segment {}/{} refused by the SC: {}", seg_ + 1, tpdus_.size(), RpErrorWords(rp))));
return out;
}
if (rp.type != imsd::sms::RpType::AckMt) return out;
@ -1260,6 +1314,7 @@ export namespace imsd::engine {
void SendSegment(std::vector<SmsAction>& out, double now) {
callid_ = std::format("{}@{}", rng_.Token(16), ctx_.local);
submitted_.Sent(callid_, id_, seg_ + 1, tpdus_.size(), now);
std::vector<std::uint8_t> rp = imsd::sms::BuildRpData(CurrentRpMr(), p_.scAddress, tpdus_[seg_]);
out.push_back(SmsAction{SmsAction::Type::SendClient, imsd::msg::BuildMessageSms(ctx_, p_.scTarget, callid_, rng_.Token(8), rng_.Token(16), rp)});
out.push_back(LogAct(std::format("sms {}: segment {}/{} to {} via {}: MESSAGE sent (RP-MR {}, TP-MR {}, {} bytes)", id_, seg_ + 1, tpdus_.size(), p_.number, p_.scTarget, CurrentRpMr(), tpMrs_[seg_], tpdus_[seg_].size())));
@ -1282,6 +1337,7 @@ export namespace imsd::engine {
const imsd::msg::Context& ctx_;
imsd::util::Rng& rng_;
RecentSubmissions& submitted_;
std::string id_;
Params p_;
std::vector<std::vector<std::uint8_t>> tpdus_;
@ -1478,8 +1534,9 @@ export namespace imsd::engine {
// else its From; one that gives neither URI gets none, which is logged.
// An RP-ACK/RP-ERROR is a submit report: the message in
// flight (`outbound`) takes it and the MESSAGE is answered 200; one the
// message in flight does not own is answered 488 when its In-Reply-To is
// not empty, else 200 and logged. A MESSAGE whose Content-Type
// message in flight does not own is answered 200 and logged when its
// In-Reply-To names a MESSAGE of ours on record (`submitted`), 488 when
// it names none, and 200 and logged when it has no In-Reply-To. A MESSAGE whose Content-Type
// does not contain the SMS one, or whose body is no RP message, is
// answered 200 and logged (the journal keeps what the 200 acknowledged). While SMS over IP is off (`ctx.smsOverIp`)
// every MESSAGE whose Content-Type contains the SMS one is refused
@ -1487,7 +1544,7 @@ export namespace imsd::engine {
// and an RP-ACK would count a short message delivered to nobody, a failure lets the IP-SM-GW try the
// modem's path (TS 24.341 5.3.3.4.2 NOTE 2) where the network gives the
// modem one.
SmsInbound OnSmsMessage(const imsd::msg::Context& ctx, imsd::util::Rng& rng, std::string_view message, imsd::sms::Reassembler& reassembler, SmsOutbound* outbound, double now) {
SmsInbound OnSmsMessage(const imsd::msg::Context& ctx, imsd::util::Rng& rng, std::string_view message, imsd::sms::Reassembler& reassembler, SmsOutbound* outbound, RecentSubmissions& submitted, double now) {
using imsd::sms::RpType;
SmsInbound r;
const std::string toTag = rng.Token(10); // the To tag for the finals we give this request (RFC 3261 8.2.6.2; BuildResponse adds it unless the To holds `tag=`)
@ -1524,11 +1581,16 @@ export namespace imsd::engine {
return r;
}
if (!inReplyTo.empty()) {
if (auto ours = submitted.Named(inReplyTo, now)) {
// a MESSAGE of ours that is no longer in flight: the 200 stands (TS 24.341 5.3.1.2), and the journal keeps what the report said
r.actions.push_back(SmsAction{SmsAction::Type::Log, std::format("submit report ({}) In-Reply-To {}: sms {}'s segment {}/{}, no longer in flight; 200", rp.type == RpType::AckMt ? "RP-ACK" : std::format("RP-ERROR, {}", RpErrorWords(rp)), inReplyTo, ours->id, ours->segment, ours->segments)});
return r;
}
r.response = imsd::msg::BuildResponse(ctx, message, 488, "Not Acceptable Here", toTag);
r.actions.push_back(SmsAction{SmsAction::Type::Log, std::format("submit report ({}) In-Reply-To {}: no MESSAGE of ours in flight; 488", rp.type == RpType::AckMt ? "RP-ACK" : "RP-ERROR", inReplyTo)});
r.actions.push_back(SmsAction{SmsAction::Type::Log, std::format("submit report ({}) In-Reply-To {}: no MESSAGE of ours on record; 488", rp.type == RpType::AckMt ? "RP-ACK" : "RP-ERROR", inReplyTo)});
return r;
}
r.actions.push_back(SmsAction{SmsAction::Type::Log, std::format("submit report ({}, RP-MR {}) for no message in flight; 200", rp.type == RpType::AckMt ? "RP-ACK" : "RP-ERROR", rp.mr)});
r.actions.push_back(SmsAction{SmsAction::Type::Log, std::format("submit report ({}, RP-MR {}) that no message in flight owns; 200", rp.type == RpType::AckMt ? "RP-ACK" : "RP-ERROR", rp.mr)});
return r;
}
// what is left is an RP-DATA: ParseRp gave one of the network's three