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:
parent
5c1fca12b9
commit
1267fe1ada
6 changed files with 367 additions and 48 deletions
|
|
@ -7,9 +7,11 @@ 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. Edge-case behavior (empty header values,
|
||||
line anchoring, keepalive discards) is pinned by tests/Sip — a change that
|
||||
flips one of those tests is a behavior change, not a cleanup.
|
||||
loggable and testable verbatim. 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
|
||||
those tests is a behavior change, not a cleanup.
|
||||
|
||||
Pure std C++ (no GLib).
|
||||
*/
|
||||
|
|
@ -39,6 +41,19 @@ namespace imsd::sip {
|
|||
return s;
|
||||
}
|
||||
|
||||
// Where the piece of `s` that begins at `from` ends: at the first `sep`
|
||||
// outside a quoted string (RFC 3261 25.1: in one, a backslash takes the
|
||||
// next character with it), or at the end of `s`.
|
||||
constexpr std::size_t PieceEnd(std::string_view s, std::size_t from, char sep) {
|
||||
bool quoted = false;
|
||||
for (std::size_t i = from; i < s.size(); i++) {
|
||||
if (quoted && s[i] == '\\') i++;
|
||||
else if (s[i] == '"') quoted = !quoted;
|
||||
else if (!quoted && s[i] == sep) return i;
|
||||
}
|
||||
return s.size();
|
||||
}
|
||||
|
||||
// Iterate the \n-separated lines of the header section (any trailing CR
|
||||
// stays on the line and is stripped by Trim when a value is extracted).
|
||||
// The first empty line after a line of text ends the headers (RFC 3261
|
||||
|
|
@ -64,22 +79,52 @@ namespace imsd::sip {
|
|||
}
|
||||
}
|
||||
|
||||
// One "Name: value" match: name anchored at line start (case-insensitive,
|
||||
// per RFC 3261 header names), immediately followed by the colon — no
|
||||
// whitespace before it — and at least one character (even just the CR)
|
||||
// after it. A bare "Name:\r" line matches with value ""; a "Name:" that
|
||||
// ends the buffer does not match. 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).
|
||||
std::optional<std::string_view> MatchHeaderLine(std::string_view line, std::string_view name) {
|
||||
// The one-letter form a header name may be written in (RFC 3261 7.3.3:
|
||||
// "Implementations MUST accept both the long and short forms of each
|
||||
// header name"): the ten RFC 3261 defines (its section 20), and those
|
||||
// of the two extension headers imsd reads that have one — Event (RFC
|
||||
// 6665 7.3) and Session-Expires (RFC 4028 4). 0 for any other name:
|
||||
// whoever starts reading another extension header adds its form here.
|
||||
constexpr char CompactForm(std::string_view name) {
|
||||
constexpr 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'},
|
||||
};
|
||||
for (const auto& [longForm, letter] : Forms)
|
||||
if (name.size() == longForm.size() && IEqualsPrefix(name, longForm)) return letter;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The value of a line that begins with the field name `name`: the name
|
||||
// anchored at line start (case-insensitive, per RFC 3261 header names),
|
||||
// then spaces or tabs, or none, then the colon (RFC 3261 25.1's HCOLON:
|
||||
// "Subject : lunch" is 7.3.1's example) and at least one
|
||||
// character (even just the CR) after it. A bare "Name:\r" line matches
|
||||
// with value ""; a "Name:" that ends the buffer does not match.
|
||||
// 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.
|
||||
std::optional<std::string_view> ValueOfField(std::string_view line, std::string_view name) {
|
||||
if (!IEqualsPrefix(line, name)) return std::nullopt;
|
||||
if (line.size() <= name.size() || line[name.size()] != ':') return std::nullopt;
|
||||
std::string_view rest = line.substr(name.size() + 1);
|
||||
std::size_t colon = name.size();
|
||||
while (colon < line.size() && (line[colon] == ' ' || line[colon] == '\t')) colon++;
|
||||
if (colon >= line.size() || line[colon] != ':') return std::nullopt;
|
||||
std::string_view rest = line.substr(colon + 1);
|
||||
if (rest.empty()) return std::nullopt;
|
||||
return Trim(rest);
|
||||
}
|
||||
|
||||
// One header line's match to `name`, the long form a reader asks by:
|
||||
// the line carries that name, or its compact form.
|
||||
std::optional<std::string_view> MatchHeaderLine(std::string_view line, std::string_view name) {
|
||||
if (auto v = ValueOfField(line, name)) return v;
|
||||
if (const char letter = CompactForm(name)) return ValueOfField(line, std::string_view(&letter, 1));
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// First run of ASCII digits starting at `s`; nullopt if none.
|
||||
std::optional<long> LeadingDigits(std::string_view s) {
|
||||
std::size_t n = 0;
|
||||
|
|
@ -223,13 +268,23 @@ export namespace imsd::sip {
|
|||
// the caller should reply to the request's source port instead
|
||||
// (nullopt). Also nullopt when the Via has no explicit port (5060 is
|
||||
// the default, but our P-CSCF flows always carry one). Handles
|
||||
// "[v6]:port" and "host:port" sent-by forms.
|
||||
// "[v6]:port" and "host:port" sent-by forms. rport (RFC 3581) is a
|
||||
// parameter by its name, in any case (7.3.1): not those letters in a
|
||||
// value or in a longer name. The top Via is the first value of the
|
||||
// first Via row: a row may hold a list (7.3.1), and what follows its
|
||||
// first comma outside a quoted string is another hop's.
|
||||
std::optional<int> ViaResponsePort(std::string_view msg) {
|
||||
auto via = Header(msg, "Via");
|
||||
if (!via) return std::nullopt;
|
||||
std::string_view v = *via;
|
||||
std::string_view v = via->substr(0, PieceEnd(*via, 0, ','));
|
||||
if (std::size_t sc = v.find(';'); sc != std::string_view::npos) {
|
||||
if (v.substr(sc).find("rport") != std::string_view::npos) return std::nullopt;
|
||||
for (std::size_t at = sc; at < v.size();) { // each parameter, from its semicolon to the next
|
||||
const std::size_t next = PieceEnd(v, at + 1, ';');
|
||||
std::string_view name = v.substr(at + 1, next - at - 1);
|
||||
name = Trim(name.substr(0, name.find('=')));
|
||||
if (name.size() == 5 && IEqualsPrefix(name, "rport")) return std::nullopt;
|
||||
at = next;
|
||||
}
|
||||
v = v.substr(0, sc);
|
||||
}
|
||||
std::size_t colon;
|
||||
|
|
|
|||
Loading…
Reference in a new issue