1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! Calls carry the caller's original admitted wire request inside Noise.
//! The far exchange executes it through its ordinary admitted connection path.
use crate::link_handshake::{LinkTransport, NOISE_MAX_MESSAGE, NOISE_OVERHEAD};
use liminal::protocol::{Frame, codec};
use zeroize::Zeroizing;
#[path = "link_route_call/client.rs"]
pub mod client;
#[path = "link_route_call/pending.rs"]
pub mod pending;
#[cfg(test)]
#[path = "link_route_call_tests.rs"]
mod tests;
#[path = "link_route_call/wire.rs"]
mod wire;
/// A full far connection execution, owned until its exact call settles.
pub trait CallExecution: Send + std::fmt::Debug {
/// Cancels this call's full connection and joins its owned reader.
///
/// # Errors
/// The connection's cancellation or worker join failed.
fn finish(&mut self) -> Result<(), CallError>;
}
/// The receiving supervisor supplies its ordinary admitted connection path.
pub trait CallExecutor: Send + Sync + std::fmt::Debug {
/// Actual retained execution owners, for capacity and leak observations.
fn retained(&self) -> usize;
/// Waits on execution retirement signals, with no clock or polling.
///
/// # Errors
/// The retirement signal state was poisoned.
fn observe_empty(&self) -> Result<(), CallError>;
/// Starts only after the keyed link has decided its two crossing grants.
///
/// # Errors
/// Full connection admission, wire validation or owned reader startup failed.
fn start(
&self,
request: CallRequest,
completed: Box<dyn FnOnce(Result<CallReply, CallError>) + Send>,
) -> Result<Box<dyn CallExecution>, CallError>;
}
/// A named crossing refusal; no request or credential bytes enter its text.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum CallError {
/// The complete call cannot fit its configured bound or the Noise ceiling.
#[error("link_call_too_large")]
Bound,
/// No additional call is admitted until an existing reservation settles.
#[error("link_call_capacity")]
Capacity,
/// The call envelope is malformed or has unexpected framing.
#[error("link_call_invalid")]
Invalid,
/// A reply did not match this exact call and subject.
#[error("link_call_correlation_mismatch")]
Correlation,
/// The keyed peer disappeared before its answer.
#[error("link_call_peer_gone")]
PeerGone,
/// The local caller cancelled this exact pending operation.
#[error("link_call_cancelled")]
Cancelled,
/// The caller's already configured pending-reply deadline elapsed.
#[error("link_call_deadline")]
Deadline,
/// State was poisoned or a response delivery could not be completed.
#[error("link_call_unavailable")]
Unavailable,
/// No live server on either end advertises this subject.
#[error("no_server")]
NoServer,
/// A machine no longer holds its crossing right when the answer is released.
#[error("link_call_grant_refused")]
GrantRefused,
}
/// A zeroized sequence of Connect, `ConversationOpen` and `ConversationMessage`.
/// Private fields prevent the receiving adapter from accepting arbitrary acts.
pub struct CallRequest {
id: u64,
subject: String,
bytes: Zeroizing<Vec<u8>>,
}
impl std::fmt::Debug for CallRequest {
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
out.debug_struct("CallRequest")
.field("id", &self.id)
.field("bytes", &self.bytes.len())
.finish_non_exhaustive()
}
}
impl CallRequest {
/// Builds the call from its caller's original credential and request frames.
///
/// # Errors
/// Wrong frame sequence, correlations/subject, non-Lys credential or oversize call.
pub fn new(
id: u64,
subject: String,
frames: &[Frame],
bound: usize,
) -> Result<Self, CallError> {
let bytes = wire::encode_frames(frames, bound)?;
Self::decode(id, subject, bytes, bound)
}
fn decode(
id: u64,
subject: String,
bytes: Zeroizing<Vec<u8>>,
bound: usize,
) -> Result<Self, CallError> {
if id == 0 || bytes.len() > bound {
return Err(CallError::Invalid);
}
crate::auth_lys::SubjectBinding::new("wire".into(), subject.clone(), subject.clone())
.map_err(|_| CallError::Invalid)?;
let mut frames = wire::request_frames(&bytes, &subject)?;
for frame in &mut frames {
if let Frame::Connect { auth_token, .. } = frame {
zeroize::Zeroize::zeroize(auth_token);
}
}
drop(frames);
Ok(Self { id, subject, bytes })
}
/// The monotonic link-local operation id, never reused on this transport.
#[must_use]
pub const fn id(&self) -> u64 {
self.id
}
/// The concrete placed service subject.
#[must_use]
pub fn subject(&self) -> &str {
&self.subject
}
/// The checked original frames, for the full far admission path.
///
/// # Errors
/// The stored wire bytes could not be decoded.
pub fn frames(&self) -> Result<Vec<Frame>, CallError> {
wire::request_frames(&self.bytes, &self.subject)
}
/// The checked wire image, borrowed only while it is sent to the far door.
#[must_use]
pub fn wire_bytes(&self) -> &[u8] {
&self.bytes
}
/// The caller's original wire correlation, with copied credential bytes zeroized.
///
/// # Errors
/// The checked stored request could not be read.
pub fn correlation(&self) -> Result<(u32, u64), CallError> {
let mut frames = self.frames()?;
for frame in &mut frames {
if let Frame::Connect { auth_token, .. } = frame {
zeroize::Zeroize::zeroize(auth_token);
}
}
let Some(Frame::ConversationOpen {
stream_id,
conversation_id,
..
}) = frames.get(1)
else {
return Err(CallError::Invalid);
};
Ok((*stream_id, *conversation_id))
}
}
/// The exact far wire answer, including its named refusal and correlation fields.
pub struct CallReply {
id: u64,
frame: Frame,
}
impl std::fmt::Debug for CallReply {
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
out.debug_struct("CallReply")
.field("id", &self.id)
.finish_non_exhaustive()
}
}
impl CallReply {
/// Keeps the far connection's answer byte-for-byte at the wire-frame level.
///
/// # Errors
/// A zero operation id or a frame that is not a conversation answer.
pub fn new(id: u64, frame: Frame) -> Result<Self, CallError> {
if id == 0
|| !matches!(
frame,
Frame::ConversationMessage { .. } | Frame::ConversationError { .. }
)
{
return Err(CallError::Invalid);
}
Ok(Self { id, frame })
}
/// This reply's exact link-local operation.
#[must_use]
pub const fn id(&self) -> u64 {
self.id
}
/// Releases the answer only after the near side checks the operation binding.
#[must_use]
pub fn into_frame(self) -> Frame {
self.frame
}
}
/// A control/body kind inside the existing sealed `LinkMessage` envelope.
/// Nothing here derives Debug over a credential, payload or contract.
pub enum CallPacket {
/// Execute one original request through the far admission path.
Request(CallRequest),
/// Return its original wire answer.
Reply(CallReply),
/// Cancel only this exact link-local operation.
Cancel(u64),
/// A far exchange's own whole topology, never a guessed station list.
Topology(Zeroizing<Vec<u8>>),
}
impl CallPacket {
/// Whether a decrypted crossing uses this explicitly versioned body.
#[must_use]
pub fn is_call(bytes: &[u8]) -> bool {
bytes.starts_with(wire::MAGIC)
}
/// Encodes within the configured body bound before Noise allocation.
///
/// # Errors
/// The packet or nested frame exceeds the bound or is invalid.
pub fn encode(&self, bound: usize) -> Result<Zeroizing<Vec<u8>>, CallError> {
wire::encode(self, bound)
}
/// Decodes exact lengths and refuses any trailing bytes.
///
/// # Errors
/// Unknown version/kind, malformed nested wire or a configured bound refusal.
pub fn decode(bytes: &[u8], bound: usize) -> Result<Self, CallError> {
wire::decode(bytes, bound)
}
/// Seals an encoded packet once on the existing keyed session.
///
/// # Errors
/// Bound or Noise framing refusal; payload bytes are excluded from the error.
pub fn seal(&self, transport: &mut LinkTransport, bound: usize) -> Result<Vec<u8>, CallError> {
let bytes = self.encode(bound)?;
let mut sealed = vec![0; bytes.len() + NOISE_OVERHEAD];
let written = transport
.seal(&bytes, &mut sealed)
.map_err(|_| CallError::Invalid)?;
sealed.truncate(written);
Ok(sealed)
}
/// Opens the exact keyed message then applies its configured body bound.
///
/// # Errors
/// Oversize, Noise authentication or packet decoding refusal.
pub fn open(
transport: &mut LinkTransport,
sealed: &[u8],
bound: usize,
) -> Result<Self, CallError> {
if sealed.len() > NOISE_MAX_MESSAGE || sealed.len() > bound.saturating_add(NOISE_OVERHEAD) {
return Err(CallError::Bound);
}
let mut plain = Zeroizing::new(vec![0; sealed.len()]);
let written = transport
.open(sealed, &mut plain)
.map_err(|_| CallError::Invalid)?;
plain.truncate(written);
Self::decode(&plain, bound)
}
}