imsd: a datagram on a server port or the init port is framed as one on the client flow is

SipFlow passes a datagram of the client flow through DatagramMessage:
one datagram is one message, the bytes past the Content-Length read
from it are no part of it and one whose body is shorter than that
length is no message (RFC 3261 18.3), nor is one that has no end of
headers. The two other places that read a datagram handed it on as it
came: the protected server ports, where the network's requests arrive,
and the init port, where the 401 to the unprotected REGISTER does.
Bytes after a body so reached its readers: behind an RP-ACK or an
RP-ERROR without user data, bytes shaped like that element (0x41, a
length and at least as many bytes) would be read as its submit report.

Both go through DatagramMessage now. On a server port a request cut
short of the Content-Length read from it is answered 400 as on the
client flow (Jorijn's decision of 2026-11-22, asked of him for this
port), by the way any answer from that port goes, and its line reads
`UDP: … on server socket <port> — answered 400`; whatever else
DatagramMessage refuses there is ignored with that line's `— ignored`.
The init port answers no request, whole or cut short: what
DatagramMessage refuses there is ignored with `UDP: … on the init
port, from port <n> — ignored`. A datagram of line breaks alone is
passed over on all three, and so is one of no bytes: on the init port
it ended the wait for the 401, and on a server port that pass's read
of the socket, with whatever stood behind it left for the next pass.
The server ports pass over one of other white space alone, as before.

Tested on the workstation's loopback with stand-ins for the network
(the fp6 journal's 2026-11-20-issues-2-5 capture, init-port and
server-ports; each at this commit and at the one before). The init
port, with the daemon in dev mode: an empty datagram and one of line
breaks are passed over, a 401 cut short of its Content-Length is
ignored with its line, and the 401 after each is taken, where the
commit before ended its wait at the empty one and took the cut-short
one for the reply; a request, cut short or whole, draws no answer at
either. The server ports, their class built around a bench main: a
request with bytes after its body comes out without them; one with a
short body is answered 400 on either port; a response, an ACK and a
CANCEL cut short so and a request without an end of headers are
ignored with their lines; a request behind a datagram of no bytes
comes out in the same pass. The commit before handed each of those
requests on as it came and stopped its pass at the datagram of no
bytes. A request whose Content-Length is folded is handed on at either
commit, with a body shorter than that length or with the bytes past
it, a folded value being read by its first line. On the dev phone the
binary of this range's last commit registered fresh through the init
port after a reboot; the submit report and the delivery of each of two
texts came to the server port as datagrams, by a packet counter; and a
MESSAGE with a Content-Length larger than its body, sent from the
phone to its own server port, was answered 400 (the capture's
phone-form6: fresh-check.log, checks.log, inject-bench.log).
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:11 +01:00
commit 5f01b46909
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 107 additions and 31 deletions

View file

