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).
This commit is contained in:
parent
5f01b46909
commit
9333621c66
6 changed files with 178 additions and 47 deletions
|
|
@ -41,7 +41,8 @@
|
|||
// unusable TPDU acknowledged with an RP-ERROR, a body that is no RP
|
||||
// message, a page-mode text 200'd and logged, a short message while SMS
|
||||
// over IP is off refused with 480, a delivered MESSAGE and a final whose
|
||||
// header names come in compact form), and the store's record codec
|
||||
// header names come in compact form, a delivered MESSAGE whose
|
||||
// Content-Type came folded), and the store's record codec
|
||||
// (FormatStoredSms/ParseStoredSms: a full record round trip, defaults
|
||||
// for absent keys, a value that does not parse or a missing id refused) and order
|
||||
// (StoredSmsBefore: the arrival second, then within a run the sequence
|
||||
|
|
@ -1557,6 +1558,20 @@ int main() {
|
|||
const auto* repo = findSms(ro.actions, SmsAction::Type::SendClient);
|
||||
Check(repo && repo->text.starts_with("MESSAGE sip:ipsmgw@ims.mnc001.mcc001.3gppnetwork.org SIP/2.0\r\n"), "inbound: with a From that names another, the report still goes to the P-Asserted-Identity");
|
||||
}
|
||||
// the same delivery with its Content-Type folded onto a second line (RFC 3261 7.3.1), as a framer hands it on
|
||||
{
|
||||
const std::string longForm = inbound("011107911356131313F3001E040B911346610089F60000208062917314080CC8F71D14969741F977FD07");
|
||||
std::string folded = longForm;
|
||||
folded.replace(folded.find("Content-Type: "), 14, "Content-Type:\r\n\t");
|
||||
const auto framed = imsd::sip::DatagramMessage(folded);
|
||||
Check(folded.contains("\r\nContent-Type:\r\n\tapplication/vnd.3gpp.sms\r\n") && framed == longForm, "folded MESSAGE: the datagram framer hands the long form's message on");
|
||||
imsd::util::Rng rng(80), rngLong(80);
|
||||
Reassembler re, reLong;
|
||||
auto r = OnSmsMessage(c, rng, framed.value_or(""), re, nullptr, 50.0);
|
||||
auto want = OnSmsMessage(c, rngLong, longForm, reLong, nullptr, 50.0);
|
||||
const auto* rec = findSms(r.actions, SmsAction::Type::Received);
|
||||
Check(rec && rec->received.text == "How are you?" && r.response == want.response && hasSms(r.actions, SmsAction::Type::SendClient), "folded MESSAGE: taken for a short message, answered and reported on as its long form");
|
||||
}
|
||||
// the same delivery with its header names in compact form (RFC 3261 7.3.3): `c:` is the content type, `i:` the Call-ID, `v:` the Via
|
||||
{
|
||||
const std::string longForm = inbound("011107911356131313F3001E040B911346610089F60000208062917314080CC8F71D14969741F977FD07");
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
// caller-identity extraction, the port a Via names for the response
|
||||
// (ViaResponsePort), the two framers — the TCP frame buffer (split delivery,
|
||||
// keepalive pongs, pipelined messages) and a datagram's (DatagramMessage),
|
||||
// with the Content-Length they read —, 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.
|
||||
// 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.
|
||||
import std;
|
||||
import Imsd;
|
||||
|
||||
|
|
@ -290,6 +290,8 @@ int main() {
|
|||
// 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");
|
||||
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");
|
||||
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");
|
||||
|
|
@ -354,6 +356,55 @@ int main() {
|
|||
}
|
||||
}
|
||||
|
||||
// ---- 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\t2") == "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\nB: 2\r\n") == "A: 1\r\nB: 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\t1") == " 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"
|
||||
"\t5\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\nCall-ID:\r\n a\r\n\r\nv=0\r\n b") == "SIP/2.0 200 OK\r\nCall-ID: a\r\n\r\nv=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\nVia: SIP/2.0/UDP\r\n [2001:db8::2]:6000\r\nContent-Length:\r\n\t9\r\n\r\nhello") == "MESSAGE sip:a@example.net SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::2]:6000\r\nContent-Length: 9\r\n\r\n", "a request cut short of a folded Content-Length: its header section unfolded, for the 400");
|
||||
}
|
||||
|
||||
// ---- RetryAfter / RefusalSummary (what a refused final is logged as)
|
||||
{
|
||||
std::string r503 =
|
||||
|
|
|
|||
Loading…
Reference in a new issue