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

@ -10,7 +10,9 @@
// reject -> 486, remote CANCEL -> 200+487, unusable offer -> 488 (naming
// what was offered), a landline gateway's G.711-only and AMR-NB offers
// ring and are answered at the offer's own payload types, ring timeout ->
// 480, BYE both ways. DTMF: the tones reach the media leg on an active
// 480, BYE both ways; an incoming call from an INVITE, and an outgoing
// call from responses, whose header names come in compact form (RFC 3261
// 7.3.3). DTMF: the tones reach the media leg on an active
// call that negotiated telephone-event, and are dropped — with the reason
// — before the answer, after the call, without the negotiation or with a
// character that is no tone. Then RegRecovery, the registration recovery policy:
@ -38,7 +40,8 @@
// report routed to the message in flight, 488 for one that is not ours, an
// 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), and the store's record codec
// 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
// (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
@ -199,6 +202,20 @@ namespace {
"Content-Length: {}\r\n\r\n{}",
extraHeaders, sdp.size(), sdp);
}
// The same message with the header names that have a compact form
// written in it (RFC 3261 7.3.3: Via, From, To, Call-ID, Contact,
// Content-Type, Content-Length, Supported; Event, Session-Expires) —
// in the header section only, the start line and the body as they are.
std::string Compact(std::string_view msg) {
const std::size_t end = msg.find("\r\n\r\n");
std::string head(msg.substr(0, end));
for (auto [name, letter] : std::initializer_list<std::pair<std::string_view, std::string_view>>{
{"\r\nVia:", "\r\nv:"}, {"\r\nFrom:", "\r\nf:"}, {"\r\nTo:", "\r\nt:"}, {"\r\nCall-ID:", "\r\ni:"}, {"\r\nContact:", "\r\nm:"},
{"\r\nContent-Type:", "\r\nc:"}, {"\r\nContent-Length:", "\r\nl:"}, {"\r\nSupported:", "\r\nk:"}, {"\r\nEvent:", "\r\no:"}, {"\r\nSession-Expires:", "\r\nx:"}})
for (std::size_t at = 0; (at = head.find(name, at)) != std::string::npos; at += letter.size()) head.replace(at, name.size(), letter);
return end == std::string_view::npos ? head : head + std::string(msg.substr(end));
}
}
int main() {
@ -754,6 +771,56 @@ int main() {
Check(t && t->reason == "local-hangup", "active hangup -> local-hangup");
}
// ---- an incoming call whose INVITE carries its header names in compact
// form (RFC 3261 7.3.3: "MUST accept both the long and short forms"):
// the same dialog as from the long names
{
const std::string invite = Compact(MtInvite());
Check(!invite.contains("Via:") && !invite.contains("From:") && !invite.contains("\r\nTo:") && !invite.contains("Call-ID:") && !invite.contains("Contact:") && !invite.contains("Session-Expires:") && !invite.contains("Content-Type:") && !invite.contains("Content-Length:"), "the compact INVITE carries none of the eight long names");
Check(invite.contains("\r\nv: SIP/2.0/UDP [2001:db8::9]:5060;branch=z9hG4bKscscf1\r\n") && invite.contains("\r\ni: mt-call-abc123\r\n") && invite.contains("\r\nx: 1800\r\n") && invite.ends_with("a=ptime:20\r\na=sendrecv\r\n"), "...their compact forms instead, the body untouched");
imsd::util::Rng rng(33);
CallMachine m(c, rng, "ims-call-70", IncomingInvite{invite}, 50004);
Check(m.CallId() == "mt-call-abc123" && m.Number() == "+31612345678", "compact INVITE: the Call-ID from `i:`");
auto ai = m.OnInvite();
const Action* ringing = Responded(ai, "SIP/2.0 180 Ringing");
Check(ringing && ringing->text.contains("\r\nVia: SIP/2.0/UDP [2001:db8::105]:5100;branch=z9hG4bKpcscf1\r\nVia: SIP/2.0/UDP [2001:db8::9]:5060;branch=z9hG4bKscscf1\r\n"), "compact INVITE: the 180 echoes both `v:` rows, in order, as Via");
Check(ringing && ringing->text.contains("\r\nFrom: <sip:+31612345678@ims.mnc001.mcc001.3gppnetwork.org;user=phone>;tag=caller1\r\n") && ringing->text.contains("\r\nTo: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org;user=phone>;tag=") && ringing->text.contains("\r\nCall-ID: mt-call-abc123\r\n"), "compact INVITE: the 180 echoes the `f:`, the `t:` with our tag and the `i:`");
auto aa = m.OnAccept();
const Action* ok = Responded(aa, "SIP/2.0 200 OK");
const Action* media = Find(aa, Action::Type::StartMedia);
Check(ok && ok->text.contains("Session-Expires: 1800;refresher=uac\r\n") && media && media->media.remotePort == 27864 && m.State() == CallState::Active, "compact INVITE: the 200 echoes the `x:` session timer, the offer is answered");
// a refresh whose Session-Expires comes as `x:`
const std::string upd = Compact(std::format(
"UPDATE sip:me SIP/2.0\r\nVia: SIP/2.0/UDP [x]:1;branch=z9hG4bKu2\r\n"
"From: <sip:+31612345678@ims;user=phone>;tag=caller1\r\n"
"To: <sip:+31611111111@ims;user=phone>;tag=x\r\n"
"Min-SE: 90\r\nSession-Expires: 1800;refresher=uac\r\n"
"Call-ID: {}\r\nCSeq: 3 UPDATE\r\nContent-Length: 0\r\n\r\n", m.CallId()));
auto au = m.OnRequest(upd);
const Action* se200 = Responded(au, "SIP/2.0 200 OK");
Check(upd.contains("\r\nx: 1800;refresher=uac\r\n") && se200 && se200->text.contains("Session-Expires: 1800;refresher=uac\r\n") && se200->text.contains("\r\nVia: SIP/2.0/UDP [x]:1;branch=z9hG4bKu2\r\n") && se200->text.contains(std::format("\r\nCall-ID: {}\r\n", m.CallId())), "compact UPDATE: the 200 echoes the `x:` timer and the `v:`, `i:` rows");
// our BYE: to the caller's `m:` Contact, the `f:` From as its To, the `t:` To's URI as its From
auto ah = m.OnHangup();
const Action* bye = Sent(ah, "BYE ");
Check(bye && bye->text.starts_with("BYE sip:origgw@[2001:db8::9]:5060 SIP/2.0\r\n") && bye->text.contains("\r\nTo: <sip:+31612345678@ims.mnc001.mcc001.3gppnetwork.org;user=phone>;tag=caller1\r\n") && bye->text.contains("\r\nFrom: <sip:+31611111111@ims.mnc001.mcc001.3gppnetwork.org;user=phone>;tag=") && bye->text.contains("\r\nCall-ID: mt-call-abc123\r\n"), "compact INVITE: our BYE goes to the `m:` Contact with the dialog the `f:`, `t:` and `i:` rows made");
}
// ---- an outgoing call whose responses carry compact names: the dialog
// from `t:` and `m:`
{
imsd::util::Rng rng(42);
CallMachine m(c, rng, "ims-call-71", "1233", 50004, false);
m.Start();
const std::string r183 = Compact(R183(m.CallId())), r200 = Compact(R200(m.CallId()));
Check(!r183.contains("\r\nTo:") && !r183.contains("Contact:") && r183.contains("\r\nt: <sip:1233@ims>;tag=remote7\r\n") && r200.contains("\r\nm: <sip:termgw@[2001:db8::9]:5060>\r\n"), "the compact responses carry `t:` and `m:`");
auto a183 = m.OnInviteResponse(r183);
const Action* prack = Sent(a183, "PRACK ");
Check(prack && prack->text.starts_with("PRACK sip:termgw@[2001:db8::9]:5060 SIP/2.0\r\n") && prack->text.contains("\r\nTo: <sip:1233@ims>;tag=remote7\r\n"), "compact 183: the PRACK targets the `m:` Contact and carries the `t:` To");
auto a200 = m.OnInviteResponse(r200);
const Action* ack = Sent(a200, "ACK ");
Check(ack && ack->text.starts_with("ACK sip:termgw@[2001:db8::9]:5060 SIP/2.0\r\n") && ack->text.contains("\r\nTo: <sip:1233@ims>;tag=remote7\r\n") && Has(a200, Action::Type::StartMedia) && m.State() == CallState::Active, "compact 200: the ACK to the `m:` Contact with the `t:` To, the call active");
}
// ---- media death after accept releases the call
{
imsd::util::Rng rng(35);
@ -1490,6 +1557,28 @@ 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 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");
const std::string compact = Compact(longForm);
Check(!compact.contains("Content-Type:") && !compact.contains("Call-ID:") && !compact.contains("Via:") && compact.contains("\r\nc: application/vnd.3gpp.sms\r\n") && compact.contains("\r\ni: in-011107@net\r\n") && compact.contains("\r\nv: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKn1\r\n") && compact.contains(std::format("\r\nl: {}\r\n", imsd::sip::Body(longForm).size())) && imsd::sip::Body(longForm).size() == 42 && imsd::sip::Body(compact) == imsd::sip::Body(longForm), "the compact MESSAGE carries `c:`, `i:`, `v:` and `l:`, the body of 42 bytes as it was");
imsd::util::Rng rng(80), rngLong(80);
Reassembler re, reLong;
auto r = OnSmsMessage(c, rng, compact, re, nullptr, 50.0);
auto want = OnSmsMessage(c, rngLong, longForm, reLong, nullptr, 50.0);
const auto* rec = findSms(r.actions, SmsAction::Type::Received);
const auto* rep = findSms(r.actions, SmsAction::Type::SendClient);
const auto* wantRep = findSms(want.actions, SmsAction::Type::SendClient);
Check(r.response == want.response && r.response.starts_with("SIP/2.0 200 OK\r\nVia: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKn1\r\n") && r.response.contains("\r\nCall-ID: in-011107@net\r\n"), "compact MESSAGE: the 200 of the long form, the `v:` and the `i:` echoed as Via and Call-ID");
Check(rec && rec->received.text == "How are you?" && rep && wantRep && rep->text == wantRep->text && rep->text.contains("\r\nIn-Reply-To: in-011107@net\r\n"), "compact MESSAGE: taken for a short message, and its delivery report names the `i:` Call-ID in In-Reply-To");
Check(RecentTransactions::Key(compact) == "in-011107@net|1 MESSAGE|z9hG4bKn1" && RecentTransactions::Key(compact) == RecentTransactions::Key(longForm), "compact MESSAGE: the retransmission key from `i:` and `v:`");
// the final to a MESSAGE of ours, its Call-ID as `i:`
imsd::util::Rng rngOut(81);
SmsOutbound m(c, rngOut, "ims-sms-20", p);
const std::string callid(imsd::sip::Header(findSms(m.Start(0.0), SmsAction::Type::SendClient)->text, "Call-ID").value_or(""));
const std::string final202 = Compact(final(callid, 202, "Accepted"));
Check(final202.contains(std::format("\r\ni: {}\r\n", callid)) && !final202.contains("Call-ID:") && m.OwnsResponse(final202), "outbound: a final whose Call-ID comes as `i:` is the message in flight's");
}
// a Call-ID the delivery report cannot carry — a byte up to 0x20 in it (but an LF, which ends the line) or a DEL: no In-Reply-To, the message and its report as ever
for (int b = 0; b <= 0x7F; b++) {
if ((b > 0x20 && b != 0x7F) || b == 0x0A) continue;
@ -1735,6 +1824,8 @@ int main() {
const std::string plain = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKn3\r\nFrom: <sip:x@ims>;tag=1\r\nTo: <sip:me@ims>\r\nCall-ID: t2@net\r\nCSeq: 1 MESSAGE\r\nContent-Type: text/plain\r\nContent-Length: 2\r\n\r\nhi";
auto r2 = OnSmsMessage(off, rng, plain, re, nullptr, 9.0);
Check(r2.response.starts_with("SIP/2.0 200 OK") && hasSms(r2.actions, SmsAction::Type::Log), "off: a page-mode text is still 200'd and logged");
auto r3 = OnSmsMessage(off, rng, Compact(offered), re, nullptr, 9.5);
Check(r3.response.starts_with("SIP/2.0 480 Temporarily Unavailable\r\n") && r3.response.contains("\r\nCall-ID: in-011107@net\r\n") && !r3.rp, "off: a short message whose content type comes as `c:` is refused with 480 too");
}
}