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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Bounded endpoint send admission and explicit completion ownership.
use super::*;
use bacnet_encoding::npdu::NpduAddress;
use bacnet_network::network_number::LocalNetworkNumber;
use bacnet_network::response_route::ResponseRoute;
use bacnet_transport::port::GroupDestinations;
pub(super) enum NetworkServicePayload {
Apdu(Vec<u8>),
LocalControl(Vec<u8>),
}
pub(super) struct NetworkServiceCommand {
pub(super) payload: NetworkServicePayload,
pub(super) response_route: Option<ResponseRoute>,
pub(super) destination: EndpointApduDestination,
pub(super) expecting_reply: bool,
pub(super) priority: NetworkPriority,
pub(super) data_attributes: Vec<DataAttribute>,
pub(super) completion: oneshot::Sender<EndpointSendOutcome>,
pub(super) deadline: Option<tokio::time::Instant>,
pub(super) cancel_on_drop: bool,
}
/// One APDU send being admitted to the bounded command queue.
struct ApduAdmission {
apdu: Vec<u8>,
destination: EndpointApduDestination,
expecting_reply: bool,
priority: NetworkPriority,
data_attributes: Vec<DataAttribute>,
/// Latest time the send may start, if bounded.
deadline: Option<tokio::time::Instant>,
/// Whether dropping the completion retracts the send.
cancel_on_drop: bool,
/// Immutable ingress authority a reply must follow, for response sends.
response_route: Option<ResponseRoute>,
}
/// Completion of an admitted endpoint send, without claiming remote receipt.
#[doc(hidden)]
pub struct EndpointSendOutcome {
/// Local send completion; success is not remote receipt or execution.
pub result: Result<(), Error>,
/// Transport execution began; an error may have an ambiguous wire outcome.
pub attempted: bool,
}
/// One queued send completion. Deadline-bound and explicitly owned sends cancel
/// when this is dropped; ordinary deadline-free admission retains detached semantics.
#[doc(hidden)]
pub struct EndpointSend(oneshot::Receiver<EndpointSendOutcome>);
impl EndpointSend {
#[doc(hidden)]
pub async fn complete(self) -> EndpointSendOutcome {
self.0.await.unwrap_or_else(|_| EndpointSendOutcome {
result: Err(shutdown_error()),
attempted: false,
})
}
}
/// Known local outcome before any endpoint transport execution.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EndpointEgressAdmissionError {
/// The bounded command queue has no free slot.
QueueFull,
/// The endpoint has sealed or its command receiver has closed.
Closed,
}
impl From<EndpointEgressAdmissionError> for Error {
fn from(error: EndpointEgressAdmissionError) -> Self {
match error {
EndpointEgressAdmissionError::QueueFull => {
Error::Encoding("endpoint egress queue is full".into())
}
EndpointEgressAdmissionError::Closed => shutdown_error(),
}
}
}
/// Bounded APDU network-service sender for roles attached to an endpoint session.
#[doc(hidden)]
#[derive(Clone)]
pub struct EndpointEgress {
pub(super) commands: mpsc::Sender<NetworkServiceCommand>,
pub(super) open: Arc<AtomicBool>,
pub(super) local_network: LocalNetworkNumber,
/// The link's group-destination rule, taken from the transport at start.
pub(super) group_destinations: GroupDestinations,
}
impl EndpointApduDestination {
/// This destination as sent once the endpoint knows `local_network`, the
/// number of the network its link is attached to (#1403). A routed
/// destination on that network is a station on this link, so it goes to
/// the station's MAC with no DNET and not through the router (Clause
/// 6.5.1): a non-routing peer discards an APDU whose DNET names a network
/// (Clause 6.5.2.1). A broadcast to that network is a local broadcast.
/// Every other destination, and every one while the number is unknown,
/// is unchanged. It checks nothing itself: the requester checks a routed
/// unicast's DADR before calling it, and source Audit applies its stricter
/// B/IP unicast check after.
#[doc(hidden)]
pub fn localized(self, local_network: Option<u16>) -> Self {
let here = |network: u16| Some(network) == local_network;
match self {
Self::Routed {
destination_network,
destination_mac,
..
}
| Self::RoutedViaLocalBroadcast {
destination_network,
destination_mac,
} if here(destination_network) => Self::Direct { destination_mac },
Self::RemoteBroadcast {
destination_network,
} if here(destination_network) => Self::LocalBroadcast,
destination => destination,
}
}
}
impl EndpointEgress {
/// The number of the network this endpoint's link is attached to: the
/// layer's own [`LocalNetworkNumber`], which the endpoint's Number owner
/// publishes. Senders read it to send traffic for that network as local
/// traffic ([`EndpointApduDestination::localized`]); the egress itself
/// sends every destination as its caller names it, so answers keep the
/// route their request arrived by.
#[doc(hidden)]
pub fn local_network_number(&self) -> &LocalNetworkNumber {
&self.local_network
}
/// Whether a `Direct` send to `mac` would reach a group of nodes, by the
/// rule the transport gave at start
/// ([`TransportPort::group_destinations`](bacnet_transport::port::TransportPort::group_destinations)).
/// The egress refuses anything but an Unconfirmed-Request there when it
/// sends; a sender that takes state for a send, such as a transaction,
/// asks here first (#1479).
#[doc(hidden)]
pub fn is_group_destination(&self, mac: &[u8]) -> bool {
self.group_destinations.contains(mac)
}
/// Whether this ingress still admits transport work.
#[doc(hidden)]
pub fn is_open(&self) -> bool {
self.open.load(Ordering::Acquire) && !self.commands.is_closed()
}
/// Sends one APDU without granting network lifecycle access.
#[doc(hidden)]
pub async fn send_apdu(
&self,
apdu: Vec<u8>,
destination: EndpointApduDestination,
expecting_reply: bool,
priority: NetworkPriority,
data_attributes: Vec<DataAttribute>,
) -> Result<(), Error> {
self.admit_apdu(
apdu,
destination,
expecting_reply,
priority,
data_attributes,
None,
)?
.complete()
.await
.result
}
/// Queue one send synchronously, distinguishing admission from execution.
/// A deadline also makes receiver cancellation retract queued work. Sends
/// already in progress may have reached the peer when cancellation wins.
#[doc(hidden)]
pub fn admit_apdu(
&self,
apdu: Vec<u8>,
destination: EndpointApduDestination,
expecting_reply: bool,
priority: NetworkPriority,
data_attributes: Vec<DataAttribute>,
deadline: Option<tokio::time::Instant>,
) -> Result<EndpointSend, EndpointEgressAdmissionError> {
self.admit(ApduAdmission {
apdu,
destination,
expecting_reply,
priority,
data_attributes,
deadline,
cancel_on_drop: deadline.is_some(),
response_route: None,
})
}
/// Queue caller-owned work without a deadline. Dropping its completion retracts
/// queued work and cancels an in-progress transport future, whose wire outcome
/// may already be ambiguous. A session-owned caller can retain this same guard.
#[doc(hidden)]
pub fn admit_owned_apdu(
&self,
apdu: Vec<u8>,
destination: EndpointApduDestination,
expecting_reply: bool,
priority: NetworkPriority,
data_attributes: Vec<DataAttribute>,
) -> Result<EndpointSend, EndpointEgressAdmissionError> {
self.admit(ApduAdmission {
apdu,
destination,
expecting_reply,
priority,
data_attributes,
deadline: None,
cancel_on_drop: true,
response_route: None,
})
}
/// Queue a caller-owned reply under its immutable ingress authority.
/// Dropping completion retracts queued work; a started socket write may
/// already have reached the peer. Invalid/stale direct routes never fall back.
#[doc(hidden)]
pub fn admit_response_apdu(
&self,
apdu: Vec<u8>,
next_hop: MacAddr,
destination: Option<NpduAddress>,
route: ResponseRoute,
) -> Result<EndpointSend, EndpointEgressAdmissionError> {
let destination = match destination {
Some(address) => EndpointApduDestination::Routed {
destination_network: address.network,
destination_mac: address.mac_address,
router_mac: next_hop,
},
None => EndpointApduDestination::Direct {
destination_mac: next_hop,
},
};
self.admit(ApduAdmission {
apdu,
destination,
expecting_reply: false,
priority: NetworkPriority::Normal,
data_attributes: Vec::new(),
deadline: None,
cancel_on_drop: true,
response_route: Some(route),
})
}
fn admit(&self, send: ApduAdmission) -> Result<EndpointSend, EndpointEgressAdmissionError> {
let ApduAdmission {
apdu,
destination,
expecting_reply,
priority,
data_attributes,
deadline,
cancel_on_drop,
response_route,
} = send;
if !self.open.load(Ordering::Acquire) {
return Err(EndpointEgressAdmissionError::Closed);
}
let (completion, result) = oneshot::channel();
let command = NetworkServiceCommand {
payload: NetworkServicePayload::Apdu(apdu),
response_route,
destination,
expecting_reply,
priority,
data_attributes,
completion,
deadline,
cancel_on_drop,
};
match self.commands.try_send(command) {
Ok(()) => Ok(EndpointSend(result)),
Err(mpsc::error::TrySendError::Closed(_)) => Err(EndpointEgressAdmissionError::Closed),
Err(mpsc::error::TrySendError::Full(_)) => Err(EndpointEgressAdmissionError::QueueFull),
}
}
/// Queue a local Network-Number-Is NPDU, with caller-owned cancellation.
/// Dropping the waiter retracts queued work; a started send may have hit wire.
#[doc(hidden)]
pub async fn send_network_number_is(&self, npdu: Vec<u8>) -> Result<(), Error> {
if npdu.len() != 6
|| npdu[..3] != [1, 0x80, 0x13]
|| npdu[5] > 1
|| matches!(u16::from_be_bytes([npdu[3], npdu[4]]), 0 | 65535)
{
return Err(Error::Encoding(
"invalid local Network-Number-Is NPDU".into(),
));
}
if !self.is_open() {
return Err(shutdown_error());
}
let (completion, result) = oneshot::channel();
self.commands
.try_send(NetworkServiceCommand {
payload: NetworkServicePayload::LocalControl(npdu),
response_route: None,
destination: EndpointApduDestination::LocalBroadcast,
expecting_reply: false,
priority: NetworkPriority::Normal,
data_attributes: Vec::new(),
completion,
deadline: None,
cancel_on_drop: true,
})
.map_err(|error| match error {
mpsc::error::TrySendError::Closed(_) => {
Error::from(EndpointEgressAdmissionError::Closed)
}
mpsc::error::TrySendError::Full(_) => {
Error::from(EndpointEgressAdmissionError::QueueFull)
}
})?;
EndpointSend(result).complete().await.result
}
/// Wait for queue capacity without retaining an ordinary notification.
/// This is a wake hint, not a reserved slot; callers retry synchronous admission.
#[doc(hidden)]
pub async fn wait_for_capacity(&self) -> Result<(), EndpointEgressAdmissionError> {
if !self.open.load(Ordering::Acquire) {
return Err(EndpointEgressAdmissionError::Closed);
}
let permit = self
.commands
.reserve()
.await
.map_err(|_| EndpointEgressAdmissionError::Closed)?;
drop(permit);
if self.open.load(Ordering::Acquire) {
Ok(())
} else {
Err(EndpointEgressAdmissionError::Closed)
}
}
/// Sends one direct unicast APDU without granting network lifecycle access.
#[doc(hidden)]
pub async fn send_direct(
&self,
apdu: Vec<u8>,
destination_mac: MacAddr,
expecting_reply: bool,
priority: NetworkPriority,
) -> Result<(), Error> {
self.send_apdu(
apdu,
EndpointApduDestination::Direct { destination_mac },
expecting_reply,
priority,
Vec::new(),
)
.await
}
}