Skip to main content

zaun_community_protocol/command/
session.rs

1//! `session.hello` and its `session.welcome` response (specification §6.2).
2
3use serde::{Deserialize, Serialize};
4use serde_json::{Map, Value};
5
6use super::object::Binding;
7use super::{BodyError, Validate, check};
8use crate::ids::{Challenge, CommunityId, DescriptorHash, HostId, Nonce};
9use crate::json::SignedJson;
10use crate::keys::PublicKey;
11use crate::limits::CAPABILITIES;
12
13/// `session.hello` (§6.2).
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct Hello {
16    /// The client's supported protocol range.
17    pub versions: Versions,
18    /// Fresh per hello; echoed in the host's proof.
19    pub client_nonce: Nonce,
20    /// ≤ 32 entries, each ≤ 64 bytes; unknown ones are ignored by the host.
21    pub capabilities: Vec<String>,
22    /// Unbound only: the community the client intends to join. Required, may be `null`.
23    #[serde(deserialize_with = "check::nullable::deserialize")]
24    pub community_id: Option<CommunityId>,
25    /// The hash of the verified descriptor the client holds for the bound or requested
26    /// community. A host whose current descriptor has this hash leaves the pair out of the
27    /// welcome. Absent when the client holds none.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    pub held_descriptor: Option<DescriptorHash>,
30}
31
32/// `{min, max}` protocol versions (§1.1, §6.2).
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
34pub struct Versions {
35    /// Lowest supported version (≥ 1).
36    pub min: u16,
37    /// Highest supported version (≥ `min`).
38    pub max: u16,
39}
40
41impl Validate for Hello {
42    fn validate(&self) -> Result<(), BodyError> {
43        check::range("versions.min", self.versions.min, 1, u16::MAX)?;
44        check::range(
45            "versions.max",
46            self.versions.max,
47            self.versions.min,
48            u16::MAX,
49        )?;
50        let (count, bytes) = CAPABILITIES;
51        check::count("capabilities", self.capabilities.len(), count)?;
52        match self.capabilities.iter().any(|c| c.len() > bytes) {
53            true => Err(BodyError::too_large("capabilities", bytes)),
54            false => Ok(()),
55        }
56    }
57}
58
59/// `session.welcome` (§6.2).
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61pub struct Welcome {
62    /// The negotiated version.
63    pub version: u16,
64    /// The host key; `host_id` derives from it.
65    pub host_public_key: PublicKey,
66    /// The host id.
67    pub host_id: HostId,
68    /// Fresh; valid until the next hello or 10 minutes.
69    pub host_challenge: Challenge,
70    /// Signed `HostSessionProof` (§4.5).
71    pub proof: SignedJson,
72    /// Intersection of both sides' capabilities.
73    pub capabilities: Vec<String>,
74    /// Effective limits (§12) the client MUST respect.
75    ///
76    /// SPEC: the key names are never defined; kept as an opaque object.
77    pub limits: Map<String, Value>,
78    /// The connection's binding, if bound.
79    pub binding: Option<Binding>,
80    /// The bound or requested community's signed descriptor; `null` also when it has the
81    /// hello's `held_descriptor` hash (the client uses its own).
82    pub descriptor: Option<SignedJson>,
83    /// Its signed delegation.
84    pub delegation: Option<SignedJson>,
85    /// What the host accepts as attachments (§8.5); absent when it takes none.
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub attachments: Option<HostAttachments>,
88}
89
90/// `session.welcome.attachments` (§8.5).
91#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
92pub struct HostAttachments {
93    /// The largest file the host takes, bytes.
94    pub max_bytes: u64,
95    /// The XFTP servers (`xftp://…`) the host downloads uploads from. A member's client
96    /// uploads only to these; the host never fetches from any other server, so an upload
97    /// to another one fails with `failure: file_server`.
98    pub file_servers: Vec<String>,
99}