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
61
AGENTS.md
61
AGENTS.md
|
|
@ -180,7 +180,11 @@ sleep (1.2.5, §7.10) is the shell's but for when the phone has to be
|
||||||
awake (`WakeAt`) and the policy's wake after a local wait
|
awake (`WakeAt`) and the policy's wake after a local wait
|
||||||
(`RegRecovery::WakeTime`), which are `Imsd:Engine`'s, with tests; §7.10
|
(`RegRecovery::WakeTime`), which are `Imsd:Engine`'s, with tests; §7.10
|
||||||
has what the shell does for it, and §5 what of that ran against
|
has what the shell does for it, and §5 what of that ran against
|
||||||
stand-ins on the workstation and what on the dev phone. SMS over IP
|
stand-ins on the workstation and what on the dev phone. Where an answer
|
||||||
|
goes that the server ports send (§12: the connection found by its port
|
||||||
|
and its peer, an answer that is not sent, the socket a datagram leaves
|
||||||
|
from) is the shell's (`ServerPorts`); §5 has what of it ran against a
|
||||||
|
stand-in network on the workstation and what on the dev phone. SMS over IP
|
||||||
(1.2.0, §7.9) keeps in the
|
(1.2.0, §7.9) keeps in the
|
||||||
shell, among others, the service centre's source (`SMSC=` over EF_SMSP, `LoadSmsParameters`),
|
shell, among others, the service centre's source (`SMSC=` over EF_SMSP, `LoadSmsParameters`),
|
||||||
the three counters (TP-MR, RP-MR, the concatenation reference), their
|
the three counters (TP-MR, RP-MR, the concatenation reference), their
|
||||||
|
|
@ -1309,6 +1313,46 @@ Rules:
|
||||||
through a step's wait while a text is in flight, which is read from
|
through a step's wait while a text is in flight, which is read from
|
||||||
the code;
|
the code;
|
||||||
the reg-event subscription's renewal on a network.
|
the reg-event subscription's renewal on a network.
|
||||||
|
- Where an answer goes that the server ports send (§12) is the shell's
|
||||||
|
(`ServerPorts`), and no suite reaches it. On the workstation the daemon
|
||||||
|
ran against a stand-in network that sends it requests over TCP and UDP
|
||||||
|
and can close or reset a connection behind one (the fp6 journal's
|
||||||
|
`2026-12-04-pani-in-dialog` capture, `bench/bench-pani.sh`: the daemon
|
||||||
|
in dev mode, its card and modem reads answered by stand-ins, an
|
||||||
|
incoming call accepted on its bus). Where the network closed the
|
||||||
|
connection its INVITE had come on while the call rang, the 200 of the
|
||||||
|
Accept arrived on the connection the same peer had made again to the
|
||||||
|
same port, also where the network had reset the first right behind its
|
||||||
|
INVITE (that call's 100 and 180 did not arrive: the connection they
|
||||||
|
were written to had been reset); it was not sent, with the journal's
|
||||||
|
line, where the only new connection was another host's, another port's
|
||||||
|
of the same host, or the same peer's to the daemon's other port (the
|
||||||
|
release before, 1.2.6, wrote it on the other host's connection and on
|
||||||
|
the one to the other port; with another port of the same host it was
|
||||||
|
not run); and it arrived on the INVITE's own connection where the peer
|
||||||
|
made a second one to the other port while the call rang. Two requests
|
||||||
|
whose connections the network closed right behind them were not
|
||||||
|
answered, with one line. Over UDP five OPTIONS requests were answered
|
||||||
|
from the ports the release before answered them from; two of the five
|
||||||
|
had no `rport` in their Via and were answered at the port that Via
|
||||||
|
names. On the dev phone on KPN (2026-12-04; the fp6 journal's
|
||||||
|
`2026-12-04-imsd-1.2.7-range-on-dev-phone` capture: the daemon in the
|
||||||
|
packaged one's place, resuming its registration, and the SA pair's
|
||||||
|
packets read on the phone) the two requests that a text to the phone's
|
||||||
|
own number brings came in over UDP, and each 200 left from 45061 for
|
||||||
|
the P-CSCF's protected server port, which their Via names; the text
|
||||||
|
was delivered, and the network sent neither request again. A call
|
||||||
|
came in there over TCP, on a connection the P-CSCF had made to 45062
|
||||||
|
and had sent two NOTIFYs on: the 200s to those, the call's 100, its
|
||||||
|
180 and the 200 of the Accept left on that connection, and the 200 to
|
||||||
|
its BYE, which came over UDP, from 45061. Of what ran against the
|
||||||
|
stand-in network nothing else has been seen on a real one (a
|
||||||
|
connection closed or reset behind a request and an answer that is not
|
||||||
|
sent among it).
|
||||||
|
Not run: an answer given after the daemon has moved to another
|
||||||
|
address; a peer on a link-local or an IPv4 address; a connection that
|
||||||
|
ends in an error and is made again within one turn of the daemon's
|
||||||
|
loop.
|
||||||
- `imsd-pcscf` ran on the dev phone (KPN) on 2026-10-11 against the live ims
|
- `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`
|
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,
|
on an unbound mux (the fp6 journal's `2026-10-11-pcscf-pco-probe` capture,
|
||||||
|
|
@ -2295,6 +2339,9 @@ Lines the operator (and the scripts) look for, all prefixed `imsd: `:
|
||||||
`INVITE final 422, Min-SE N` or `INVITE final 422, no Min-SE read`: the call's `ended:` line, or an
|
`INVITE final 422, Min-SE N` or `INVITE final 422, no Min-SE read`: the call's `ended:` line, or an
|
||||||
emergency attempt's `— retrying as plain INVITE of the digits`. In each of the three a `Reason` header of
|
emergency attempt's `— retrying as plain INVITE of the digits`. In each of the three a `Reason` header of
|
||||||
the 422 follows the `Min-SE` part (`…, Reason: …`).
|
the 422 follows the `Min-SE` part (`…, Reason: …`).
|
||||||
|
- `an answer from port N to [address]:port is not sent: no connection from that peer to that port is held
|
||||||
|
(said once)` the first time in a run that an answer is given to the channel of a request that came in over
|
||||||
|
TCP and `ServerPorts` holds no connection from that request's peer to that port (§12).
|
||||||
- `sleep is held off while the call lasts (logind's inhibitor)` when the
|
- `sleep is held off while the call lasts (logind's inhibitor)` when the
|
||||||
calls table's first call has its lock and `sleep is no longer held off
|
calls table's first call has its lock and `sleep is no longer held off
|
||||||
(no call)` when that lock is released, the table's last call gone
|
(no call)` when that lock is released, the table's last call gone
|
||||||
|
|
@ -3346,6 +3393,18 @@ only the imsd side of its bus choice.
|
||||||
kernel drops an ESP packet no SA matches, and what an SA decapsulates
|
kernel drops an ESP packet no SA matches, and what an SA decapsulates
|
||||||
passes the chain again, under the same rules as a packet that came
|
passes the chain again, under the same rules as a packet that came
|
||||||
without ESP.
|
without ESP.
|
||||||
|
- `ServerPorts` (the protected ports' TCP listeners and UDP sockets)
|
||||||
|
takes a request from whatever reaches it on the ims PDN (the entry on
|
||||||
|
short messages below). The channel of a request that came in there
|
||||||
|
over TCP is the newest connection `ServerPorts` holds from that
|
||||||
|
request's peer to the port it came in on; where it holds none, an
|
||||||
|
answer given to the channel is not sent (§7.8). A connection that has
|
||||||
|
ended in an error is held until `Poll` has read its end, and what is
|
||||||
|
written to it meanwhile is lost. Over UDP the channel is the socket
|
||||||
|
that the port the answer leaves by has when the answer is sent, if it
|
||||||
|
has one. A channel keeps no descriptor: the kernel gives a closed
|
||||||
|
descriptor's number out again, to a pipe, a file or the connection of
|
||||||
|
whoever connects next.
|
||||||
- Short messages (§7.9): `Send` is behind `net.catcrafts.IMS1.Messaging`
|
- Short messages (§7.9): `Send` is behind `net.catcrafts.IMS1.Messaging`
|
||||||
(an ungated `Send` would let any local account text from the
|
(an ungated `Send` would let any local account text from the
|
||||||
subscriber's number); the journal names a delivered message's sender, size and
|
subscriber's number); the journal names a delivered message's sender, size and
|
||||||
|
|
|
||||||
|
|
@ -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);
|
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 --------------------------------------
|
// ---- SIP over the protected TCP flow --------------------------------------
|
||||||
enum class Transport { Tcp, Udp };
|
enum class Transport { Tcp, Udp };
|
||||||
constexpr std::string_view TransportName(Transport t) { return t == Transport::Udp ? "UDP" : "TCP"; }
|
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) {
|
if (bind(us, reinterpret_cast<sockaddr*>(&a), l) == 0) {
|
||||||
fcntl(us, F_SETFL, O_NONBLOCK);
|
fcntl(us, F_SETFL, O_NONBLOCK);
|
||||||
udp_.push_back(us);
|
udp_.push_back(us);
|
||||||
if (port == PortUc) udpUc_ = us;
|
(port == PortUc ? udpUc_ : udpUs_) = us;
|
||||||
} else { close(us); }
|
} else { close(us); }
|
||||||
}
|
}
|
||||||
Log(std::format("listening on protected port {} (TCP+UDP)", port));
|
Log(std::format("listening on protected port {} (TCP+UDP)", port));
|
||||||
|
|
@ -764,10 +788,11 @@ public:
|
||||||
std::vector<Inbound> out;
|
std::vector<Inbound> out;
|
||||||
for (int ls : listeners_) {
|
for (int ls : listeners_) {
|
||||||
for (;;) {
|
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;
|
if (c < 0) break;
|
||||||
fcntl(c, F_SETFL, O_NONBLOCK);
|
fcntl(c, F_SETFL, O_NONBLOCK);
|
||||||
conns_.push_back({c, {}});
|
conns_.push_back({c, {}, PortOf(NameOf(c)), peer});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int us : udp_) {
|
for (int us : udp_) {
|
||||||
|
|
@ -827,8 +852,14 @@ public:
|
||||||
htons(static_cast<uint16_t>(*vp));
|
htons(static_cast<uint16_t>(*vp));
|
||||||
if (udpUc_ >= 0) rs = udpUc_;
|
if (udpUc_ >= 0) rs = udpUc_;
|
||||||
}
|
}
|
||||||
auto reply = [rs, s, sll](const std::string& r) mutable {
|
// The closure keeps which of the two ports the answer
|
||||||
sendto(rs, r.data(), r.size(), 0, reinterpret_cast<sockaddr*>(&s), sll);
|
// 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) {
|
if (cutShort) {
|
||||||
Log(std::format("UDP: {} on server socket {} — answered 400", why, us == udpUc_ ? PortUc : PortUs));
|
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()) {
|
while (auto m = it->fb.TryExtract()) {
|
||||||
if (!imsd::sip::Status(*m).has_value()) {
|
if (!imsd::sip::Status(*m).has_value()) {
|
||||||
int fd = it->fd;
|
// What is sent through this closure is written to the
|
||||||
out.push_back({*m, [fd](const std::string& r) {
|
// newest connection held from this request's peer to the
|
||||||
(void)!write(fd, r.data(), r.size());
|
// 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 (int f : udp_) close(f);
|
||||||
for (auto& c : conns_) close(c.fd);
|
for (auto& c : conns_) close(c.fd);
|
||||||
listeners_.clear(); udp_.clear(); conns_.clear();
|
listeners_.clear(); udp_.clear(); conns_.clear();
|
||||||
udpUc_ = -1;
|
udpUc_ = udpUs_ = -1;
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
struct Conn { int fd; imsd::sip::FrameBuffer fb; };
|
// An accepted connection: the port of ours it came in on and the peer
|
||||||
int udpUc_ = -1;
|
// 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<int> listeners_, udp_;
|
||||||
std::vector<Conn> conns_;
|
std::vector<Conn> conns_;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue