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).
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:11 +01:00
commit 1267fe1ada
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 367 additions and 48 deletions

View file

@ -1,14 +1,15 @@
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Imsd:Sip unit tests — header access, 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, keepalive pongs, pipelined messages) and a datagram's
// (DatagramMessage) —, 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.
// 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,
// keepalive pongs, pipelined messages) and a datagram's (DatagramMessage) —,
// 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;
@ -69,6 +70,63 @@ int main() {
Check(!Header(BodyVia, "X-Body").has_value(), "Header never reads the body");
Check(Header("\r\n\r\nSIP/2.0 200 OK\r\nCall-ID: k\r\n\r\n", "Call-ID") == "k", "empty lines before the first line of text (a keepalive's CRLFs) are skipped");
// ---- 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\nr: <sip:x>\r\ns: active\r\n\r\n", "Record-Route").has_value() && !Header("NOTIFY sip:a@b SIP/2.0\r\nr: <sip:x>\r\ns: 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\nVia: one\r\nv: two\r\nV: three\r\nVia: four\r\ni: 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\nTimestamp: 54\r\nVersion: 1\r\n\r\n", "To").has_value() && !Header("MESSAGE sip:a@b SIP/2.0\r\nTimestamp: 54\r\nVersion: 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\nCall-ID\t: c2@h\r\ni \t : c3@h\r\n\r\n", "Call-ID") == "c2@h" && Headers("MESSAGE sip:a@b SIP/2.0\r\nCall-ID\t: c2@h\r\ni \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\nTo be: or not\r\nt 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\nX-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\nTo", "To").has_value() && !Header("A: 1\r\nTo \t", "To").has_value() && !Header("A: 1\r\nt", "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\nTo:v").substr(0, 8), "To").has_value() && !Header(std::string_view("A: 1\r\nTo \t:v").substr(0, 10), "To").has_value(), "nor is it where a colon follows in the bytes past the text's end");
}
// ---- GrantedExpires
Check(GrantedExpires(Resp200) == 600000, "GrantedExpires from Contact expires=");
Check(GrantedExpires("SIP/2.0 200 OK\r\nExpires: 3600\r\n\r\n") == 3600, "GrantedExpires falls back to Expires");
@ -123,6 +181,18 @@ int main() {
Check(fb.TryExtract() == msg, "no Content-Length -> body length 0");
Check(fb.Size() == 8, "trailing bytes stay buffered");
}
{
// 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\ni: c1@h\r\nl: 5\r\n\r\nhello";
const std::string spaced = "MESSAGE sip:a@b SIP/2.0\r\nCall-ID: c2@h\r\nContent-Length : 3\r\n\r\nabc";
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");
}
// ---- CallerId: P-Asserted-Identity preferred, From fallback
{
@ -163,6 +233,14 @@ int main() {
Check(CallerId("INVITE sip:me SIP/2.0\r\nContent-Length: 0\r\n\r\n").empty(), "no identity headers -> empty");
}
// ---- the readers above under compact names
{
Check(CallerId("INVITE sip:me SIP/2.0\r\nf: <sip:+31687654321@ims.example;user=phone>;tag=f\r\nl: 0\r\n\r\n") == "+31687654321", "CallerId: a From that comes as `f:`");
Check(HeaderUri("MESSAGE sip:me SIP/2.0\r\nf: \"GW\" <sip:ipsmgw@ims.example>;tag=f\r\nl: 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\nm: <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\nv: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKx\r\nl: 0\r\n\r\n") == 6000, "ViaResponsePort: a Via that comes as `v:`");
}
// ---- ViaResponsePort: RFC 3261 18.2.2 UDP response routing
{
std::string upd =
@ -186,6 +264,25 @@ int main() {
"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");
// 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\nVia: {}\r\nContent-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");
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");
}
// ---- DatagramMessage (one UDP datagram = one message, RFC 3261 18.3)
@ -208,6 +305,10 @@ int main() {
Check(m3 && *m3 == hdr + "X-Note: Content-Length: 99\r\n\r\nv=0\n", "a header name inside a value does not match");
auto m4 = DatagramMessage(hdr + "Content-Length: 99999999999999999999\r\n\r\nv=0\n", &why);
Check(!m4.has_value(), "absurd Content-Length: discarded, no overflow");
auto m5 = DatagramMessage(hdr + "l: 4\r\n\r\nv=0\nJUNK");
Check(m5 && *m5 == hdr + "l: 4\r\n\r\nv=0\n", "`l:` that fits truncates the body like Content-Length");
why.clear();
Check(!DatagramMessage(hdr + "L : 40\r\n\r\nv=0\n", &why).has_value() && why.contains("truncated"), "`L :` larger than the datagram: discarded, problem set");
}
// ---- RetryAfter / RefusalSummary (what a refused final is logged as)
@ -253,6 +354,7 @@ int main() {
Check(ResponseOf(stale, "earlier@[2001:db8::db43]", "1 REGISTER"), "matched by Call-ID and CSeq");
Check(!ResponseOf("NOTIFY sip:x SIP/2.0\r\nCall-ID: 12345@host\r\nCSeq: 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");
Check(ResponseOf("SIP/2.0 202 Accepted\r\nv: SIP/2.0/TCP [2001:db8::db43]:45062;branch=z9hG4bKx\r\ni: 12345@host\r\nCSeq: 1 MESSAGE\r\nl: 0\r\n\r\n", "12345@host", "1 MESSAGE"), "a final whose Call-ID comes as `i:` is the transaction's");
}
// ---- RegInfoContactState: our binding in a reg-event NOTIFY (RFC 3680)