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:
parent
c81dd3d267
commit
7cae603c0c
6 changed files with 164 additions and 11 deletions
|
|
@ -542,6 +542,19 @@ export namespace imsd::msg {
|
|||
return std::format("{}\r\n", detail::Join(l));
|
||||
}
|
||||
|
||||
// The 400 to a request whose datagram ended before its body did (RFC
|
||||
// 3261 18.3), from the request's header section
|
||||
// (imsd::sip::CutShortRequest), as BuildResponse makes a final. imsd
|
||||
// keeps nothing of such a request, so the To tag is made from the
|
||||
// request itself, "in a manner that will generate the same tag for the
|
||||
// same request consistently" (8.2.7), and from `secret`, which the
|
||||
// caller draws once for the run: the randomness 19.3 asks of a tag. It
|
||||
// is the first sixteen hex digits of the MD5 of the secret and that
|
||||
// header section.
|
||||
std::string BuildBadRequest(std::string_view head, std::string_view secret) {
|
||||
return BuildResponse({}, head, 400, "Bad Request", imsd::util::Md5::HexDigest(std::format("{}{}", secret, head)).substr(0, 16));
|
||||
}
|
||||
|
||||
// A 200 OK for an inbound request: echo Via/From/To/Call-ID/CSeq in order,
|
||||
// then either an SDP answer (with our Contact) or an empty body.
|
||||
// `extra` appends headers (Session-Expires echo for refreshes...).
|
||||
|
|
|
|||
|
|
@ -490,4 +490,35 @@ export namespace imsd::sip {
|
|||
}
|
||||
return std::string(d);
|
||||
}
|
||||
|
||||
// RFC 3261 18.3 of 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 header section of such a request, with the empty line
|
||||
// that ends it and no body — what the 400 is built from — or nullopt
|
||||
// for every other datagram: one whose body is all there, by its
|
||||
// Content-Length or for want of one; one without an end of headers;
|
||||
// one whose first line is no request's; an ACK or a CANCEL. A request's
|
||||
// first line ends with the SIP version (7.1: "Request-Line = Method SP
|
||||
// Request-URI SP SIP-Version CRLF") and a status line begins with it
|
||||
// (7.2), in whatever letter case ("The SIP-Version string is
|
||||
// case-insensitive"): a response is none, and neither are bytes that
|
||||
// are no SIP. imsd keeps nothing of a request it answers so, and of an
|
||||
// element that keeps nothing 8.2.7 says: "A stateless UAS MUST ignore
|
||||
// ACK requests." "A stateless UAS MUST ignore CANCEL requests."
|
||||
std::optional<std::string> CutShortRequest(std::string_view d) {
|
||||
const std::size_t start = d.find_first_not_of("\r\n");
|
||||
if (start == std::string_view::npos) return std::nullopt;
|
||||
d.remove_prefix(start);
|
||||
const std::size_t idx = d.find("\r\n\r\n");
|
||||
if (idx == std::string_view::npos) return std::nullopt;
|
||||
const std::string_view head = d.substr(0, idx);
|
||||
const auto length = Header(head, "Content-Length").and_then(LengthOf);
|
||||
if (!length || *length <= d.size() - (idx + 4)) return std::nullopt;
|
||||
const std::string_view line = head.substr(0, head.find("\r\n"));
|
||||
const std::string_view last = line.substr(line.rfind(' ') + 1); // of a line without a space, the line
|
||||
if (last.size() != 7 || !IEqualsPrefix(last, "SIP/2.0") || IEqualsPrefix(line, "SIP/")) return std::nullopt;
|
||||
if (line.starts_with("ACK ") || line.starts_with("CANCEL ")) return std::nullopt;
|
||||
return std::string(head) + "\r\n\r\n";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue