messages: the SIP MESSAGE of SMS over IP, +g.3gpp.smsip behind a context flag

TS 24.341 5.3.1.2 has a submission travel as a SIP MESSAGE to the
service centre's PSI with the RP-DATA as an application/vnd.3gpp.sms
body, and 5.3.2.4 the delivery report as one to the IP-SM-GW with
In-Reply-To naming the delivered MESSAGE: BuildMessageSms builds both,
out of dialog over the registration's Route with the same
P-Preferred-Identity, P-Access-Network-Info and sec-agree headers as the
INVITE and the reg-event SUBSCRIBE, and no Contact (RFC 3428 4). The REGISTER Contact carries +g.3gpp.smsip only
when Context::smsOverIp says so — the tag routes terminating short
messages to imsd (the 2026-10-17 bench's mt-last-message-in.raw), which
at this commit still answers them 200 and logs them — after the ICSI.
BuildResponse builds a final for an inbound request outside a call (the
488 of 5.3.1.2 for a report that is not ours, or a 200); the shell does
not call it in this commit — it still answers a MESSAGE with
BuildResponse200.

tests/Messages pins the Contact with the tag, the MESSAGE's headers and
body bytes both ways, the absence of Contact, and the 488.

BuildResponse echoes each Via: line and the To, with a tag added unless
the To already holds `tag=` (RFC 3261 8.2.6.2 has a UAS add a tag to a
To that carries none); tests/Messages pins the 488 and a 200.

ScPsi gives the service centre's PSI from its international number
(5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone, or
a tel URI; the home domain is imsd's choice); RuriFor's dialled-number guess, whose trunk 0 is Dutch, has no
place there. Pinned in tests/Messages.
This commit is contained in:
Jorijn van der Graaf 2026-10-16 23:13:39 +02:00
commit 0717c3008a
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 206 additions and 17 deletions

View file

@ -9,15 +9,20 @@ Every VoLTE message the daemon sends is assembled here from an explicit
context and the per-request variables (tags, branches, CSeq) so the builders
are pure functions with no hidden state: the engine owns the dialog bookkeeping
and the transport owns the socket, this module only formats bytes. That makes
each message byte-pinnable — tests/Messages compares the output against strings
cross-generated from the Python reference stack, header for header.
each message byte-pinnable — tests/Messages compares a REGISTER pair and an INVITE
with strings cross-generated from the Python reference stack, header for header.
Covers: the unprotected + protected REGISTER and the refresh re-REGISTER
(sec-agree, Security-Client/Verify, AKAv1-MD5 Authorization); the INVITE with
an AMR-WB SDP offer and IMS MMTEL feature tags; the in-dialog PRACK/BYE and
2xx/non-2xx ACK; CANCEL (reusing the INVITE branch); the 200 OK that echoes
an inbound request's dialog headers; and the UAS responses to an inbound
INVITE (100/180/200 and rejection finals). Pure std C++.
an inbound request's dialog headers; the finals of the inbound MESSAGE
path, each `Via:` line echoed and the To tagged unless it holds `tag=`
(RFC 3261 8.2.6.2; long header names only); the UAS responses to an
inbound INVITE (100/180/200
and rejection finals); and the SIP MESSAGE that carries a short message's
RP-DATA to the service centre, or its RP-ACK/RP-ERROR back to the IP-SM-GW
(TS 24.341). Pure std C++.
*/
export module Imsd:Messages;
@ -70,6 +75,13 @@ export namespace imsd::msg {
// never fabricate one: the network reads it to learn which access
// the UE is reachable on (stock sends it in every REGISTER/INVITE).
std::string panInfo;
// Advertise SMS over IP in the REGISTER Contact (+g.3gpp.smsip, TS
// 24.341 5.3.2.2): the network then delivers terminating short
// messages and submit reports to imsd as SIP MESSAGEs (a network that
// also has a NAS SMS path chooses by operator policy, 5.3.3.4.2 NOTE
// 2). Off unless the shell has SMS over IP handling to offer and turns
// it on from its configuration.
bool smsOverIp = false;
};
// A Via line: "Via: SIP/2.0/<transport> [local]:<port>;branch=z9hG4bK<b>;rport".
@ -124,12 +136,13 @@ export namespace imsd::msg {
// matching compares the value as a string, so the encoding must match
// what the network selects on — the stock modem sends exactly
// urn%3Aurn-7%3A..., diag capture 2026-07-18). +sip.instance carries the
// IMEI URN when known. Deliberately NOT +g.3gpp.smsip: it routes
// terminating SMS over IMS to a stack with no SMSoIP handling yet,
// silently losing texts — and exact tag parity including smsip +
// nw-init-ussi was tested live (2026-07-20, stored binding echo-verified)
// and did NOT change terminating access-domain selection, so the tags
// buy nothing.
// IMEI URN when known. +g.3gpp.smsip only when the shell asks
// (Context::smsOverIp): it routes terminating SMS over IMS to imsd,
// which loses texts unless the daemon handles them — and exact tag
// parity including smsip + nw-init-ussi was tested live (2026-07-20,
// stored binding echo-verified) and did NOT change terminating
// access-domain selection for calls, so for calls the tags buy nothing.
// The tag follows the ICSI.
// The binding user-part every Contact we emit carries (see
// Context::contactUser).
inline std::string_view ContactUser(const Context& c) {
@ -141,6 +154,7 @@ export namespace imsd::msg {
std::string s;
if (!c.instanceId.empty()) s += std::format(";+sip.instance=\"<{}>\"", c.instanceId);
s += ";+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\"";
if (c.smsOverIp) s += ";+g.3gpp.smsip";
return s;
}
@ -178,6 +192,19 @@ export namespace imsd::msg {
return std::format("sip:{};phone-context={}@{};user=phone", t, domain, domain);
}
// The service centre's PSI a submission is addressed to (TS 24.341
// 5.3.1.2 NOTE 1: the SC's E.164 number as a SIP URI with user=phone,
// or a tel URI; the home domain as its host is imsd's choice). Only an
// international number makes one — RuriFor's
// dialled-number guess (a trunk 0 is Dutch) has no place here; a
// service centre in another form is nullopt, and the caller says so.
inline std::optional<std::string> ScPsi(std::string_view e164, std::string_view domain) {
if (e164.size() < 2 || e164[0] != '+') return std::nullopt;
for (char ch : e164.substr(1))
if (ch < '0' || ch > '9') return std::nullopt;
return std::format("sip:{}@{};user=phone", e164, domain);
}
namespace detail {
inline std::string Join(std::span<const std::string> lines) {
std::string s;
@ -495,6 +522,28 @@ export namespace imsd::msg {
return std::format("{}\r\n", detail::Join(l));
}
// The final for an inbound MESSAGE outside a call's dialog — the 200 to one we took, the 488 to
// one we did not: each `Via:` line echoed in order (the response
// travels the proxy chain back, RFC 3261 8.2.6.2; long header names only,
// not the compact forms), From, To with
// `toTag` added unless the request's To holds `tag=` (8.2.6.2 again),
// Call-ID and CSeq; no body. The shell's answers to any other request
// outside a call but an INVITE (an OPTIONS, a NOTIFY, a method it does
// not handle) keep BuildResponse200's hop-by-hop shape, as before.
std::string BuildResponse(const Context&, std::string_view request, int code, std::string_view text, std::string_view toTag) {
std::vector<std::string> l = {std::format("SIP/2.0 {} {}", code, text)};
for (auto v : imsd::sip::Headers(request, "Via")) l.push_back(std::format("Via: {}", v));
if (auto f = imsd::sip::Header(request, "From")) l.push_back(std::format("From: {}", *f));
if (auto t = imsd::sip::Header(request, "To")) {
if (t->contains("tag=")) l.push_back(std::format("To: {}", *t));
else l.push_back(std::format("To: {};tag={}", *t, toTag));
}
if (auto ci = imsd::sip::Header(request, "Call-ID")) l.push_back(std::format("Call-ID: {}", *ci));
if (auto cs = imsd::sip::Header(request, "CSeq")) l.push_back(std::format("CSeq: {}", *cs));
l.push_back("Content-Length: 0");
return std::format("{}\r\n", detail::Join(l));
}
// A 200 OK for an inbound request: echo Via/From/To/Call-ID/CSeq in order,
// then either an SDP answer (with our Contact) or an empty body.
// `extra` appends headers (Session-Expires echo for refreshes...).
@ -514,4 +563,42 @@ export namespace imsd::msg {
l.push_back("Content-Length: 0");
return std::format("{}\r\n", detail::Join(l));
}
// ---- SMS over IP ----------------------------------------------------
constexpr std::string_view SmsContentType = "application/vnd.3gpp.sms";
// A SIP MESSAGE carrying an SM-RL message (TS 24.011) as its body — the
// RP-DATA of a submission to the service centre's PSI (TS 24.341
// 5.3.1.2: `target` is the Request-URI and the To — `ScPsi`),
// or the RP-ACK/RP-ERROR of a delivery report to the IP-SM-GW (5.3.2.4:
// `target` is the IP-SM-GW, `inReplyTo` the delivered MESSAGE's
// Call-ID). Out of dialog: a Call-ID of its own, CSeq 1,
// the registration's Route, our public identity in From, and no Contact
// (RFC 3428 §4). The same P-Preferred-Identity, P-Access-Network-Info
// and sec-agree headers as the INVITE and the reg-event SUBSCRIBE.
std::string BuildMessageSms(const Context& c, std::string_view target, std::string_view callid, std::string_view ftag, std::string_view branch, std::span<const std::uint8_t> rp, std::string_view inReplyTo = {}) {
std::vector<std::string> l = {
std::format("MESSAGE {} SIP/2.0", target),
ViaLine(c, c.transport, c.portUs, branch),
"Max-Forwards: 70",
std::format("Route: {}", c.route),
std::format("From: <{}>;tag={}", CallerId(c), ftag),
std::format("To: <{}>", target),
std::format("Call-ID: {}", callid),
"CSeq: 1 MESSAGE",
};
if (!inReplyTo.empty()) l.push_back(std::format("In-Reply-To: {}", inReplyTo));
if (!c.ppi.empty()) l.push_back(std::format("P-Preferred-Identity: <{}>", c.ppi));
if (!c.panInfo.empty()) l.push_back(std::format("P-Access-Network-Info: {}", c.panInfo));
l.push_back(std::format("Security-Verify: {}", c.securityServer));
l.push_back("Proxy-Require: sec-agree");
l.push_back("Require: sec-agree");
l.push_back(std::format("Content-Type: {}", SmsContentType));
l.push_back(std::format("Content-Length: {}", rp.size()));
l.push_back("");
std::string out = detail::Join(l);
out.append(reinterpret_cast<const char*>(rp.data()), rp.size()); // lint-disable-line no-char-pointer
return out;
}
}