2026-07-22 22:53:28 +02:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
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
// Imsd:Sip unit tests — header access (compact forms and white space before
// the colon among it), status-line parsing, granted-expires extraction,
// caller-identity extraction, the port a Via names for the response
// (ViaResponsePort), the two framers — the TCP frame buffer (split delivery,
2026-11-22 17:35:11 +01:00
// keepalive pongs, pipelined messages) and a datagram's (DatagramMessage),
sip: a received message's header section is unfolded where it is framed
RFC 3261 7.3.1 lets a header value go on over further lines, each begun
with a space or a tab: "The line break and the whitespace at the
beginning of the next line are treated as a single SP character." The
text layer read a header from one line, so of a folded value it had the
first line only. Networks fold: KPN's 401 to the unprotected REGISTER
of 2026-07-18 and Odido's of 2026-07-13 had the WWW-Authenticate over
four lines (AGENTS.md §13; imsd finds a 401's nonce by a search of the
whole message, on a continuation line as on any other). A
Security-Server folded the same way would be read by its first line
alone, without the SPIs and ports on the further lines, and the
bring-up deferred; a short message whose Content-Type stands on a
continuation line would be taken for a page-mode text, answered 200
and never shown; a folded Content-Length on a connection would frame
no body, and the body's bytes begin the next message.
Unfolded takes the folds out of a header section, and the two framers
— FrameBuffer::TryExtract for a stream, DatagramMessage for a datagram
— hand a message on with its header section unfolded and its body as
it came; the Content-Length that frames the body is read from the
unfolded section. CutShortRequest reads its Content-Length so too, and
the header section it gives for the 400 is unfolded. Every socket imsd
reads SIP from goes through one of the two framers, so each reader of
a header still reads one line. A DUMP_SIP dump of a received message
holds it as framed.
Jorijn's decision of 2026-11-21 ("Unfold now").
Tested by tests/Sip — Unfolded on 7.3.1's own example, a tab, a bare LF,
a value begun on the next line and a line break that nothing follows;
both framers on a 401 whose WWW-Authenticate goes over four lines and
whose Content-Length is folded; CutShortRequest on a request cut short
of a folded Content-Length; ViaResponsePort on a Via list folded after
its comma, through the datagram framer — and by tests/Engine, a
delivered MESSAGE whose Content-Type came folded. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, init-port,
server-ports and cut-short, at this commit and at the one before): a
stand-in's 401 with its Security-Server over three lines gives the whole
value, where the commit before deferred the bring-up for want of SPIs; a
request with its Content-Type and Content-Length on continuation lines
comes out of the server ports unfolded and framed by that length, as a
datagram and over a connection; a request cut short of a folded
Content-Length, its Via on a continuation line, is answered 400 with its
Via whole, on a server port and on the client flow, where the commit
before did not read the folded length and handed the request on as it
came; a request on a server port whose Via row is a list folded after
its comma, `rport` on its lower value, is answered from the client-port
socket, the way to the port its top value names, as at the commit
before. On the dev phone the binary of this range's last commit
registered fresh on KPN after a reboot; the 401 was not dumped, so
whether it came folded that time is not on record. A MESSAGE the phone
sent to its own protected server port, its Via row a list folded after
its comma with `rport` on the lower value, was answered from the
client-port socket (the capture's phone-form6, fresh-check.log and
inject-bench.log).
2026-11-22 17:35:12 +01:00
// with the Content-Length they read and the unfolding of a folded header
// section (Unfolded) —, a request whose datagram ends before its body does
// (CutShortRequest), a refused final's Retry-After and summary (RetryAfter,
// RefusalSummary), transaction matching (ResponseOf), our binding in a
// reg-event document (RegInfoContactState), and HeaderUri and Body. These pin
// the module's edge-case behavior; see the module header before "fixing" one.
2026-07-22 22:53:28 +02:00
import std ;
import Imsd ;
using namespace imsd : : sip ;
namespace {
int Failures = 0 ;
void Check ( bool cond , std : : string_view msg ) {
if ( ! cond ) {
std : : println ( std : : cerr , " FAIL: {} " , msg ) ;
+ + Failures ;
}
}
const std : : string Resp200 =
" SIP/2.0 200 OK \r \n "
" Via: SIP/2.0/TCP [2001:db8::1]:45061;branch=z9hG4bK1 \r \n "
" Via: SIP/2.0/TCP [2001:db8::2]:5060;branch=z9hG4bK2 \r \n "
" From: <sip:user@ims.mnc001.mcc001.3gppnetwork.org>;tag=abc \r \n "
" To: <sip:user@ims.mnc001.mcc001.3gppnetwork.org>;tag=def \r \n "
" Call-ID: 12345@host \r \n "
" CSeq: 2 REGISTER \r \n "
" contact: <sip:user@[2001:db8::1]:45061>;expires=600000 \r \n "
" P-Associated-URI: <sip:+31612345678@ims.mnc001.mcc001.3gppnetwork.org> \r \n "
" Content-Length: 0 \r \n "
" \r \n " ;
}
int main ( ) {
// ---- Status
Check ( Status ( Resp200 ) = = 200 , " Status parses 200 " ) ;
Check ( Status ( " SIP/2.0 183 Session Progress \r \n \r \n " ) = = 183 , " Status parses 183 " ) ;
Check ( ! Status ( " INVITE sip:+31@x SIP/2.0 \r \n \r \n " ) . has_value ( ) , " Status rejects a request start-line " ) ;
Check ( ! Status ( " SIP/2.0 xx3 \r \n " ) . has_value ( ) , " Status rejects non-digits " ) ;
// ---- Header / Headers
Check ( Header ( Resp200 , " Call-ID " ) = = " 12345@host " , " Header finds Call-ID " ) ;
Check ( Header ( Resp200 , " call-id " ) = = " 12345@host " , " Header is case-insensitive (query) " ) ;
Check ( Header ( Resp200 , " Contact " ) = = " <sip:user@[2001:db8::1]:45061>;expires=600000 " , " Header is case-insensitive (message) " ) ;
Check ( ! Header ( Resp200 , " Route " ) . has_value ( ) , " Header absent -> nullopt " ) ;
Check ( Header ( Resp200 , " CSeq " ) = = " 2 REGISTER " , " Header trims the value " ) ;
Check ( Headers ( Resp200 , " Via " ) . size ( ) = = 2 , " Headers returns every Via " ) ;
Check ( Headers ( Resp200 , " Via " ) [ 1 ] = = " SIP/2.0/TCP [2001:db8::2]:5060;branch=z9hG4bK2 " , " Headers keeps order " ) ;
// name must sit at line start: "P-Associated-URI" must not match "URI"
Check ( ! Header ( Resp200 , " URI " ) . has_value ( ) , " Header anchors at line start " ) ;
// bare "Name:" (only CR after the colon) matches with empty value
Check ( Header ( " X-Empty: \r \n X-Real: v \r \n \r \n " , " X-Empty " ) = = " " , " bare header matches with empty value " ) ;
2026-10-18 05:44:20 +02:00
// a header read stops at the first empty line: a body line spelling a header name is no header
const std : : string BodyVia =
" MESSAGE sip:a@b SIP/2.0 \r \n "
" Via: SIP/2.0/TCP [2001:db8::1]:5060;branch=z9hG4bK1 \r \n "
" Call-ID: c1@h \r \n "
" Content-Length: 36 \r \n "
" \r \n "
" hi \r \n Via: SIP/2.0/UDP body:5060 \r \n X-Body: here \r \n " ;
Check ( Headers ( BodyVia , " Via " ) . size ( ) = = 1 & & Header ( BodyVia , " Call-ID " ) = = " c1@h " , " Headers stops at the blank line: the body's Via is no Via " ) ;
Check ( ! Header ( BodyVia , " X-Body " ) . has_value ( ) , " Header never reads the body " ) ;
Check ( Header ( " \r \n \r \n SIP/2.0 200 OK \r \n Call-ID: k \r \n \r \n " , " Call-ID " ) = = " k " , " empty lines before the first line of text (a keepalive's CRLFs) are skipped " ) ;
2026-07-22 22:53:28 +02:00
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
// ---- compact forms (RFC 3261 7.3.3: "MUST accept both the long and short forms of each header name")
{
// RFC 3261's ten (section 20), then Event (RFC 6665 7.3) and Session-Expires (RFC 4028 4)
const std : : pair < std : : string_view , char > forms [ ] = {
{ " Call-ID " , ' i ' } , { " Contact " , ' m ' } , { " Content-Encoding " , ' e ' } , { " Content-Length " , ' l ' } , { " Content-Type " , ' c ' } , { " From " , ' f ' } ,
{ " Subject " , ' s ' } , { " Supported " , ' k ' } , { " To " , ' t ' } , { " Via " , ' v ' } , { " Event " , ' o ' } , { " Session-Expires " , ' x ' } ,
} ;
// every letter against the header names below — those imsd reads, and the three of the RFC's ten it does not: the twelve pairs match, nothing else does
{
const std : : string_view names [ ] = { " Call-ID " , " CSeq " , " To " , " From " , " Via " , " Contact " , " Record-Route " , " P-Associated-URI " , " Session-Expires " , " Require " , " P-Asserted-Identity " , " Subscription-State " , " Service-Route " , " Security-Server " , " RSeq " , " Retry-After " , " Reason " , " Path " , " In-Reply-To " , " Expires " , " Event " , " Content-Type " , " Content-Length " , " Warning " , " Server " , " Content-Encoding " , " Subject " , " Supported " } ;
int pairs = 0 ;
bool onlyThose = true ;
for ( char letter = ' a ' ; letter < = ' z ' ; letter + + )
for ( std : : string_view name : names ) {
const bool found = Header ( std : : format ( " OPTIONS sip:a@b SIP/2.0 \r \n {}: v \r \n \r \n " , letter ) , name ) . has_value ( ) ;
const bool paired = std : : ranges : : any_of ( forms , [ & ] ( const auto & form ) { return form . first = = name & & form . second = = letter ; } ) ;
if ( found ) pairs + + ;
if ( found ! = paired ) onlyThose = false ;
}
Check ( pairs = = 12 & & onlyThose , " of the letters a to z against the header names listed, the twelve pairs are compact forms and no other letter is taken for a name " ) ;
}
for ( const auto & [ name , letter ] : forms ) {
const std : : string lower = std : : format ( " OPTIONS sip:a@b SIP/2.0 \r \n {}: value-{} \r \n \r \n " , letter , letter ) ;
const std : : string upper = std : : format ( " OPTIONS sip:a@b SIP/2.0 \r \n {}: value-{} \r \n \r \n " , static_cast < char > ( letter - ' a ' + ' A ' ) , letter ) ;
const std : : string expect = std : : format ( " value-{} " , letter ) ;
Check ( Header ( lower , name ) = = expect , std : : format ( " Header: `{}:` is read as {} " , letter , name ) ) ;
Check ( Header ( upper , name ) = = expect , std : : format ( " Header: the compact form of {} in upper case is read too " , name ) ) ;
Check ( Headers ( lower , name ) = = std : : vector < std : : string_view > { expect } , std : : format ( " Headers: `{}:` is a row of {} " , letter , name ) ) ;
// no other name of the twelve takes that line
for ( const auto & [ other , otherLetter ] : forms )
if ( other ! = name ) Check ( ! Header ( lower , other ) . has_value ( ) , std : : format ( " Header: `{}:` is no {} " , letter , other ) ) ;
}
// a name with no compact form is found under its long form only
Check ( ! Header ( " NOTIFY sip:a@b SIP/2.0 \r \n r: <sip:x> \r \n s: active \r \n \r \n " , " Record-Route " ) . has_value ( ) & & ! Header ( " NOTIFY sip:a@b SIP/2.0 \r \n r: <sip:x> \r \n s: active \r \n \r \n " , " Subscription-State " ) . has_value ( ) , " Header: a name that has no compact form is not found under a letter " ) ;
// both forms in one message, in the order they stand (7.3.3: a name MAY appear in both forms)
const std : : string mixed = " MESSAGE sip:a@b SIP/2.0 \r \n Via: one \r \n v: two \r \n V: three \r \n Via: four \r \n i: c1@h \r \n \r \n " ;
Check ( Headers ( mixed , " Via " ) = = std : : vector < std : : string_view > { " one " , " two " , " three " , " four " } & & Header ( mixed , " Via " ) = = " one " & & Header ( mixed , " Call-ID " ) = = " c1@h " , " Headers: long and compact rows of one name are one list, in order " ) ;
// asked for in another case, the compact form is found all the same; a longer name that begins with a long form has none
Check ( Header ( mixed , " call-id " ) = = " c1@h " & & Header ( mixed , " CALL-ID " ) = = " c1@h " & & Headers ( mixed , " VIA " ) . size ( ) = = 4 , " Header: the name asked for in lower or upper case finds its compact form " ) ;
Check ( ! Header ( mixed , " Via-Info " ) . has_value ( ) & & ! Header ( mixed , " Call-ID-Extra " ) . has_value ( ) , " Header: a name that only begins with one that has a compact form has none " ) ;
// a longer name that begins with the letter is not the compact form
Check ( ! Header ( " MESSAGE sip:a@b SIP/2.0 \r \n Timestamp: 54 \r \n Version: 1 \r \n \r \n " , " To " ) . has_value ( ) & & ! Header ( " MESSAGE sip:a@b SIP/2.0 \r \n Timestamp: 54 \r \n Version: 1 \r \n \r \n " , " Via " ) . has_value ( ) , " Header: a name that only begins with a compact letter is not that header " ) ;
}
// ---- white space before the colon (RFC 3261 7.3.1, 25.1 HCOLON)
{
// 7.3.1's four equivalent spellings
for ( std : : string_view line : { " Subject: lunch " , " Subject : lunch " , " Subject :lunch " , " Subject: lunch " } )
Check ( Header ( std : : format ( " MESSAGE sip:a@b SIP/2.0 \r \n {} \r \n \r \n " , line ) , " Subject " ) = = " lunch " , std : : format ( " Header: `{}` reads as lunch " , line ) ) ;
Check ( Header ( " MESSAGE sip:a@b SIP/2.0 \r \n Call-ID \t : c2@h \r \n i \t : c3@h \r \n \r \n " , " Call-ID " ) = = " c2@h " & & Headers ( " MESSAGE sip:a@b SIP/2.0 \r \n Call-ID \t : c2@h \r \n i \t : c3@h \r \n \r \n " , " Call-ID " ) . size ( ) = = 2 , " Header: a tab or spaces before the colon, after the long or the compact form " ) ;
Check ( ! Header ( " MESSAGE sip:a@b SIP/2.0 \r \n To be: or not \r \n t x: y \r \n \r \n " , " To " ) . has_value ( ) , " Header: a word between the name and the colon is another field name " ) ;
Check ( Header ( " X-Empty : \r \n X-Real: v \r \n \r \n " , " X-Empty " ) = = " " & & ! Header ( " X-Last : " , " X-Last " ) . has_value ( ) , " a bare header with a space before its colon matches with an empty value; one that ends the buffer does not " ) ;
// a name, or a name and white space, that ends the text is no header — also where a colon stands in the bytes right after the text
Check ( ! Header ( " A: 1 \r \n To " , " To " ) . has_value ( ) & & ! Header ( " A: 1 \r \n To \t " , " To " ) . has_value ( ) & & ! Header ( " A: 1 \r \n t " , " To " ) . has_value ( ) , " a name that ends the text, with or without white space after it, is no header " ) ;
Check ( ! Header ( std : : string_view ( " A: 1 \r \n To:v " ) . substr ( 0 , 8 ) , " To " ) . has_value ( ) & & ! Header ( std : : string_view ( " A: 1 \r \n To \t :v " ) . substr ( 0 , 10 ) , " To " ) . has_value ( ) , " nor is it where a colon follows in the bytes past the text's end " ) ;
}
2026-07-22 22:53:28 +02:00
// ---- GrantedExpires
Check ( GrantedExpires ( Resp200 ) = = 600000 , " GrantedExpires from Contact expires= " ) ;
Check ( GrantedExpires ( " SIP/2.0 200 OK \r \n Expires: 3600 \r \n \r \n " ) = = 3600 , " GrantedExpires falls back to Expires " ) ;
Check ( ! GrantedExpires ( " SIP/2.0 200 OK \r \n Expires: abc \r \n \r \n " ) . has_value ( ) , " non-numeric Expires -> nullopt " ) ;
Check ( ! GrantedExpires ( " SIP/2.0 200 OK \r \n \r \n " ) . has_value ( ) , " no lifetime -> nullopt " ) ;
Check ( GrantedExpires ( " SIP/2.0 200 OK \r \n " " Contact: <sip:a@b> \r \n " " Contact: <sip:c@d>;expires=7200 \r \n \r \n " ) = = 7200 , " GrantedExpires scans all Contacts " ) ;
// ---- FrameBuffer
{
FrameBuffer fb ;
fb . Append ( Resp200 ) ;
auto m = fb . TryExtract ( ) ;
Check ( m = = Resp200 , " whole message extracts intact " ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " buffer empty after extract " ) ;
}
{
// split mid-header and mid-body
std : : string body = " v=0 \r \n o=- 1 1 IN IP6 ::1 \r \n " ;
std : : string msg = std : : format ( " SIP/2.0 200 OK \r \n Content-Length: {} \r \n \r \n {} " , body . size ( ) , body ) ;
FrameBuffer fb ;
fb . Append ( msg . substr ( 0 , 20 ) ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " incomplete head -> nullopt " ) ;
fb . Append ( msg . substr ( 20 , msg . size ( ) - 20 - 5 ) ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " incomplete body -> nullopt " ) ;
fb . Append ( msg . substr ( msg . size ( ) - 5 ) ) ;
Check ( fb . TryExtract ( ) = = msg , " split message reassembles " ) ;
}
{
// leading CRLF keepalive pongs are discarded (RFC 5626)
FrameBuffer fb ;
fb . Append ( " \r \n \r \n " ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " pure keepalive -> nullopt " ) ;
Check ( fb . Size ( ) = = 0 , " keepalive bytes discarded " ) ;
fb . Append ( " \r \n " ) ;
fb . Append ( Resp200 ) ;
Check ( fb . TryExtract ( ) = = Resp200 , " message after keepalive extracts " ) ;
}
{
// two pipelined messages come out one at a time, in order
std : : string first = " SIP/2.0 100 Trying \r \n Content-Length: 0 \r \n \r \n " ;
FrameBuffer fb ;
fb . Append ( first + Resp200 ) ;
Check ( fb . TryExtract ( ) = = first , " pipelined: first message " ) ;
Check ( fb . TryExtract ( ) = = Resp200 , " pipelined: second message " ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " pipelined: then empty " ) ;
}
{
// missing Content-Length counts as 0
std : : string msg = " SIP/2.0 200 OK \r \n Call-ID: x \r \n \r \n " ;
FrameBuffer fb ;
fb . Append ( std : : format ( " {}LEFTOVER " , msg ) ) ;
Check ( fb . TryExtract ( ) = = msg , " no Content-Length -> body length 0 " ) ;
Check ( fb . Size ( ) = = 8 , " trailing bytes stay buffered " ) ;
}
2026-11-22 17:35:11 +01:00
{
// the Content-Length is read as a number: zeros before it do not count, and one too long to be a length is no zero
const std : : string head = " SIP/2.0 200 OK \r \n Call-ID: x \r \n " ;
FrameBuffer fb ;
fb . Append ( head + " Content-Length: 00000000005 \r \n \r \n hello " + head + " Content-Length: 000 \r \n \r \n " + head + " Content-Length: x \r \n \r \n " ) ;
Check ( fb . TryExtract ( ) = = head + " Content-Length: 00000000005 \r \n \r \n hello " , " a Content-Length of 5 written with ten zeros before it frames a body of five bytes " ) ;
Check ( fb . TryExtract ( ) = = head + " Content-Length: 000 \r \n \r \n " & & fb . TryExtract ( ) = = head + " Content-Length: x \r \n \r \n " & & fb . Size ( ) = = 0 , " zero in three digits is zero, and a value without a digit counts as none " ) ;
fb . Append ( head + " Content-Length: 18446744073709551616 \r \n \r \n hello " ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) & & fb . Size ( ) > 0 , " a Content-Length past any number that fits is not read as zero: the body is waited for " ) ;
}
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
{
// the body's length under the compact name (RFC 3261 7.3.3), and with a space before the colon (7.3.1)
const std : : string compact = " MESSAGE sip:a@b SIP/2.0 \r \n i: c1@h \r \n l: 5 \r \n \r \n hello " ;
const std : : string spaced = " MESSAGE sip:a@b SIP/2.0 \r \n Call-ID: c2@h \r \n Content-Length : 3 \r \n \r \n abc " ;
FrameBuffer fb ;
fb . Append ( compact + spaced + Resp200 ) ;
Check ( fb . TryExtract ( ) = = compact , " `l: 5` frames a body of five bytes " ) ;
Check ( fb . TryExtract ( ) = = spaced , " `Content-Length : 3` frames a body of three " ) ;
Check ( fb . TryExtract ( ) = = Resp200 & & ! fb . TryExtract ( ) . has_value ( ) , " the message after them starts where their bodies end " ) ;
fb . Append ( compact . substr ( 0 , compact . size ( ) - 2 ) ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " a body shorter than `l:` says is waited for " ) ;
}
2026-07-22 22:53:28 +02:00
// ---- CallerId: P-Asserted-Identity preferred, From fallback
{
std : : string inv =
" INVITE sip:me SIP/2.0 \r \n "
" From: \" Someone \" <sip:+31687654321@ims.example;user=phone>;tag=f \r \n "
" P-Asserted-Identity: <tel:+31612345678> \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( CallerId ( inv ) = = " +31612345678 " , " tel: P-Asserted-Identity wins " ) ;
}
{
std : : string inv =
" INVITE sip:me SIP/2.0 \r \n "
" P-Asserted-Identity: <sip:+31612345678@ims.example;user=phone> \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( CallerId ( inv ) = = " +31612345678 " , " sip: identity yields the user part " ) ;
}
{
std : : string inv =
" INVITE sip:me SIP/2.0 \r \n "
" From: <sip:+31687654321@ims.example;user=phone>;tag=f \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( CallerId ( inv ) = = " +31687654321 " , " From fallback without P-Asserted-Identity " ) ;
}
{
std : : string inv =
" INVITE sip:me SIP/2.0 \r \n "
" From: <sip:anonymous@anonymous.invalid>;tag=f \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( CallerId ( inv ) = = " anonymous " , " anonymous caller passes through " ) ;
}
{
std : : string inv =
" INVITE sip:me SIP/2.0 \r \n "
" From: tel:+31600000001;tag=f \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( CallerId ( inv ) = = " +31600000001 " , " bare (bracketless) tel URI " ) ;
Check ( CallerId ( " INVITE sip:me SIP/2.0 \r \n Content-Length: 0 \r \n \r \n " ) . empty ( ) , " no identity headers -> empty " ) ;
}
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
// ---- the readers above under compact names
{
Check ( CallerId ( " INVITE sip:me SIP/2.0 \r \n f: <sip:+31687654321@ims.example;user=phone>;tag=f \r \n l: 0 \r \n \r \n " ) = = " +31687654321 " , " CallerId: a From that comes as `f:` " ) ;
Check ( HeaderUri ( " MESSAGE sip:me SIP/2.0 \r \n f: \" GW \" <sip:ipsmgw@ims.example>;tag=f \r \n l: 0 \r \n \r \n " , " From " ) = = " sip:ipsmgw@ims.example " , " HeaderUri: a From that comes as `f:` " ) ;
Check ( GrantedExpires ( " SIP/2.0 200 OK \r \n m: <sip:a@b>;expires=7200 \r \n \r \n " ) = = 7200 , " GrantedExpires: a Contact that comes as `m:` " ) ;
Check ( ViaResponsePort ( " UPDATE sip:me SIP/2.0 \r \n v: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKx \r \n l: 0 \r \n \r \n " ) = = 6000 , " ViaResponsePort: a Via that comes as `v:` " ) ;
}
2026-07-22 22:53:28 +02:00
// ---- ViaResponsePort: RFC 3261 18.2.2 UDP response routing
{
std : : string upd =
" UPDATE sip:me SIP/2.0 \r \n "
" Via: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKx;_aluscr_ \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ViaResponsePort ( upd ) = = 6000 , " v6 sent-by port extracted " ) ;
std : : string rp =
" UPDATE sip:me SIP/2.0 \r \n "
" Via: SIP/2.0/UDP [2001:db8::105]:6000;rport;branch=z9hG4bKx \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ! ViaResponsePort ( rp ) . has_value ( ) , " rport -> symmetric (nullopt) " ) ;
std : : string v4 =
" UPDATE sip:me SIP/2.0 \r \n "
" Via: SIP/2.0/UDP 192.0.2.1:5062;branch=z9hG4bKx \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ViaResponsePort ( v4 ) = = 5062 , " v4 sent-by port extracted " ) ;
std : : string np =
" UPDATE sip:me SIP/2.0 \r \n "
" Via: SIP/2.0/UDP [2001:db8::105];branch=z9hG4bKx \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ! ViaResponsePort ( np ) . has_value ( ) , " portless v6 sent-by -> nullopt " ) ;
Check ( ! ViaResponsePort ( " UPDATE sip:me SIP/2.0 \r \n \r \n " ) . has_value ( ) , " no Via -> nullopt " ) ;
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
// rport is a parameter by its name (RFC 3581), in any case (RFC 3261 7.3.1)
const std : : string top = " SIP/2.0/UDP [2001:db8::105]:6000 " ;
auto row = [ ] ( std : : string_view value ) { return std : : format ( " BYE sip:me SIP/2.0 \r \n Via: {} \r \n Content-Length: 0 \r \n \r \n " , value ) ; } ;
Check ( ! ViaResponsePort ( row ( top + " ;RPort;branch=z9hG4bKp " ) ) . has_value ( ) , " rport in another case " ) ;
Check ( ! ViaResponsePort ( row ( top + " ; rport ;branch=z9hG4bKp " ) ) . has_value ( ) , " rport with white space around it " ) ;
Check ( ! ViaResponsePort ( row ( top + " ;rport=6001;branch=z9hG4bKp " ) ) . has_value ( ) , " rport that came with a value " ) ;
Check ( ViaResponsePort ( row ( top + " ;branch=z9hG4bKrport;xrport;rportx=1 " ) ) = = 6000 , " those letters in a value or in a longer name are no rport " ) ;
Check ( ViaResponsePort ( row ( top + " ;x= \" a;rport;b \" ;branch=z9hG4bKp " ) ) = = 6000 , " nor between semicolons in a quoted string " ) ;
Check ( ! ViaResponsePort ( row ( top + " ;x= \" a;b \" ;rport " ) ) . has_value ( ) , " a quoted string ends at its closing quote " ) ;
Check ( ViaResponsePort ( row ( top + " ;x= \" a \\ \" ;rport;b \" " ) ) = = 6000 , " and a quoted pair in it does not close it " ) ;
Check ( ! ViaResponsePort ( row ( top + " ;x= \" a \\ \\ \" ;rport " ) ) . has_value ( ) , " the quote behind an escaped backslash does " ) ;
// a row that holds a list (RFC 3261 7.3.1): the top value alone speaks
const std : : string next = " SIP/2.0/UDP [2001:db8::9]:5060 " ;
Check ( ViaResponsePort ( row ( top + " ;branch=z9hG4bKp, " + next + " ;branch=z9hG4bKs;rport=5060 " ) ) = = 6000 , " a list on one line: the top value's port, a later value's rport not counted " ) ;
sip: a received message's header section is unfolded where it is framed
RFC 3261 7.3.1 lets a header value go on over further lines, each begun
with a space or a tab: "The line break and the whitespace at the
beginning of the next line are treated as a single SP character." The
text layer read a header from one line, so of a folded value it had the
first line only. Networks fold: KPN's 401 to the unprotected REGISTER
of 2026-07-18 and Odido's of 2026-07-13 had the WWW-Authenticate over
four lines (AGENTS.md §13; imsd finds a 401's nonce by a search of the
whole message, on a continuation line as on any other). A
Security-Server folded the same way would be read by its first line
alone, without the SPIs and ports on the further lines, and the
bring-up deferred; a short message whose Content-Type stands on a
continuation line would be taken for a page-mode text, answered 200
and never shown; a folded Content-Length on a connection would frame
no body, and the body's bytes begin the next message.
Unfolded takes the folds out of a header section, and the two framers
— FrameBuffer::TryExtract for a stream, DatagramMessage for a datagram
— hand a message on with its header section unfolded and its body as
it came; the Content-Length that frames the body is read from the
unfolded section. CutShortRequest reads its Content-Length so too, and
the header section it gives for the 400 is unfolded. Every socket imsd
reads SIP from goes through one of the two framers, so each reader of
a header still reads one line. A DUMP_SIP dump of a received message
holds it as framed.
Jorijn's decision of 2026-11-21 ("Unfold now").
Tested by tests/Sip — Unfolded on 7.3.1's own example, a tab, a bare LF,
a value begun on the next line and a line break that nothing follows;
both framers on a 401 whose WWW-Authenticate goes over four lines and
whose Content-Length is folded; CutShortRequest on a request cut short
of a folded Content-Length; ViaResponsePort on a Via list folded after
its comma, through the datagram framer — and by tests/Engine, a
delivered MESSAGE whose Content-Type came folded. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, init-port,
server-ports and cut-short, at this commit and at the one before): a
stand-in's 401 with its Security-Server over three lines gives the whole
value, where the commit before deferred the bring-up for want of SPIs; a
request with its Content-Type and Content-Length on continuation lines
comes out of the server ports unfolded and framed by that length, as a
datagram and over a connection; a request cut short of a folded
Content-Length, its Via on a continuation line, is answered 400 with its
Via whole, on a server port and on the client flow, where the commit
before did not read the folded length and handed the request on as it
came; a request on a server port whose Via row is a list folded after
its comma, `rport` on its lower value, is answered from the client-port
socket, the way to the port its top value names, as at the commit
before. On the dev phone the binary of this range's last commit
registered fresh on KPN after a reboot; the 401 was not dumped, so
whether it came folded that time is not on record. A MESSAGE the phone
sent to its own protected server port, its Via row a list folded after
its comma with `rport` on the lower value, was answered from the
client-port socket (the capture's phone-form6, fresh-check.log and
inject-bench.log).
2026-11-22 17:35:12 +01:00
auto folded = DatagramMessage ( row ( top + " ;branch=z9hG4bKp, \r \n " + next + " ;branch=z9hG4bKs;rport=5060 " ) ) ;
Check ( folded & & ViaResponsePort ( * folded ) = = 6000 , " the same list folded after its comma, as the datagram framer hands it on " ) ;
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
Check ( ! ViaResponsePort ( row ( top + " ;rport;branch=z9hG4bKp, " + next + " ;branch=z9hG4bKs " ) ) . has_value ( ) , " the top value's own rport counts " ) ;
Check ( ! ViaResponsePort ( row ( top + " ;branch=z9hG4bKp;rport, " + next + " ;branch=z9hG4bKs " ) ) . has_value ( ) , " also where it stands last before the comma " ) ;
Check ( ViaResponsePort ( row ( top + " , " + next + " ;branch=z9hG4bKs " ) ) = = 6000 , " a top value without parameters: its own port, not the next value's " ) ;
Check ( ! ViaResponsePort ( row ( top + " ;x= \" a,b \" ;rport, " + next ) ) . has_value ( ) , " a comma in a quoted string does not end the top value " ) ;
Check ( ViaResponsePort ( row ( top + " ;x= \" a,b \" ;branch=z9hG4bKp, " + next + " ;rport=5060 " ) ) = = 6000 , " the comma behind that string's closing quote does " ) ;
2026-07-22 22:53:28 +02:00
}
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
// ---- DatagramMessage (one UDP datagram = one message, RFC 3261 18.3)
{
std : : string why ;
Check ( ! DatagramMessage ( " \r \n " , & why ) . has_value ( ) & & why . empty ( ) , " CRLF-only datagram is a pong, not a message, no problem " ) ;
Check ( ! DatagramMessage ( " \r \n \r \n \r \n " ) . has_value ( ) , " CRLFCRLF-only datagram is a pong " ) ;
std : : string hdr = " SIP/2.0 200 OK \r \n Call-ID: a \r \n " ;
auto m0 = DatagramMessage ( hdr + " Content-Length: 0 \r \n \r \n " ) ;
Check ( m0 & & * m0 = = hdr + " Content-Length: 0 \r \n \r \n " , " Content-Length 0, no body: whole datagram " ) ;
auto m1 = DatagramMessage ( hdr + " Content-Length: 4 \r \n \r \n v=0 \n JUNK " ) ;
Check ( m1 & & * m1 = = hdr + " Content-Length: 4 \r \n \r \n v=0 \n " , " Content-Length that fits truncates the body " ) ;
why . clear ( ) ;
Check ( ! DatagramMessage ( hdr + " Content-Length: 40 \r \n \r \n v=0 \n " , & why ) . has_value ( ) & & why . contains ( " truncated " ) , " Content-Length larger than the datagram: discarded, problem set " ) ;
auto m2 = DatagramMessage ( " \r \n " + hdr + " \r \n v=0 \n " ) ;
Check ( m2 & & * m2 = = hdr + " \r \n v=0 \n " , " no Content-Length: body is the rest of the datagram, leading CRLF stripped " ) ;
why . clear ( ) ;
Check ( ! DatagramMessage ( " SIP/2.0 200 OK \r \n Call-ID: a \r \n " , & why ) . has_value ( ) & & why . contains ( " terminator " ) , " no header terminator: discarded, problem set " ) ;
auto m3 = DatagramMessage ( hdr + " X-Note: Content-Length: 99 \r \n \r \n v=0 \n " ) ;
Check ( m3 & & * m3 = = hdr + " X-Note: Content-Length: 99 \r \n \r \n v=0 \n " , " a header name inside a value does not match " ) ;
auto m4 = DatagramMessage ( hdr + " Content-Length: 99999999999999999999 \r \n \r \n v=0 \n " , & why ) ;
Check ( ! m4 . has_value ( ) , " absurd Content-Length: discarded, no overflow " ) ;
2026-11-22 17:35:11 +01:00
why . clear ( ) ;
Check ( ! DatagramMessage ( hdr + " Content-Length: 18446744073709551616 \r \n \r \n v=0 \n " , & why ) . has_value ( ) & & why . contains ( " truncated " ) , " nor is one that would wrap to zero: discarded as truncated " ) ;
Check ( DatagramMessage ( hdr + " Content-Length: 00000000004 \r \n \r \n v=0 \n JUNK " ) = = hdr + " Content-Length: 00000000004 \r \n \r \n v=0 \n " , " a Content-Length of 4 written with ten zeros before it cuts the body at four bytes " ) ;
Check ( DatagramMessage ( hdr + " Content-Length: x4 \r \n \r \n v=0 \n JUNK " ) = = hdr + " Content-Length: x4 \r \n \r \n v=0 \n JUNK " , " a Content-Length that begins with no digit: the body is the rest of the datagram " ) ;
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
auto m5 = DatagramMessage ( hdr + " l: 4 \r \n \r \n v=0 \n JUNK " ) ;
Check ( m5 & & * m5 = = hdr + " l: 4 \r \n \r \n v=0 \n " , " `l:` that fits truncates the body like Content-Length " ) ;
why . clear ( ) ;
Check ( ! DatagramMessage ( hdr + " L : 40 \r \n \r \n v=0 \n " , & why ) . has_value ( ) & & why . contains ( " truncated " ) , " `L :` larger than the datagram: discarded, problem set " ) ;
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
}
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
// ---- CutShortRequest (a request whose datagram ends before its body does: the header section its 400 is built from, RFC 3261 18.3)
{
const std : : string req = " MESSAGE sip:a@example.net SIP/2.0 \r \n Via: SIP/2.0/UDP [2001:db8::2]:6000;branch=z9hG4bK1 \r \n Call-ID: c \r \n CSeq: 7 MESSAGE \r \n " ;
Check ( CutShortRequest ( req + " Content-Length: 9 \r \n \r \n hello " ) = = req + " Content-Length: 9 \r \n \r \n " , " a request with five body bytes of nine: its header section and the empty line, no body " ) ;
Check ( CutShortRequest ( " \r \n \r \n " + req + " l: 6 \r \n \r \n hello " ) = = req + " l: 6 \r \n \r \n " , " line breaks before it are no part of it, and the length is found under its compact form " ) ;
Check ( CutShortRequest ( req + " Content-Length: 0006 \r \n \r \n hello " ) . has_value ( ) , " zeros before the length are no part of the number " ) ;
Check ( ! CutShortRequest ( req + " Content-Length: 5 \r \n \r \n hello " ) & & ! CutShortRequest ( req + " Content-Length: 4 \r \n \r \n hello " ) , " a body that is all there, with or without bytes after it: none " ) ;
Check ( ! CutShortRequest ( req + " \r \n hello " ) & & ! CutShortRequest ( req + " Content-Length: x9 \r \n \r \n hello " ) , " no Content-Length, or one that begins with no digit: none (the body is the rest of the datagram) " ) ;
Check ( ! CutShortRequest ( req + " Content-Length: 9 \r \n " ) & & ! CutShortRequest ( " \r \n \r \n " ) & & ! CutShortRequest ( " " ) , " no end of headers, line breaks alone, no bytes: none " ) ;
Check ( ! CutShortRequest ( " SIP/2.0 200 OK \r \n Call-ID: c \r \n Content-Length: 9 \r \n \r \n hello " ) & & ! CutShortRequest ( " sip/2.0 200 OK \r \n Call-ID: c \r \n Content-Length: 9 \r \n \r \n hello " ) , " a response cut short: none (it is discarded), its version in upper or in lower case " ) ;
Check ( ! CutShortRequest ( " SIP/2.0 200 SIP/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) & & ! CutShortRequest ( " sip/2.0 200 sip/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) , " nor is a status line a request's first line because its phrase ends like a version " ) ;
Check ( CutShortRequest ( " MESSAGE sip:a@example.net sip/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) . has_value ( ) , " a request whose version is in lower case is a request " ) ;
Check ( ! CutShortRequest ( " MESSAGE sip:a@example.net SIP/2.01 \r \n Content-Length: 9 \r \n \r \n hello " ) , " a first line that ends in more than the version: none " ) ;
Check ( ! CutShortRequest ( " MESSAGE sip:a@example.net SIP/3.0 \r \n Content-Length: 9 \r \n \r \n hello " ) & & ! CutShortRequest ( " MESSAGE sip:a@example.net bar1234 \r \n Content-Length: 9 \r \n \r \n hello " ) , " nor one that ends in another version, or in seven other bytes " ) ;
Check ( ! CutShortRequest ( " l:9 \r \n \r \n " ) & & ! CutShortRequest ( " HTTP/1.1 200 OK \r \n Content-Length: 9 \r \n \r \n hello " ) , " bytes that are no SIP, a Content-Length larger than what follows among them: none " ) ;
Check ( ! CutShortRequest ( " ACK sip:a@example.net SIP/2.0 \r \n Call-ID: c \r \n Content-Length: 9 \r \n \r \n hello " ) , " an ACK cut short: none " ) ;
Check ( ! CutShortRequest ( " CANCEL sip:a@example.net SIP/2.0 \r \n Call-ID: c \r \n Content-Length: 9 \r \n \r \n hello " ) , " a CANCEL cut short: none " ) ;
Check ( CutShortRequest ( " ACKNOWLEDGE sip:a@example.net SIP/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) . has_value ( ) & & CutShortRequest ( " ack sip:a@example.net SIP/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) . has_value ( ) , " a method that begins with ACK, or spells it in other letter case, is no ACK " ) ;
Check ( CutShortRequest ( " CANCELLED sip:a@example.net SIP/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) . has_value ( ) & & CutShortRequest ( " cancel sip:a@example.net SIP/2.0 \r \n Content-Length: 9 \r \n \r \n hello " ) . has_value ( ) , " nor is one that begins with CANCEL, or spells it in other letter case, a CANCEL " ) ;
// the two agree on what is cut short: the same bytes skipped, the same length read, the same count of body bytes
for ( std : : string_view tail : { " Content-Length: 9 \r \n \r \n hello " , " Content-Length: 5 \r \n \r \n hello " , " Content-Length: 4 \r \n \r \n hello " , " \r \n hello " , " Content-Length: 9 \r \n " } ) {
std : : string why ;
const bool truncated = ! DatagramMessage ( req + std : : string ( tail ) , & why ) . has_value ( ) & & why . contains ( " truncated " ) ;
Check ( CutShortRequest ( req + std : : string ( tail ) ) . has_value ( ) = = truncated , std : : format ( " a MESSAGE's header section comes exactly where DatagramMessage calls its datagram truncated ({:?}) " , tail ) ) ;
}
}
sip: a received message's header section is unfolded where it is framed
RFC 3261 7.3.1 lets a header value go on over further lines, each begun
with a space or a tab: "The line break and the whitespace at the
beginning of the next line are treated as a single SP character." The
text layer read a header from one line, so of a folded value it had the
first line only. Networks fold: KPN's 401 to the unprotected REGISTER
of 2026-07-18 and Odido's of 2026-07-13 had the WWW-Authenticate over
four lines (AGENTS.md §13; imsd finds a 401's nonce by a search of the
whole message, on a continuation line as on any other). A
Security-Server folded the same way would be read by its first line
alone, without the SPIs and ports on the further lines, and the
bring-up deferred; a short message whose Content-Type stands on a
continuation line would be taken for a page-mode text, answered 200
and never shown; a folded Content-Length on a connection would frame
no body, and the body's bytes begin the next message.
Unfolded takes the folds out of a header section, and the two framers
— FrameBuffer::TryExtract for a stream, DatagramMessage for a datagram
— hand a message on with its header section unfolded and its body as
it came; the Content-Length that frames the body is read from the
unfolded section. CutShortRequest reads its Content-Length so too, and
the header section it gives for the 400 is unfolded. Every socket imsd
reads SIP from goes through one of the two framers, so each reader of
a header still reads one line. A DUMP_SIP dump of a received message
holds it as framed.
Jorijn's decision of 2026-11-21 ("Unfold now").
Tested by tests/Sip — Unfolded on 7.3.1's own example, a tab, a bare LF,
a value begun on the next line and a line break that nothing follows;
both framers on a 401 whose WWW-Authenticate goes over four lines and
whose Content-Length is folded; CutShortRequest on a request cut short
of a folded Content-Length; ViaResponsePort on a Via list folded after
its comma, through the datagram framer — and by tests/Engine, a
delivered MESSAGE whose Content-Type came folded. On the workstation's
loopback (the fp6 journal's 2026-11-20-issues-2-5 capture, init-port,
server-ports and cut-short, at this commit and at the one before): a
stand-in's 401 with its Security-Server over three lines gives the whole
value, where the commit before deferred the bring-up for want of SPIs; a
request with its Content-Type and Content-Length on continuation lines
comes out of the server ports unfolded and framed by that length, as a
datagram and over a connection; a request cut short of a folded
Content-Length, its Via on a continuation line, is answered 400 with its
Via whole, on a server port and on the client flow, where the commit
before did not read the folded length and handed the request on as it
came; a request on a server port whose Via row is a list folded after
its comma, `rport` on its lower value, is answered from the client-port
socket, the way to the port its top value names, as at the commit
before. On the dev phone the binary of this range's last commit
registered fresh on KPN after a reboot; the 401 was not dumped, so
whether it came folded that time is not on record. A MESSAGE the phone
sent to its own protected server port, its Via row a list folded after
its comma with `rport` on the lower value, was answered from the
client-port socket (the capture's phone-form6, fresh-check.log and
inject-bench.log).
2026-11-22 17:35:12 +01:00
// ---- Unfolded, and the framers that use it (RFC 3261 7.3.1: a line begun with a space or a tab goes on with the line before it)
{
Check ( Unfolded ( " Subject: I know you're there, \r \n pick up the phone \r \n and talk to me! " ) = = " Subject: I know you're there, pick up the phone and talk to me! " , " 7.3.1's own example: each line break and the white space after it are one space " ) ;
Check ( Unfolded ( " A: 1 \r \n \t 2 " ) = = " A: 1 2 " & & Unfolded ( " A: 1 \r \n \t 2 " ) = = " A: 1 2 " , " a tab begins a continuation as a space does, and a run of them is one space " ) ;
Check ( Unfolded ( " A: \r \n 1 " ) = = " A: 1 " , " a value that begins on the next line " ) ;
Check ( Unfolded ( " A: 1 \r \n 2 " ) = = " A: 1 2 " , " white space before the line break is kept " ) ;
Check ( Unfolded ( " A: 1 \n 2 " ) = = " A: 1 2 " , " a bare LF folds as a CRLF does " ) ;
Check ( Unfolded ( " A: 1 \r \n B: 2 \r \n " ) = = " A: 1 \r \n B: 2 \r \n " , " a line break that no white space follows is kept " ) ;
Check ( Unfolded ( " A: 1 \r \n " ) = = " A: 1 \r \n " & & Unfolded ( " A: 1 \n " ) = = " A: 1 \n " & & Unfolded ( " \n " ) = = " \n " & & Unfolded ( " " ) . empty ( ) , " a line break that ends the text is kept " ) ;
Check ( Unfolded ( " A: 1 \r \n \r \n 2 " ) = = " A: 1 2 " , " a continuation line of white space alone " ) ;
Check ( Unfolded ( " A: 1 \r 2 " ) = = " A: 1 \r 2 " , " a CR alone is no line break " ) ;
Check ( Unfolded ( " A: 1 \r \n " ) = = " A: 1 " & & Unfolded ( " A: 1 \r \n \t " ) = = " A: 1 " , " white space after a line break that ends the text is that one space " ) ;
Check ( Unfolded ( " \n 1 " ) = = " 1 " & & Unfolded ( " \r \n \t 1 " ) = = " 1 " , " a text that begins with a fold " ) ;
// a 401 whose WWW-Authenticate goes over four lines, its nonce on the second
const std : : string folded401 =
" SIP/2.0 401 Unauthorized \r \n "
" Call-ID: f1@host \r \n "
" CSeq: 1 \r \n REGISTER \r \n "
" WWW-Authenticate: Digest realm= \" ims.example \" , \r \n "
" nonce= \" bm9uY2U= \" , \r \n "
" algorithm=AKAv1-MD5, \r \n "
" qop= \" auth \" \r \n "
" Content-Length: \r \n "
" \t 5 \r \n \r \n " ;
const std : : string body = " a \r \n b " ; // a body line that begins with a space is no fold
const std : : string unfolded401 =
" SIP/2.0 401 Unauthorized \r \n "
" Call-ID: f1@host \r \n "
" CSeq: 1 REGISTER \r \n "
" WWW-Authenticate: Digest realm= \" ims.example \" , nonce= \" bm9uY2U= \" , algorithm=AKAv1-MD5, qop= \" auth \" \r \n "
" Content-Length: 5 \r \n \r \n " ;
FrameBuffer fb ;
fb . Append ( folded401 + body + Resp200 ) ;
auto m = fb . TryExtract ( ) ;
Check ( m = = unfolded401 + body , " the stream framer hands a message out with its header section unfolded and its body as it came " ) ;
Check ( m & & Header ( * m , " WWW-Authenticate " ) = = " Digest realm= \" ims.example \" , nonce= \" bm9uY2U= \" , algorithm=AKAv1-MD5, qop= \" auth \" " , " a folded WWW-Authenticate is read whole " ) ;
Check ( m & & ResponseOf ( * m , " f1@host " , " 1 REGISTER " ) , " a CSeq folded between its number and its method is the transaction's " ) ;
Check ( fb . TryExtract ( ) = = Resp200 & & ! fb . TryExtract ( ) . has_value ( ) , " a folded Content-Length frames the body: the next message starts after it " ) ;
fb . Append ( folded401 + body . substr ( 0 , 3 ) ) ;
Check ( ! fb . TryExtract ( ) . has_value ( ) , " a body shorter than a folded Content-Length says is waited for " ) ;
fb . Append ( body . substr ( 3 ) ) ;
Check ( fb . TryExtract ( ) = = unfolded401 + body , " and taken once it is whole " ) ;
std : : string why ;
Check ( DatagramMessage ( folded401 + body + " JUNK " , & why ) = = unfolded401 + body & & why . empty ( ) , " the datagram framer does the same, and cuts at a folded Content-Length " ) ;
Check ( ! DatagramMessage ( folded401 + " a \r \n " , & why ) . has_value ( ) & & why . contains ( " truncated " ) , " a datagram shorter than its folded Content-Length: discarded, problem set " ) ;
Check ( DatagramMessage ( " SIP/2.0 200 OK \r \n Call-ID: \r \n a \r \n \r \n v=0 \r \n b " ) = = " SIP/2.0 200 OK \r \n Call-ID: a \r \n \r \n v=0 \r \n b " , " no Content-Length: the header section unfolded, the rest of the datagram the body as it came " ) ;
Check ( CutShortRequest ( " MESSAGE sip:a@example.net SIP/2.0 \r \n Via: SIP/2.0/UDP \r \n [2001:db8::2]:6000 \r \n Content-Length: \r \n \t 9 \r \n \r \n hello " ) = = " MESSAGE sip:a@example.net SIP/2.0 \r \n Via: SIP/2.0/UDP [2001:db8::2]:6000 \r \n Content-Length: 9 \r \n \r \n " , " a request cut short of a folded Content-Length: its header section unfolded, for the 400 " ) ;
}
sip: log a refused REGISTER or SUBSCRIBE with its status line and reason headers
A refused final was logged as its bare status code ("re-REGISTER failed
503", "reg-event SUBSCRIBE -> 500") — the unprotected REGISTER's as
"expected 401", without even the code — so a field journal could not say
which node refused or what it said. O2 UK answered a phone's 30-minute
refresh with 503 twenty-one times over seventy minutes and then refused
a fresh registration's reg-event SUBSCRIBE with 500 (alyx, 2026-09-24);
the journal holds nothing but the numbers.
Log the status line with the headers that carry the reason —
Retry-After, Warning, Reason, Server — for a REGISTER's refused final
(initial, protected, refresh) and for a refused SUBSCRIBE the run loop
reads, and dump the refused message under DUMP_SIP as its own fail dump
next to the 200s.
To tell a REGISTER's final from what else arrives, the final is now
matched by Call-ID and CSeq, and a request that arrives on the client
flow while one is awaited is dispatched: a refresh no longer reads such
a request as a failed REGISTER. An INVITE among those requests is a call
like any other, announced on the bus: a Dial whose reconnect it arrived
under meets the call-in-progress rule again before it places its own,
and when a resume, a refresh or a reconnect fails the call that arrived
under it ends with the flow it came in on (486 while the flow still
carries one, and the bus told). The unprotected REGISTER's reply is
matched the same way on the init port.
Tested on the dev phone (KPN) in builds of this series; not every path
has run there.
2026-09-24 18:51:03 +02:00
// ---- RetryAfter / RefusalSummary (what a refused final is logged as)
{
std : : string r503 =
" SIP/2.0 503 Service Unavailable \r \n "
" Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKx \r \n "
" Retry-After: 120 \r \n "
" Warning: 399 pcscf1.example \" S-CSCF unreachable \" \r \n "
" Server: Mavenir-P-CSCF/4.2 \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( RetryAfter ( r503 ) = = 120 , " Retry-After parsed " ) ;
Check ( RefusalSummary ( r503 ) = = " 503 Service Unavailable; Retry-After: 120; Warning: 399 pcscf1.example \" S-CSCF unreachable \" ; Server: Mavenir-P-CSCF/4.2 " , " summary: status line + reason headers in order " ) ;
std : : string r500 =
" SIP/2.0 500 Server Internal Error \r \n "
" Via: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKx \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ! RetryAfter ( r500 ) . has_value ( ) , " no Retry-After -> nullopt " ) ;
Check ( RefusalSummary ( r500 ) = = " 500 Server Internal Error " , " summary without reason headers is the status line " ) ;
std : : string two =
" SIP/2.0 403 Forbidden \r \n "
" Reason: Q.850;cause=21;text= \" Call rejected \" \r \n "
" Reason: SIP;cause=403 \r \n "
" Retry-After: soon \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ! RetryAfter ( two ) . has_value ( ) , " non-numeric Retry-After -> nullopt " ) ;
Check ( RefusalSummary ( two ) = = " 403 Forbidden; Retry-After: soon; Reason: Q.850;cause=21;text= \" Call rejected \" ; Reason: SIP;cause=403 " , " every instance of a reason header is kept " ) ;
Check ( RefusalSummary ( " NOTIFY sip:x SIP/2.0 \r \n Event: reg \r \n \r \n " ) = = " NOTIFY sip:x SIP/2.0 " , " a request summarises to its request line " ) ;
Check ( RefusalSummary ( " " ) = = " " , " empty message -> empty summary " ) ;
}
// ---- ResponseOf (which datagram a REGISTER wait may judge)
{
Check ( ResponseOf ( Resp200 , " 12345@host " , " 2 REGISTER " ) , " the transaction's own final " ) ;
Check ( ! ResponseOf ( Resp200 , " 12345@host " , " 1 REGISTER " ) , " another CSeq is not it " ) ;
std : : string stale =
" SIP/2.0 408 Request Timeout \r \n "
" Via: SIP/2.0/UDP [2001:db8::db43]:5060;branch=z9hG4bKa;rport=5060 \r \n "
" Call-ID: earlier@[2001:db8::db43] \r \n "
" CSeq: 1 REGISTER \r \n "
" Content-Length: 0 \r \n \r \n " ;
Check ( ! ResponseOf ( stale , " 12345@host " , " 1 REGISTER " ) , " an earlier attempt's late final is not it " ) ;
Check ( ResponseOf ( stale , " earlier@[2001:db8::db43] " , " 1 REGISTER " ) , " matched by Call-ID and CSeq " ) ;
Check ( ! ResponseOf ( " NOTIFY sip:x SIP/2.0 \r \n Call-ID: 12345@host \r \n CSeq: 2 REGISTER \r \n \r \n " , " 12345@host " , " 2 REGISTER " ) , " a request is never a response " ) ;
Check ( ! ResponseOf ( " " , " 12345@host " , " 2 REGISTER " ) , " nothing is not it " ) ;
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
Check ( ResponseOf ( " SIP/2.0 202 Accepted \r \n v: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKx \r \n i: 12345@host \r \n CSeq: 1 MESSAGE \r \n l: 0 \r \n \r \n " , " 12345@host " , " 1 MESSAGE " ) , " a final whose Call-ID comes as `i:` is the transaction's " ) ;
sip: ResponseOf compares a response's CSeq as its number and its method
ResponseOf matched a response to a request by its Call-ID and by its
CSeq compared as text. RFC 3261 25.1 writes the CSeq as 1*DIGIT LWS
Method, so a response may part the two by a tab or by more than one
space, and one written so was no transaction's: a REGISTER's wait ran
out without a reply.
CSeqParts takes a CSeq value as its number and its method: the digits it
begins with, the spaces or tabs after them, the method to the value's
end. ResponseOf compares the two as 20.16 has two CSeqs equal, "if the
sequence number and the request method are identical": the number as a
number — zeros before it do not make it another — and the method to the
letter. A value of another shape matches nothing.
Tested by tests/Sip: a final whose CSeq has two spaces, a tab or both
between its parts, or zeros before its number, is the transaction's, and
so is its 100 Trying; another number, another method, a method in other
letter case, and a CSeq without its number, its method or the white
space between them are not.
2026-11-22 17:35:11 +01:00
// a provisional of the transaction is its response too: both REGISTER waits start over on one
Check ( ResponseOf ( " SIP/2.0 100 Trying \r \n Call-ID: 12345@host \r \n CSeq: 2 REGISTER \r \n Content-Length: 0 \r \n \r \n " , " 12345@host " , " 2 REGISTER " ) , " the transaction's 100 Trying is its response " ) ;
// the CSeq as its number and its method (RFC 3261 20.16: equal "if the sequence number and the request method are identical"; 25.1: 1*DIGIT LWS Method), whatever spaces or tabs part them
auto finalWith = [ ] ( std : : string_view cseq ) { return std : : format ( " SIP/2.0 202 Accepted \r \n Call-ID: 12345@host \r \n CSeq: {} \r \n Content-Length: 0 \r \n \r \n " , cseq ) ; } ;
Check ( ResponseOf ( finalWith ( " 1 MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " two spaces between the CSeq's number and its method: the transaction's " ) ;
Check ( ResponseOf ( finalWith ( " 1 \t MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " a tab between them: the transaction's " ) ;
Check ( ResponseOf ( finalWith ( " 1 \t MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " spaces and a tab between them: the transaction's " ) ;
Check ( ResponseOf ( finalWith ( " 01 MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) & & ResponseOf ( finalWith ( " 1 MESSAGE " ) , " 12345@host " , " 001 MESSAGE " ) , " zeros before the number do not make it another " ) ;
Check ( ResponseOf ( finalWith ( " 0 MESSAGE " ) , " 12345@host " , " 00 MESSAGE " ) & & ! ResponseOf ( finalWith ( " 10 MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " zero is zero however long, and a zero after the number counts " ) ;
Check ( ResponseOf ( finalWith ( " 1 MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " white space around the CSeq asked for is no part of it " ) ;
Check ( ! ResponseOf ( finalWith ( " 11 MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) & & ! ResponseOf ( finalWith ( " 1 MESSAGE " ) , " 12345@host " , " 11 MESSAGE " ) , " another number that begins alike is not it " ) ;
Check ( ! ResponseOf ( finalWith ( " 1 MESSAGES " ) , " 12345@host " , " 1 MESSAGE " ) & & ! ResponseOf ( finalWith ( " 1 MESSAG " ) , " 12345@host " , " 1 MESSAGE " ) , " another method that begins alike is not it " ) ;
Check ( ! ResponseOf ( finalWith ( " 1 message " ) , " 12345@host " , " 1 MESSAGE " ) , " a method is a case-sensitive token " ) ;
Check ( ! ResponseOf ( finalWith ( " 1MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " no white space between number and method: not a CSeq " ) ;
Check ( ! ResponseOf ( finalWith ( " 1 " ) , " 12345@host " , " 1 MESSAGE " ) & & ! ResponseOf ( finalWith ( " 1 " ) , " 12345@host " , " 1 MESSAGE " ) , " a CSeq without a method is not it " ) ;
Check ( ! ResponseOf ( finalWith ( " MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) & & ! ResponseOf ( finalWith ( " MESSAGE " ) , " 12345@host " , " 1 MESSAGE " ) , " a CSeq without a number is not it " ) ;
Check ( ! ResponseOf ( " SIP/2.0 202 Accepted \r \n Call-ID: 12345@host \r \n Content-Length: 0 \r \n \r \n " , " 12345@host " , " 1 MESSAGE " ) , " a final without a CSeq is not it " ) ;
Check ( ! ResponseOf ( finalWith ( " 1 MESSAGE " ) , " 12345@host " , " MESSAGE " ) , " asked with a CSeq of another shape: nothing is it " ) ;
Check ( ! ResponseOf ( finalWith ( " MESSAGE " ) , " 12345@host " , " MESSAGE " ) & & ! ResponseOf ( finalWith ( " 1MESSAGE " ) , " 12345@host " , " 1MESSAGE " ) , " two CSeqs of another shape are no match, alike or not " ) ;
sip: log a refused REGISTER or SUBSCRIBE with its status line and reason headers
A refused final was logged as its bare status code ("re-REGISTER failed
503", "reg-event SUBSCRIBE -> 500") — the unprotected REGISTER's as
"expected 401", without even the code — so a field journal could not say
which node refused or what it said. O2 UK answered a phone's 30-minute
refresh with 503 twenty-one times over seventy minutes and then refused
a fresh registration's reg-event SUBSCRIBE with 500 (alyx, 2026-09-24);
the journal holds nothing but the numbers.
Log the status line with the headers that carry the reason —
Retry-After, Warning, Reason, Server — for a REGISTER's refused final
(initial, protected, refresh) and for a refused SUBSCRIBE the run loop
reads, and dump the refused message under DUMP_SIP as its own fail dump
next to the 200s.
To tell a REGISTER's final from what else arrives, the final is now
matched by Call-ID and CSeq, and a request that arrives on the client
flow while one is awaited is dispatched: a refresh no longer reads such
a request as a failed REGISTER. An INVITE among those requests is a call
like any other, announced on the bus: a Dial whose reconnect it arrived
under meets the call-in-progress rule again before it places its own,
and when a resume, a refresh or a reconnect fails the call that arrived
under it ends with the flow it came in on (486 while the flow still
carries one, and the bus told). The unprotected REGISTER's reply is
matched the same way on the init port.
Tested on the dev phone (KPN) in builds of this series; not every path
has run there.
2026-09-24 18:51:03 +02:00
}
engine: keep the reg-event subscription alive and act on a terminated binding
The reg-event subscription is the network's own account of our binding
(TS 24.229 5.1.1.3), and the daemon opened a new dialog for it at every
refresh and reconnect and otherwise never looked back: a refused
SUBSCRIBE was forgotten, an unanswered one had no timeout, an ended
subscription got nothing until the next refresh's new dialog, and a
NOTIFY was logged whatever it said. O2 UK, 2026-09-24: a fresh
registration's SUBSCRIBE got 500, a NOTIFY arrived all the same and
showed our contact active, the flow died eight seconds after the 200 and
no connect over the SA succeeded after that; the 500 was logged and
nothing was done about it.
Keep the subscription alive: retry a refused or unanswered SUBSCRIBE on
a backoff, renew one the S-CSCF ends with deactivated, timeout, giveup
or probation (RFC 6665 4.1.3; after any other reason, or none, the next
refresh subscribes anew), and subscribe anew ahead of the lifetime the
S-CSCF granted. A refresh or a reconnect opens no dialog while imsd
holds one for the registration or has a retry scheduled.
Read the reginfo for our own contact and act on its state: a NOTIFY that
terminates our binding schedules a fresh registration — at once for
deactivated or expired, after the network's retry-after (up to an hour)
for probation, on imsd's own refusal schedule (21 min, growing to 60)
for rejected or unregistered (RFC 3680). A hold already running stands,
and a REGISTER wait a call kept open that still gets its 200 after such
a NOTIFY leaves that registration standing. The parser is in Imsd:Sip
and pinned by tests/Sip with the O2 document shapes; the policy is
RegRecovery's (OnBindingTerminated, added here with its tests in
tests/Engine).
Tested on the dev phone (KPN) in builds of this series; not every path
has run there.
2026-10-03 19:36:23 +02:00
// ---- RegInfoContactState: our binding in a reg-event NOTIFY (RFC 3680)
{
using S = RegInfoContact : : State ;
auto contact = [ ] ( std : : string_view id , std : : string_view state , std : : string_view extra , std : : string_view user ) {
return std : : format (
" \t \t <contact id= \" {} \" state= \" {} \" {}> \n "
tests: the Sip suite's header names its sections, and its reginfo fixture takes a documentation address and the suites' instance id
The suite's header comment named five of its sections — header access,
the status line, the granted lifetime, the caller's identity and the
frame buffer — and left out six: ViaResponsePort, DatagramMessage,
RetryAfter and RefusalSummary, ResponseOf, RegInfoContactState,
HeaderUri and Body. Its reg-event fixture had a contact at
[2a03:db8::1], outside the documentation prefix 2001:db8::/32, and an
instance id built on a real phone model's type allocation code. The
header now names every section; the fixture has [2001:db8::1] and the
instance id that tests/Aka and tests/Messages use, 35999999-000000-1.
No check changes.
2026-11-22 17:35:11 +01:00
" \t \t \t <uri>sip:{}@[2001:db8::1]:45062</uri> \n "
" \t \t \t <unknown-param name= \" +sip.instance \" >"<urn:gsma:imei:35999999-000000-1>" </unknown-param> \n "
engine: keep the reg-event subscription alive and act on a terminated binding
The reg-event subscription is the network's own account of our binding
(TS 24.229 5.1.1.3), and the daemon opened a new dialog for it at every
refresh and reconnect and otherwise never looked back: a refused
SUBSCRIBE was forgotten, an unanswered one had no timeout, an ended
subscription got nothing until the next refresh's new dialog, and a
NOTIFY was logged whatever it said. O2 UK, 2026-09-24: a fresh
registration's SUBSCRIBE got 500, a NOTIFY arrived all the same and
showed our contact active, the flow died eight seconds after the 200 and
no connect over the SA succeeded after that; the 500 was logged and
nothing was done about it.
Keep the subscription alive: retry a refused or unanswered SUBSCRIBE on
a backoff, renew one the S-CSCF ends with deactivated, timeout, giveup
or probation (RFC 6665 4.1.3; after any other reason, or none, the next
refresh subscribes anew), and subscribe anew ahead of the lifetime the
S-CSCF granted. A refresh or a reconnect opens no dialog while imsd
holds one for the registration or has a retry scheduled.
Read the reginfo for our own contact and act on its state: a NOTIFY that
terminates our binding schedules a fresh registration — at once for
deactivated or expired, after the network's retry-after (up to an hour)
for probation, on imsd's own refusal schedule (21 min, growing to 60)
for rejected or unregistered (RFC 3680). A hold already running stands,
and a REGISTER wait a call kept open that still gets its 200 after such
a NOTIFY leaves that registration standing. The parser is in Imsd:Sip
and pinned by tests/Sip with the O2 document shapes; the policy is
RegRecovery's (OnBindingTerminated, added here with its tests in
tests/Engine).
Tested on the dev phone (KPN) in builds of this series; not every path
has run there.
2026-10-03 19:36:23 +02:00
" \t \t </contact> \n " , id , state , extra , user ) ;
} ;
auto reginfo = [ ] ( std : : string_view contacts ) {
return std : : format (
" <?xml version= \" 1.0 \" ?> \n "
" <reginfo xmlns= \" urn:ietf:params:xml:ns:reginfo \" version= \" 1 \" state= \" full \" > \n "
" \t <registration aor= \" sip:+447700900000@example.net \" id= \" 2 \" state= \" active \" > \n {0} \t </registration> \n "
" \t <registration aor= \" tel:+447700900000 \" id= \" 3 \" state= \" active \" > \n {0} \t </registration> \n "
" </reginfo> \n " , contacts ) ;
} ;
const std : : string me = " aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa " ;
const std : : string old = " bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb " ;
// the first NOTIFY of a registration: our contact active in both registrations
auto r0 = RegInfoContactState ( reginfo ( contact ( " 3 " , " active " , " event= \" created \" expires= \" 10800 \" " , me ) ) , me ) ;
Check ( r0 . state = = S : : Active , " our contact active -> Active " ) ;
// O2 2026-09-24 10:43: the previous registration's contact deactivated by ours, ours created
std : : string both = contact ( " 3 " , " terminated " , " event= \" deactivated \" " , old ) + contact ( " 4 " , " active " , " event= \" created \" expires= \" 10800 \" " , me ) ;
auto r1 = RegInfoContactState ( reginfo ( both ) , me ) ;
Check ( r1 . state = = S : : Active , " old contact terminated + ours active -> Active (the old one is not ours) " ) ;
auto r1o = RegInfoContactState ( reginfo ( both ) , old ) ;
Check ( r1o . state = = S : : Terminated & & r1o . event = = " deactivated " & & r1o . retryAfter = = 0 , " seen from the old contact's side: Terminated/deactivated " ) ;
// a network-initiated deregistration of our own binding
auto r2 = RegInfoContactState ( reginfo ( contact ( " 4 " , " terminated " , " event= \" probation \" retry-after= \" 120 \" " , me ) ) , me ) ;
Check ( r2 . state = = S : : Terminated & & r2 . event = = " probation " & & r2 . retryAfter = = 120 , " probation with retry-after " ) ;
auto r3 = RegInfoContactState ( reginfo ( contact ( " 4 " , " terminated " , " event= \" rejected \" " , me ) ) , me ) ;
Check ( r3 . state = = S : : Terminated & & r3 . event = = " rejected " , " rejected " ) ;
// active in one registration of the set beats terminated in another
std : : string mixed =
" <reginfo version= \" 2 \" state= \" partial \" > \n <registration aor= \" tel:+1 \" id= \" 3 \" state= \" active \" > \n " + contact ( " 4 " , " terminated " , " event= \" expired \" " , me ) +
" </registration> \n <registration aor= \" sip:+1@x \" id= \" 2 \" state= \" active \" > \n " + contact ( " 4 " , " active " , " " , me ) + " </registration> \n </reginfo> " ;
Check ( RegInfoContactState ( mixed , me ) . state = = S : : Active , " active anywhere in the set -> Active " ) ;
// not ours at all
auto r4 = RegInfoContactState ( reginfo ( contact ( " 9 " , " terminated " , " event= \" unregistered \" " , old ) ) , me ) ;
Check ( r4 . state = = S : : NotListed & & r4 . event . empty ( ) , " a document without our contact -> NotListed " ) ;
Check ( RegInfoContactState ( " " , me ) . state = = S : : NotListed , " empty body -> NotListed " ) ;
Check ( RegInfoContactState ( " <reginfo><registration><contact id= \" 1 \" state= \" active \" ><uri>sip:aaaaaaaa@x</uri></contact> " , me ) . state = = S : : NotListed , " a user part that is only a prefix of ours does not match " ) ;
Check ( RegInfoContactState ( " <contact state= \" terminated \" event= \" expired \" ><uri>sip: " + me + " @h</uri> " , me ) . state = = S : : Terminated , " an unterminated element still parses " ) ;
}
sip, engine: the SMS over IP transactions as reducers
TS 24.341 has a submission travel as a MESSAGE with the RP-DATA, answered
with a final and then with the submit report in a MESSAGE of the
IP-SM-GW's own, and a delivery arrive as a MESSAGE with the RP-DATA to be
answered and acknowledged with an RP-ACK in a MESSAGE of ours. Two
pure reducers carry that, in the shape CallMachine has: SmsOutbound takes
one message of ours segment by segment — the MESSAGE, its final (a 32 s
wait, which a provisional starts over), the RP-ACK or RP-ERROR matched by
In-Reply-To, else by RP-MR (within 40 s of the final, a length TR1M may
have),
then the next segment with the next RP-MR and TP-MR — and ends it
as sent or failed, its State action carrying the reason; OnSmsMessage turns an
inbound MESSAGE into its final, the message or status report it carried
and the delivery report to send, 488 for a
submit report In-Reply-To no MESSAGE of ours in flight — one that is not
ours (5.3.1.2), or ours once the message has ended (its report's wait
run out, judged by the reducer's caller; a failure; sent) or its segment
was superseded by the next, which gets the same answer — an RP-ERROR for
a TPDU that is
unusable (a delivery or a status report that does not parse, or one of
the submit-report type), and a 200 with a log line for what it makes
nothing of: a MESSAGE whose Content-Type does not contain the SMS one, a
body that is no RP message, a submit report without In-Reply-To for no
message in flight. Imsd:Sip gains
HeaderUri, the URI of a header's first element, a quoted display name
before it passed over (none when that URI holds an ASCII control
character, a space, `<`, `>` or `"`: it goes into a request line), and
Body, moved out of CallMachine (its four call sites and the shell's
NOTIFY/MESSAGE branch follow).
No retransmission (TS 23.040 9.2.3.6's repeat with TP-RD is not done):
an unanswered MESSAGE or a report that never comes fails the message.
RecentTransactions, the reducer, keys a request by Call-ID, CSeq and Via
branch — the value of the first `branch=` of its first Via line; a
request with none or an empty one is not remembered — and gives back the
final remembered for it within 32 s (TimerJ, on every transport) — what
a caller needs to answer a retransmitted request again and not handle
it again (RFC 3261 17.2.2); without it a repeated RP-DATA would be
handled again: a second delivery report where the first got one and, of
a text in one segment, a second Received.
Every final OnSmsMessage gives an inbound MESSAGE — the 200, the 488 —
goes through BuildResponse: each Via: line echoed, the To echoed with a
tag minted for the request unless it holds `tag=` (RFC 3261 8.2.6.2).
tests/Engine drives both reducers through the dialogs above and the
retransmission table; tests/Sip pins the two helpers.
OnSmsMessage's header reads — the delivery report's target
(P-Asserted-Identity, else From) among them — go through the text
layer, which ends at the header section: a body line may spell a header
name (a text, or UCS-2 bytes that do) and must pick nothing. Pinned with
an 8-bit SMS-DELIVER whose data spells a P-Asserted-Identity line.
A UCS-2 surrogate pair a sender split across two segments comes out
whole in the Received action OnSmsMessage returns (the codec decodes a
set from its joined units); pinned.
Pinned as well: a submit report that arrives before the MESSAGE's own
final is taken (the message sent, or the next segment out under its own
Call-ID, and the late final nobody's), a text past 255 segments fails at
once naming the fit, and a delivery with no P-Asserted-Identity and no
From URI is handed over with no delivery report to send. A submit report
whose In-Reply-To lists Call-IDs (RFC 3261 20.21) is ours when ours is
among them, and a delivered MESSAGE's Call-ID with an ASCII control
character or a space in it is not written into the delivery report's
In-Reply-To.
2026-10-16 23:20:56 +02:00
// ---- HeaderUri / Body: the party a header names, a message's body
{
const std : : string m =
" MESSAGE sip:me@ims SIP/2.0 \r \n "
" P-Asserted-Identity: \" IP-SM-GW \" <sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org> \r \n "
" P-Asserted-Identity: <tel:+31653131313> \r \n "
" From: sip:bare@ims;tag=x \r \n "
" To: <> \r \n "
" Call-ID: c1 \r \n "
" Content-Length: 3 \r \n "
" \r \n "
" abc " ;
Check ( imsd : : sip : : HeaderUri ( m , " P-Asserted-Identity " ) = = " sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org " , " HeaderUri: the first header's bracketed URI, display name dropped " ) ;
Check ( imsd : : sip : : HeaderUri ( m , " From " ) = = " sip:bare@ims " , " HeaderUri: a bare value up to its parameters " ) ;
Check ( ! imsd : : sip : : HeaderUri ( m , " To " ) , " HeaderUri: empty brackets hold no URI " ) ;
Check ( ! imsd : : sip : : HeaderUri ( m , " Contact " ) , " HeaderUri: an absent header " ) ;
const std : : string crafted = " MESSAGE sip:u@h SIP/2.0 \r \n P-Asserted-Identity: <sip:a \r Route:x@h> \r \n From: <sip:b c@h> \r \n Reply-To: sip:e \r Route:x@h \r \n Referred-By: sip:g h@h \r \n To: <sip:ok@h> \r \n \r \n " ;
Check ( ! imsd : : sip : : HeaderUri ( crafted , " P-Asserted-Identity " ) & & ! imsd : : sip : : HeaderUri ( crafted , " From " ) & & ! imsd : : sip : : HeaderUri ( crafted , " Reply-To " ) & & ! imsd : : sip : : HeaderUri ( crafted , " Referred-By " ) & & imsd : : sip : : HeaderUri ( crafted , " To " ) = = " sip:ok@h " , " HeaderUri: a URI with a bare CR or a space in it is none, in brackets or bare " ) ;
bool none = true ;
for ( int b = 0 ; b < = 0x7F ; b + + ) {
if ( b > 0x20 & & b ! = 0x7F ) continue ;
if ( imsd : : sip : : HeaderUri ( std : : string ( " MESSAGE sip:u@h SIP/2.0 \r \n From: <sip:a " ) + static_cast < char > ( b ) + " c@h> \r \n \r \n " , " From " ) ) none = false ;
}
Check ( none , " HeaderUri: a URI with a byte up to 0x20 or a DEL in it is none " ) ;
// the value's first element: a list in one row, a quoted display name, a value that holds no URI
auto one = [ ] ( std : : string_view pai ) { return imsd : : sip : : HeaderUri ( std : : format ( " MESSAGE sip:u@h SIP/2.0 \r \n P-Asserted-Identity: {} \r \n \r \n " , pai ) , " P-Asserted-Identity " ) ; } ;
Check ( one ( " sip:ipsmgw.home2.net,tel:+31653085001 " ) = = " sip:ipsmgw.home2.net " & & one ( " sip:ipsmgw.home2.net, tel:+31653085001 " ) = = " sip:ipsmgw.home2.net " & & one ( " sip:ipsmgw.home2.net \t ,tel:+31653085001 " ) = = " sip:ipsmgw.home2.net " & & one ( " sip:ipsmgw.home2.net,<tel:+31653085001> " ) = = " sip:ipsmgw.home2.net " & & one ( " <sip:ipsmgw.home2.net>,<tel:+31653085001> " ) = = " sip:ipsmgw.home2.net " & & one ( " sip:ipsmgw.home2.net;x=1,tel:+31653085001 " ) = = " sip:ipsmgw.home2.net " , " HeaderUri: of a list in one row, the first element's URI " ) ;
Check ( one ( " \" Doe, John \" <sip:j@h> " ) = = " sip:j@h " & & one ( " \" a<b \" <sip:gw@h> " ) = = " sip:gw@h " & & one ( " \" a \\ \" <b \" <sip:gw@h> " ) = = " sip:gw@h " & & one ( " \" a \\ \\ \" <sip:gw@h> " ) = = " sip:gw@h " & & one ( " \" \" <sip:gw@h> " ) = = " sip:gw@h " & & one ( " \" <sip:x@y> \" <sip:gw@h>;tag=1 " ) = = " sip:gw@h " , " HeaderUri: a quoted display name is passed over — a `<`, a `,`, an escaped quote or an escaped backslash in it too, and an empty one " ) ;
Check ( ! one ( " \" open<sip:x@h> " ) & & ! one ( " \" x \" sip:a@h " ) & & ! one ( " \" x \\ " ) , " HeaderUri: a quote that does not close, or a display name with no bracketed URI after it, gives none " ) ;
Check ( one ( " sip:gw@h;tag=1;x= \" <a:b> \" " ) = = " sip:gw@h " , " HeaderUri: a bare URI ends at its first parameter, also when a later one holds a `<` " ) ;
Check ( ! one ( " <sip:a@h,<tel:+1> " ) & & ! one ( " sip:a>b@h " ) & & ! one ( " <sip:a \" b@h> " ) , " HeaderUri: a URI with `<`, `>` or ` \" ` in it is none " ) ;
Check ( imsd : : sip : : Body ( m ) = = " abc " , " Body: what follows the header terminator " ) ;
Check ( imsd : : sip : : Body ( " OPTIONS sip:x SIP/2.0 \r \n CSeq: 1 OPTIONS \r \n " ) . empty ( ) , " Body: none without a terminator " ) ;
}
2026-07-22 22:53:28 +02:00
if ( Failures = = 0 ) std : : println ( " Sip: all tests passed " ) ;
2026-10-18 05:44:20 +02:00
return Failures ! = 0 ; // not the count: `crafter-build test` takes an exit status of 77 for a skipped suite, and this suite has more checks than that
2026-07-22 22:53:28 +02:00
}