imsd, engine, ImsdBus, packaging, README: SMS over IP behind SMS_OVER_IMS=1, net.catcrafts.IMS1.Messaging
With the modem's own IMS client off, a carrier that carries SMS over IMS only leaves the phone no path for texts but imsd; on one with SMS over NAS the +g.3gpp.smsip tag may take them off a path that works (the order is operator policy, TS 24.341 5.3.3.4.2 NOTE 2). So the knob is the user's and off by default: SMS_OVER_IMS=1 advertises the tag (Context::smsOverIp), reads the service centre from EF_SMSP record 1 at bring-up — SMSC= instead — and opens Send on a second interface, net.catcrafts.IMS1.Messaging, beside the frozen one, gated by a polkit action of its own (Gated's Action parameter; the Voice action's callers are unchanged). The shell keeps one message in flight on the flow and queues the rest, routes the in-flight MESSAGE's final to it (OnSmsResponse; a submit report reaches it through OnSmsMessage), seeds and advances the TP-MR, RP-MR and concatenation counters, answers an inbound MESSAGE outside a call through OnSmsMessage (one inside the call's dialog stays the CallMachine's, and is not the MESSAGE of any sentence below) — the message to the bus as Received, the delivery report on the flow with its final logged, a status report matched by TP-MR and recipient to the message that asked for one — and reports a message's fate as StateChanged. A Send is judged when the engine takes its command up: unregistered or with the flow down then, it fails with that reason. The journal names a delivered message's sender, size and time — a text reaches it only as the body of a MESSAGE whose Content-Type does not contain application/vnd.3gpp.sms or as the hex of a body that is no RP message or of an unusable TPDU; DUMP_SIP adds the two MESSAGE dumps. While the knob is off a network may deliver a short message over IP regardless; answering it 200 and acknowledging it would count it delivered to nobody, so it is refused with 480 Temporarily Unavailable, which lets the IP-SM-GW try the modem's path where the network has one (TS 24.341 5.3.3.4.2 NOTE 2); a MESSAGE outside a call's dialog whose Content-Type does not contain application/vnd.3gpp.sms (letter case ignored) is still answered 200 and logged with its body — the 200 in BuildResponse's shape now, the line with the content type. A retransmitted MESSAGE — the same Call-ID, CSeq and Via branch (the value of the first `branch=` of its first Via line; a MESSAGE with none or an empty one is never taken for a copy) within 32 s of our final, in the same run of the daemon — is answered again and not handled again (the engine's RecentTransactions, a table in memory), where a repeated RP-DATA would otherwise, with SMS over IP on, be handled again: a second delivery report where the first got one and, of a text in one segment, a second text. The Messaging signals carry a text's content, and a D-Bus broadcast reaches every account the bus policy lets receive from imsd — which polkit cannot narrow — so they are never broadcast: Subscribe, behind the same action as Send, records the caller's unique name, and Received and StateChanged go unicast to each subscriber (the submission's sent or failed also to the Send caller), which the bus delivers to those connections — root's busctl monitor sees them as well. A subscription ends with its connection; a consumer subscribes at start and again when imsd's name reappears. GetStatus answers `enabled` from the daemon's configuration, not from the registration snapshot, so an asker before the first registration reads the truth. A message's id holds the run's token, eight hex digits drawn from std::random_device at the daemon's start, ahead of a second and a count: `ims-sms-<run>-<start>-<N>` for a sent text, with the start second and a counter, and `ims-sms-in-<run>-<epoch>-<N>` for a received one, with its second. A counter starts over with the daemon, and the phone's wall clock reads 1970 until the first time sync, so a second repeats across runs: the token is what tells one run's ids from another's, and a client keys by id (RunToken, SentSmsId and ReceivedSmsId, pinned by tests/Engine). Tested on the dev phone on KPN (2026-10-17; the fp6 journal's 2026-10-17-sms-over-ip-bench capture) on sms-branch builds ahead of this history (195e91b, then c57765a with the retransmission absorption): the tag on a resumed and on a fresh registration, Sends to 202 and their submit reports in one and two segments and in UCS-2, texts received in one and two segments and in UCS-2, every delivery report answered 202 but one of the two for the 01:23 text, refused 488 on the build before the absorption, and a same-second retransmission absorbed under c57765a (01:44) — AGENTS.md §5 has the record and §13 the network facts, among them that the modem was CS-detached that night. On the host's session bus in dev mode: two Sends answered with ids and failed "imsd is not registered" (2026-10-16), and three connections — a subscriber, a match-rule-only listener, an unsubscribed sender — where only the subscriber and the sender heard the StateChanged and the subscription ended with its connection (2026-10-17); with the knob off, GetStatus's `enabled` false, Send refused NotSupported and Subscribe answered (a private session bus, this commit's host build, 2026-11-19; its sha256 begins 98e30a9522409d99). The SMSC= override ran on 2026-10-18 from 02:17 (smsc-override-2026-10-18-0217): the EF_SMSP number as an override and a Send through it to sent, and SMSC=abc stopped at bring-up with "is not a number" (ForNumber dials digits, * and # only; the bench's first run, on a build outside this history that dialled the BCD table's a, b and c, submitted with them and got an RP-ERROR back, RP cause 28). A status report is matched to the message that asked for one by its TP-MR and the recipient's number — among this run's messages: the book is in memory — and the message gets a word for the whole (StatusReportBook, pinned), by TS 23.040 9.2.3.15's classes of TP-ST: delivery-failed at the first report in which the SC is not making any more transfer attempts (a permanent error or a temporary one; a reserved value too, read as "Service Rejected" as the clause has the MS read it), delivered when the last of its segments is reported in the completed class (the SC's word in the reason) — never a later segment's word over an earlier failure. The next TP-MR is kept in <state dir>/sms-tpmr across restarts, so the TP-MRs continue instead of starting over at random: a reuse needs a full 256-segment wrap, not a restart (a report can be days late) — as long as the file's last write reached the disk: it is neither checked nor synced. One message's StateChanged words come in the order they mean, which is not the order imsd learns them: the service centre can deliver and report on segment 1 while segment 2 is still on its way. SmsWordOrder (Imsd:Engine, pure) gives the order §8.4 states: the submission's word first — `sent`, or `failed`, after which nothing follows — and a report's only after `sent`. A report's word on the message in flight is held until the submission ends (the latest word; one message is in flight at a time) and posted after `sent`. A submission that fails after the centre reported every segment delivered posts `sent`, the failure in its reason, and then `delivered`. A `failed` otherwise drops the held word and StatusReportBook::ForgetMessage forgets the message's TP-MRs, so a later report on a part that went matches nothing and is logged. tests/Engine pins the book's forgetting and each of these orders. BuildResponse (the messages commit) is the final of the inbound MESSAGE path from here on — the 200, the 480 and the 488; BuildResponse200's hop-by-hop shape stays for the other answers that had it. The reassembler's 30-minute age-out is judged in the run loop beside the SMS deadline, whether or not another MESSAGE ever comes. The MESSAGE's target is the service centre's own international number (ScPsi), not RuriFor's dialled-number guess, whose trunk-0 rule is Dutch: a national SMSC= of nine or more characters starting with 0 would have become a +31 number, a 00-prefixed one +<the rest>, any other non-international value a phone-context URI at the home domain; a service centre ScPsi makes nothing of (national, 00-prefixed, or a '*' or '#' among the digits, which ForNumber dials) is refused at bring-up on that same predicate with a journal line, never announced, and Send then fails with no service centre. A failed submission's StateChanged reason can hold the network's text (a refused final's, RefusalSummary); a D-Bus string must be valid UTF-8, so the emission makes it so (g_utf8_make_valid: bytes that are not become U+FFFD, and a NUL byte ends the string).
This commit is contained in:
parent
4de5f0be45
commit
aa3c394041
11 changed files with 1269 additions and 78 deletions
|
|
@ -19,7 +19,14 @@
|
|||
// fresh backoffs, the throttle schedule, Retry-After, which waits a Dial
|
||||
// may cut short, when the old registration counts as gone, the holds a
|
||||
// restart takes over (Holds/Restore), and a binding the network terminated
|
||||
// (OnBindingTerminated). Then SMS over IP: SmsOutbound through a
|
||||
// (OnBindingTerminated). Then SMS over IP: the status-report book
|
||||
// (StatusReportBook with SameRecipient — a word for the whole message: a two-segment
|
||||
// message delivered with its last segment, a permanent failure the last
|
||||
// word, pending kept, another recipient no match, a reused TP-MR, a
|
||||
// message forgotten whole by its id), the order of one message's words
|
||||
// (SmsWordOrder: the submission's first, a report's after sent) and the
|
||||
// ids (RunToken, SentSmsId, ReceivedSmsId);
|
||||
// SmsOutbound through a
|
||||
// submission (the MESSAGE with the RP-DATA, its 202, the RP-ACK -> sent; a
|
||||
// refused final, an RP-ERROR with its cause, the final's wait and the
|
||||
// report's running out, a failed send, two segments with consecutive
|
||||
|
|
@ -31,9 +38,10 @@
|
|||
// In-Reply-To, a concatenated pair whole once, a status report, a submit
|
||||
// report routed to the message in flight, 488 for one that is not ours, an
|
||||
// unusable TPDU acknowledged with an RP-ERROR, a body that is no RP
|
||||
// message, a page-mode text 200'd and logged), and RecentTransactions (a
|
||||
// retransmitted request gets its final again, for 32 s; no branch, no
|
||||
// match). Pure reducers, no I/O.
|
||||
// message, a page-mode text 200'd and logged, a short message while SMS
|
||||
// over IP is off refused with 480), and RecentTransactions (a retransmitted
|
||||
// request gets its final again, for 32 s; no branch, no match). Pure
|
||||
// reducers, no I/O.
|
||||
import std;
|
||||
import Imsd;
|
||||
|
||||
|
|
@ -1251,6 +1259,7 @@ int main() {
|
|||
const auto kpnSc = *imsd::util::FromHex("07911356131313f3");
|
||||
Context c = MakeCtx();
|
||||
c.aor = "sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org";
|
||||
c.smsOverIp = true;
|
||||
const SmsOutbound::Params p{"+31612345678", "Hello", "sip:+31653131313@ims.mnc001.mcc001.3gppnetwork.org;user=phone", kpnSc, 10, 20, 0x2A, 0xA9, false};
|
||||
|
||||
// a one-segment message: MESSAGE, 202, RP-ACK -> sent
|
||||
|
|
@ -1374,6 +1383,80 @@ int main() {
|
|||
Check(st && st->state == "failed" && st->reason.contains("no service centre"), "outbound: no service centre fails at once");
|
||||
}
|
||||
|
||||
// the status-report book: a word for the whole message, by TP-MR and recipient
|
||||
{
|
||||
using Word = StatusReportBook::Word;
|
||||
StatusReportBook book;
|
||||
const std::vector<std::uint8_t> two{10, 11}, one{12}, eleven{11};
|
||||
book.Sent("ims-sms-1", "+31612345678", two);
|
||||
book.Sent("ims-sms-2", "0612345679", one);
|
||||
Check(book.Report(10, "+31612345678", 0x00).word == Word::SegmentDelivered && book.Report(11, "+31612345678", 0x00).word == Word::Delivered && book.Size() == 1, "book: a two-segment message is delivered with its last segment");
|
||||
Check(book.Report(10, "+31612345678", 0x00).word == Word::NoMessage, "book: a delivered TP-MR is forgotten");
|
||||
Check(book.Report(12, "+31612345670", 0x00).word == Word::NoMessage && book.Size() == 1, "book: another recipient is no match");
|
||||
Check(book.Report(12, "+31612345679", 0x30).word == Word::Pending && book.Size() == 1, "book: a temporary status is pending, the entry kept");
|
||||
Check(book.Report(12, "+31612345679", 0x00).word == Word::Delivered && book.Size() == 0, "book: then delivered");
|
||||
book.Sent("ims-sms-3", "+31612345678", two);
|
||||
const auto f = book.Report(11, "+31612345678", 0x41);
|
||||
Check(f.word == Word::Failed && f.id == "ims-sms-3" && book.Size() == 0 && book.Report(10, "+31612345678", 0x00).word == Word::NoMessage, "book: a permanent failure is the message's last word; a later segment finds nothing");
|
||||
book.Sent("ims-sms-4", "+31612345678", two);
|
||||
book.Sent("ims-sms-5", "+31612345678", eleven);
|
||||
Check(book.Report(10, "+31612345678", 0x00).word == Word::NoMessage && book.Report(11, "+31612345678", 0x00).id == "ims-sms-5" && book.Size() == 0, "book: a TP-MR reused before its word forgets the older message whole");
|
||||
book.Sent("ims-sms-8", "+31612345678", one);
|
||||
Check(book.Report(12, "+31612345678", 0x62).word == Word::Failed && book.Size() == 0, "book: a temporary error after which the SC has stopped trying is a failure");
|
||||
book.Sent("ims-sms-9", "+31612345678", one);
|
||||
Check(book.Report(12, "+31612345678", 0x80).word == Word::Failed && book.Size() == 0, "book: a status with bit 7 set is reserved, so a failure — not the delivered its low bits spell");
|
||||
book.Sent("ims-sms-10", "+31612345678", one);
|
||||
Check(book.Report(12, "+31612345678", 0x26).word == Word::Failed && book.Size() == 0, "book: a reserved value among the still-trying ones is a failure, not pending");
|
||||
book.Sent("ims-sms-11", "+31612345678", one);
|
||||
Check(book.Report(12, "+31612345678", 0x03).word == Word::Failed && book.Size() == 0, "book: a reserved value among the completed ones is a failure, not delivered");
|
||||
book.Sent("ims-sms-12", "+31612345678", one);
|
||||
Check(book.Report(12, "+31612345678", 0x10).word == Word::Delivered && book.Size() == 0, "book: an SC's own completed value is delivered");
|
||||
}
|
||||
// the book forgets a message whole when its submission failed
|
||||
{
|
||||
using Word = StatusReportBook::Word;
|
||||
StatusReportBook book;
|
||||
const std::vector<std::uint8_t> two{10, 11}, one{12};
|
||||
book.Sent("ims-sms-6", "+31612345678", two);
|
||||
book.Sent("ims-sms-7", "+31612345678", one);
|
||||
book.ForgetMessage("ims-sms-6");
|
||||
Check(book.Size() == 1 && book.Report(10, "+31612345678", 0x30).word == Word::NoMessage && book.Report(11, "+31612345678", 0x00).word == Word::NoMessage, "book: a forgotten message's reports match nothing");
|
||||
book.ForgetMessage("ims-sms-99");
|
||||
Check(book.Size() == 1, "book: forgetting an unknown id changes nothing");
|
||||
Check(book.Report(12, "+31612345678", 0x00).word == Word::Delivered, "book: the other message is still on record");
|
||||
}
|
||||
// the order of one message's words: the submission's first, a report's after `sent`
|
||||
{
|
||||
SmsWordOrder order;
|
||||
auto now = order.Report("ims-sms-1", SmsStateDelivered, "received by the SME", false);
|
||||
Check(now.size() == 1 && now[0].id == "ims-sms-1" && now[0].state == "delivered" && !order.Holding(), "order: a report on a message whose submission ended is posted at once");
|
||||
Check(order.Report("ims-sms-2", SmsStateDeliveryPending, "congestion", true).empty() && order.Holding(), "order: a report on the message in flight is held");
|
||||
auto e = order.Submission("ims-sms-2", SmsStateSent, "2 segment(s) accepted by the SC");
|
||||
Check(e.post.size() == 2 && e.post[0].state == "sent" && e.post[1].state == "delivery-pending" && e.post[1].reason == "congestion" && !e.forget && !order.Holding(), "order: sent, then the held word");
|
||||
e = order.Submission("ims-sms-3", SmsStateSent, "1 segment(s) accepted by the SC");
|
||||
Check(e.post.size() == 1 && e.post[0].state == "sent" && !e.forget, "order: sent with nothing held");
|
||||
order.Report("ims-sms-4", SmsStateDeliveryPending, "congestion", true);
|
||||
order.Report("ims-sms-4", SmsStateDelivered, "received by the SME", true);
|
||||
e = order.Submission("ims-sms-4", SmsStateFailed, "no submit report for segment 2/2 within 40 s of the final");
|
||||
Check(e.post.size() == 2 && e.post[0].state == "sent" && e.post[0].reason.starts_with("every segment reported delivered by the SC; no submit report") && e.post[1].state == "delivered" && e.post[1].reason == "received by the SME" && !e.forget, "order: a failed after every segment was reported delivered is sent and delivered (the latest held word counts)");
|
||||
order.Report("ims-sms-5", SmsStateDeliveryPending, "congestion", true);
|
||||
e = order.Submission("ims-sms-5", SmsStateFailed, "segment 2/2 refused by the SC: RP cause 38");
|
||||
Check(e.post.size() == 1 && e.post[0].state == "failed" && e.post[0].reason.starts_with("segment 2/2 refused") && e.forget && !order.Holding(), "order: a failed drops the held word and forgets the message's TP-MRs");
|
||||
e = order.Submission("ims-sms-6", SmsStateFailed, "no service centre");
|
||||
Check(e.post.size() == 1 && e.post[0].state == "failed" && e.forget, "order: a failed with nothing held is posted and forgets");
|
||||
order.Report("ims-sms-7", SmsStateDelivered, "received by the SME", true);
|
||||
e = order.Submission("ims-sms-8", SmsStateSent, "1 segment(s) accepted by the SC");
|
||||
Check(e.post.size() == 1 && order.Holding(), "order: another message's submission leaves the held word alone");
|
||||
}
|
||||
// a status report's recipient against the number a message went to
|
||||
Check(SameRecipient("+31612345678", "+31612345678") && SameRecipient("0612345678", "+31612345678") && SameRecipient("+31 6 1234 5678", "31612345678") && SameRecipient("0031612345678", "+31612345678") && SameRecipient("6125551234", "+16125551234"), "SameRecipient: equal digits, or a number dialled with a trunk 0, with 00 or without its country code against its international form");
|
||||
Check(!SameRecipient("+31612345678", "+31687654321") && !SameRecipient("+31612345678", "+49612345678") && !SameRecipient("112", "+31612345112") && !SameRecipient("1266", "+31612341266") && !SameRecipient("", "+31612345678"), "SameRecipient: another number, +31 against +49, a short code as a suffix, an empty one");
|
||||
// the ids of the Messaging interface: the run's token first after the prefix, so two runs with different tokens give different ids whatever the clock reads
|
||||
{
|
||||
Check(RunToken(0x7f3a9c21u) == "7f3a9c21" && RunToken(0) == "00000000" && RunToken(0xffffffffu) == "ffffffff", "RunToken: eight hex digits, zero-padded");
|
||||
Check(SentSmsId("7f3a9c21", 1793474321, 3) == "ims-sms-7f3a9c21-1793474321-3" && ReceivedSmsId("7f3a9c21", 1793474321, 1) == "ims-sms-in-7f3a9c21-1793474321-1", "the sent and the received shape");
|
||||
Check(ReceivedSmsId("7f3a9c21", 42, 1) != ReceivedSmsId("0b1d2e3f", 42, 1) && SentSmsId("7f3a9c21", 42, 1) != SentSmsId("0b1d2e3f", 42, 1), "two runs with different tokens: their ids differ at the same second and counter");
|
||||
}
|
||||
// inbound MESSAGEs, as the IP-SM-GW sends them
|
||||
auto inbound = [](std::string_view bodyHex, std::string_view extraHeaders = "") {
|
||||
auto b = *imsd::util::FromHex(bodyHex);
|
||||
|
|
@ -1594,6 +1677,23 @@ int main() {
|
|||
const std::string branchWithParams = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKabc;_aluscr_\r\nCall-ID: c@net\r\nCSeq: 7 MESSAGE\r\nContent-Length: 0\r\n\r\n";
|
||||
Check(RecentTransactions::Key(branchWithParams) == "c@net|7 MESSAGE|z9hG4bKabc", "recent: the branch ends at the next parameter");
|
||||
}
|
||||
// SMS over IP off: a short message the network sends anyway is refused, a page-mode text still 200'd
|
||||
{
|
||||
imsd::util::Rng rng(85);
|
||||
Reassembler re;
|
||||
Context off = MakeCtx();
|
||||
off.aor = c.aor;
|
||||
const std::string_view offeredHex = "011107911356131313F3001E040B911346610089F60000208062917314080CC8F71D14969741F977FD07";
|
||||
const std::string offered = inbound(offeredHex);
|
||||
auto r = OnSmsMessage(off, rng, offered, re, nullptr, 8.0);
|
||||
Check(r.response.starts_with("SIP/2.0 480 Temporarily Unavailable\r\n") && r.response.contains("Call-ID: in-011107@net\r\n"), "off: a short message is refused with 480");
|
||||
Check(!hasSms(r.actions, SmsAction::Type::Received) && !hasSms(r.actions, SmsAction::Type::SendClient) && r.reportCallId.empty() && !r.rp && re.Pending() == 0, "off: nothing to the bus, no delivery report, nothing parsed");
|
||||
const auto* log = findSms(r.actions, SmsAction::Type::Log);
|
||||
Check(log && log->text.ends_with("while SMS_OVER_IMS is off: 480") && !log->text.contains("How are you") && !log->text.contains(std::string_view(offered).substr(offered.find("\r\n\r\n") + 4)) && !log->text.contains(imsd::util::ToHex(*imsd::util::FromHex(offeredHex))), "off: logged with a line that ends at the 480, without the text, the body or its hex");
|
||||
const std::string plain = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKn3\r\nFrom: <sip:x@ims>;tag=1\r\nTo: <sip:me@ims>\r\nCall-ID: t2@net\r\nCSeq: 1 MESSAGE\r\nContent-Type: text/plain\r\nContent-Length: 2\r\n\r\nhi";
|
||||
auto r2 = OnSmsMessage(off, rng, plain, re, nullptr, 9.0);
|
||||
Check(r2.response.starts_with("SIP/2.0 200 OK") && hasSms(r2.actions, SmsAction::Type::Log), "off: a page-mode text is still 200'd and logged");
|
||||
}
|
||||
}
|
||||
|
||||
if (Failures == 0) std::println("Engine: all tests passed");
|
||||
|
|
|
|||
Loading…
Reference in a new issue