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");
}
}

View file

@ -5,7 +5,8 @@
// compared byte-for-byte against a reference message cross-generated from the
// Python VoLTE stack with pinned tags/branches/SPIs; their variants and the other
// builders against a message written here or by their lines (BuildInviteResponse
// is not called). A divergence here is a wire-format change, i.e. a
// by the lines of one 180 and by its answers to an INVITE in long and in
// compact form). A divergence here is a wire-format change, i.e. a
// registration or call that the commercial IMS core may reject; these are the
// hardest pins in the project. Two deliberate post-parity divergences:
// the REGISTER Contact carries +sip.instance and the MMTEL ICSI feature tag
@ -501,6 +502,56 @@ int main() {
"Content-Length: 0\r\n\r\n", "a 488 for an inbound MESSAGE echoes every Via and tags To");
const std::string tagged = "MESSAGE sip:me@ims SIP/2.0\r\nVia: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet2\r\nFrom: <sip:ipsmgw@ims>;tag=N2\r\nTo: <sip:me@ims>;tag=had\r\nCall-ID: in2@net\r\nCSeq: 1 MESSAGE\r\nContent-Length: 0\r\n\r\n";
Check(BuildResponse(c, tagged, 200, "OK", "unused").contains("\r\nTo: <sip:me@ims>;tag=had\r\n"), "a To that already carries a tag is echoed as it is");
// a request whose header names come in compact form (RFC 3261 7.3.3): the finals echo them under the long names
const std::string compact =
"MESSAGE sip:me@[2001:db8::db43]:45062 SIP/2.0\r\n"
"v: SIP/2.0/UDP [2001:db8::105]:6000;branch=z9hG4bKnet1\r\n"
"V: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw0\r\n"
"f: <sip:ipsmgw@ims>;tag=N1\r\n"
"t: <sip:me@ims>\r\n"
"i: in1@net\r\n"
"CSeq: 9 MESSAGE\r\n"
"l: 0\r\n\r\n";
Eq(BuildResponse(c, compact, 488, "Not Acceptable Here", "T488"), BuildResponse(c, inbound, 488, "Not Acceptable Here", "T488"), "a 488 for a MESSAGE in compact form is the one for its long form");
Eq(BuildResponse200(c, compact), BuildResponse200(c, inbound), "the hop-by-hop 200 for a request in compact form is the one for its long form");
Check(BuildResponse200(c, compact).contains("\r\nFrom: <sip:ipsmgw@ims>;tag=N1\r\nTo: <sip:me@ims>\r\nCall-ID: in1@net\r\nCSeq: 9 MESSAGE\r\n"), "with its `f:`, `t:` and `i:` under the long names");
// BuildInviteResponse reads the same names: an INVITE in compact form gets the responses of its long form
const std::string invite =
"INVITE sip:me@[2001:db8::db43]:45062 SIP/2.0\r\n"
"Via: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3\r\n"
"Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3\r\n"
"Record-Route: <sip:[2001:db8::105]:6000;lr>\r\n"
"From: <sip:caller@ims>;tag=N3\r\n"
"To: <sip:me@ims>\r\n"
"Call-ID: call3@net\r\n"
"CSeq: 1 INVITE\r\n"
"Content-Length: 0\r\n\r\n";
const std::string compactInvite =
"INVITE sip:me@[2001:db8::db43]:45062 SIP/2.0\r\n"
"v: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3\r\n"
"V: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3\r\n"
"Record-Route: <sip:[2001:db8::105]:6000;lr>\r\n"
"f: <sip:caller@ims>;tag=N3\r\n"
"t: <sip:me@ims>\r\n"
"i: call3@net\r\n"
"CSeq: 1 INVITE\r\n"
"l: 0\r\n\r\n";
const std::string ringing = BuildInviteResponse(c, invite, 180, "Ringing", "T180");
Eq(ringing,
"SIP/2.0 180 Ringing\r\n"
"Via: SIP/2.0/TCP [2001:db8::105]:6000;branch=z9hG4bKnet3\r\n"
"Via: SIP/2.0/UDP [2001:db8::77]:5060;branch=z9hG4bKgw3\r\n"
"Record-Route: <sip:[2001:db8::105]:6000;lr>\r\n"
"From: <sip:caller@ims>;tag=N3\r\n"
"To: <sip:me@ims>;tag=T180\r\n"
"Call-ID: call3@net\r\n"
"CSeq: 1 INVITE\r\n"
"Contact: <sip:001010123456789@[2001:db8::db43]:45062>\r\n"
"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, PRACK, UPDATE, MESSAGE, REFER\r\n"
"Content-Length: 0\r\n\r\n", "a 180 for an INVITE echoes every Via and the Record-Route, tags To and carries our Contact and Allow");
Eq(BuildInviteResponse(c, compactInvite, 180, "Ringing", "T180"), ringing, "a 180 for an INVITE in compact form is the one for its long form");
Eq(BuildInviteResponse(c, compactInvite, 486, "Busy Here", "T486"), BuildInviteResponse(c, invite, 486, "Busy Here", "T486"), "and so is a 486");
Check(BuildInviteResponse(c, invite, 486, "Busy Here", "T486").contains("\r\nTo: <sip:me@ims>;tag=T486\r\n") && !BuildInviteResponse(c, invite, 486, "Busy Here", "T486").contains("Record-Route"), "a 486 tags To and returns no Record-Route");
{
const std::string body = "hi\r\nVia: SIP/2.0/UDP 192.0.2.66;branch=z9hG4bKx";
const std::string tail = "Content-Length: 0\r\n\r\n";

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)