imsd: a server port's answer finds its connection or socket when it is sent
A request that came in on a server port (the protected ports' TCP listeners and UDP sockets) was answered through a closure that kept a descriptor's number: its connection's over TCP, a socket's over UDP. The daemon closes a connection's descriptor when the peer has ended the connection, and the kernel gives a closed descriptor's number out again. A call's later answers go through the closure of the call's latest request when the user answers or refuses or the ringing ends (the 200 of an Accept, a 486, a 480): they were written to that number, whatever had it by then, the connection of whoever had connected since among them. A closure now keeps the port its request came in on and the peer it came from, or which of the two UDP ports its answer leaves by. Over TCP an answer is written to the newest connection the daemon holds from that peer to that port, and is not sent where it holds none: the journal says so once in a run. Over UDP it leaves from the socket its port has at that moment, if it has one. Tested: the daemon of this commit on the workstation against a stand-in network that closes the connection its INVITE came on while the call rings and then connects from another host, from another port, from the same port to the daemon's other port or from the same port to the same one, and one that resets the connection right behind its INVITE and makes it again (the fp6 journal's 2026-12-04-pani-in-dialog capture, bench/); and on the dev phone on KPN, where it answered a call that came in and the two requests that a text to the phone's own number brings (the journal's 2026-12-04-imsd-1.2.7-range-on-dev-phone capture).
This commit is contained in:
parent
1eee41f0ed
commit
6d05aaed0d
2 changed files with 132 additions and 12 deletions
|
|
@ -435,6 +435,30 @@ 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);
|
||||
}
|
||||
|
||||
// The host a socket address names, as text.
|
||||
std::string HostOf(const sockaddr_storage& ss) {
|
||||
char buf[INET6_ADDRSTRLEN] = {};
|
||||
if (ss.ss_family == AF_INET6) inet_ntop(AF_INET6, &reinterpret_cast<const sockaddr_in6*>(&ss)->sin6_addr, buf, sizeof buf);
|
||||
else inet_ntop(AF_INET, &reinterpret_cast<const sockaddr_in*>(&ss)->sin_addr, buf, sizeof buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Whether two socket addresses name the same host and port: for IPv6 the
|
||||
// scope too (a link-local address is a host of one link). One that names
|
||||
// none is the same as nothing, another such one included.
|
||||
bool SameEnd(const sockaddr_storage& a, const sockaddr_storage& b) {
|
||||
if (a.ss_family == AF_UNSPEC || a.ss_family != b.ss_family || PortOf(a) != PortOf(b) || HostOf(a) != HostOf(b)) return false;
|
||||
return a.ss_family != AF_INET6 ||
|
||||
reinterpret_cast<const sockaddr_in6*>(&a)->sin6_scope_id == reinterpret_cast<const sockaddr_in6*>(&b)->sin6_scope_id;
|
||||
}
|
||||
|
||||
// A socket's own name (getsockname); all zeros where that fails.
|
||||
sockaddr_storage NameOf(int fd) {
|
||||
sockaddr_storage ss{}; socklen_t l = sizeof ss;
|
||||
if (getsockname(fd, reinterpret_cast<sockaddr*>(&ss), &l) != 0) ss = {};
|
||||
return ss;
|
||||
}
|
||||
|
||||
// ---- SIP over the protected TCP flow --------------------------------------
|
||||
enum class Transport { Tcp, Udp };
|
||||
constexpr std::string_view TransportName(Transport t) { return t == Transport::Udp ? "UDP" : "TCP"; }
|
||||
|
|
@ -753,7 +777,7 @@ public:
|
|||
if (bind(us, reinterpret_cast<sockaddr*>(&a), l) == 0) {
|
||||
fcntl(us, F_SETFL, O_NONBLOCK);
|
||||
udp_.push_back(us);
|
||||
if (port == PortUc) udpUc_ = us;
|
||||
(port == PortUc ? udpUc_ : udpUs_) = us;
|
||||
} else { close(us); }
|
||||
}
|
||||
Log(std::format("listening on protected port {} (TCP+UDP)", port));
|
||||
|
|
@ -764,10 +788,11 @@ public:
|
|||
std::vector<Inbound> out;
|
||||
for (int ls : listeners_) {
|
||||
for (;;) {
|
||||
int c = accept(ls, nullptr, nullptr);
|
||||
sockaddr_storage peer{}; socklen_t pl = sizeof peer;
|
||||
int c = accept(ls, reinterpret_cast<sockaddr*>(&peer), &pl);
|
||||
if (c < 0) break;
|
||||
fcntl(c, F_SETFL, O_NONBLOCK);
|
||||
conns_.push_back({c, {}});
|
||||
conns_.push_back({c, {}, PortOf(NameOf(c)), peer});
|
||||
}
|
||||
}
|
||||
for (int us : udp_) {
|
||||
|
|
@ -827,8 +852,14 @@ public:
|
|||
htons(static_cast<uint16_t>(*vp));
|
||||
if (udpUc_ >= 0) rs = udpUc_;
|
||||
}
|
||||
auto reply = [rs, s, sll](const std::string& r) mutable {
|
||||
sendto(rs, r.data(), r.size(), 0, reinterpret_cast<sockaddr*>(&s), sll);
|
||||
// The closure keeps which of the two ports the answer
|
||||
// leaves by, not the socket's descriptor: CloseAll closes
|
||||
// that, and the kernel gives a closed descriptor's number
|
||||
// out again.
|
||||
auto reply = [this, uc = rs == udpUc_, s, sll](const std::string& r) mutable {
|
||||
const int from = uc ? udpUc_ : udpUs_;
|
||||
if (from < 0) return;
|
||||
sendto(from, 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));
|
||||
|
|
@ -850,9 +881,18 @@ public:
|
|||
}
|
||||
while (auto m = it->fb.TryExtract()) {
|
||||
if (!imsd::sip::Status(*m).has_value()) {
|
||||
int fd = it->fd;
|
||||
out.push_back({*m, [fd](const std::string& r) {
|
||||
(void)!write(fd, r.data(), r.size());
|
||||
// What is sent through this closure is written to the
|
||||
// newest connection held from this request's peer to the
|
||||
// port the request came in on, and is not sent where none
|
||||
// is held. The closure keeps the port and the peer, not
|
||||
// the descriptor: Poll closes that when the peer has ended
|
||||
// the connection, and the kernel gives a closed
|
||||
// descriptor's number out again, to a pipe, a file or the
|
||||
// connection of whoever connects next.
|
||||
out.push_back({*m, [this, port = it->port, peer = it->peer](const std::string& r) {
|
||||
const Conn* c = Find(port, peer);
|
||||
if (!c) { Gone(port, peer); return; }
|
||||
(void)!write(c->fd, r.data(), r.size());
|
||||
}});
|
||||
}
|
||||
}
|
||||
|
|
@ -866,11 +906,32 @@ public:
|
|||
for (int f : udp_) close(f);
|
||||
for (auto& c : conns_) close(c.fd);
|
||||
listeners_.clear(); udp_.clear(); conns_.clear();
|
||||
udpUc_ = -1;
|
||||
udpUc_ = udpUs_ = -1;
|
||||
}
|
||||
private:
|
||||
struct Conn { int fd; imsd::sip::FrameBuffer fb; };
|
||||
int udpUc_ = -1;
|
||||
// An accepted connection: the port of ours it came in on and the peer
|
||||
// accept named.
|
||||
struct Conn { int fd; imsd::sip::FrameBuffer fb; int port; sockaddr_storage peer; };
|
||||
|
||||
// The connection held from `peer` to our port `port`: the newest, where
|
||||
// an older one that ended in an error stands until Poll reads its end;
|
||||
// none where none is held.
|
||||
const Conn* Find(int port, const sockaddr_storage& peer) const {
|
||||
const Conn* found = nullptr;
|
||||
for (const Conn& c : conns_)
|
||||
if (c.port == port && SameEnd(c.peer, peer)) found = &c;
|
||||
return found;
|
||||
}
|
||||
|
||||
// An answer with no connection to go out on.
|
||||
void Gone(int port, const sockaddr_storage& peer) {
|
||||
if (!saidClosed_) Log(std::format("an answer from port {} to {} is not sent: no connection from that peer to that port is held (said once)", port,
|
||||
imsd::util::HostPort(HostOf(peer), PortOf(peer))));
|
||||
saidClosed_ = true;
|
||||
}
|
||||
|
||||
bool saidClosed_ = false;
|
||||
int udpUc_ = -1, udpUs_ = -1;
|
||||
std::vector<int> listeners_, udp_;
|
||||
std::vector<Conn> conns_;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue