Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/sendspin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -854,8 +854,9 @@ class SendspinClient {
/// only.
void process_inbound(SendspinConnection& conn, InboundMessage& message);

/// @brief Builds the client/hello fields from config and every role. Protocol task.
ClientHelloMessage build_client_hello();
/// @brief Fills `msg` with the client/hello fields from config and every role, viewing them.
/// Protocol task.
void build_client_hello(ClientHelloMessage& msg);

/// @brief Builds the client/state snapshot publish_state() hands the protocol task: the
/// availability and every role's state object. Main loop only, where that state lives.
Expand Down
66 changes: 19 additions & 47 deletions src/admission.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,13 @@
#include <optional>
#include <span>
#include <string>
#include <vector>

namespace sendspin {

// ============================================================================
// Trust enforcement (PSK category -> allowed activities)
// ============================================================================

/// @brief Whether `activities` contains `target`. Activity lists are always tiny, so a linear
/// scan is used everywhere in this file instead of a set.
inline bool contains_activity(const std::vector<SendspinActivity>& activities,
SendspinActivity target) {
for (const auto& a : activities) {
if (a == target) {
return true;
}
}
return false;
}

/// @brief Whether an activity set is allowed for the matched PSK category.
///
/// The table in messaging.md "server/activate":
Expand Down Expand Up @@ -83,13 +70,10 @@ inline bool activity_set_allowed(PskCategory category, bool has_playback, bool h
return !has_playback || unpaired_access;
}

/// @brief activity_set_allowed() for a declared activity list.
inline bool activities_allowed(PskCategory category,
const std::vector<SendspinActivity>& activities,
bool unpaired_access) {
return activity_set_allowed(category, contains_activity(activities, SendspinActivity::PLAYBACK),
contains_activity(activities, SendspinActivity::PAIRING),
unpaired_access);
/// @brief activity_set_allowed() for a declared activity set.
inline bool activities_allowed(PskCategory category, ActivitySet activities, bool unpaired_access) {
return activity_set_allowed(category, activities.contains(SendspinActivity::PLAYBACK),
activities.contains(SendspinActivity::PAIRING), unpaired_access);
}

/// @brief Whether a connection declaring `activities` is "playback-capable": `activities`
Expand All @@ -100,12 +84,10 @@ inline bool activities_allowed(PskCategory category,
/// @param activities Activities declared in the server/activate message.
/// @param unpaired_access Whether unpaired (Sentinel) access is enabled.
/// @return true if the connection is playback-capable under these activities.
inline bool is_playback_capable(PskCategory category,
const std::vector<SendspinActivity>& activities,
inline bool is_playback_capable(PskCategory category, ActivitySet activities,
bool unpaired_access) {
return activity_set_allowed(category, /*has_playback=*/true,
contains_activity(activities, SendspinActivity::PAIRING),
unpaired_access);
activities.contains(SendspinActivity::PAIRING), unpaired_access);
}

/// @brief Whether `activities`/`active_roles` satisfy the matched PSK's structural constraints.
Expand All @@ -119,8 +101,8 @@ inline bool is_playback_capable(PskCategory category,
/// @param has_roles Whether the effective active_roles set is non-empty.
/// @param unpaired_access Whether unpaired (Sentinel) access is enabled.
/// @return true if the activate is structurally admissible.
inline bool admissible(PskCategory category, const std::vector<SendspinActivity>& activities,
bool has_roles, bool unpaired_access) {
inline bool admissible(PskCategory category, ActivitySet activities, bool has_roles,
bool unpaired_access) {
if (!activities_allowed(category, activities, unpaired_access)) {
return false;
}
Expand All @@ -133,8 +115,7 @@ inline bool admissible(PskCategory category, const std::vector<SendspinActivity>
/// stops admitting unpaired access.
///
/// @param category, activities, has_roles Same as admissible().
inline bool relies_on_unpaired_access(PskCategory category,
const std::vector<SendspinActivity>& activities,
inline bool relies_on_unpaired_access(PskCategory category, ActivitySet activities,
bool has_roles) {
return admissible(category, activities, has_roles, /*unpaired_access=*/true) &&
!admissible(category, activities, has_roles, /*unpaired_access=*/false);
Expand All @@ -152,9 +133,9 @@ inline bool relies_on_unpaired_access(PskCategory category,
///
/// @param category, activities, has_roles, unpaired_access Same as admissible().
/// @return PAIRING_REQUIRED if enabling unpaired access would have admitted it, else UNAUTHORIZED.
inline SendspinGoodbyeReason inadmissible_reject_reason(
PskCategory category, const std::vector<SendspinActivity>& activities, bool has_roles,
bool unpaired_access) {
inline SendspinGoodbyeReason inadmissible_reject_reason(PskCategory category,
ActivitySet activities, bool has_roles,
bool unpaired_access) {
if (!unpaired_access && relies_on_unpaired_access(category, activities, has_roles)) {
return SendspinGoodbyeReason::PAIRING_REQUIRED;
}
Expand All @@ -169,20 +150,11 @@ inline SendspinGoodbyeReason inadmissible_reject_reason(
///
/// connection.md "Multiple servers (server-initiated)" ranks playback above pairing and an empty
/// set lowest, so playback=2 > pairing=1 > none=0.
inline int activity_rank(const std::vector<SendspinActivity>& activities) {
bool has_playback = false;
bool has_pairing = false;
for (const auto& a : activities) {
if (a == SendspinActivity::PLAYBACK) {
has_playback = true;
} else if (a == SendspinActivity::PAIRING) {
has_pairing = true;
}
}
if (has_playback) {
inline int activity_rank(ActivitySet activities) {
if (activities.contains(SendspinActivity::PLAYBACK)) {
return 2;
}
if (has_pairing) {
if (activities.contains(SendspinActivity::PAIRING)) {
return 1;
}
return 0;
Expand All @@ -204,9 +176,9 @@ inline int activity_rank(const std::vector<SendspinActivity>& activities) {
/// declares PAIRING but has already been acked with server/pair-finalize, so rule 2 stops
/// shielding a pairing that has finished.
/// @return true if the incoming connection should become the admitted one.
inline bool should_admit_connection(const std::vector<SendspinActivity>& incoming_activities,
inline bool should_admit_connection(ActivitySet incoming_activities,
const std::string& incoming_server_id,
const std::vector<SendspinActivity>& admitted_activities,
ActivitySet admitted_activities,
const std::string& admitted_server_id, bool has_admitted,
const std::optional<std::string>& last_playback_server_id,
bool admitted_pairing_in_flight) {
Expand All @@ -221,8 +193,8 @@ inline bool should_admit_connection(const std::vector<SendspinActivity>& incomin
// admitted_pairing_in_flight=false once server/pair-finalize is acked. Substituting an empty
// activity set instead would drop the admitted side to rank 0 and hand a rank-0 newcomer the
// last_playback tiebreak below, which it could never have won before.
if (admitted_pairing_in_flight &&
contains_activity(admitted_activities, SendspinActivity::PAIRING) && incoming_rank != 0) {
if (admitted_pairing_in_flight && admitted_activities.contains(SendspinActivity::PAIRING) &&
incoming_rank != 0) {
return false;
}

Expand Down
55 changes: 23 additions & 32 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,6 @@ struct HighPerformanceRequests {
uint32_t releases{0};
};

/// @brief Resolve the `locations` hint for a pair-method descriptor in client/hello.
/// @return The hint to advertise, or nullopt to omit the field.
///
/// The hint is informational (pairing.md "client/hello pair-method descriptor"): only the
/// application knows where its secret was published, so an unset value means the client has
/// nothing to say rather than a default worth sending.
std::optional<std::vector<std::string>> locations_hint(const std::vector<std::string>& configured) {
if (configured.empty()) {
return std::nullopt;
}
return configured;
}

/// @brief Whether `role` is in `mask`, logging the teardown once per role rather than once per
/// call site.
[[maybe_unused]] bool role_removed(uint16_t mask, SendspinRole role) {
Expand Down Expand Up @@ -1402,32 +1389,38 @@ void SendspinClient::cleanup_connection_state(uint16_t teardown_roles) {
#endif
}

ClientHelloMessage SendspinClient::build_client_hello() {
void SendspinClient::build_client_hello(ClientHelloMessage& msg) {
// Reached only from ConnectionManager::send_hello_message(), so only for a connection, so
// only after a successful start(): record_store_ exists and is dereferenced unguarded below.
ClientHelloMessage msg;
msg.name = this->config_.name;

const auto view = [](const std::optional<std::string>& value) {
return value.has_value() ? std::optional<std::string_view>(value.value()) : std::nullopt;
};
DeviceInfoObject& device_info = msg.device_info.emplace();
device_info.product_name = view(this->config_.product_name);
device_info.manufacturer = view(this->config_.manufacturer);
device_info.software_version = view(this->config_.software_version);
// Use the explicitly configured MAC when provided; otherwise fall back to platform detection
// (reliable on ESP, best-effort on host). Leaves the field absent if neither is available.
const std::optional<std::string> interface_mac =
this->config_.mac_address ? this->config_.mac_address : platform_get_interface_mac();

DeviceInfoObject device_info{};
device_info.product_name = this->config_.product_name;
device_info.manufacturer = this->config_.manufacturer;
device_info.software_version = this->config_.software_version;
device_info.mac_address = interface_mac;
msg.device_info = device_info;
device_info.mac_address = view(this->config_.mac_address);
if (!device_info.mac_address.has_value()) {
if (const std::optional<MacAddressText> detected = platform_get_interface_mac()) {
msg.detected_mac = detected.value();
device_info.mac_address = msg.detected_mac.data();
}
}

// pairing.md "client/hello pair-method descriptor". offers_*() is the single source both this
// and the server/activate admissibility check read, so a method advertised here is one an
// activation can select. pairing_psk is always advertised (see pairing_offers.h).
// activation can select. pairing_psk is always advertised (see pairing_offers.h). The
// `locations` hint is informational: only the application knows where its secret was
// published, so an empty list is omitted rather than replaced by a default.
{
PairMethodDescriptor psk_desc;
psk_desc.method = SendspinPairMethod::PAIRING_PSK;
psk_desc.locations = locations_hint(this->config_.pairing_psk_locations);
msg.supported_pair_methods.push_back(std::move(psk_desc));
psk_desc.locations = this->config_.pairing_psk_locations;
msg.supported_pair_methods.push_back(psk_desc);
}
// out_channels and formats are both required and non-empty. No `locations`: a per-session code
// has no resting place for the operator to look it up in.
Expand All @@ -1436,15 +1429,15 @@ ClientHelloMessage SendspinClient::build_client_hello() {
dynamic_desc.method = SendspinPairMethod::DYNAMIC_PAIRING_CODE;
dynamic_desc.out_channels = this->config_.pairing_code_out_channels;
dynamic_desc.formats = this->config_.pairing_code_formats;
msg.supported_pair_methods.push_back(std::move(dynamic_desc));
msg.supported_pair_methods.push_back(dynamic_desc);
}
// Offered only when the dynamic code is not: messaging.md "client/hello" permits at most one
// pairing-code method.
if (offers_static_pairing_code(this->config_)) {
PairMethodDescriptor static_desc;
static_desc.method = SendspinPairMethod::STATIC_PAIRING_CODE;
static_desc.locations = locations_hint(this->config_.static_pairing_code_locations);
msg.supported_pair_methods.push_back(std::move(static_desc));
static_desc.locations = this->config_.static_pairing_code_locations;
msg.supported_pair_methods.push_back(static_desc);
}

msg.unpaired_access_enabled = this->unpaired_access_enabled_.load(std::memory_order_acquire);
Expand Down Expand Up @@ -1485,8 +1478,6 @@ ClientHelloMessage SendspinClient::build_client_hello() {
this->source_->impl_->build_hello_fields(msg);
}
#endif

return msg;
}

// ============================================================================
Expand Down
7 changes: 4 additions & 3 deletions src/client_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,14 @@ void SendspinClient::process_json_message(SendspinConnection& connection, const
case SendspinServerToClientMessageType::SERVER_ACTIVATE: {
ServerActivateMessage activate_msg;
if (parsed.extract<process_server_activate_message>(&activate_msg)) {
SS_LOGD(TAG, "server/activate received (activities_count=%zu)",
activate_msg.activities.size());
SS_LOGD(TAG, "server/activate received (playback=%d, pairing=%d)",
activate_msg.activities.contains(SendspinActivity::PLAYBACK),
activate_msg.activities.contains(SendspinActivity::PAIRING));
// Applied before the next message is parsed: trust enforcement, role
// ownership and, for a nursery connection that is now operational, admission
// all land here, so the role traffic the server sends behind this activate
// meets the gate this activation set.
this->connection_manager_->on_server_activate(conn, std::move(activate_msg));
this->connection_manager_->on_server_activate(conn, activate_msg);
}
break;
}
Expand Down
Loading
Loading