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:
parent
7cae603c0c
commit
5f01b46909
2 changed files with 107 additions and 31 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue