2026-07-22 22:53:28 +02:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
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.
2026-10-16 23:13:39 +02:00
// 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
sip: a header is found under its compact form, and with white space before the colon
RFC 3261 7.3.3 lets a sender write a header name in its one-letter form
at any time and has every implementation accept both forms. The text
layer matched the long name only, so to every reader a header sent in
compact form was absent: the TCP framer took no body length from `l:`
and read the body as the start of the next message, a response with
`i:` matched no transaction and a request no call, the finals imsd
builds left out a Via, From, To or Call-ID that came in compact form,
and a short message whose content type came as `c:` was answered 200
and logged instead of taken (issue #2).
MatchHeaderLine now takes a line under the name it is asked for or
under that name's compact form: the ten RFC 3261 defines, and `o` for
Event (RFC 6665) and `x` for Session-Expires (RFC 4028), the two
extension headers imsd reads that have one. Every reader goes through
it — Header, Headers, the framer, DatagramMessage — so no reader
changes for it. It also takes white space between the name and the
colon, which the same grammar allows (25.1's HCOLON; 7.3.1's
"Subject : lunch") and the matcher refused. A value folded
onto further lines is still read by its first line.
One reader is changed for the rows it is now handed. ViaResponsePort
gives the port the top Via names, to which the answer to a request that
came in a datagram on a server port goes (RFC 3261 18.2.2), and none
where that Via carries `rport` (RFC 3581): the answer then goes back to
the port the request came from. It took `rport` for present where
those five letters, in lower case, stood anywhere behind the row's
first semicolon. Parameter names are case-insensitive (7.3.1), and a
request whose top Via came as `v: …;RPORT`, a row 1.2.0 passed over,
would have been answered to the port that Via names, its `rport` not
seen. ViaResponsePort now reads the Via's parameters one by one, each
to the next semicolon outside a quoted string, and `rport` is one by
its name, in any case: not those letters in a value or in a longer
name. Read so, the last parameter of the top value would run on into
the next value where a row holds a list — `rport, SIP/2.0/UDP …` is no
`rport`, and 1.2.0's search of the letters found it —, so the top value
is cut from the row first, at the first comma outside a quoted string.
With that cut a lower value's `rport` no longer counts for the top
one's, as it did in 1.2.0 for a list on one line.
Tested by the suites: tests/Sip pins each of the twelve forms through
Header, in lower and in upper case, and through Headers, `l:` through
the framer and DatagramMessage, 7.3.1's four spellings of the colon, and
ViaResponsePort's reading of a Via row, `rport` by its name and of a
list on one line the top value alone; tests/Engine drives an incoming
call from an INVITE in compact form, an outgoing call from responses in
compact form and a delivered MESSAGE in compact form through the
reducers; tests/Messages builds BuildResponse, BuildResponse200 and
BuildInviteResponse for a request in compact form. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, server-ports,
at this commit and at the one before) a request on a server port whose
Via row is a list on one line with `rport` on its lower value is
answered from the client-port socket, the way to the port its top value
names, where the commit before answered it from the socket it had come
on. On the dev phone the binary of this range's last commit answered a
MESSAGE the phone sent to its own protected server port from the
client-port socket where its Via row was such a list, and from the
socket it had come on where its one Via came as `v: …;RPORT` (the
capture's phone-form6, inject-bench.log).
2026-11-22 17:35:11 +01:00
// by the lines of one 180 and by its answers to an INVITE in long and in
// compact form). A divergence here is a wire-format change, i.e. a
2026-07-22 22:53:28 +02:00
// registration or call that the commercial IMS core may reject; these are the
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
// hardest pins in the project. Three deliberate post-parity divergences:
2026-07-22 22:53:28 +02:00
// the REGISTER Contact carries +sip.instance and the MMTEL ICSI feature tag
// (the Python stack registered without them, which made the network divert
// incoming calls to voicemail — see ContactFeatures in Imsd:Messages), and
// every ICSI feature-tag value is percent-encoded (urn%3Aurn-7%3A...) — the
// 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
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
// CS fallback (observed live 2026-07-20); and the REGISTERs' Security-Client
// carries the default offer's two mechanisms, where the Python stack offered
aka: the ISIM application of a card, its identity files and the identity they hold
An ISIM holds the parameters a registration starts from (TS 24.229
5.1.1.1A): the private identity in EF_IMPI, the home domain in
EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4),
of which the first record's is the one to register (GSMA IR.92 2.2.1).
TS 23.003 has a UE derive its home domain and a temporary public
identity from the IMSI "if there is no ISIM application" (13.2, 13.4B);
those derived names are the only ones imsd has. This is the part of
reading an ISIM that has no I/O; the daemon does not use it yet.
ParseIsimApp finds the ISIM application of one slot in qmicli's card
status, in whatever state the modem lists it. On the two cards with an
ISIM that a read-only probe read (a US card, 2026-11-23, and a
Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM
`detected` beside the `ready` USIM, and on both a logical channel
opened on it and its identity files were read.
The commands are the ones that probe sent: SELECT by file identifier,
READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5).
ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal
endings (10.2.1.1), and ApduFollowUp the command it asks for next when
the answer brought no data: GET RESPONSE for '61 XX', a command without
data again with the length named for '6C XX' (7.3.1.1.5); an answer
with data is taken for the command's last. HoldsRead takes a read's
data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes
the value of the data object with tag '80' that a file begins with,
cuts 'FF' bytes from the end of its length as the probe does, and gives
none for a value with a blank or a byte that is not printable ASCII.
IsimIdentity checks the three values before they replace the derived
names: they are written into a quoted string and between the angle
brackets of SIP header fields. It refuses a value with a quote, a
backslash, an angle bracket, a blank, a line break or another byte
outside printable ASCII, a private or a public identity of more than
255 characters, a private identity without an @ between two parts, a
domain that is not labels of letters, digits and inner hyphens, 63
characters a label and 253 in all at most, and a public identity that
does not begin sip: or is that alone.
The two readers of the card status share how they read a line;
ParseCardStatus gives what it gave.
Tested: tests/Aka (the application out of a card status with the US
card's two applications, in either order, and out of a second slot with
the Norwegian card's AID; the commands, the answers and their
follow-ups, the values, the identity and what is refused) and
tests/Messages (the two REGISTERs of an ISIM's identity against the
pinned ones).
2026-11-23 19:32:58 +01:00
// one cipher. The two REGISTERs once more on an identity an ISIM holds: the
// pinned ones with the card's names in place of the derived ones. Then the
// SMS over IP shapes: the
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.
2026-10-16 23:13:39 +02:00
// +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.
2026-07-22 22:53:28 +02:00
import std ;
import Imsd ;
using namespace imsd : : msg ;
namespace {
int Failures = 0 ;
void Check ( bool cond , std : : string_view msg ) {
if ( ! cond ) {
std : : println ( std : : cerr , " FAIL: {} " , msg ) ;
+ + Failures ;
}
}
void Eq ( std : : string_view got , std : : string_view want , std : : string_view msg ) {
if ( got ! = want ) {
std : : println ( std : : cerr , " FAIL: {} \n --- got --- \n {} \n --- want --- \n {} " , msg , got , want ) ;
+ + Failures ;
}
}
Context MakeCtx ( ) {
Context c ;
c . id = imsd : : aka : : MakeIdentity ( " 001010123456789 " , " 001 " , " 01 " ) ;
c . local = " 2001:db8::db43 " ;
c . route = " <sip:[2001:db8::105]:6000;lr> " ;
c . ppi = " tel:+31611111111 " ;
c . securityServer =
" 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 " ;
c . instanceId = " urn:gsma:imei:35999999-000000-1 " ;
c . panInfo = " 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0 " ;
return c ;
}
constexpr std : : string_view Reg1 =
" REGISTER sip:ims.mnc001.mcc001.3gppnetwork.org SIP/2.0 \r \n "
" Via: SIP/2.0/UDP [2001:db8::db43]:5060;branch=z9hG4bKREGBRANCH123456;rport \r \n "
" Max-Forwards: 70 \r \n "
" From: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org>;tag=FTAG5678 \r \n "
" To: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org> \r \n "
" Call-ID: REGCALLID@2001:db8::db43 \r \n "
" CSeq: 1 REGISTER \r \n "
" Contact: <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 \" ;expires=600000 \r \n "
" Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY, PRACK, UPDATE, MESSAGE, REFER \r \n "
" Authorization: Digest username= \" 001010123456789@ims.mnc001.mcc001.3gppnetwork.org \" , realm= \" ims.mnc001.mcc001.3gppnetwork.org \" , uri= \" sip:ims.mnc001.mcc001.3gppnetwork.org \" , nonce= \" \" , response= \" \" \r \n "
" Require: sec-agree \r \n "
" Proxy-Require: sec-agree \r \n "
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
" Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062 \r \n "
2026-07-22 22:53:28 +02:00
" Supported: sec-agree, path \r \n "
" Expires: 600000 \r \n "
" P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0 \r \n "
" Content-Length: 0 \r \n "
" \r \n " ;
constexpr std : : string_view Reg2 =
" REGISTER sip:ims.mnc001.mcc001.3gppnetwork.org SIP/2.0 \r \n "
" Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKREGBRANCH123456;rport \r \n "
" Max-Forwards: 70 \r \n "
" From: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org>;tag=FTAG5678 \r \n "
" To: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org> \r \n "
" Call-ID: REGCALLID@2001:db8::db43 \r \n "
" CSeq: 2 REGISTER \r \n "
" Contact: <sip:001010123456789@[2001:db8::db43]:45062>;+sip.instance= \" <urn:gsma:imei:35999999-000000-1> \" ;+g.3gpp.icsi-ref= \" urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel \" ;expires=600000 \r \n "
" Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY, PRACK, UPDATE, MESSAGE, REFER \r \n "
" Authorization: Digest username= \" 001010123456789@ims.mnc001.mcc001.3gppnetwork.org \" , realm= \" ims.mnc001.mcc001.3gppnetwork.org \" , uri= \" sip:ims.mnc001.mcc001.3gppnetwork.org \" , nonce= \" NONCEXYZ \" , qop=auth, nc=00000001, cnonce= \" CNONCE0123456789 \" , response= \" RESP0000 \" , algorithm=AKAv1-MD5 \r \n "
" Require: sec-agree \r \n "
" Proxy-Require: sec-agree \r \n "
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
" Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062 \r \n "
2026-07-22 22:53:28 +02:00
" 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 "
" Supported: sec-agree, path \r \n "
" Expires: 600000 \r \n "
" P-Access-Network-Info: 3GPP-E-UTRAN-FDD;utran-cell-id-3gpp=0010112340ABCDE0 \r \n "
" Content-Length: 0 \r \n "
" \r \n " ;
constexpr std : : string_view Sdp =
" v=0 \r \n "
" o=- 7777777 7777777 IN IP6 2001:db8::db43 \r \n "
" s=- \r \n "
" c=IN IP6 2001:db8::db43 \r \n "
" t=0 0 \r \n "
2026-09-02 03:21:06 +02:00
" m=audio 50004 RTP/AVP 97 99 98 100 \r \n "
2026-07-22 22:53:28 +02:00
" b=AS:41 \r \n "
" b=RS:512 \r \n "
" b=RR:1536 \r \n "
" a=rtpmap:97 AMR-WB/16000/1 \r \n "
" a=fmtp:97 octet-align=1;mode-change-capability=2;max-red=0 \r \n "
2026-09-02 03:21:06 +02:00
" a=rtpmap:99 AMR/8000/1 \r \n "
" a=fmtp:99 octet-align=1;mode-change-capability=2;max-red=0 \r \n "
2026-07-22 22:53:28 +02:00
" a=rtpmap:98 telephone-event/16000 \r \n "
" a=fmtp:98 0-15 \r \n "
2026-09-02 03:21:06 +02:00
" a=rtpmap:100 telephone-event/8000 \r \n "
" a=fmtp:100 0-15 \r \n "
2026-07-22 22:53:28 +02:00
" a=ptime:20 \r \n "
" a=maxptime:240 \r \n "
" a=sendrecv \r \n " ;
constexpr std : : string_view Invite =
" INVITE sip:1233;phone-context=ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org;user=phone SIP/2.0 \r \n "
" Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKINVBRANCH0000000000;rport \r \n "
" Max-Forwards: 70 \r \n "
" Route: <sip:[2001:db8::105]:6000;lr> \r \n "
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
" From: <tel:+31611111111>;tag=ITAG123456 \r \n "
2026-07-22 22:53:28 +02:00
" To: <sip:1233;phone-context=ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org;user=phone> \r \n "
" Call-ID: CALLID789@2001:db8::db43 \r \n "
" CSeq: 1 INVITE \r \n "
" Contact: <sip:001010123456789@[2001:db8::db43]:45062>;+g.3gpp.icsi-ref= \" urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel \" \r \n "
" Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER \r \n "
" Supported: 100rel, timer \r \n "
" Accept-Contact: *;+g.3gpp.icsi-ref= \" urn%3Aurn-7%3A3gpp-service.ims.icsi.mmtel \" \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 "
" Session-Expires: 1800;refresher=uac \r \n "
" Min-SE: 90 \r \n "
" Content-Type: application/sdp \r \n "
2026-09-02 03:21:06 +02:00
" Content-Length: 466 \r \n "
2026-07-22 22:53:28 +02:00
" \r \n "
" v=0 \r \n "
" o=- 7777777 7777777 IN IP6 2001:db8::db43 \r \n "
" s=- \r \n "
" c=IN IP6 2001:db8::db43 \r \n "
" t=0 0 \r \n "
2026-09-02 03:21:06 +02:00
" m=audio 50004 RTP/AVP 97 99 98 100 \r \n "
2026-07-22 22:53:28 +02:00
" b=AS:41 \r \n "
" b=RS:512 \r \n "
" b=RR:1536 \r \n "
" a=rtpmap:97 AMR-WB/16000/1 \r \n "
" a=fmtp:97 octet-align=1;mode-change-capability=2;max-red=0 \r \n "
2026-09-02 03:21:06 +02:00
" a=rtpmap:99 AMR/8000/1 \r \n "
" a=fmtp:99 octet-align=1;mode-change-capability=2;max-red=0 \r \n "
2026-07-22 22:53:28 +02:00
" a=rtpmap:98 telephone-event/16000 \r \n "
" a=fmtp:98 0-15 \r \n "
2026-09-02 03:21:06 +02:00
" a=rtpmap:100 telephone-event/8000 \r \n "
" a=fmtp:100 0-15 \r \n "
2026-07-22 22:53:28 +02:00
" a=ptime:20 \r \n "
" a=maxptime:240 \r \n "
" a=sendrecv \r \n " ;
constexpr std : : string_view Subscribe =
" SUBSCRIBE sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org SIP/2.0 \r \n "
" Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKSUBBRANCH0000000000;rport \r \n "
" Max-Forwards: 70 \r \n "
" Route: <sip:[2001:db8::105]:6000;lr> \r \n "
" From: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org>;tag=STAG1234 \r \n "
" To: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org> \r \n "
" Call-ID: SUBCALLID@2001:db8::db43 \r \n "
" CSeq: 1 SUBSCRIBE \r \n "
" Contact: <sip:001010123456789@[2001:db8::db43]:45062> \r \n "
" Event: reg \r \n "
" Accept: application/reginfo+xml \r \n "
" Expires: 600000 \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-Length: 0 \r \n "
" \r \n " ;
}
int main ( ) {
Context c = MakeCtx ( ) ;
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
// ---- REGISTER (initial + protected), byte-exact (the header of this
// file has what differs from the Python stack's bytes)
2026-07-22 22:53:28 +02:00
Eq ( BuildRegisterInitial ( c , " REGCALLID@2001:db8::db43 " , " FTAG5678 " ,
" REGBRANCH123456 " , 7449812u , 176466735u ) ,
Reg1 , " unprotected REGISTER " ) ;
{
std : : string auth = AuthAka ( c , " NONCEXYZ " , " CNONCE0123456789 " , " RESP0000 " ) ;
Eq ( BuildRegisterProtected ( c , " REGCALLID@2001:db8::db43 " , " FTAG5678 " ,
" REGBRANCH123456 " , 2 , 7449812u , 176466735u , auth ) ,
Reg2 , " protected REGISTER " ) ;
}
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
// ---- a reregistration (the keepalive refresh, a resume): the last
// challenge answer verbatim, new SPIs and a new client port in
// Security-Client, the server port unchanged (TS 24.229 5.1.1.4.2,
// TS 33.203 7.4.0)
{
std : : string auth = AuthAka ( c , " NONCEXYZ " , " CNONCE0123456789 " , " RESP0000 " ) ;
std : : string r3 = BuildRegisterProtected ( c , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 3 , 55555u , 66666u , auth , 45063 ) ;
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
Check ( r3 . contains ( " \r \n Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=55555; spi-s=66666; port-c=45063; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=55555; spi-s=66666; port-c=45063; port-s=45062 \r \n " ) , " refresh Security-Client: new SPIs, a new client port, the same server port, in both mechanisms " ) ;
engine, messages, util, imsd: the reregistration carries the last challenge answer and new SA parameters
The keepalive refresh and the resume's re-REGISTER carried an initial
registration's first-REGISTER Authorization — an empty nonce and
response — in the second REGISTER's frame, with a Security-Client
naming the SPIs and ports in use. TS 24.229 5.1.1.4.2 has a
reregistration carry the Authorization
of the last successful authentication — the last received nonce and
the last calculated response — and a Security-Client with the new
parameter values for the two SA pairs a re-authentication would set
up; TS 33.203 7.4.0 keeps the server ports and changes the client
ports. O2 UK refused the refresh of the old shape with 503 on every
occasion on record (alyx, 2026-09-24 to 10-08, a stationary phone
included) and accepted the fresh registrations that followed; KPN
accepted the old shape without a challenge.
The fresh registration now records the 401's nonce, our cnonce and
the response in the registration state (auth_nonce, auth_cnonce,
auth_response in the state file); a reregistration sends them
verbatim, mints new SPIs and proposes PortUcNext (45063) as the
client port with the server port unchanged. A registration resumed from a
state file written before this change has no answer: its
reregistrations carry the empty digest, as before, until its next
fresh registration, and the resume says so. A 401 to a reregistration asks
for re-authentication over temporary SAs on the proposed ports (TS
24.229 5.1.1.5.1), which imsd does not implement: the refresh throws
it as a refusal and RegRecovery makes the next step the fresh
registration, replacing the in-place answer over the old SAs.
Tested with the Messages and Engine suites and on the dev phone (KPN)
with a build of this commit's tree: a resume from the previous state
file, a fresh registration, a resume that sent the stored answer,
three refreshes at a 120-s interval, an outgoing call to a bench line
and an incoming call's ring.
2026-10-10 21:00:28 +02:00
Check ( r3 . contains ( " \r \n CSeq: 3 REGISTER \r \n " ) & & r3 . contains ( " \r \n Via: SIP/2.0/TCP [2001:db8::db43]:45062; " ) & & r3 . contains ( " @[2001:db8::db43]:45062> " ) , " refresh: CSeq+1, the server port in Via and Contact " ) ;
Check ( r3 . contains ( std : : format ( " \r \n {} \r \n " , auth ) ) , " refresh carries the last challenge answer verbatim " ) ;
std : : string r3same = BuildRegisterProtected ( c , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 3 , 55555u , 66666u , auth ) ;
Check ( r3same . contains ( " port-c=45061; port-s=45062 \r \n " ) , " without a client port the in-use one is offered (the initial registration's second REGISTER) " ) ;
}
ipsec, messages, imsd: the offered ESP ciphers are a list
imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.
EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).
An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.
Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
2026-11-23 10:08:04 +01:00
// ---- the offer: one mechanism per offered cipher, in the list's order,
// every one with the REGISTER's SPIs and ports
{
Check ( c . ealgOffer = = imsd : : ipsec : : DefaultEalgOffer ( ) , " the default context offers the default list " ) ;
ipsec: the default offer names the null cipher beside aes-cbc
The UE is to announce the IPsec algorithms it supports (TS 24.229
5.1.1.2.2 d), and TS 33.203 7.2 has its first REGISTER carry "a list of
identifiers for the integrity and encryption algorithms, which the UE
supports". The null cipher is one a UE must support (7.1 by TS 33.210
5.3.3 and RFC 8221; GSMA IR.92 5.3), and imsd can install it, but its
default offer named aes-cbc alone.
Ice Norway has answered imsd's first REGISTER 403 (2026-11-01, -02),
where the phone's own modem had carried a call with the CS domain
detached (2026-09-02). On Odido it drew an answer that the imsd of that
day did not log, and the Python stack's REGISTER minutes later a 403
(2026-09-16), with a SIM that another Fairphone 6's own IMS client had
registered with (2026-07-15). That client's four mechanisms were
answered with one naming ealg=null, as the Python stack's offer of the
null cipher was (2026-07-13). Whether the offer is the reason for either
answer is not measured.
The default offer is aes-cbc, then null. A P-CSCF that selects the null
cipher from it gets SAs that protect the signalling's integrity and do
not encrypt it; EALG=aes-cbc gives 1.2.1's offer.
Tested: the suites; the daemon of this commit against a stand-in P-CSCF
on the workstation that refuses an offer without the null cipher (imsd
1.2.1 is answered 403 there, this commit's is challenged and selects the
null mechanism); on the FP6 on KPN, in an earlier build of this change,
on 1.2.0, a reregistration and a fresh registration with the default
offer: KPN named aes-cbc, and answered the reregistration, the fresh
registration's protected REGISTER and the reg-event SUBSCRIBEs 200.
2026-11-23 09:32:26 +01:00
Context one = c ;
one . ealgOffer = { " aes-cbc " } ;
Check ( BuildRegisterInitial ( one , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 7449812u , 176466735u ) . contains ( " \r \n Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062 \r \n " ) , " one cipher offered: one mechanism, the header as it was before the default offer had two " ) ;
ipsec, messages, imsd: the offered ESP ciphers are a list
imsd's Security-Client held one mechanism, its cipher the value of EALG,
aes-cbc when unset. TS 33.203 7.2 has the UE's first REGISTER carry "a
list of identifiers for the integrity and encryption algorithms, which
the UE supports", and imsd can install three ciphers: an offer of one
could not say so.
EALG is now a comma-separated list, offered in the order written: one
mechanism per cipher in a single Security-Client, each with the
REGISTER's SPIs and ports (7.2 NOTE 1, NOTE 2). The default stays
aes-cbc alone. A name imsd cannot install is ignored with a journal
line where it used to go out as written, `3des` is offered as
des-ede3-cbc, RFC 3329's name for it, a setting that names no cipher
gives the default, and the bring-up logs what is offered (`ESP ciphers
offered: …`).
An offer of more than one cipher needs a further rule for the 401's
list. O2 UK's P-CSCF lists six mechanisms and puts the SPIs on one: the
sha1 entry without a cipher for a null offer, the sha1 entry with
aes-cbc for an aes-cbc offer (2026-09-17, 2026-09-20). The second has
the higher q, so with both ciphers offered the highest-q offered entry
and the entry carrying the SPIs can be two entries. The entry carrying
the SPIs is therefore taken when it is the only such entry and one of
the offer; otherwise the highest-q offered entry, as before. With one
cipher offered the two name the same algorithms; they can differ in mod
or prot, and there the marked entry's are now taken, where 1.2.1 took
the highest-q entry's.
Tested: tests/Ipsec (the setting; the selection against two offered
ciphers) and tests/Messages (a Security-Client of two mechanisms); the
daemon of this commit against a stand-in P-CSCF on the workstation, with
one cipher offered and with two.
2026-11-23 10:08:04 +01:00
Context two = c ;
two . ealgOffer = { " aes-cbc " , " null " } ;
std : : string r1 = BuildRegisterInitial ( two , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 7449812u , 176466735u ) ;
Check ( r1 . contains ( " \r \n Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062 \r \n " ) , " two ciphers offered: two mechanisms in one Security-Client " ) ;
std : : string auth = AuthAka ( two , " NONCEXYZ " , " CNONCE0123456789 " , " RESP0000 " ) ;
std : : string r2 = BuildRegisterProtected ( two , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 2 , 7449812u , 176466735u , auth ) ;
Check ( r2 . contains ( " \r \n Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062 \r \n " ) , " the protected REGISTER repeats the initial one's Security-Client (TS 33.203 Annex H) " ) ;
std : : string r3 = BuildRegisterProtected ( two , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 3 , 55555u , 66666u , auth , 45063 ) ;
Check ( r3 . contains ( " \r \n Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=aes-cbc; spi-c=55555; spi-s=66666; port-c=45063; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=55555; spi-s=66666; port-c=45063; port-s=45062 \r \n " ) , " a reregistration's new SPIs and client port in every mechanism " ) ;
two . ealgOffer = { " null " , " des-ede3-cbc " } ;
std : : string r4 = BuildRegisterInitial ( two , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 7449812u , 176466735u ) ;
Check ( r4 . contains ( " \r \n Security-Client: ipsec-3gpp; alg=hmac-sha-1-96; ealg=null; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062, ipsec-3gpp; alg=hmac-sha-1-96; ealg=des-ede3-cbc; spi-c=7449812; spi-s=176466735; port-c=45061; port-s=45062 \r \n " ) , " the list's order is the header's order " ) ;
}
sip: UDP for the protected leg, TCP first with a fallback
imsd's protected leg (the second REGISTER and everything after it) was
TCP only. A P-CSCF that never answers the TCP connect on its protected
server port (O2 UK: the SYNs leave ESP-protected, nothing comes back)
left the unit looping with no way forward, although the SAs, the
listener sockets and the firewall rule already covered UDP.
The client flow now carries a transport. UDP binds the same protected
client port and connect()s the datagram socket to the P-CSCF's protected
server port, so the kernel delivers that peer's datagrams to it ahead of
the unconnected listener on the same port. One datagram is one message
(RFC 3261 18.3: a Content-Length that fits truncates, one that does not
fit discards the datagram, none means the rest of the datagram); a
receive error is logged and marks the flow dead; a message over the
single-packet ESP budget at the ims PDN's MTU is logged once per flow,
since the kernel fragments it and a P-CSCF may drop the fragments.
Every protected request's Via follows the transport; the challenge
stays UDP.
SIP_TRANSPORT selects the policy: auto (default) registers over TCP and,
after two unanswered connects, registers again over UDP (new challenge,
new SA pair) - but only on a phone whose state file does not record a
successful TCP registration, so an outage on a TCP carrier never becomes
a second initial REGISTER; a phone whose last registration was UDP goes
straight to UDP. tcp and udp force one. A connect refused locally
(EADDRNOTAVAIL: the 4-tuple still in TIME_WAIT from the previous flow)
keeps retrying and is not counted as silence; SO_ERROR results are
logged by name. The registration's transport is persisted so a warm
resume reconnects the same way, and the resume gives up after two
silences.
Two things the change exposed and fixes: an engine-fatal event exited 0
("exiting for systemd restart" with Restart=on-failure never firing),
and a fresh registration refused with the P-CSCF's fresh-SA throttle
(Security-Server spi-s=0) must not be retried by a 120-s restart loop,
since every attempt re-arms the ~20-min window - it is now waited out
in-process, growing on repeats, with D-Bus commands still served. A
resume the network refuses falls through to a fresh registration. A
retransmitted 200 OK to our INVITE is ACKed again and no longer starts
a second media leg.
Verified on KPN: resume over TCP after a binary swap, MT and MO calls
with media both ways, the throttle deferral and its self-recovery, the
UDP client path up to KPN dropping the datagram. The UDP success path
is a field test; SIP retransmission timers over UDP are not in this
change.
2026-09-17 14:58:46 +02:00
// ---- protected-leg transport follows Context.transport (SIP_TRANSPORT=udp,
// or the auto fallback): every protected request's Via says UDP, the
// challenge's Via stays UDP either way, and TCP is the untouched default.
{
Context cu = c ;
cu . transport = " UDP " ;
std : : string auth = AuthAka ( cu , " NONCEXYZ " , " CNONCE0123456789 " , " RESP0000 " ) ;
std : : string r2u = BuildRegisterProtected ( cu , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 2 , 7449812u , 176466735u , auth ) ;
Check ( r2u . contains ( " \r \n Via: SIP/2.0/UDP [2001:db8::db43]:45062;branch=z9hG4bKREGBRANCH123456;rport \r \n " ) , " protected REGISTER Via follows transport=UDP " ) ;
Check ( ! r2u . contains ( " SIP/2.0/TCP " ) , " no TCP token left in a UDP protected REGISTER " ) ;
Check ( BuildSubscribeReg ( cu , " SUBCALLID@2001:db8::db43 " , " STAG1234 " , " SUBBRANCH0000000000 " ) . contains ( " \r \n Via: SIP/2.0/UDP [2001:db8::db43]:45062; " ) , " SUBSCRIBE Via follows transport=UDP " ) ;
Check ( BuildRegisterInitial ( cu , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 7449812u , 176466735u ) . contains ( " \r \n Via: SIP/2.0/UDP [2001:db8::db43]:5060; " ) , " initial REGISTER Via is UDP regardless " ) ;
Check ( BuildRegisterProtected ( c , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 2 , 7449812u , 176466735u , auth ) . contains ( " \r \n Via: SIP/2.0/TCP [2001:db8::db43]:45062; " ) , " default transport stays TCP " ) ;
}
aka: the ISIM application of a card, its identity files and the identity they hold
An ISIM holds the parameters a registration starts from (TS 24.229
5.1.1.1A): the private identity in EF_IMPI, the home domain in
EF_DOMAIN and public identities in EF_IMPU (TS 31.103 4.2.2 to 4.2.4),
of which the first record's is the one to register (GSMA IR.92 2.2.1).
TS 23.003 has a UE derive its home domain and a temporary public
identity from the IMSI "if there is no ISIM application" (13.2, 13.4B);
those derived names are the only ones imsd has. This is the part of
reading an ISIM that has no I/O; the daemon does not use it yet.
ParseIsimApp finds the ISIM application of one slot in qmicli's card
status, in whatever state the modem lists it. On the two cards with an
ISIM that a read-only probe read (a US card, 2026-11-23, and a
Norwegian one, 2026-11-21) the Fairphone 6's modem lists the ISIM
`detected` beside the `ready` USIM, and on both a logical channel
opened on it and its identity files were read.
The commands are the ones that probe sent: SELECT by file identifier,
READ BINARY, READ RECORD (TS 102 221 11.1.1, 11.1.3, 11.1.5).
ApduAnswer is a card's answer, '90 00', '91 XX' and '92 XX' its normal
endings (10.2.1.1), and ApduFollowUp the command it asks for next when
the answer brought no data: GET RESPONSE for '61 XX', a command without
data again with the length named for '6C XX' (7.3.1.1.5); an answer
with data is taken for the command's last. HoldsRead takes a read's
data under the warning '62 82' as well (10.2.1.3). ParseIsimValue takes
the value of the data object with tag '80' that a file begins with,
cuts 'FF' bytes from the end of its length as the probe does, and gives
none for a value with a blank or a byte that is not printable ASCII.
IsimIdentity checks the three values before they replace the derived
names: they are written into a quoted string and between the angle
brackets of SIP header fields. It refuses a value with a quote, a
backslash, an angle bracket, a blank, a line break or another byte
outside printable ASCII, a private or a public identity of more than
255 characters, a private identity without an @ between two parts, a
domain that is not labels of letters, digits and inner hyphens, 63
characters a label and 253 in all at most, and a public identity that
does not begin sip: or is that alone.
The two readers of the card status share how they read a line;
ParseCardStatus gives what it gave.
Tested: tests/Aka (the application out of a card status with the US
card's two applications, in either order, and out of a second slot with
the Norwegian card's AID; the commands, the answers and their
follow-ups, the values, the identity and what is refused) and
tests/Messages (the two REGISTERs of an ISIM's identity against the
pinned ones).
2026-11-23 19:32:58 +01:00
// ---- the REGISTERs of an identity an ISIM holds (imsd::aka::IsimIdentity):
// every name is the card's — the Request-URI the home domain's, From and
// To the public identity, the digest's username the private identity,
// its realm the home domain; the rest of the message is the one pinned
// above.
{
Context ci = c ;
ci . id = * imsd : : aka : : IsimIdentity ( c . id , " 001010123456789@private.example.net " , " one.example.net " , " sip:001010123456789@one.example.net " ) ;
std : : string want1 ( Reg1 ) ;
auto swap = [ ] ( std : : string & s , std : : string_view from , std : : string_view to ) {
int n = 0 ;
for ( std : : size_t at = s . find ( from ) ; at ! = std : : string : : npos ; at = s . find ( from , at + to . size ( ) ) ) { s . replace ( at , from . size ( ) , to ) ; n + + ; }
return n ;
} ;
Check ( swap ( want1 , " REGISTER sip:ims.mnc001.mcc001.3gppnetwork.org SIP/2.0 " , " REGISTER sip:one.example.net SIP/2.0 " ) = = 1 , " the pinned REGISTER has one Request-URI " ) ;
Check ( swap ( want1 , " <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org> " , " <sip:001010123456789@one.example.net> " ) = = 2 , " the pinned REGISTER has the public identity in From and To " ) ;
Check ( swap ( want1 , " username= \" 001010123456789@ims.mnc001.mcc001.3gppnetwork.org \" , realm= \" ims.mnc001.mcc001.3gppnetwork.org \" , uri= \" sip:ims.mnc001.mcc001.3gppnetwork.org \" " ,
" username= \" 001010123456789@private.example.net \" , realm= \" one.example.net \" , uri= \" sip:one.example.net \" " ) = = 1 , " the pinned REGISTER has one digest " ) ;
Check ( ! want1 . contains ( " 3gppnetwork.org " ) , " no derived name is left in the expected REGISTER " ) ;
Eq ( BuildRegisterInitial ( ci , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 7449812u , 176466735u ) , want1 , " unprotected REGISTER of an ISIM's identity " ) ;
std : : string want2 ( Reg2 ) ;
swap ( want2 , " REGISTER sip:ims.mnc001.mcc001.3gppnetwork.org SIP/2.0 " , " REGISTER sip:one.example.net SIP/2.0 " ) ;
swap ( want2 , " <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org> " , " <sip:001010123456789@one.example.net> " ) ;
swap ( want2 , " username= \" 001010123456789@ims.mnc001.mcc001.3gppnetwork.org \" , realm= \" ims.mnc001.mcc001.3gppnetwork.org \" , uri= \" sip:ims.mnc001.mcc001.3gppnetwork.org \" " ,
" username= \" 001010123456789@private.example.net \" , realm= \" one.example.net \" , uri= \" sip:one.example.net \" " ) ;
Check ( ! want2 . contains ( " 3gppnetwork.org " ) , " no derived name is left in the expected protected REGISTER " ) ;
std : : string auth = AuthAka ( ci , " NONCEXYZ " , " CNONCE0123456789 " , " RESP0000 " ) ;
Eq ( BuildRegisterProtected ( ci , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 2 , 7449812u , 176466735u , auth ) , want2 , " protected REGISTER of an ISIM's identity " ) ;
// the Contact keeps the binding's own user-part: the IMSI here, as in
// the pinned message (an old state file's shape)
Check ( BuildRegisterInitial ( ci , " REGCALLID@2001:db8::db43 " , " FTAG5678 " , " REGBRANCH123456 " , 7449812u , 176466735u ) . contains ( " \r \n Contact: <sip:001010123456789@[2001:db8::db43]:5060> " ) , " the Contact is not the public identity " ) ;
// RuriFor with a home domain that is not a derived one
Eq ( RuriFor ( " +31612345678 " , ci . id . domain ) , " sip:+31612345678@one.example.net;user=phone " , " RuriFor puts a number at the domain it is given, an ISIM's here " ) ;
}
2026-07-22 22:53:28 +02:00
// ---- RuriFor variants
Eq ( RuriFor ( " 1233 " , c . id . domain ) , " sip:1233;phone-context=ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org;user=phone " , " ruri short code " ) ;
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 " ) ;
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.
2026-10-16 23:13:39 +02:00
// ---- 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 " ) ;
2026-07-22 22:53:28 +02:00
// ---- SUBSCRIBE (reg event, RFC 3680) — no Python counterpart; pinned
// against the shape of the other protected in-flow requests
Eq ( BuildSubscribeReg ( c , " SUBCALLID@2001:db8::db43 " , " STAG1234 " , " SUBBRANCH0000000000 " ) , Subscribe , " reg-event SUBSCRIBE (no aor: temporary IMPU fallback) " ) ;
{
Context c2 = MakeCtx ( ) ;
c2 . aor = " sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org " ;
std : : string sub = BuildSubscribeReg ( c2 , " SUBCALLID@2001:db8::db43 " , " STAG1234 " , " SUBBRANCH0000000000 " ) ;
Check ( sub . starts_with ( " SUBSCRIBE sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org SIP/2.0 \r \n " ) , " reg-event SUBSCRIBE aor R-URI " ) ;
Check ( sub . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org>;tag=STAG1234 \r \n " ) , " reg-event SUBSCRIBE aor From " ) ;
Check ( sub . contains ( " \r \n To: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org> \r \n " ) , " reg-event SUBSCRIBE aor To " ) ;
}
{
// no identity learned yet: P-Preferred-Identity is omitted, not "<>"
Context c3 = MakeCtx ( ) ;
c3 . ppi . clear ( ) ;
Check ( ! BuildSubscribeReg ( c3 , " SUBCALLID@2001:db8::db43 " , " STAG1234 " , " SUBBRANCH0000000000 " ) . contains ( " P-Preferred-Identity " ) , " empty ppi omits P-Preferred-Identity in SUBSCRIBE " ) ;
Dialog d ;
d . ruri = RuriFor ( " 1233 " , c3 . id . domain ) ;
d . callid = " CID@x " ; d . itag = " it " ; d . invBranch = " ib " ;
Check ( ! BuildInvite ( c3 , d , " v=0 \r \n " , false ) . contains ( " P-Preferred-Identity " ) , " empty ppi omits P-Preferred-Identity in INVITE " ) ;
}
// ---- INVITE, byte-exact vs Python
{
Dialog d ;
d . ruri = RuriFor ( " 1233 " , c . id . domain ) ;
d . callid = " CALLID789@2001:db8::db43 " ;
d . itag = " ITAG123456 " ;
d . invBranch = " INVBRANCH0000000000 " ;
Eq ( BuildInvite ( c , d , Sdp , /*precond=*/ false ) , Invite , " INVITE + AMR-WB SDP offer " ) ;
}
messages: sign our requests with the public identity, not the IMSI IMPU
Every request we originated — INVITE, CANCEL, both ACKs, in-dialog BYE and
friends — put the IMSI-derived temporary IMPU in From. 3GPP allows that
identity in REGISTER only; the same lesson was learned for the reg-event
SUBSCRIBE (480) and never carried to calls. Most P-CSCFs overwrite From
and hid it; a Telia node did not, and a reporter's IMSI appeared on the
callee's screen (field report 2026-09-01). A strict P-CSCF may reject the
INVITE outright.
CallerId(): the registered sip: public identity (P-Associated-URI), else
the tel: one, and the temporary IMPU only before either is learned. One
helper feeds all five builders, so a dialog's From never drifts. As UAS the
dialog's local URI is the INVITE's To (RFC 3261 12.2.1.1), stored on the
Dialog, so an incoming call's BYE is signed the way the network addressed
us. The identity is persisted in the state file and restored on warm
resume, so a call placed before the refresh 200 re-learns it cannot fall
back. DUMP_SIP now also writes the last outgoing INVITE
(imsd-invite-out.raw): the one request a field log could never show.
The byte-pinned INVITE fixture moves to the tel: identity its test context
knows; new scenarios cover the sip: identity, the tel: fallback, the
pre-learning case and the UAS BYE.
Bench-verified on KPN 2026-09-08: outgoing INVITE From is the registered
sip: identity, call accepted and carried; caller ID at the far end
unchanged.
2026-09-08 20:37:47 +02:00
// ---- From carries our PUBLIC identity, never the IMSI-derived temporary
// IMPU once anything better is known (ledger F1: a Telia P-CSCF showed
// the reporter's IMSI to the callee). aor > tel: ppi > temp IMPU; the
// same URI on every request of the dialog; a UAS signs with the
// INVITE's To.
{
Dialog d ;
d . ruri = RuriFor ( " 1233 " , c . id . domain ) ;
d . callid = " CID@x " ; d . itag = " it " ; d . invBranch = " ib " ;
Context c2 = MakeCtx ( ) ;
c2 . aor = " sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org " ;
std : : string inv = BuildInvite ( c2 , d , " v=0 \r \n " , false ) ;
Check ( inv . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org>;tag=it \r \n " ) , " INVITE From is the registered sip: identity when known " ) ;
Check ( ! inv . contains ( " 001010123456789@ims " ) , " INVITE never carries the IMSI IMPU once the aor is known " ) ;
Check ( inv . contains ( " \r \n P-Preferred-Identity: <tel:+31611111111> \r \n " ) , " P-Preferred-Identity unchanged " ) ;
Check ( BuildCancel ( c2 , d ) . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org>;tag=it \r \n " ) , " CANCEL From follows " ) ;
Check ( BuildAckNon2xx ( c2 , d , " <sip:x@y>;tag=r " ) . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org>;tag=it \r \n " ) , " non-2xx ACK From follows " ) ;
d . dialogTo = " <sip:x@y>;tag=r " ; d . remoteTarget = " sip:x@[2001:db8::9]:5060 " ;
Check ( BuildAck2xx ( c2 , d , " ab " , " <sip:x@y>;tag=r " ) . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org>;tag=it \r \n " ) , " 2xx ACK From follows " ) ;
Check ( BuildInDialog ( c2 , d , " BYE " , 2 , " bb " ) . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org>;tag=it \r \n " ) , " BYE From follows " ) ;
// only tel: known (pre-aor): the tel: identity, as the pinned INVITE shows
Check ( BuildInvite ( c , d , " v=0 \r \n " , false ) . contains ( " \r \n From: <tel:+31611111111>;tag=it \r \n " ) , " INVITE From falls back to the tel: identity " ) ;
// nothing learned yet: the temporary IMPU is all there is
Context c4 = MakeCtx ( ) ;
c4 . ppi . clear ( ) ;
Check ( BuildInvite ( c4 , d , " v=0 \r \n " , false ) . contains ( " \r \n From: <sip:001010123456789@ims.mnc001.mcc001.3gppnetwork.org>;tag=it \r \n " ) , " INVITE From is the temporary IMPU only before any identity is learned " ) ;
// UAS: the dialog's local URI is the INVITE's To, whatever we registered as
Dialog du = d ;
du . localUri = " sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org;user=phone " ;
Check ( BuildInDialog ( c4 , du , " BYE " , 1 , " cc " ) . contains ( " \r \n From: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org;user=phone>;tag=it \r \n " ) , " UAS BYE From is the INVITE's To URI " ) ;
}
Emergency calling, stage 1: urn:service:sos with a digits fallback
Classify 112/911 (plus EMERGENCY_NUMBERS) at Dial. A classified call
INVITEs urn:service:sos over the existing registration and, on any
non-2xx final not caused by the user hanging up (including the
setup-timeout CANCEL paths), retries once as a plain INVITE of the
dialled digits — the pre-0.3.0 behavior, so classification can never
place a call worse than the status quo. The reverse edge: a 380
Alternative Service whose body carries the emergency indication
upgrades an unclassified call to the sos URN; a bare 380 stays an
error, since promoting an arbitrary redirect would put a
non-emergency call through to a PSAP. An emergency dial preempts an
in-progress call, and an answer racing a deadline-initiated CANCEL is
taken instead of BYE'd (user-initiated CANCEL races still BYE).
No emergency registration, no emergency PDN, no CS fallback, no
SIM-less calling, no AML — and no carrier has confirmed the sos path
end-to-end. The README warning states exactly that.
Assisted-by: Claude:claude-fable-5
2026-08-01 22:29:12 +02:00
// ---- emergency INVITE: byte-identical to the plain INVITE except the
// Request-URI and To carry the service URN (RFC 5031 / TS 24.229 5.1.6)
{
Dialog d ;
d . ruri = std : : string ( SosUrn ) ;
d . callid = " CALLID789@2001:db8::db43 " ;
d . itag = " ITAG123456 " ;
d . invBranch = " INVBRANCH0000000000 " ;
std : : string want ( Invite ) ;
auto swap = [ & ] ( std : : string_view from , std : : string_view to ) {
std : : size_t at = want . find ( from ) ;
Check ( at ! = std : : string : : npos , " sos pin template line present " ) ;
if ( at ! = std : : string : : npos ) want . replace ( at , from . size ( ) , to ) ;
} ;
swap ( " INVITE sip:1233;phone-context=ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org;user=phone SIP/2.0 \r \n " ,
" INVITE urn:service:sos SIP/2.0 \r \n " ) ;
swap ( " To: <sip:1233;phone-context=ims.mnc001.mcc001.3gppnetwork.org@ims.mnc001.mcc001.3gppnetwork.org;user=phone> \r \n " ,
" To: <urn:service:sos> \r \n " ) ;
Eq ( BuildInvite ( c , d , Sdp , /*precond=*/ false ) , want , " emergency INVITE (urn:service:sos) " ) ;
}
// ---- IsEmergency classification
Check ( IsEmergency ( " 112 " ) , " 112 is emergency " ) ;
Check ( IsEmergency ( " 911 " ) , " 911 is emergency " ) ;
Check ( IsEmergency ( " 1 1 2 " ) , " space separators stripped " ) ;
Check ( IsEmergency ( " 1-1-2 " ) , " dash separators stripped " ) ;
Check ( ! IsEmergency ( " 0112 " ) , " prefixed 112 is not emergency " ) ;
Check ( ! IsEmergency ( " 1120 " ) , " suffixed 112 is not emergency " ) ;
Check ( ! IsEmergency ( " +31612345678 " ) , " E.164 number is not emergency " ) ;
Check ( ! IsEmergency ( " " ) , " empty string is not emergency " ) ;
Check ( ! IsEmergency ( " 199 " ) , " extra number not classified by default " ) ;
{
std : : vector < std : : string > extra = { " 199 " , " 088 " } ;
Check ( IsEmergency ( " 199 " , extra ) , " EMERGENCY_NUMBERS extra matches " ) ;
Check ( IsEmergency ( " 088 " , extra ) , " second extra matches " ) ;
Check ( ! IsEmergency ( " 19 " , extra ) , " extra requires exact match " ) ;
}
2026-07-22 22:53:28 +02:00
// ---- in-dialog PRACK/BYE + ACK + CANCEL + 200 (structural checks)
{
Dialog d ;
d . ruri = RuriFor ( " 1233 " , c . id . domain ) ;
d . callid = " CID@x " ;
d . itag = " it " ;
d . invBranch = " ib " ;
d . dialogTo = " <sip:1233@x>;tag=remote99 " ;
d . remoteTarget = " sip:termgw@2001:db8::9 " ;
d . recordRoute = { " <sip:rr1;lr> " , " <sip:rr2;lr> " } ;
// dialog route = reversed record-route
Check ( DialogRoute ( c , d ) = = " <sip:rr2;lr>, <sip:rr1;lr> " , " dialog route reversed " ) ;
std : : array < std : : string , 1 > rack = { " RAck: 42 1 INVITE " } ;
std : : string prack = BuildInDialog ( c , d , " PRACK " , 2 , " prbranch " , rack ) ;
Check ( prack . starts_with ( " PRACK sip:termgw@2001:db8::9 SIP/2.0 \r \n " ) , " PRACK request-URI = remote target " ) ;
Check ( prack . contains ( " Route: <sip:rr2;lr>, <sip:rr1;lr> \r \n " ) , " PRACK dialog route " ) ;
Check ( prack . contains ( " To: <sip:1233@x>;tag=remote99 \r \n " ) , " PRACK To has remote tag " ) ;
Check ( prack . contains ( " CSeq: 2 PRACK \r \n " ) , " PRACK CSeq " ) ;
Check ( prack . contains ( " RAck: 42 1 INVITE \r \n " ) , " PRACK RAck " ) ;
Check ( prack . ends_with ( " Content-Length: 0 \r \n \r \n " ) , " PRACK empty body " ) ;
std : : string bye = BuildInDialog ( c , d , " BYE " , 3 , " byebranch " ) ;
Check ( bye . starts_with ( " BYE sip:termgw@2001:db8::9 SIP/2.0 \r \n " ) , " BYE line " ) ;
Check ( bye . contains ( " CSeq: 3 BYE \r \n " ) , " BYE CSeq " ) ;
std : : string ack = BuildAck2xx ( c , d , " ackbranch " , " <ignored> " ) ;
Check ( ack . starts_with ( " ACK sip:termgw@2001:db8::9 SIP/2.0 \r \n " ) , " ACK 2xx target " ) ;
Check ( ack . contains ( " To: <sip:1233@x>;tag=remote99 \r \n " ) , " ACK 2xx dialog To " ) ;
Check ( ack . contains ( " CSeq: 1 ACK \r \n " ) , " ACK CSeq 1 " ) ;
std : : string cancel = BuildCancel ( c , d ) ;
Check ( cancel . starts_with ( std : : format ( " CANCEL {} SIP/2.0 \r \n " , d . ruri ) ) , " CANCEL request-URI = ruri " ) ;
Check ( cancel . contains ( " branch=z9hG4bKib;rport \r \n " ) , " CANCEL reuses INVITE branch " ) ;
Check ( cancel . contains ( " CSeq: 1 CANCEL \r \n " ) , " CANCEL CSeq copies INVITE " ) ;
}
{
// non-2xx ACK uses the INVITE branch + registration route + resp To
Dialog d ;
d . ruri = " sip:x@y " ; d . callid = " CID " ; d . itag = " it " ; d . invBranch = " ib9 " ;
std : : string ack = BuildAckNon2xx ( c , d , " <sip:x@y>;tag=z " ) ;
Check ( ack . contains ( " branch=z9hG4bKib9;rport \r \n " ) , " non-2xx ACK INVITE branch " ) ;
Check ( ack . contains ( std : : format ( " Route: {} \r \n " , c . route ) ) , " non-2xx ACK registration route " ) ;
Check ( ack . contains ( " To: <sip:x@y>;tag=z \r \n " ) , " non-2xx ACK echoes response To " ) ;
}
// ---- 200 OK response echoes the request's dialog headers, in order
{
std : : string req =
" BYE sip:me@here SIP/2.0 \r \n "
" Via: SIP/2.0/TCP [a::1]:45061;branch=z9hG4bKother \r \n "
" From: <sip:them@far>;tag=T \r \n "
" To: <sip:me@here>;tag=M \r \n "
" Call-ID: xyz@far \r \n "
" CSeq: 7 BYE \r \n "
" Content-Length: 0 \r \n \r \n " ;
std : : string r = BuildResponse200 ( c , req ) ;
Check ( r . starts_with ( " SIP/2.0 200 OK \r \n " ) , " 200 status line " ) ;
Check ( r . contains ( " Via: SIP/2.0/TCP [a::1]:45061;branch=z9hG4bKother \r \n " ) , " 200 echoes Via " ) ;
Check ( r . contains ( " Call-ID: xyz@far \r \n " ) , " 200 echoes Call-ID " ) ;
Check ( r . contains ( " CSeq: 7 BYE \r \n " ) , " 200 echoes CSeq " ) ;
Check ( r . ends_with ( " Content-Length: 0 \r \n \r \n " ) , " 200 empty body " ) ;
// with an SDP answer: our Contact + application/sdp appear
std : : string r2 = BuildResponse200 ( c , req , Sdp ) ;
Check ( r2 . contains ( " Content-Type: application/sdp \r \n " ) , " 200 w/ SDP content-type " ) ;
Check ( r2 . contains ( " Contact: <sip:001010123456789@[2001:db8::db43]:45062> \r \n " ) , " 200 w/ SDP includes our Contact " ) ;
Check ( r2 . ends_with ( Sdp ) , " 200 w/ SDP body " ) ;
}
// ---- s53 oracle diff: UUID Contact user-part + User-Agent ------------
{
Context c = MakeCtx ( ) ;
c . contactUser = " 029dd36c-857c-4270-ad7f-0ae70d31d50f " ;
c . userAgent = " Fairphone_Fairphone 6_FP6.QREL.16.82.0 " ;
std : : string r1 = BuildRegisterInitial ( c , " cid@h " , " ft " , " br " , 1 , 2 ) ;
Check ( r1 . contains ( " Contact: <sip:029dd36c-857c-4270-ad7f-0ae70d31d50f@ " " [2001:db8::db43]:5060> " ) , " reg1 Contact carries contactUser, not the IMSI " ) ;
Check ( ! r1 . contains ( " sip:001010123456789@[2001:db8::db43] " ) , " reg1 has no IMSI-user Contact when contactUser set " ) ;
Check ( r1 . contains ( " From: <sip:001010123456789@ims.mnc001.mcc001 " ) , " reg1 From/AOR stays the IMSI IMPU " ) ;
Check ( r1 . contains ( " User-Agent: Fairphone_Fairphone 6_FP6.QREL.16.82.0 \r \n " ) , " reg1 User-Agent emitted " ) ;
std : : string r2 = BuildRegisterProtected ( c , " cid@h " , " ft " , " br " , 2 , 1 , 2 , " Authorization: x " ) ;
Check ( r2 . contains ( " Contact: <sip:029dd36c-857c-4270-ad7f-0ae70d31d50f@ " " [2001:db8::db43]:45062> " ) , " reg2 Contact carries contactUser at the protected port " ) ;
Check ( r2 . contains ( " User-Agent: Fairphone_Fairphone 6_FP6.QREL.16.82.0 \r \n " ) , " reg2 User-Agent emitted " ) ;
c . userAgent . clear ( ) ;
Check ( ! BuildRegisterInitial ( c , " cid@h " , " ft " , " br " , 1 , 2 ) . contains ( " User-Agent: " ) , " empty userAgent omits the header " ) ;
}
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.
2026-10-16 23:13:39 +02:00
// ---- 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 \n Contact: <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 \n Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet2 \r \n From: <sip:ipsmgw@ims>;tag=N2 \r \n To: <sip:me@ims>;tag=had \r \n Call-ID: in2@net \r \n CSeq: 1 MESSAGE \r \n Content-Length: 0 \r \n \r \n " ;
Check ( BuildResponse ( c , tagged , 200 , " OK " , " unused " ) . contains ( " \r \n To: <sip:me@ims>;tag=had \r \n " ) , " a To that already carries a tag is echoed as it is " ) ;
sip, messages, imsd: the client flow answers a request datagram cut short of the Content-Length it reads with a 400, unless an ACK or a CANCEL
RFC 3261 18.3 on a datagram that ends before its message's body does:
"If the message is a response, it MUST be discarded. If the message is
a request, the element SHOULD generate a 400 (Bad Request) response."
The client flow's datagram read ignored either, where the
Content-Length it read said more bytes than came, with one line, `UDP:
datagram truncated (…) — ignored`.
CutShortRequest gives the header section of a request whose datagram
has fewer body bytes than the Content-Length read from it says, and
BuildBadRequest the 400 for it, in the shape of BuildResponse's finals:
every Via row echoed, the To tagged unless it holds a tag. The client
flow sends that 400 and its line ends `— answered 400`. Such a request
is known by its first line, which ends with the SIP version where a
status line begins with it (7.1, 7.2), in upper or in lower case: a
response cut short so, bytes that are no SIP cut short so and a
datagram without an end of headers are ignored as before. imsd keeps
nothing of a request it answers so, and follows 8.2.7 for an element
that answers without state: a To tag it adds is made from the
request's header section and a secret drawn once for the run, "in a
manner that will generate the same tag for the same request
consistently", and an ACK or a CANCEL cut short so gets no answer. The
server ports and the init port hand a datagram on as it came, as
before this commit.
Tested by tests/Sip (CutShortRequest: the header section of a request
cut short, with line breaks before it, under a compact Content-Length
and with its version in lower case; nothing for a body that is all
there, no Content-Length, no end of headers, a response in either
letter case, bytes that are no SIP, an ACK, a CANCEL) and by
tests/Messages (the 400 by its lines, its tag against Python's
hashlib; the same request, the same 400; another request or another
secret, another tag). On the workstation's loopback, SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
cut-short, at this commit and at the one before): a MESSAGE with five
body bytes of nine draws the 400, and the same one when it is sent
again, where the commit before ignored it; a response cut short the
same way, in upper and in lower case, an ACK, a CANCEL, bytes that are
no SIP and a request without an end of headers draw nothing at either
commit; the MESSAGE with its Content-Length folded is handed on at
either commit, a folded value being read by its first line; a whole
request sent after them is returned by the flow.
2026-11-22 17:35:11 +01:00
// the 400 to a request whose datagram ended before its body did (RFC 3261 18.3), built from what imsd::sip::CutShortRequest gives of it
{
std : : string cut = inbound ;
cut . replace ( cut . find ( " Content-Length: 0 " ) , 17 , " Content-Length: 12 " ) ;
const std : : string head = imsd : : sip : : CutShortRequest ( cut + " seven b " ) . value_or ( " " ) ;
Check ( ! head . empty ( ) , " an inbound MESSAGE with seven body bytes of twelve is a request cut short " ) ;
// the To tag below was made apart from imsd's MD5: Python's hashlib over the secret and the header section with its empty line, the first sixteen digits
const std : : string_view secret = " run-secret-of-16 " ;
Eq ( BuildBadRequest ( head , secret ) ,
" SIP/2.0 400 Bad Request \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=d930d9cad06d5cba \r \n "
" Call-ID: in1@net \r \n "
" CSeq: 9 MESSAGE \r \n "
" Content-Length: 0 \r \n \r \n " , " its 400 echoes every Via and tags To with sixteen hex digits of the MD5 of the secret and the header section " ) ;
Eq ( BuildBadRequest ( imsd : : sip : : CutShortRequest ( cut + " s " ) . value_or ( " " ) , secret ) , BuildBadRequest ( head , secret ) , " the same request cut short elsewhere draws the same 400, tag and all " ) ;
const auto toTag = [ ] ( std : : string_view r ) { const std : : size_t at = r . find ( " ;tag= " , r . find ( " \r \n To: " ) ) ; return r . substr ( at , r . find ( " \r \n " , at ) - at ) ; } ;
const std : : string r1 = BuildBadRequest ( head , secret ) ;
std : : string other = cut ;
other . replace ( other . find ( " in1@net " ) , 7 , " in2@net " ) ;
const std : : string r2 = BuildBadRequest ( imsd : : sip : : CutShortRequest ( other + " seven b " ) . value_or ( " " ) , secret ) ;
Check ( r2 . contains ( " \r \n Call-ID: in2@net \r \n " ) & & toTag ( r2 ) . size ( ) = = 5 + 16 & & toTag ( r2 ) ! = toTag ( r1 ) , " another request draws another tag, of sixteen digits too " ) ;
const std : : string r3 = BuildBadRequest ( head , " another-secret-16 " ) ;
Check ( toTag ( r3 ) . size ( ) = = 5 + 16 & & toTag ( r3 ) ! = toTag ( r1 ) , " and the same request under another secret draws another tag " ) ;
std : : string had = tagged ;
had . replace ( had . find ( " Content-Length: 0 " ) , 17 , " Content-Length: 12 " ) ;
Check ( BuildBadRequest ( imsd : : sip : : CutShortRequest ( had + " seven b " ) . value_or ( " " ) , secret ) . contains ( " \r \n To: <sip:me@ims>;tag=had \r \n " ) , " and the 400 echoes a To that already carries a tag as it is " ) ;
}
sip: a header is found under its compact form, and with white space before the colon
RFC 3261 7.3.3 lets a sender write a header name in its one-letter form
at any time and has every implementation accept both forms. The text
layer matched the long name only, so to every reader a header sent in
compact form was absent: the TCP framer took no body length from `l:`
and read the body as the start of the next message, a response with
`i:` matched no transaction and a request no call, the finals imsd
builds left out a Via, From, To or Call-ID that came in compact form,
and a short message whose content type came as `c:` was answered 200
and logged instead of taken (issue #2).
MatchHeaderLine now takes a line under the name it is asked for or
under that name's compact form: the ten RFC 3261 defines, and `o` for
Event (RFC 6665) and `x` for Session-Expires (RFC 4028), the two
extension headers imsd reads that have one. Every reader goes through
it — Header, Headers, the framer, DatagramMessage — so no reader
changes for it. It also takes white space between the name and the
colon, which the same grammar allows (25.1's HCOLON; 7.3.1's
"Subject : lunch") and the matcher refused. A value folded
onto further lines is still read by its first line.
One reader is changed for the rows it is now handed. ViaResponsePort
gives the port the top Via names, to which the answer to a request that
came in a datagram on a server port goes (RFC 3261 18.2.2), and none
where that Via carries `rport` (RFC 3581): the answer then goes back to
the port the request came from. It took `rport` for present where
those five letters, in lower case, stood anywhere behind the row's
first semicolon. Parameter names are case-insensitive (7.3.1), and a
request whose top Via came as `v: …;RPORT`, a row 1.2.0 passed over,
would have been answered to the port that Via names, its `rport` not
seen. ViaResponsePort now reads the Via's parameters one by one, each
to the next semicolon outside a quoted string, and `rport` is one by
its name, in any case: not those letters in a value or in a longer
name. Read so, the last parameter of the top value would run on into
the next value where a row holds a list — `rport, SIP/2.0/UDP …` is no
`rport`, and 1.2.0's search of the letters found it —, so the top value
is cut from the row first, at the first comma outside a quoted string.
With that cut a lower value's `rport` no longer counts for the top
one's, as it did in 1.2.0 for a list on one line.
Tested by the suites: tests/Sip pins each of the twelve forms through
Header, in lower and in upper case, and through Headers, `l:` through
the framer and DatagramMessage, 7.3.1's four spellings of the colon, and
ViaResponsePort's reading of a Via row, `rport` by its name and of a
list on one line the top value alone; tests/Engine drives an incoming
call from an INVITE in compact form, an outgoing call from responses in
compact form and a delivered MESSAGE in compact form through the
reducers; tests/Messages builds BuildResponse, BuildResponse200 and
BuildInviteResponse for a request in compact form. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, server-ports,
at this commit and at the one before) a request on a server port whose
Via row is a list on one line with `rport` on its lower value is
answered from the client-port socket, the way to the port its top value
names, where the commit before answered it from the socket it had come
on. On the dev phone the binary of this range's last commit answered a
MESSAGE the phone sent to its own protected server port from the
client-port socket where its Via row was such a list, and from the
socket it had come on where its one Via came as `v: …;RPORT` (the
capture's phone-form6, inject-bench.log).
2026-11-22 17:35:11 +01:00
// a request whose header names come in compact form (RFC 3261 7.3.3): the finals echo them under the long names
const std : : string compact =
" MESSAGE sip:me@[2001:db8::db43]:45062 SIP/2.0 \r \n "
" v: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1 \r \n "
" V: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0 \r \n "
" f: <sip:ipsmgw@ims>;tag=N1 \r \n "
" t: <sip:me@ims> \r \n "
" i: in1@net \r \n "
" CSeq: 9 MESSAGE \r \n "
" l: 0 \r \n \r \n " ;
Eq ( BuildResponse ( c , compact , 488 , " Not Acceptable Here " , " T488 " ) , BuildResponse ( c , inbound , 488 , " Not Acceptable Here " , " T488 " ) , " a 488 for a MESSAGE in compact form is the one for its long form " ) ;
Eq ( BuildResponse200 ( c , compact ) , BuildResponse200 ( c , inbound ) , " the hop-by-hop 200 for a request in compact form is the one for its long form " ) ;
Check ( BuildResponse200 ( c , compact ) . contains ( " \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 " ) , " with its `f:`, `t:` and `i:` under the long names " ) ;
// BuildInviteResponse reads the same names: an INVITE in compact form gets the responses of its long form
const std : : string invite =
" INVITE sip:me@[2001:db8::db43]:45062 SIP/2.0 \r \n "
" Via: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3 \r \n "
" Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3 \r \n "
" Record-Route: <sip:[2001:db8::105]:6000;lr> \r \n "
" From: <sip:caller@ims>;tag=N3 \r \n "
" To: <sip:me@ims> \r \n "
" Call-ID: call3@net \r \n "
" CSeq: 1 INVITE \r \n "
" Content-Length: 0 \r \n \r \n " ;
const std : : string compactInvite =
" INVITE sip:me@[2001:db8::db43]:45062 SIP/2.0 \r \n "
" v: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3 \r \n "
" V: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3 \r \n "
" Record-Route: <sip:[2001:db8::105]:6000;lr> \r \n "
" f: <sip:caller@ims>;tag=N3 \r \n "
" t: <sip:me@ims> \r \n "
" i: call3@net \r \n "
" CSeq: 1 INVITE \r \n "
" l: 0 \r \n \r \n " ;
const std : : string ringing = BuildInviteResponse ( c , invite , 180 , " Ringing " , " T180 " ) ;
Eq ( ringing ,
" SIP/2.0 180 Ringing \r \n "
" Via: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3 \r \n "
" Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3 \r \n "
" Record-Route: <sip:[2001:db8::105]:6000;lr> \r \n "
" From: <sip:caller@ims>;tag=N3 \r \n "
" To: <sip:me@ims>;tag=T180 \r \n "
" Call-ID: call3@net \r \n "
" CSeq: 1 INVITE \r \n "
" Contact: <sip:001010123456789@[2001:db8::db43]:45062> \r \n "
" Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER \r \n "
" Content-Length: 0 \r \n \r \n " , " a 180 for an INVITE echoes every Via and the Record-Route, tags To and carries our Contact and Allow " ) ;
Eq ( BuildInviteResponse ( c , compactInvite , 180 , " Ringing " , " T180 " ) , ringing , " a 180 for an INVITE in compact form is the one for its long form " ) ;
Eq ( BuildInviteResponse ( c , compactInvite , 486 , " Busy Here " , " T486 " ) , BuildInviteResponse ( c , invite , 486 , " Busy Here " , " T486 " ) , " and so is a 486 " ) ;
Check ( BuildInviteResponse ( c , invite , 486 , " Busy Here " , " T486 " ) . contains ( " \r \n To: <sip:me@ims>;tag=T486 \r \n " ) & & ! BuildInviteResponse ( c , invite , 486 , " Busy Here " , " T486 " ) . contains ( " Record-Route " ) , " a 486 tags To and returns no Record-Route " ) ;
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.
2026-10-16 23:13:39 +02:00
{
const std : : string body = " hi \r \n Via: 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 \n Content-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 " ) ;
}
}
2026-07-22 22:53:28 +02:00
if ( Failures = = 0 ) std : : println ( " Messages: all tests passed " ) ;
tests: every suite returns 1 for a failure, not the count
Six suites ended in `return Failures;`. `crafter-build test` takes an
exit status of 77 for a skipped suite and then succeeds, and an exit
status keeps 8 bits: 77 failing checks read as a skip, 256 as a pass.
All nine now return 1 after any failure, as Engine, Sip and Sms did.
Tested with 77 failing checks added to Ipsec, to Messages and to Sdp
in turn (the fp6 journal's 2026-11-20-issues-2-5 capture, exit-status):
before, `crafter-build test` ended `8 passed, 1 skipped` with exit
status 0; now it ends `8 passed, 1 failed` with exit status 1.
2026-11-22 17:35:11 +01:00
return Failures ! = 0 ; // not the count: `crafter-build test` takes an exit status of 77 for a skipped suite, and an exit status keeps 8 bits
2026-07-22 22:53:28 +02:00
}