Implement M9.7 dev region sessions with durable no-effect intentions

Run a non-blocking TLS 1.3 session server inside evo-server's loop:
environment views come from the region's own field/terrain stores with an
explicitly empty organism presentation; admitted intentions ride the next
synthetic input (content unchanged) and return a durable no-effect outcome
from the host's final acknowledgement, or not_committed when dropped.
Sessions are bound to their connection, epochs close stale sessions, and
connections have bounded output, input rate and deadlines.

SessionClient and evo-client --connect consume one single-use grant per
Hello (refusals before admission keep it), reset to a fresh baseline on
every session, keep stale views visible and report in-flight intentions as
unknown after loss. evo-dev-session generates dev credentials, grant
bundles and runs a restart probe. The dev deployment gains a drill-time,
loopback-only listener with an optional Secret, enforced by the policy
check, and tools/ops/session-drill.sh. Protocol v1 gains IntentOutcome and
the capacity close reason before any deployment. ADR-0083 records the
maintainer-confirmed decisions; M8-dev/stage remains not passed and no
cluster change was made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-01 20:12:18 -04:00
co-authored by Claude Opus 5.5
parent 8fbe093cc8
commit 8e319322f4
49 changed files with 3467 additions and 130 deletions
+30 -2
View File
@@ -5,9 +5,10 @@
namespace evo::network {
namespace {
constexpr std::uint8_t last_type = static_cast<std::uint8_t>(MessageType::close);
constexpr std::uint8_t last_type = static_cast<std::uint8_t>(MessageType::intent_outcome);
constexpr std::uint8_t last_status = static_cast<std::uint8_t>(IntentStatus::rate_limited);
constexpr std::uint8_t last_reason = static_cast<std::uint8_t>(CloseReason::server_shutdown);
constexpr std::uint8_t last_reason = static_cast<std::uint8_t>(CloseReason::capacity);
constexpr std::uint8_t last_finality = static_cast<std::uint8_t>(IntentFinality::not_committed);
constexpr std::uint8_t last_presence = static_cast<std::uint8_t>(client::FieldPresence::present);
constexpr std::uint8_t last_moisture = static_cast<std::uint8_t>(world::MoistureStatus::available);
constexpr std::uint8_t last_state = static_cast<std::uint8_t>(client::OrganismState::dead);
@@ -279,6 +280,15 @@ std::vector<std::uint8_t> encode(CloseReason reason) {
return framed(MessageType::close, w);
}
std::vector<std::uint8_t> encode(const IntentOutcome& m) {
Writer w;
w.u64(m.sequence);
w.u8(static_cast<std::uint8_t>(m.finality));
w.u64(m.tick);
w.u64(m.commit_sequence);
return framed(MessageType::intent_outcome, w);
}
std::vector<std::uint8_t> encode(const ViewHeader& header,
const client::EnvironmentPresentation& view) {
Writer w;
@@ -431,6 +441,24 @@ std::expected<CloseReason, ProtocolError> decode_close(FrameView frame) {
return reason;
}
std::expected<IntentOutcome, ProtocolError> decode_intent_outcome(FrameView frame) {
auto r = open(frame, MessageType::intent_outcome);
if (!r)
return std::unexpected(r.error());
IntentOutcome m;
m.sequence = r->u64();
m.finality = enumerator<IntentFinality>(*r, last_finality);
m.tick = r->u64();
m.commit_sequence = r->u64();
if (auto done = r->finish(); !done)
return std::unexpected(done.error());
// Committed outcomes name their commit; lost ones carry no tick or commit.
if ((m.finality == IntentFinality::committed_no_effect) != (m.commit_sequence != 0) ||
(m.finality == IntentFinality::not_committed && m.tick != 0))
return std::unexpected(ProtocolError::invalid_value);
return m;
}
std::expected<std::pair<ViewHeader, client::EnvironmentPresentation>, ProtocolError>
decode_environment_view(FrameView frame) {
auto r = open(frame, MessageType::environment_view);
+297
View File
@@ -0,0 +1,297 @@
#include "evo/network/session_client.hpp"
#include "evo/network/tcp.hpp"
#include <algorithm>
#include <array>
#include <utility>
namespace evo::network {
namespace {
using Clock = std::chrono::steady_clock;
constexpr std::size_t max_read_per_poll = std::size_t{1} << 20;
} // namespace
struct SessionClient::Impl {
enum class Step : std::uint8_t { handshake, awaiting_welcome, established };
explicit Impl(SessionClientConfig c) : config{std::move(c)}, delay{config.first_retry} {}
SessionClientConfig config;
std::size_t next_grant{};
SessionPhase phase{SessionPhase::backoff};
Step step{Step::handshake};
Clock::time_point retry_at{}, deadline{};
std::chrono::milliseconds delay;
std::unique_ptr<TcpStream> stream; // Declared before the channel that borrows it.
std::unique_ptr<TlsChannel> tls;
FrameReader reader;
std::vector<std::uint8_t> out;
std::optional<Welcome> session;
std::optional<ViewHeader> view;
std::vector<std::uint64_t> outstanding; // Admitted, awaiting a final outcome.
client::EnvironmentLayer environment;
client::OrganismLayer organisms;
std::vector<ClientEvent> events;
void disconnect(Clock::time_point now, std::optional<CloseReason> reason) {
if (step == Step::established || step == Step::awaiting_welcome || reason)
events.push_back({reason ? ClientEvent::Kind::closed : ClientEvent::Kind::lost,
{},
reason.value_or(CloseReason::normal),
{},
{},
0});
for (const auto sequence : outstanding)
events.push_back({ClientEvent::Kind::unknown, {}, {}, {}, {}, sequence});
outstanding.clear();
tls.reset();
stream.reset();
reader = FrameReader{};
out.clear();
session.reset();
view.reset();
// Keep showing the last presentation, explicitly stale, until a new baseline.
environment.mark_stale();
organisms.mark_stale();
phase = SessionPhase::backoff;
step = Step::handshake;
retry_at = now + delay;
delay = std::min(delay * 2, config.max_retry);
}
void connect(Clock::time_point now) {
if (next_grant >= config.grants.size()) {
phase = SessionPhase::exhausted;
events.push_back({ClientEvent::Kind::exhausted, {}, {}, {}, {}, 0});
return;
}
auto s = TcpStream::connect(config.host, config.port);
if (!s) {
disconnect(now, std::nullopt);
return;
}
stream = std::move(*s);
auto t = TlsChannel::client(config.trust, *stream);
if (!t) {
disconnect(now, std::nullopt);
return;
}
tls = std::move(*t);
phase = SessionPhase::connecting;
step = Step::handshake;
deadline = now + config.attempt_deadline;
}
bool frame(const OwnedFrame& f, Clock::time_point now) {
const FrameView v{f.type, f.payload};
if (step == Step::awaiting_welcome) {
if (f.type == MessageType::close) {
const auto reason = decode_close(v);
// Refused before admission (not ready / full): the server never recorded
// the grant, so it stays usable for the next attempt.
if (reason &&
(*reason == CloseReason::server_shutdown || *reason == CloseReason::capacity))
--next_grant;
disconnect(now, reason.value_or(CloseReason::malformed));
return false;
}
const auto w = decode_welcome(v);
if (!w) {
disconnect(now, std::nullopt);
return false;
}
session = *w;
step = Step::established;
phase = SessionPhase::established;
delay = config.first_retry;
// A new session is a new authority view: drop everything from before.
environment.reset();
organisms.reset();
events.push_back({ClientEvent::Kind::welcome, *w, {}, {}, {}, 0});
return true;
}
switch (f.type) {
case MessageType::environment_view: {
auto d = decode_environment_view(v);
if (!d || d->first.session != session->session || d->first.epoch != session->epoch ||
!environment.accept(std::move(d->second))) {
disconnect(now, std::nullopt);
return false;
}
view = d->first;
return true;
}
case MessageType::organism_view: {
auto d = decode_organism_view(v);
if (!d || d->first.session != session->session || d->first.epoch != session->epoch ||
!organisms.accept(std::move(d->second))) {
disconnect(now, std::nullopt);
return false;
}
return true;
}
case MessageType::intent_result: {
const auto r = decode_intent_result(v);
if (!r) {
disconnect(now, std::nullopt);
return false;
}
if (r->status == IntentStatus::admitted) {
if (outstanding.size() >= max_outstanding) {
events.push_back(
{ClientEvent::Kind::unknown, {}, {}, {}, {}, outstanding.front()});
outstanding.erase(outstanding.begin());
}
outstanding.push_back(r->sequence);
}
events.push_back({ClientEvent::Kind::result, {}, {}, *r, {}, r->sequence});
return true;
}
case MessageType::intent_outcome: {
const auto o = decode_intent_outcome(v);
if (!o) {
disconnect(now, std::nullopt);
return false;
}
std::erase(outstanding, o->sequence);
events.push_back({ClientEvent::Kind::outcome, {}, {}, {}, *o, o->sequence});
return true;
}
case MessageType::close: {
const auto reason = decode_close(v);
disconnect(now, reason.value_or(CloseReason::malformed));
return false;
}
default:
disconnect(now, std::nullopt);
return false;
}
}
void flush(Clock::time_point now) {
std::size_t sent = 0;
while (sent < out.size()) {
const auto n = tls->send(std::span{out}.subspan(sent));
if (!n) {
disconnect(now, std::nullopt);
return;
}
if (*n == 0)
break;
sent += *n;
}
out.erase(out.begin(), out.begin() + static_cast<std::ptrdiff_t>(sent));
}
void receive(Clock::time_point now) {
std::array<std::uint8_t, 16384> buffer{};
std::size_t total = 0;
while (tls && total < max_read_per_poll) {
const auto n = tls->receive(buffer);
if (!n) {
disconnect(now, std::nullopt);
return;
}
if (*n == 0)
return;
total += *n;
std::span<const std::uint8_t> rest{buffer.data(), *n};
while (tls && !rest.empty()) {
const auto taken = reader.feed(rest);
if (!taken) {
disconnect(now, std::nullopt);
return;
}
rest = rest.subspan(*taken);
while (tls) {
auto f = reader.take();
if (!f || !frame(*f, now))
break;
}
}
}
}
};
SessionClient::SessionClient(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
SessionClient::~SessionClient() = default;
std::expected<std::unique_ptr<SessionClient>, SessionClientError>
SessionClient::create(SessionClientConfig config) {
if (config.host.empty() || config.port == 0 || config.grants.empty() ||
config.grants.size() > 1024 || config.lead_ticks == 0 || config.lead_ticks > 64 ||
config.first_retry.count() <= 0 || config.max_retry < config.first_retry ||
config.attempt_deadline.count() <= 0)
return std::unexpected(SessionClientError::configuration);
return std::unique_ptr<SessionClient>(
new SessionClient(std::make_unique<Impl>(std::move(config))));
}
void SessionClient::poll(std::chrono::steady_clock::time_point now) {
auto& m = *impl_;
if (m.phase == SessionPhase::exhausted)
return;
if (m.phase == SessionPhase::backoff) {
if (now < m.retry_at)
return;
m.connect(now);
if (m.phase != SessionPhase::connecting)
return;
}
if (m.step != Impl::Step::established && now > m.deadline) {
m.disconnect(now, std::nullopt);
return;
}
if (m.step == Impl::Step::handshake) {
const auto p = m.tls->handshake();
if (!p) {
m.disconnect(now, std::nullopt);
return;
}
if (*p == TlsProgress::want_io)
return;
// One single-use grant per session attempt, consumed when it is sent.
const auto& grant = m.config.grants.at(m.next_grant++);
const auto hello = encode(Hello{0, {grant.begin(), grant.end()}});
if (!hello) {
m.disconnect(now, std::nullopt);
return;
}
m.out.insert(m.out.end(), hello->begin(), hello->end());
m.step = Impl::Step::awaiting_welcome;
}
m.flush(now);
if (m.tls)
m.receive(now);
if (m.tls)
m.flush(now);
}
bool SessionClient::send(const client::Intention& intention) {
auto& m = *impl_;
if (m.phase != SessionPhase::established || !m.session || !m.view)
return false;
const auto tick = m.view->server_tick + m.config.lead_ticks;
const auto bytes =
encode(make_intent(m.session->session, m.session->epoch,
{intention.sequence, tick, intention.move_x, intention.move_y}));
m.out.insert(m.out.end(), bytes.begin(), bytes.end());
return true;
}
SessionPhase SessionClient::phase() const noexcept {
return impl_->phase;
}
const client::EnvironmentLayer& SessionClient::environment() const noexcept {
return impl_->environment;
}
const client::OrganismLayer& SessionClient::organisms() const noexcept {
return impl_->organisms;
}
std::optional<Welcome> SessionClient::welcome() const noexcept {
return impl_->session;
}
std::optional<ViewHeader> SessionClient::latest_view() const noexcept {
return impl_->view;
}
std::size_t SessionClient::grants_left() const noexcept {
return impl_->config.grants.size() - impl_->next_grant;
}
std::vector<ClientEvent> SessionClient::take_events() {
return std::exchange(impl_->events, {});
}
} // namespace evo::network
+415
View File
@@ -0,0 +1,415 @@
#include "evo/network/session_server.hpp"
#include <algorithm>
#include <utility>
namespace evo::network {
namespace {
using Clock = std::chrono::steady_clock;
constexpr std::size_t max_accepts_per_poll = 4;
constexpr std::size_t max_read_per_poll = 65536;
CloseReason close_reason(AdmissionError e) {
switch (e) {
case AdmissionError::duplicate_subject:
return CloseReason::duplicate_session;
case AdmissionError::expired:
return CloseReason::expired;
case AdmissionError::sessions_full:
case AdmissionError::grants_full:
return CloseReason::capacity;
default:
return CloseReason::unauthenticated;
}
}
CloseReason close_reason(ProtocolError e) {
return e == ProtocolError::unsupported_version ? CloseReason::unsupported_version
: CloseReason::malformed;
}
void append(std::vector<std::uint8_t>& out, const std::vector<std::uint8_t>& bytes) {
out.insert(out.end(), bytes.begin(), bytes.end());
}
} // namespace
struct SessionServer::Impl {
enum class State : std::uint8_t { handshake, hello, established, closing };
struct Connection {
std::uint64_t id{};
std::unique_ptr<TcpStream> stream; // Declared first: outlives the channel.
std::unique_ptr<TlsChannel> tls;
State state{State::handshake};
Clock::time_point deadline;
FrameReader reader;
std::vector<std::uint8_t> out, view;
std::uint64_t session{}, subject{}, epoch{}, expires_at{};
std::size_t window_bytes{};
Clock::time_point window_start;
bool dead{};
};
struct Pending {
RequestId request{};
std::uint64_t intended_tick{}, connection{}, sequence{};
};
struct Carried {
RequestId request{};
std::uint64_t connection{}, sequence{};
};
Impl(SessionServerConfig c, DevGrantKey k, std::unique_ptr<TcpListener> l)
: config{std::move(c)}, key{std::move(k)}, listener{std::move(l)} {}
SessionServerConfig config;
DevGrantKey key;
std::unique_ptr<TcpListener> listener;
std::optional<SessionAdmission> admission;
std::vector<Connection> connections;
std::deque<Pending> pending;
std::vector<Carried> carried;
std::vector<SessionEvent> events;
std::optional<client::EnvironmentPresentation> environment;
std::optional<client::OrganismPresentation> organisms;
RegionFacts published{};
std::uint64_t baseline{}, next_connection{1};
Clock::time_point polled_at{}; // Time of the current poll, for Close deadlines.
Connection* find(std::uint64_t id) {
const auto it = std::ranges::find_if(connections, [&](const Connection& c) {
return c.id == id && !c.dead && c.state == State::established;
});
return it == connections.end() ? nullptr : &*it;
}
void close(Connection& c, CloseReason reason) {
if (c.state == State::closing || c.dead)
return;
if (c.state == State::hello || c.state == State::established)
append(c.out, encode(reason));
else
c.dead = true; // No channel yet to say anything over.
events.push_back({c.state == State::established ? SessionEvent::Kind::closed
: SessionEvent::Kind::refused,
c.session, c.subject, reason});
c.state = State::closing;
c.deadline = polled_at + std::chrono::seconds{1}; // Bounded flush of the Close frame.
c.view.clear();
}
std::vector<std::uint8_t> views_for(const Connection& c) const {
std::vector<std::uint8_t> bytes;
if (!environment || !organisms)
return bytes;
const ViewHeader header{c.session, c.epoch, published.simulated_tick,
published.committed_tick, baseline};
append(bytes, encode(header, *environment));
append(bytes, encode(header, *organisms));
return bytes;
}
void hello(Connection& c, const OwnedFrame& f, const RegionFacts& facts,
std::uint64_t now_seconds) {
if (f.type != MessageType::hello) {
close(c, CloseReason::malformed);
return;
}
const auto message = decode_hello({f.type, f.payload});
if (!message) {
close(c, close_reason(message.error()));
return;
}
if (!admission || !facts.ready || facts.epoch != admission->epoch()) {
close(c, CloseReason::server_shutdown); // No current authority: retry later.
return;
}
const auto grant = key.verify(message->grant);
if (!grant) {
close(c, CloseReason::unauthenticated);
return;
}
const auto welcome =
admission->open(*grant, now_seconds, facts.simulated_tick, facts.committed_tick);
if (!welcome) {
c.subject = grant->claims().subject;
close(c, close_reason(welcome.error()));
return;
}
c.session = welcome->session;
c.subject = grant->claims().subject;
c.epoch = welcome->epoch;
c.expires_at = welcome->grant_expires_at;
c.state = State::established;
append(c.out, encode(*welcome));
c.view = views_for(c); // Fresh baseline for every new session.
events.push_back({SessionEvent::Kind::opened, c.session, c.subject, {}});
}
void intent(Connection& c, const OwnedFrame& f, const RegionFacts& facts,
std::uint64_t now_seconds) {
const auto m = decode_intent({f.type, f.payload});
if (!m) {
close(c, close_reason(m.error()));
return;
}
IntentResult result{m->sequence, IntentStatus::unknown_session, facts.simulated_tick};
// A connection may only speak for its own session.
if (m->session == c.session && admission) {
if (pending.size() >= config.max_pending_intents)
result.status = IntentStatus::rate_limited; // Host backlog, not admission.
else
result = admission->admit(*m, facts.simulated_tick, now_seconds);
}
if (result.status == IntentStatus::admitted)
pending.push_back({request_id(c, m->sequence), m->tick, c.id, m->sequence});
append(c.out, encode(result));
}
RequestId request_id(const Connection& c, std::uint64_t sequence) const {
RequestId id{'I', 'N', 'T', '1'};
const std::array<std::uint64_t, 3> parts{c.epoch, c.session, sequence};
for (std::size_t p = 0; p < parts.size(); ++p)
for (unsigned i = 0; i < 4; ++i)
id.at(4 + 4 * p + i) = static_cast<std::uint8_t>(parts.at(p) >> (8U * (3U - i)));
return id;
}
void receive(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds,
Clock::time_point now) {
std::array<std::uint8_t, 16384> buffer{};
std::size_t total = 0;
while (!c.dead && c.state != State::closing && total < max_read_per_poll) {
const auto n = c.tls->receive(buffer);
if (!n) {
c.dead = true;
return;
}
if (*n == 0)
return;
total += *n;
if (now - c.window_start >= std::chrono::seconds{1}) {
c.window_start = now;
c.window_bytes = 0;
}
c.window_bytes += *n;
if (c.window_bytes > config.max_input_bytes_per_second) {
close(c, CloseReason::rate_limited);
return;
}
std::span<const std::uint8_t> rest{buffer.data(), *n};
while (!rest.empty() && c.state != State::closing) {
const auto taken = c.reader.feed(rest);
if (!taken) {
close(c, close_reason(taken.error()));
return;
}
rest = rest.subspan(*taken);
while (auto f = c.reader.take()) {
if (c.state == State::hello)
hello(c, *f, facts, now_seconds);
else if (f->type == MessageType::intent)
intent(c, *f, facts, now_seconds);
else if (f->type == MessageType::close) {
c.state = State::closing; // Peer is leaving; nothing to say.
events.push_back({SessionEvent::Kind::closed, c.session, c.subject,
CloseReason::normal});
} else
close(c, CloseReason::malformed);
if (c.state == State::closing)
break;
}
}
}
}
void transmit(Connection& c) {
if (c.out.empty() && !c.view.empty() && c.state == State::established)
c.out.swap(c.view); // Only the newest view waits; it is never queued twice.
std::size_t sent = 0;
while (sent < c.out.size()) {
const auto n = c.tls->send(std::span{c.out}.subspan(sent));
if (!n) {
c.dead = true;
return;
}
if (*n == 0)
break;
sent += *n;
}
c.out.erase(c.out.begin(), c.out.begin() + static_cast<std::ptrdiff_t>(sent));
if (c.out.size() + c.view.size() > config.max_output_bytes)
c.dead = true; // Slow consumer: drop rather than buffer without bound.
if (c.state == State::closing && c.out.empty()) {
// Best-effort close_notify: the connection is dropped either way.
static_cast<void>(c.tls->close().has_value());
c.dead = true;
}
}
void step(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds,
Clock::time_point now) {
if (c.state == State::handshake) {
const auto p = c.tls->handshake();
if (!p) {
c.dead = true;
return;
}
if (*p == TlsProgress::done)
c.state = State::hello;
}
if (c.state == State::closing && now > c.deadline) {
c.dead = true; // Peer would not take the Close frame.
return;
}
if ((c.state == State::handshake || c.state == State::hello) && now > c.deadline)
close(c, CloseReason::unauthenticated);
if (c.state == State::established && now_seconds >= c.expires_at)
close(c, CloseReason::expired);
if (c.state == State::established && admission && c.epoch != admission->epoch())
close(c, CloseReason::stale_epoch);
if (c.state == State::hello || c.state == State::established)
receive(c, facts, now_seconds, now);
if (!c.dead && c.state != State::handshake)
transmit(c);
}
void bury() {
for (auto& c : connections) {
if (!c.dead)
continue;
if (c.session != 0 && admission)
// The session is ours; only an already-closed session could fail.
static_cast<void>(admission->close(c.session).has_value());
std::erase_if(pending, [&](const Pending& p) { return p.connection == c.id; });
}
std::erase_if(connections, [](const Connection& c) { return c.dead; });
}
};
SessionServer::SessionServer(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
SessionServer::~SessionServer() = default;
std::expected<std::unique_ptr<SessionServer>, SessionServerError>
SessionServer::create(SessionServerConfig config, DevGrantKey key) {
if (config.max_connections == 0 || config.max_connections > 64 ||
config.max_pending_intents == 0 || config.max_carried_intents == 0 ||
config.max_output_bytes < 2 * (frame_header_bytes + max_frame_payload) ||
config.max_input_bytes_per_second < frame_header_bytes + max_grant_bytes + 6)
return std::unexpected(SessionServerError::configuration);
// Validate the admission limits now, with a placeholder epoch.
auto probe = config.admission;
probe.epoch = 1;
if (!SessionAdmission::create(probe))
return std::unexpected(SessionServerError::configuration);
// Credentials must parse before the port opens.
struct Null final : ByteTransport {
std::expected<std::size_t, TransportError> write(std::span<const std::uint8_t>) override {
return 0;
}
std::expected<std::size_t, TransportError> read(std::span<std::uint8_t>) override {
return 0;
}
} null;
if (!TlsChannel::server(config.credentials, null))
return std::unexpected(SessionServerError::credentials);
auto listener = TcpListener::listen(config.listen);
if (!listener)
return std::unexpected(SessionServerError::listen);
return std::unique_ptr<SessionServer>(new SessionServer(
std::make_unique<Impl>(std::move(config), std::move(key), std::move(*listener))));
}
std::uint16_t SessionServer::port() const noexcept {
return impl_->listener->port();
}
void SessionServer::poll(const RegionFacts& facts, std::uint64_t now_seconds,
std::chrono::steady_clock::time_point now) {
auto& m = *impl_;
m.polled_at = now;
if (facts.ready && facts.epoch != 0) {
if (!m.admission) {
auto config = m.config.admission;
config.epoch = facts.epoch;
if (auto created = SessionAdmission::create(config))
m.admission.emplace(std::move(*created));
} else if (facts.epoch > m.admission->epoch()) {
// Strictly newer (checked above), so the advance cannot be refused.
static_cast<void>(m.admission->advance_epoch(facts.epoch).has_value());
// Intentions of the old epoch never ride new-epoch inputs.
m.pending.clear();
}
}
for (std::size_t i = 0; i < max_accepts_per_poll; ++i) {
auto accepted = m.listener->accept();
if (!accepted || !*accepted)
break;
if (m.connections.size() >= m.config.max_connections)
continue; // Over capacity: the stream closes as it goes out of scope.
Impl::Connection c;
c.id = m.next_connection++;
c.stream = std::move(*accepted);
auto tls = TlsChannel::server(m.config.credentials, *c.stream);
if (!tls)
continue;
c.tls = std::move(*tls);
c.deadline = now + m.config.hello_deadline;
c.window_start = now;
m.connections.push_back(std::move(c));
}
for (auto& c : m.connections)
if (!c.dead)
m.step(c, facts, now_seconds, now);
m.bury();
}
void SessionServer::publish(const client::EnvironmentPresentation& environment,
const client::OrganismPresentation& organisms,
const RegionFacts& facts) {
auto& m = *impl_;
m.environment = environment;
m.organisms = organisms;
m.published = facts;
++m.baseline;
for (auto& c : m.connections)
if (!c.dead && c.state == Impl::State::established)
c.view = m.views_for(c);
}
std::optional<RequestId> SessionServer::carry(std::uint64_t tick) {
auto& m = *impl_;
if (m.carried.size() >= m.config.max_carried_intents)
return std::nullopt;
const auto it = std::ranges::find_if(
m.pending, [&](const Impl::Pending& p) { return p.intended_tick <= tick; });
if (it == m.pending.end())
return std::nullopt;
const Impl::Carried carried{it->request, it->connection, it->sequence};
m.pending.erase(it);
m.carried.push_back(carried);
return carried.request;
}
void SessionServer::committed(const RequestId& request, std::uint64_t tick,
std::uint64_t commit_sequence) {
auto& m = *impl_;
const auto it = std::ranges::find_if(
m.carried, [&](const Impl::Carried& c) { return c.request == request; });
if (it == m.carried.end())
return;
if (auto* c = m.find(it->connection))
append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::committed_no_effect, tick,
commit_sequence}));
m.carried.erase(it);
}
void SessionServer::not_committed(const RequestId& request) {
auto& m = *impl_;
const auto it = std::ranges::find_if(
m.carried, [&](const Impl::Carried& c) { return c.request == request; });
if (it == m.carried.end())
return;
if (auto* c = m.find(it->connection))
append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::not_committed, 0, 0}));
m.carried.erase(it);
}
std::vector<SessionEvent> SessionServer::take_events() {
return std::exchange(impl_->events, {});
}
std::size_t SessionServer::connections() const noexcept {
return impl_->connections.size();
}
std::size_t SessionServer::sessions() const noexcept {
const auto& admission = impl_->admission;
return admission ? admission->sessions() : 0;
}
} // namespace evo::network
+267
View File
@@ -0,0 +1,267 @@
#include "evo/network/tcp.hpp"
#include <charconv>
#include <cstring>
#include <limits>
#include <mutex>
#include <string>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <cerrno>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
namespace evo::network {
namespace {
#ifdef _WIN32
using Native = SOCKET;
constexpr Native invalid = INVALID_SOCKET;
bool startup() {
static std::once_flag once;
static bool ok = false;
std::call_once(once, [] {
WSADATA data{};
ok = WSAStartup(MAKEWORD(2, 2), &data) == 0; // Process lifetime; never cleaned up.
});
return ok;
}
void close_native(Native s) {
closesocket(s);
}
bool would_block() {
const int e = WSAGetLastError();
return e == WSAEWOULDBLOCK || e == WSAEINPROGRESS;
}
bool non_blocking(Native s) {
u_long on = 1;
return ioctlsocket(s, FIONBIO, &on) == 0;
}
int poll_one(Native s, short events, short& revents) {
WSAPOLLFD fd{s, events, 0};
const int r = WSAPoll(&fd, 1, 0);
revents = fd.revents;
return r;
}
constexpr int send_flags = 0;
using Length = int;
#else
using Native = int;
constexpr Native invalid = -1;
bool startup() {
return true;
}
void close_native(Native s) {
::close(s);
}
bool would_block() {
return errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == EINTR;
}
bool non_blocking(Native s) {
const int flags = ::fcntl(s, F_GETFL, 0);
return flags >= 0 && ::fcntl(s, F_SETFL, flags | O_NONBLOCK) == 0;
}
int poll_one(Native s, short events, short& revents) {
pollfd fd{s, events, 0};
const int r = ::poll(&fd, 1, 0);
revents = fd.revents;
return r;
}
constexpr int send_flags = MSG_NOSIGNAL; // A peer reset must not raise SIGPIPE.
using Length = std::size_t;
#endif
Native native(std::intptr_t s) {
return static_cast<Native>(s);
}
std::intptr_t stored(Native s) {
return static_cast<std::intptr_t>(s);
}
void no_delay(Native s) {
int on = 1;
(void)::setsockopt(s, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&on), sizeof on);
}
Length clamp(std::size_t size) {
constexpr std::size_t limit = 1U << 30; // Bounded single call.
return static_cast<Length>(size < limit ? size : limit);
}
} // namespace
std::expected<std::unique_ptr<TcpStream>, TransportError>
TcpStream::connect(const std::string& host, std::uint16_t port) {
if (!startup() || host.empty() || port == 0)
return std::unexpected(TransportError::failed);
addrinfo hints{};
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
addrinfo* found = nullptr;
if (::getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &found) != 0)
return std::unexpected(TransportError::failed);
std::unique_ptr<addrinfo, decltype(&::freeaddrinfo)> list{found, ::freeaddrinfo};
for (const auto* a = list.get(); a != nullptr; a = a->ai_next) {
const Native s = ::socket(a->ai_family, a->ai_socktype, a->ai_protocol);
if (s == invalid)
continue;
if (!non_blocking(s)) {
close_native(s);
continue;
}
no_delay(s);
const int r = ::connect(s, a->ai_addr, static_cast<socklen_t>(a->ai_addrlen));
if (r == 0 || would_block())
return std::unique_ptr<TcpStream>(new TcpStream(stored(s), r != 0));
close_native(s);
}
return std::unexpected(TransportError::failed);
}
TcpStream::~TcpStream() {
close_native(native(socket_));
}
std::expected<bool, TransportError> TcpStream::ready() {
if (!connecting_)
return true;
short revents = 0;
const int r = poll_one(native(socket_), POLLOUT, revents);
if (r < 0)
return std::unexpected(TransportError::failed);
if (r == 0)
return false;
int error = 0;
auto length = static_cast<socklen_t>(sizeof error);
if ((revents & (POLLERR | POLLHUP | POLLNVAL)) != 0 ||
::getsockopt(native(socket_), SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error),
&length) != 0 ||
error != 0)
return std::unexpected(TransportError::failed);
connecting_ = false;
return true;
}
std::expected<std::size_t, TransportError> TcpStream::write(std::span<const std::uint8_t> bytes) {
const auto open = ready();
if (!open)
return std::unexpected(open.error());
if (!*open || bytes.empty())
return 0;
const auto n = ::send(native(socket_), reinterpret_cast<const char*>(bytes.data()),
clamp(bytes.size()), send_flags);
if (n > 0)
return static_cast<std::size_t>(n);
if (n < 0 && would_block())
return 0;
return std::unexpected(TransportError::failed);
}
std::expected<std::size_t, TransportError> TcpStream::read(std::span<std::uint8_t> bytes) {
const auto open = ready();
if (!open)
return std::unexpected(open.error());
if (!*open || bytes.empty())
return 0;
const auto n =
::recv(native(socket_), reinterpret_cast<char*>(bytes.data()), clamp(bytes.size()), 0);
if (n > 0)
return static_cast<std::size_t>(n);
if (n == 0)
return std::unexpected(TransportError::closed);
if (would_block())
return 0;
return std::unexpected(TransportError::failed);
}
std::expected<std::unique_ptr<TcpListener>, TransportError>
TcpListener::listen(std::string_view address) {
if (!startup())
return std::unexpected(TransportError::failed);
const auto colon = address.rfind(':');
if (colon == std::string_view::npos || colon + 1 >= address.size())
return std::unexpected(TransportError::failed);
unsigned port{};
const auto digits = address.substr(colon + 1);
const auto [end, error] = std::from_chars(digits.data(), digits.data() + digits.size(), port);
if (error != std::errc{} || end != digits.data() + digits.size() || port > 65535)
return std::unexpected(TransportError::failed);
auto host = std::string(address.substr(0, colon));
sockaddr_storage storage{};
socklen_t length{};
if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
host = host.substr(1, host.size() - 2);
sockaddr_in6 v6{};
if (::inet_pton(AF_INET6, host.c_str(), &v6.sin6_addr) != 1)
return std::unexpected(TransportError::failed);
v6.sin6_family = AF_INET6;
v6.sin6_port = htons(static_cast<std::uint16_t>(port));
std::memcpy(&storage, &v6, sizeof v6);
length = sizeof v6;
} else {
sockaddr_in v4{};
if (::inet_pton(AF_INET, host.c_str(), &v4.sin_addr) != 1)
return std::unexpected(TransportError::failed);
v4.sin_family = AF_INET;
v4.sin_port = htons(static_cast<std::uint16_t>(port));
std::memcpy(&storage, &v4, sizeof v4);
length = sizeof v4;
}
const Native s = ::socket(storage.ss_family, SOCK_STREAM, IPPROTO_TCP);
if (s == invalid)
return std::unexpected(TransportError::failed);
#ifndef _WIN32
int reuse = 1; // Fast restart after a crash; Windows semantics differ, so not there.
(void)::setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof reuse);
#endif
sockaddr_storage bound{};
auto bound_length = static_cast<socklen_t>(sizeof bound);
if (!non_blocking(s) || ::bind(s, reinterpret_cast<const sockaddr*>(&storage), length) != 0 ||
::listen(s, 16) != 0 ||
::getsockname(s, reinterpret_cast<sockaddr*>(&bound), &bound_length) != 0) {
close_native(s);
return std::unexpected(TransportError::failed);
}
std::uint16_t actual = 0;
if (bound.ss_family == AF_INET6) {
sockaddr_in6 v6{};
std::memcpy(&v6, &bound, sizeof v6);
actual = ntohs(v6.sin6_port);
} else {
sockaddr_in v4{};
std::memcpy(&v4, &bound, sizeof v4);
actual = ntohs(v4.sin_port);
}
return std::unique_ptr<TcpListener>(new TcpListener(stored(s), actual));
}
TcpListener::~TcpListener() {
close_native(native(socket_));
}
std::expected<std::unique_ptr<TcpStream>, TransportError> TcpListener::accept() {
const Native s = ::accept(native(socket_), nullptr, nullptr);
if (s == invalid) {
if (would_block())
return nullptr;
return std::unexpected(TransportError::failed);
}
if (!non_blocking(s)) {
close_native(s);
return std::unexpected(TransportError::failed);
}
no_delay(s);
return std::unique_ptr<TcpStream>(new TcpStream(stored(s), false));
}
} // namespace evo::network
+1
View File
@@ -9,6 +9,7 @@
// The owned user config (third_party/mbedtls/evo_mbedtls_config.h) must be in effect.
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) || defined(MBEDTLS_SSL_PROTO_DTLS) || \
defined(MBEDTLS_SSL_SESSION_TICKETS) || defined(MBEDTLS_SSL_RENEGOTIATION) || \
defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) || \
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) || \
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED) || \
!defined(MBEDTLS_SSL_PROTO_TLS1_3)
+173
View File
@@ -0,0 +1,173 @@
#include "evo/network/dev_credentials.hpp"
#include <array>
#include <cctype>
#include <ctime>
#include <mbedtls/platform_util.h>
#include <mbedtls/x509_crt.h>
#include <psa/crypto.h>
#include <span>
namespace evo::network {
namespace {
constexpr std::string_view authority_name = "CN=AEvolution dev session CA (dev only)";
// Owns one PSA-generated ECDSA P-256 key as a PK context; zeroized on destruction.
class Key {
public:
Key() {
mbedtls_pk_init(&pk_);
}
~Key() {
mbedtls_pk_free(&pk_);
}
Key(const Key&) = delete;
Key& operator=(const Key&) = delete;
bool generate() {
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
psa_set_key_bits(&attributes, 256);
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT);
psa_set_key_algorithm(&attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
mbedtls_svc_key_id_t id = PSA_KEY_ID_NULL;
const bool ok = psa_generate_key(&attributes, &id) == PSA_SUCCESS &&
mbedtls_pk_copy_from_psa(id, &pk_) == 0;
(void)psa_destroy_key(id);
return ok;
}
mbedtls_pk_context* get() {
return &pk_;
}
private:
mbedtls_pk_context pk_;
};
std::string timestamp(std::chrono::system_clock::time_point t) {
const std::time_t seconds = std::chrono::system_clock::to_time_t(t);
std::tm utc{};
#ifdef _WIN32
if (gmtime_s(&utc, &seconds) != 0)
return {};
#else
if (gmtime_r(&seconds, &utc) == nullptr)
return {};
#endif
char text[16]{};
if (std::strftime(text, sizeof text, "%Y%m%d%H%M%S", &utc) != 14)
return {};
return text;
}
std::string certificate(Key& subject, Key& issuer, const std::string& subject_name, bool authority,
unsigned char serial, const std::string& from, const std::string& to) {
mbedtls_x509write_cert crt;
mbedtls_x509write_crt_init(&crt);
mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
mbedtls_x509write_crt_set_subject_key(&crt, subject.get());
mbedtls_x509write_crt_set_issuer_key(&crt, issuer.get());
std::array<unsigned char, 4096> buffer{};
const std::string issuer_name{authority_name};
const bool ok =
mbedtls_x509write_crt_set_serial_raw(&crt, &serial, 1) == 0 &&
mbedtls_x509write_crt_set_validity(&crt, from.c_str(), to.c_str()) == 0 &&
mbedtls_x509write_crt_set_subject_name(&crt, subject_name.c_str()) == 0 &&
mbedtls_x509write_crt_set_issuer_name(&crt, issuer_name.c_str()) == 0 &&
mbedtls_x509write_crt_set_basic_constraints(&crt, authority ? 1 : 0, authority ? 0 : -1) ==
0 &&
mbedtls_x509write_crt_set_key_usage(&crt, authority
? MBEDTLS_X509_KU_KEY_CERT_SIGN
: MBEDTLS_X509_KU_DIGITAL_SIGNATURE) == 0 &&
mbedtls_x509write_crt_pem(&crt, buffer.data(), buffer.size()) == 0;
mbedtls_x509write_crt_free(&crt);
return ok ? std::string{reinterpret_cast<const char*>(buffer.data())} : std::string{};
}
bool valid_name(std::string_view name) {
if (name.empty() || name.size() > 253)
return false;
for (const char ch : name)
if (!(std::isalnum(static_cast<unsigned char>(ch)) != 0 || ch == '-' || ch == '.'))
return false; // Plain DNS names only; nothing that could alter the subject DN.
return true;
}
} // namespace
std::expected<DevCredentials, GrantError>
generate_dev_credentials(DeploymentEnvironment environment, std::string_view server_name,
std::uint32_t days, std::chrono::system_clock::time_point now) {
if (environment != DeploymentEnvironment::dev)
return std::unexpected(GrantError::not_dev_environment);
if (!valid_name(server_name) || days == 0 || days > 825)
return std::unexpected(GrantError::invalid_claims);
if (psa_crypto_init() != PSA_SUCCESS)
return std::unexpected(GrantError::crypto);
Key authority, server;
if (!authority.generate() || !server.generate())
return std::unexpected(GrantError::crypto);
const auto from = timestamp(now - std::chrono::hours{1}); // Small clock-skew allowance.
const auto to = timestamp(now + std::chrono::hours{24} * days);
if (from.empty() || to.empty())
return std::unexpected(GrantError::crypto);
DevCredentials out;
out.authority_pem =
certificate(authority, authority, std::string{authority_name}, true, 1, from, to);
out.server_certificate_pem =
certificate(server, authority, "CN=" + std::string{server_name}, false, 2, from, to);
std::array<unsigned char, 4096> key{};
if (out.authority_pem.empty() || out.server_certificate_pem.empty() ||
mbedtls_pk_write_key_pem(server.get(), key.data(), key.size()) != 0 ||
psa_generate_random(out.grant_key.data(), out.grant_key.size()) != PSA_SUCCESS)
return std::unexpected(GrantError::crypto);
out.server_key_pem = reinterpret_cast<const char*>(key.data());
mbedtls_platform_zeroize(key.data(), key.size());
return out;
}
std::string format_grant_key(std::span<const std::uint8_t> key) {
constexpr char digits[] = "0123456789abcdef";
std::string out;
for (const auto b : key) {
out.push_back(digits[b >> 4U]);
out.push_back(digits[b & 0x0FU]);
}
return out;
}
std::expected<std::array<std::uint8_t, dev_key_bytes>, GrantError>
parse_grant_key(std::string_view text) {
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.front())) != 0)
text.remove_prefix(1);
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.back())) != 0)
text.remove_suffix(1);
if (text.size() != 2 * dev_key_bytes)
return std::unexpected(GrantError::invalid_key);
std::array<std::uint8_t, dev_key_bytes> key{};
const auto value = [](char ch) -> int {
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
return -1;
};
for (std::size_t i = 0; i < key.size(); ++i) {
const int hi = value(text[2 * i]), lo = value(text[2 * i + 1]);
if (hi < 0 || lo < 0)
return std::unexpected(GrantError::invalid_key);
key.at(i) = static_cast<std::uint8_t>((hi << 4) | lo);
}
return key;
}
std::expected<std::uint64_t, GrantError> random_grant_id() {
if (psa_crypto_init() != PSA_SUCCESS)
return std::unexpected(GrantError::crypto);
for (int attempt = 0; attempt < 4; ++attempt) {
std::array<std::uint8_t, 8> bytes{};
if (psa_generate_random(bytes.data(), bytes.size()) != PSA_SUCCESS)
return std::unexpected(GrantError::crypto);
std::uint64_t id = 0;
for (const auto b : bytes)
id = (id << 8U) | b;
if (id != 0)
return id;
}
return std::unexpected(GrantError::crypto);
}
} // namespace evo::network