@ -485,8 +485,8 @@ Rules:
and `detached` not counted, `connecting` with `unknown` counted), its
other states, ModemManager not answering and its second site (a refresh
unanswered over a reconnected flow) not at all, and `tests/Engine` pin
nothing of it; the REGISTER wait's diverting arms — a datagram skipped on
the init port, a response to another transaction ignored, a provisional
nothing of it; the REGISTER wait's diverting arms — a response to
another transaction ignored, a provisional
restarting the wait, a request (an INVITE among them) dispatched and
answered under the wait; an incoming call answered from a session's
dialer since the polkit gate — imsd-dialerd's `Accept` by the active
@ -638,19 +638,19 @@ Rules:
same-second retransmission was answered again and absorbed, the delivery
report answered 202. No capture of the benches on
hardware holds a journal line of one of these arms (each arm's line is searched in them in the
fp6 journal's `2026-11-19-imsd-1.2.0-r32-cut` capture, `searches/arms.out`): a refused final to a submission's MESSAGE, an RP-ERROR this
fp6 journal's `2026-11-19-imsd-1.2.0-r32-cut` capture, `searches/arms.out`, and for the phone's
visits of 2026-11-20 to 2026-11-22 in its `2026-11-20-issues-2-5`
capture, `phone/arms.out` and `phone-form6/arms.out`): a refused final to a submission's MESSAGE, an RP-ERROR this
code sends (the cause-96 unusable-TPDU path; an RP-ERROR that came in was
reached: `smsc-override-2026-10-18-0217`, 02:19:41, RP cause 28 + TP-FCS
0xc1, through `OnReport`), the final's wait and the report's
running out, a dead flow under a `Send`, a status report (one requested
0xc1, through `OnReport`), the final's wait running out, the report's wait ending a message as `failed`, a dead flow under a `Send`, a status report (one requested
at 01:33 with TP-SRR; none came in the following 11 minutes — the
unfiltered journal of 01:33:30–01:45:00,
`r23-phone-journal-2026-10-17-precise-and-modem-sms`; whether KPN's SC
sends them over this path is unknown), 8-bit data (an MMS
notification), a message while SMS over IP is off (the 480), a submit
report In-Reply-To no MESSAGE of ours in flight (the 488) or one without
In-Reply-To for no message in flight (the 200), a page-mode MESSAGE, a
body that is no RP message, a delivery that names no gateway, a text
In-Reply-To for no message in flight (the 200), a page-mode MESSAGE, a body that is no RP message, a text
past 255 segments, an EF_SMSP
record 1 that cannot be read or holds no service centre, or one whose
service centre is not an international number, a
@ -709,6 +709,35 @@ Rules:
them was returned by the flow; at the commit before, the cut-short
request was ignored as they were. Not in that bench: a 400 the flow
cannot send.
- The daemon reads SIP from a socket in four places — the client flow,
the server ports' datagrams, their connections, the init port — and
each goes through a framer of `Imsd:Sip`. The two datagram reads 1.2.1 put behind `DatagramMessage` ran on the workstation's loopback
(2026-11-22, the fp6 journal's `2026-11-20-issues-2-5` capture). The
init port (`init-port`: the daemon in dev mode, its card and modem
reads answered by stand-ins, a stand-in P-CSCF answering the
unprotected REGISTER): a datagram of no bytes and one of line
breaks alone passed over, a 401 cut short of its Content-Length ignored
with its `UDP: datagram truncated … on the init port` line, and the 401
after each taken; at the commit before, the empty datagram ended the
wait (`no 401 to unprotected REGISTER`) and the cut-short 401 was taken
for the reply. A request sent there, cut short or whole, drew a line
and no answer at either commit. The server ports (`server-ports`:
`ServerPorts` built around a bench `main`): a request with bytes after
its body handed on without them; one whose body is shorter than its
Content-Length answered 400 on either port, its Via, From, Call-ID and
CSeq echoed and its To tagged — the Content-Length of each on one line; a response, an ACK and a CANCEL cut
short so, and a request without an end of headers, ignored with their
lines; a datagram of line breaks alone passed over, and one of no bytes
with the request behind it handed on in the same pass; at the commit
before, those requests were handed on as they came, and the datagram of no
bytes ended the pass, the request behind it handed on in the next. On
the dev phone (2026-11-22, the capture's `phone-form6`, the binary of
1.2.1) the 401 of a fresh registration after a reboot
came through the init port's read, and the submit report and the
delivery of each of two texts through the server ports' as datagrams (two per
text by a packet counter, none over TCP: `checks.log`); a MESSAGE the
phone sent to its own server port with a Content-Length larger than
its body was answered 400 (`inject-bench.log`).
- `imsd-pcscf` ran on the dev phone (KPN) on 2026-10-11 against the live ims
bearer: the `--mux 1 --profile 2 -v` sequence, a plain run, and `OutOfCall`
on an unbound mux (the fp6 journal's `2026-10-11-pcscf-pco-probe` capture,
@ -1618,18 +1647,27 @@ found under its long name or, where it has one, its compact form (RFC
3261 7.3.3: a `c:` line is a `Content-Type`, an `i:` line a Call-ID, a
`v:` line a Via), with or without white space before the colon; of a
value folded onto further lines the first line is read (7.3.1 allows the
fold). A request that reaches the client flow in a datagram with fewer body
bytes than the Content-Length read from it says is answered 400 (RFC 3261 18.3: the element "SHOULD generate a 400 (Bad
Request) response"; the journal has `UDP: datagram truncated (…) —
answered 400`). Such a request is known by its first line, which ends with
the SIP version where a status line begins with it (7.1, 7.2), whatever
the version's letter case: a datagram cut short so that begins with a
status line, or with no line of SIP, is ignored, its line ending `—
ignored`. imsd keeps nothing of a request it answers so: a To tag
it adds is made from the request and a secret of the run, the same for the
same request (`BuildBadRequest`), and an ACK or a CANCEL cut short so draws
nothing — what RFC 3261 8.2.7 has an element do that answers without
state.
fold). A datagram on a server port or on the init port is
framed as one on the client flow is (RFC 3261 18.3): bytes past the
Content-Length read from it are discarded, and one that has no end of headers is
ignored with a line (`UDP: … on server socket <port> — ignored`, `UDP: …
on the init port, from port <n> — ignored`); one of no bytes or of line
breaks alone is passed over without a line, and so is one of other white
space alone on a server port. A request that reaches the client flow or a
server port in a datagram with fewer body bytes than the Content-Length
read from it says is answered 400 (RFC 3261
18.3: the element "SHOULD generate a 400 (Bad Request) response"; the
journal has `UDP: datagram truncated (…) — answered 400`, with the
server port named before the dash). Such a request is known by its first
line, which ends with the SIP version where a status line begins with
it (7.1, 7.2), whatever the version's letter case: a datagram cut short
so that begins with a status line, or with no line of SIP, is ignored,
its line ending `— ignored`, and so is a request cut short so on the init
port, which answers no request. imsd keeps nothing of a request it
answers so: a To tag it adds is made from the request and a secret of
the run, the same for the same request (`BuildBadRequest`), and an ACK
or a CANCEL cut short so draws nothing — what RFC 3261 8.2.7 has an element
do that answers without state.
- **A retransmitted MESSAGE** (the same Call-ID, CSeq and Via branch — the
value of the first `branch=` of its first Via row; a MESSAGE with none

View file

@ -341,6 +341,11 @@ socklen_t MakeAddr(std::string_view ip, int port, sockaddr_storage& ss) {
return sizeof(sockaddr_in);
}
// The port a socket address names.
int PortOf(const sockaddr_storage& ss) {
return ss.ss_family == AF_INET6 ? ntohs(reinterpret_cast<const sockaddr_in6*>(&ss)->sin6_port) : ntohs(reinterpret_cast<const sockaddr_in*>(&ss)->sin_port);
}
// ---- SIP over the protected TCP flow --------------------------------------
enum class Transport { Tcp, Udp };
constexpr std::string_view TransportName(Transport t) { return t == Transport::Udp ? "UDP" : "TCP"; }
@ -624,17 +629,35 @@ public:
char buf[65535];
sockaddr_storage src; socklen_t sl = sizeof src;
ssize_t n = recvfrom(us, buf, sizeof buf, 0, reinterpret_cast<sockaddr*>(&src), &sl);
if (n <= 0) break;
if (n < 0) break;
std::string_view sv(buf, static_cast<std::size_t>(n));
bool blank = sv.find_first_not_of("\r\n \t") == std::string_view::npos;
if (blank) continue;
std::string msg(sv);
// One datagram = one message, framed as on the client flow
// (RFC 3261 18.3): bytes past its Content-Length are
// discarded; a request whose body is shorter than its
// Content-Length is answered 400 (CutShortRequest says
// which), the way any answer goes from here (below);
// whatever else DatagramMessage refuses is ignored with a
// line. One of white space alone, or of no bytes, was
// passed over above.
std::string why;
auto framed = imsd::sip::DatagramMessage(sv, &why);
bool cutShort = false;
if (!framed) {
framed = imsd::sip::CutShortRequest(sv);
cutShort = framed.has_value();
}
if (!framed) {
if (!why.empty()) Log(std::format("UDP: {} on server socket {} — ignored", why, us == udpUc_ ? PortUc : PortUs));
continue;
}
std::string msg = std::move(*framed);
if (imsd::sip::Status(msg).has_value()) {
// A response here means the P-CSCF answered to the Via port
// instead of the source port of our request (rport) — it
// never reaches the client flow. Say so.
int sp = src.ss_family == AF_INET6 ? ntohs(reinterpret_cast<sockaddr_in6*>(&src)->sin6_port) : ntohs(reinterpret_cast<sockaddr_in*>(&src)->sin_port);
Log(std::format("UDP response on server socket {} from port {} ignored: {}", us == udpUc_ ? PortUc : PortUs, sp, sv.substr(0, sv.find("\r\n"))));
Log(std::format("UDP response on server socket {} from port {} ignored: {}", us == udpUc_ ? PortUc : PortUs, PortOf(src), std::string_view(msg).substr(0, msg.find("\r\n"))));
continue;
}
if (!imsd::sip::Status(msg).has_value()) { // request only
@ -658,9 +681,15 @@ public:
htons(static_cast<uint16_t>(*vp));
if (udpUc_ >= 0) rs = udpUc_;
}
out.push_back({std::move(msg), [rs, s, sll](const std::string& r) mutable {
auto reply = [rs, s, sll](const std::string& r) mutable {
sendto(rs, r.data(), r.size(), 0, reinterpret_cast<sockaddr*>(&s), sll);
}});
};
if (cutShort) {
Log(std::format("UDP: {} on server socket {} — answered 400", why, us == udpUc_ ? PortUc : PortUs));
reply(imsd::msg::BuildBadRequest(msg, BadRequestSecret()));
continue;
}
out.push_back({std::move(msg), std::move(reply)});
}
}
}
@ -2069,9 +2098,12 @@ private:
// The final of the unprotected REGISTER (callid, CSeq 1) on the init
// socket, within 8 s of the send or of its last provisional. The socket
// is bound to the init port, not connected: what else it delivers — a
// late final of an earlier attempt, a stray datagram — is logged with
// its source port and skipped, never judged as the reply. Closes the
// socket; throws when nothing answers.
// late final of an earlier attempt, a stray datagram — is skipped,
// never judged as the reply, and logged with its source port unless it
// holds nothing but line breaks. A datagram is framed as on the other
// sockets (RFC 3261 18.3; DatagramMessage), but a request gets no
// answer here, whole or cut short: the socket is that REGISTER's own.
// Closes the socket; throws when nothing answers.
std::string AwaitInitialFinal(int us, const std::string& callid) {
double deadline = Mono() + 8.0;
char buf[65535];
@ -2085,10 +2117,16 @@ private:
setsockopt(us, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
sockaddr_storage src; socklen_t sl = sizeof src;
ssize_t n = recvfrom(us, buf, sizeof buf, 0, reinterpret_cast<sockaddr*>(&src), &sl);
if (n <= 0) break;
std::string m(buf, static_cast<std::size_t>(n));
if (n < 0) break;
const int sp = PortOf(src);
std::string why;
auto framed = imsd::sip::DatagramMessage(std::string_view(buf, static_cast<std::size_t>(n)), &why);
if (!framed) {
if (!why.empty()) Log(std::format("UDP: {} on the init port, from port {} — ignored", why, sp));
continue;
}
std::string m = std::move(*framed);
if (!imsd::sip::ResponseOf(m, callid, "1 REGISTER")) {
int sp = src.ss_family == AF_INET6 ? ntohs(reinterpret_cast<sockaddr_in6*>(&src)->sin6_port) : ntohs(reinterpret_cast<sockaddr_in*>(&src)->sin_port);
std::string_view sv = m;
Log(std::format("ignoring a datagram on the init port from port {} that is not the unprotected REGISTER's reply: {}", sp, sv.substr(0, sv.find("\r\n"))));
continue;