sip, messages, imsd: the client flow answers a request datagram cut short of the Content-Length it reads with a 400, unless an ACK or a CANCEL

RFC 3261 18.3 on 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 client flow's datagram read ignored either, where the
Content-Length it read said more bytes than came, with one line, `UDP:
datagram truncated (…) — ignored`.

CutShortRequest gives the header section of a request whose datagram
has fewer body bytes than the Content-Length read from it says, and
BuildBadRequest the 400 for it, in the shape of BuildResponse's finals:
every Via row echoed, the To tagged unless it holds a tag. The client
flow sends that 400 and its line ends `— 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), in upper or in lower case: a
response cut short so, bytes that are no SIP cut short so and a
datagram without an end of headers are ignored as before. imsd keeps
nothing of a request it answers so, and follows 8.2.7 for an element
that answers without state: a To tag it adds is made from the
request's header section and a secret drawn once for the run, "in a
manner that will generate the same tag for the same request
consistently", and an ACK or a CANCEL cut short so gets no answer. The
server ports and the init port hand a datagram on as it came, as
before this commit.

Tested by tests/Sip (CutShortRequest: the header section of a request
cut short, with line breaks before it, under a compact Content-Length
and with its version in lower case; nothing for a body that is all
there, no Content-Length, no end of headers, a response in either
letter case, bytes that are no SIP, an ACK, a CANCEL) and by
tests/Messages (the 400 by its lines, its tag against Python's
hashlib; the same request, the same 400; another request or another
secret, another tag). On the workstation's loopback, SipFlow built
around a bench main (the fp6 journal's 2026-11-20-issues-2-5 capture,
cut-short, at this commit and at the one before): a MESSAGE with five
body bytes of nine draws the 400, and the same one when it is sent
again, where the commit before ignored it; a response cut short the
same way, in upper and in lower case, an ACK, a CANCEL, bytes that are
no SIP and a request without an end of headers draw nothing at either
commit; the MESSAGE with its Content-Length folded is handed on at
either commit, a folded value being read by its first line; a whole
request sent after them is returned by the flow.
This commit is contained in:
Jorijn van der Graaf 2026-11-22 17:35:11 +01:00
commit 7cae603c0c
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 164 additions and 11 deletions

View file

@ -370,6 +370,14 @@ struct RefusedError : std::runtime_error {
// 1280-byte MTU (40 IPv6 + 8 ESP + 16 IV + 8 UDP + padding + 12 ICV).
constexpr std::size_t UdpSinglePacketBudget = 1190;
// The secret in the To tag of a 400 to a request cut short
// (imsd::msg::BuildBadRequest): drawn at the first such request, the same
// until imsd stops.
const std::string& BadRequestSecret() {
static const std::string secret = imsd::util::Rng().Token(16);
return secret;
}
class SipFlow {
public:
// TCP: connect up to `tries` times (12-s timeout each, 10 s apart). An
@ -527,9 +535,17 @@ public:
alive_ = false; return std::nullopt;
}
if (n == 0) continue;
const std::string_view d(buf, static_cast<std::size_t>(n));
std::string why;
if (auto m = imsd::sip::DatagramMessage(std::string_view(buf, static_cast<std::size_t>(n)), &why)) return m;
if (!why.empty()) Log(std::format("UDP: {} — ignored", why));
if (auto m = imsd::sip::DatagramMessage(d, &why)) return m;
if (!why.empty()) {
// A request cut short of its Content-Length is answered 400
// (RFC 3261 18.3; CutShortRequest says which); whatever else
// DatagramMessage refuses is ignored.
const auto head = imsd::sip::CutShortRequest(d);
Log(std::format("UDP: {} — {}", why, head ? "answered 400" : "ignored"));
if (head && !Send(imsd::msg::BuildBadRequest(*head, BadRequestSecret()))) return std::nullopt;
}
continue;
}
// EOF/RST is not a timeout: mark the flow dead so the engine's