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
|
|
@ -7,7 +7,7 @@ Imsd:Sip — SIP message text layer + TCP framing.
|
|||
Deliberately a text layer, not a full RFC 3261 parser: the engine works on
|
||||
whole messages as strings and pulls out exactly what it needs (headers,
|
||||
status codes, the granted registration lifetime), which keeps every message
|
||||
loggable and testable verbatim. A header is asked for by its long name and
|
||||
loggable and testable as text. A header is asked for by its long name and
|
||||
found under that or its compact form (RFC 3261 7.3.3). Edge-case behavior
|
||||
(empty header values, line anchoring, white space before the colon,
|
||||
keepalive discards) is pinned by tests/Sip — a change that flips one of
|
||||
|
|
@ -105,8 +105,9 @@ namespace imsd::sip {
|
|||
// Matching never crosses the line boundary: an empty-valued header
|
||||
// yields "", NOT the next line's content (a regex-based implementation
|
||||
// whose whitespace class eats the CRLF gets that wrong — pinned by the
|
||||
// "bare header" test), so of a value folded onto further lines (7.3.1)
|
||||
// the first line is read.
|
||||
// "bare header" test). A value folded onto further lines (7.3.1) is
|
||||
// one line by the time a received message is read here: the framers
|
||||
// unfold its header section (Unfolded).
|
||||
std::optional<std::string_view> ValueOfField(std::string_view line, std::string_view name) {
|
||||
if (!IEqualsPrefix(line, name)) return std::nullopt;
|
||||
std::size_t colon = name.size();
|
||||
|
|
@ -421,6 +422,29 @@ export namespace imsd::sip {
|
|||
return std::string(uri);
|
||||
}
|
||||
|
||||
// A header section with its folds taken out. RFC 3261 7.3.1: "Header
|
||||
// fields can be extended over multiple lines by preceding each extra
|
||||
// line with at least one SP or horizontal tab (HT). The line break and
|
||||
// the whitespace at the beginning of the next line are treated as a
|
||||
// single SP character." So a line break that a space or a tab follows
|
||||
// becomes, with that white space, one space; every other byte is kept.
|
||||
std::string Unfolded(std::string_view head) {
|
||||
std::string out;
|
||||
out.reserve(head.size());
|
||||
for (std::size_t i = 0; i < head.size();) {
|
||||
const std::string_view next = head.substr(i + 1);
|
||||
if (head[i] == '\n' && (next.starts_with(' ') || next.starts_with('\t'))) {
|
||||
if (out.ends_with('\r')) out.pop_back();
|
||||
out.push_back(' ');
|
||||
const std::size_t text = next.find_first_not_of(" \t");
|
||||
i = text == std::string_view::npos ? head.size() : i + 1 + text;
|
||||
} else {
|
||||
out.push_back(head[i++]);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The body of a message: what follows the header terminator, empty
|
||||
// without one.
|
||||
std::string_view Body(std::string_view msg) {
|
||||
|
|
@ -431,25 +455,27 @@ export namespace imsd::sip {
|
|||
// SIP-over-TCP stream framing, transport-free: feed received bytes into
|
||||
// Append(), pull complete messages (headers + Content-Length body) out
|
||||
// of TryExtract(). Leading CRLFs (RFC 5626 keepalive pongs) are
|
||||
// discarded.
|
||||
// discarded. A message comes out with its header section unfolded
|
||||
// (Unfolded) and its body as it came.
|
||||
class FrameBuffer {
|
||||
public:
|
||||
void Append(std::string_view data) { buf_.append(data); }
|
||||
|
||||
// One complete SIP message off the front of the buffer, or nullopt
|
||||
// if more bytes are needed. Content-Length is located with the same
|
||||
// line-anchored matching rules as Header().
|
||||
// line-anchored matching rules as Header(), in the unfolded header
|
||||
// section.
|
||||
std::optional<std::string> TryExtract() {
|
||||
std::size_t keep = buf_.find_first_not_of("\r\n");
|
||||
buf_.erase(0, keep == std::string::npos ? buf_.size() : keep);
|
||||
std::size_t idx = buf_.find("\r\n\r\n");
|
||||
if (idx == std::string::npos) return std::nullopt;
|
||||
std::string_view head = std::string_view(buf_).substr(0, idx);
|
||||
std::string msg = Unfolded(std::string_view(buf_).substr(0, idx));
|
||||
long clen = 0;
|
||||
if (auto v = Header(head, "Content-Length")) clen = static_cast<long>(LengthOf(*v).value_or(0));
|
||||
if (auto v = Header(msg, "Content-Length")) clen = static_cast<long>(LengthOf(*v).value_or(0));
|
||||
std::size_t total = idx + 4 + static_cast<std::size_t>(clen);
|
||||
if (buf_.size() < total) return std::nullopt;
|
||||
std::string msg = buf_.substr(0, total);
|
||||
msg.append(buf_, idx, total - idx);
|
||||
buf_.erase(0, total);
|
||||
return msg;
|
||||
}
|
||||
|
|
@ -467,6 +493,8 @@ export namespace imsd::sip {
|
|||
// fit is a truncated datagram → nullopt with `problem` set; no
|
||||
// Content-Length means the body is the rest of the datagram. A datagram
|
||||
// without a header terminator is malformed → nullopt with `problem`.
|
||||
// The message comes out with its header section unfolded (Unfolded),
|
||||
// where the Content-Length is read, and its body as it came.
|
||||
std::optional<std::string> DatagramMessage(std::string_view d, std::string* problem = nullptr) {
|
||||
std::size_t start = d.find_first_not_of("\r\n");
|
||||
if (start == std::string_view::npos) return std::nullopt;
|
||||
|
|
@ -476,49 +504,52 @@ export namespace imsd::sip {
|
|||
if (problem) *problem = std::format("datagram without a header terminator ({} bytes)", d.size());
|
||||
return std::nullopt;
|
||||
}
|
||||
std::size_t bodyStart = idx + 4;
|
||||
std::size_t body = d.size() - bodyStart;
|
||||
if (auto v = Header(d.substr(0, idx), "Content-Length")) {
|
||||
std::size_t body = d.size() - (idx + 4);
|
||||
std::string msg = Unfolded(d.substr(0, idx));
|
||||
if (auto v = Header(msg, "Content-Length")) {
|
||||
if (const auto length = LengthOf(*v)) {
|
||||
const std::size_t clen = *length;
|
||||
if (clen > body) {
|
||||
if (problem) *problem = std::format("datagram truncated (Content-Length {} > {} body bytes)", clen, body);
|
||||
return std::nullopt;
|
||||
}
|
||||
return std::string(d.substr(0, bodyStart + clen));
|
||||
body = clen;
|
||||
}
|
||||
}
|
||||
return std::string(d);
|
||||
msg.append(d.substr(idx, 4 + body));
|
||||
return msg;
|
||||
}
|
||||
|
||||
// RFC 3261 18.3 of 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 header section of such a request, with the empty line
|
||||
// that ends it and no body — what the 400 is built from — or nullopt
|
||||
// for every other datagram: one whose body is all there, by its
|
||||
// Content-Length or for want of one; one without an end of headers;
|
||||
// one whose first line is no request's; an ACK or a CANCEL. A request's
|
||||
// first line ends with the SIP version (7.1: "Request-Line = Method SP
|
||||
// Request-URI SP SIP-Version CRLF") and a status line begins with it
|
||||
// (7.2), in whatever letter case ("The SIP-Version string is
|
||||
// case-insensitive"): a response is none, and neither are bytes that
|
||||
// are no SIP. imsd keeps nothing of a request it answers so, and of an
|
||||
// element that keeps nothing 8.2.7 says: "A stateless UAS MUST ignore
|
||||
// ACK requests." "A stateless UAS MUST ignore CANCEL requests."
|
||||
// response." The header section of such a request, unfolded (Unfolded),
|
||||
// with the empty line that ends it and no body — what the 400 is built
|
||||
// from — or nullopt for every other datagram: one whose body is all
|
||||
// there, by its Content-Length or for want of one; one without an end
|
||||
// of headers; one whose first line is no request's; an ACK or a CANCEL.
|
||||
// A request's first line ends with the SIP version (7.1: "Request-Line
|
||||
// = Method SP Request-URI SP SIP-Version CRLF") and a status line
|
||||
// begins with it (7.2), in whatever letter case ("The SIP-Version
|
||||
// string is case-insensitive"): a response is none, and neither are
|
||||
// bytes that are no SIP. imsd keeps nothing of a request it answers so,
|
||||
// and of an element that keeps nothing 8.2.7 says: "A stateless UAS
|
||||
// MUST ignore ACK requests." "A stateless UAS MUST ignore CANCEL
|
||||
// requests."
|
||||
std::optional<std::string> CutShortRequest(std::string_view d) {
|
||||
const std::size_t start = d.find_first_not_of("\r\n");
|
||||
if (start == std::string_view::npos) return std::nullopt;
|
||||
d.remove_prefix(start);
|
||||
const std::size_t idx = d.find("\r\n\r\n");
|
||||
if (idx == std::string_view::npos) return std::nullopt;
|
||||
const std::string_view head = d.substr(0, idx);
|
||||
std::string head = Unfolded(d.substr(0, idx));
|
||||
const auto length = Header(head, "Content-Length").and_then(LengthOf);
|
||||
if (!length || *length <= d.size() - (idx + 4)) return std::nullopt;
|
||||
const std::string_view line = head.substr(0, head.find("\r\n"));
|
||||
const std::string_view line = std::string_view(head).substr(0, head.find("\r\n"));
|
||||
const std::string_view last = line.substr(line.rfind(' ') + 1); // of a line without a space, the line
|
||||
if (last.size() != 7 || !IEqualsPrefix(last, "SIP/2.0") || IEqualsPrefix(line, "SIP/")) return std::nullopt;
|
||||
if (line.starts_with("ACK ") || line.starts_with("CANCEL ")) return std::nullopt;
|
||||
return std::string(head) + "\r\n\r\n";
|
||||
head.append("\r\n\r\n");
|
||||
return head;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue