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

@ -2,9 +2,11 @@
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// lint-disable-file fixed-width-types
// Imsd:Messages unit tests — every builder compared byte-for-byte against a
// reference message cross-generated from the Python VoLTE stack with pinned
// tags/branches/SPIs. A divergence here is a wire-format change, i.e. a
// Imsd:Messages unit tests — the initial and the protected REGISTER and the INVITE
// compared byte-for-byte against a reference message cross-generated from the
// Python VoLTE stack with pinned tags/branches/SPIs; their variants and the other
// builders against a message written here or by their lines (BuildInviteResponse
// is not called). A divergence here is a wire-format change, i.e. a
// registration or call that the commercial IMS core may reject; these are the
// hardest pins in the project. Two deliberate post-parity divergences:
// the REGISTER Contact carries +sip.instance and the MMTEL ICSI feature tag
@ -14,7 +16,11 @@
// TS 24.229 coding and what the stock modem sends on the wire; the Python
// stack's plain-colon form is a different string under RFC 3841 matching
// and left terminating access-domain selection routing incoming calls to
// CS fallback (observed live 2026-07-20).
// CS fallback (observed live 2026-07-20). Then the SMS over IP shapes: the
// +g.3gpp.smsip Contact tag behind Context::smsOverIp, the MESSAGE carrying
// an RP-DATA to the service centre and the one carrying an RP-ACK to the
// IP-SM-GW with In-Reply-To (TS 24.341 5.3.1.2, 5.3.2.4), and a final other
// than 200 for an inbound request.
import std;
import Imsd;
@ -226,6 +232,9 @@ int main() {
Eq(RuriFor("+31612345678", c.id.domain), "sip:+31612345678@ims.mnc001.mcc001.3gppnetwork.org;user=phone", "ruri E.164");
Eq(RuriFor("0612345678", c.id.domain), "sip:+31612345678@ims.mnc001.mcc001.3gppnetwork.org;user=phone", "ruri Dutch national");
Eq(RuriFor("0031612345678", c.id.domain), "sip:+31612345678@ims.mnc001.mcc001.3gppnetwork.org;user=phone", "ruri 00 intl");
// ---- ScPsi: the service centre's PSI is its international number, never a dialled-number guess
Eq(ScPsi("+31653131313", c.id.domain).value_or(""), "sip:+31653131313@ims.mnc001.mcc001.3gppnetwork.org;user=phone", "the service centre's PSI");
Check(!ScPsi("0653131313", c.id.domain) && !ScPsi("0032475161616", c.id.domain) && !ScPsi("+", c.id.domain) && !ScPsi("+31 65", c.id.domain) && !ScPsi("+31653131313#", c.id.domain) && !ScPsi("+31*65", c.id.domain), "a national, a 00-prefixed, an empty or a spaced number, or a * or # in it, makes no PSI");
// ---- SUBSCRIBE (reg event, RFC 3680) — no Python counterpart; pinned
// against the shape of the other protected in-flow requests
@ -416,6 +425,93 @@ int main() {
Check(!BuildRegisterInitial(c, "cid@h", "ft", "br", 1, 2) .contains("User-Agent:"), "empty userAgent omits the header");
}
// ---- SMS over IP: the Contact tag, the MESSAGE both ways, a 488
{
Context cs = MakeCtx();
cs.smsOverIp = true;
std::string r1 = BuildRegisterInitial(cs, "REGCALLID@2001:db8::db43", "FTAG5678", "REGBRANCH123456", 7449812u, 176466735u);
Check(r1.contains("\r\nContact: <sip:001010123456789@[2001:db8::db43]:5060>;+sip.instance=\"<urn:gsma:imei:35999999-000000-1>\";+g.3gpp.icsi-ref=\"urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel\";+g.3gpp.smsip;expires=600000\r\n"), "smsOverIp: +g.3gpp.smsip after the ICSI in the REGISTER Contact");
Check(!Reg1.contains("smsip") && !BuildRegisterInitial(c, "REGCALLID@2001:db8::db43", "FTAG5678", "REGBRANCH123456", 7449812u, 176466735u).contains("smsip"), "the tag is off by default");
Check(!BuildInvite(cs, Dialog{RuriFor("1233", cs.id.domain), "CID@x", "it", "ib"}, "v=0\r\n", false).contains("smsip"), "the INVITE Contact never carries it");
auto rp = imsd::util::FromHex("002a0007911356131313f31711000b916407281553f80000aa0ae8329bfd4697d9ec37");
Check(rp && rp->size() == 35, "the RP-DATA fixture");
std::string msg = BuildMessageSms(c, RuriFor("+31653131313", c.id.domain), "MSGCALLID@2001:db8::db43", "MTAG1234", "MSGBRANCH0000000000", *rp);
const std::string wantHead =
"MESSAGE sip:+31653131313@ims.mnc001.mcc001.3gppnetwork.org;user=phone SIP/2.0\r\n"
"Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKMSGBRANCH0000000000;rport\r\n"
"Max-Forwards: 70\r\n"
"Route: <sip:[2001:db8::105]:6000;lr>\r\n"
"From: <tel:+31611111111>;tag=MTAG1234\r\n"
"To: <sip:+31653131313@ims.mnc001.mcc001.3gppnetwork.org;user=phone>\r\n"
"Call-ID: MSGCALLID@2001:db8::db43\r\n"
"CSeq: 1 MESSAGE\r\n"
"P-Preferred-Identity: <tel:+31611111111>\r\n"
"P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\n"
"Security-Verify: ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=86438593; spi-s=86438592; port-c=33421; port-s=6000\r\n"
"Proxy-Require: sec-agree\r\n"
"Require: sec-agree\r\n"
"Content-Type: application/vnd.3gpp.sms\r\n"
"Content-Length: 35\r\n"
"\r\n";
Eq(msg.substr(0, wantHead.size()), wantHead, "MESSAGE with an RP-DATA to the service centre");
Check(msg.size() == wantHead.size() + 35 && std::equal(rp->begin(), rp->end(), msg.begin() + static_cast<std::ptrdiff_t>(wantHead.size()), [](std::uint8_t b, char ch) { return b == static_cast<std::uint8_t>(ch); }), "the RP-DATA bytes follow the headers verbatim");
Check(!msg.contains("Contact:"), "no Contact in a MESSAGE (RFC 3428)");
std::vector<std::uint8_t> ack = {0x02, 0x11, 0x41, 0x02, 0x00, 0x00};
std::string rep = BuildMessageSms(c, "sip:ipsmgw.ims.mnc001.mcc001.3gppnetwork.org", "RPTCALLID@2001:db8::db43", "RTAG", "RBRANCH", ack, "abc123@ipsmgw");
const std::string wantRep =
"MESSAGE sip:ipsmgw.ims.mnc001.mcc001.3gppnetwork.org SIP/2.0\r\n"
"Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKRBRANCH;rport\r\n"
"Max-Forwards: 70\r\n"
"Route: <sip:[2001:db8::105]:6000;lr>\r\n"
"From: <tel:+31611111111>;tag=RTAG\r\n"
"To: <sip:ipsmgw.ims.mnc001.mcc001.3gppnetwork.org>\r\n"
"Call-ID: RPTCALLID@2001:db8::db43\r\n"
"CSeq: 1 MESSAGE\r\n"
"In-Reply-To: abc123@ipsmgw\r\n"
"P-Preferred-Identity: <tel:+31611111111>\r\n"
"P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0\r\n"
"Security-Verify: ipsec-3gpp; q=0.1; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=86438593; spi-s=86438592; port-c=33421; port-s=6000\r\n"
"Proxy-Require: sec-agree\r\n"
"Require: sec-agree\r\n"
"Content-Type: application/vnd.3gpp.sms\r\n"
"Content-Length: 6\r\n"
"\r\n";
Eq(rep.substr(0, wantRep.size()), wantRep, "delivery report: the MESSAGE to the IP-SM-GW, In-Reply-To after the CSeq, the header block whole");
Check(rep.size() == wantRep.size() + ack.size() && std::equal(ack.begin(), ack.end(), rep.begin() + static_cast<std::ptrdiff_t>(wantRep.size()), [](std::uint8_t b, char ch) { return b == static_cast<std::uint8_t>(ch); }), "delivery report: the RP-ACK bytes follow the headers verbatim");
Check(!rep.contains("Contact:"), "no Contact in a delivery report either");
const std::string inbound =
"MESSAGE sip:me@[2001:db8::db43]:45062 SIP/2.0\r\n"
"Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n"
"Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0\r\n"
"From: <sip:ipsmgw@ims>;tag=N1\r\n"
"To: <sip:me@ims>\r\n"
"Call-ID: in1@net\r\n"
"CSeq: 9 MESSAGE\r\n"
"Content-Length: 0\r\n\r\n";
Eq(BuildResponse(c, inbound, 488, "Not Acceptable Here", "T488"),
"SIP/2.0 488 Not Acceptable Here\r\n"
"Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n"
"Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0\r\n"
"From: <sip:ipsmgw@ims>;tag=N1\r\n"
"To: <sip:me@ims>;tag=T488\r\n"
"Call-ID: in1@net\r\n"
"CSeq: 9 MESSAGE\r\n"
"Content-Length: 0\r\n\r\n", "a 488 for an inbound MESSAGE echoes every Via and tags To");
const std::string tagged = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet2\r\nFrom: <sip:ipsmgw@ims>;tag=N2\r\nTo: <sip:me@ims>;tag=had\r\nCall-ID: in2@net\r\nCSeq: 1 MESSAGE\r\nContent-Length: 0\r\n\r\n";
Check(BuildResponse(c, tagged, 200, "OK", "unused").contains("\r\nTo: <sip:me@ims>;tag=had\r\n"), "a To that already carries a tag is echoed as it is");
{
const std::string body = "hi\r\nVia: SIP/2.0/UDP 192.0.2.66;branch=z9hG4bKx";
const std::string tail = "Content-Length: 0\r\n\r\n";
std::string bodied = inbound;
bodied.replace(bodied.size() - tail.size(), tail.size(), std::format("Content-Type: text/plain\r\nContent-Length: {}\r\n\r\n{}", body.size(), body));
const std::string r = BuildResponse(c, bodied, 200, "OK", "T200");
Check(imsd::sip::Headers(r, "Via").size() == 2 && !r.contains("192.0.2.66"), "a body line that starts with Via: is no Via: the final echoes the header section's two only");
}
}
if (Failures == 0) std::println("Messages: all tests passed");
return Failures;
}