ziti-sdk 0.3.0

Ziti Rust SDK - High-performance, async-first implementation for secure, zero-trust networking
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! ZitiStream implementation
//!
//! Provides the main stream type for Ziti connections with AsyncRead/AsyncWrite traits.

use crate::error::ZitiResult;
use crate::transport::WebSocketTransport;
use bytes::Bytes;
use futures_util::{Sink, Stream};
use std::collections::VecDeque;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::sync::mpsc;
use tokio_tungstenite::tungstenite::Message;

/// Outbound frame produced by a channel-backed [`ZitiStream`].
///
/// The listener's demux task is the only writer to the underlying
/// WebSocket, so streams hand it ready-to-send `ZitiMessage` bytes plus
/// the source `conn_id` and a hint of whether the message is a `Close`
/// (so the demuxer can drop bookkeeping for that conn).
#[derive(Debug, Clone)]
pub(crate) struct OutboundFrame {
    pub conn_id: u32,
    pub bytes: Bytes,
    pub is_close: bool,
}

/// Backend driving a [`ZitiStream`].
///
/// Dial connections own their WebSocket and read/write raw binary
/// frames directly. Listener accept()s share a single WebSocket via a
/// demux task, so they exchange already-extracted payload bytes through
/// channels instead.
#[derive(Debug)]
enum StreamBackend {
    Direct(Box<WebSocketTransport>),
    Channel(ChannelBackend),
}

#[derive(Debug)]
struct ChannelBackend {
    conn_id: u32,
    inbound_rx: mpsc::UnboundedReceiver<Bytes>,
    outbound_tx: mpsc::UnboundedSender<OutboundFrame>,
    next_seq: u32,
}

impl ChannelBackend {
    fn send_close(&mut self) {
        let frame = match build_close_frame(self.conn_id, self.next_seq()) {
            Ok(f) => f,
            Err(_) => return,
        };
        let _ = self.outbound_tx.send(frame);
    }

    fn next_seq(&mut self) -> u32 {
        let seq = self.next_seq;
        self.next_seq = self.next_seq.wrapping_add(1).max(1);
        seq
    }
}

/// Build the `OutboundFrame` for a `Data` message carrying `payload`.
pub(crate) fn build_data_frame(
    conn_id: u32,
    sequence: u32,
    payload: Bytes,
) -> ZitiResult<OutboundFrame> {
    use crate::transport::protocol::{ContentType, ZitiMessage};

    let mut msg = ZitiMessage::new(ContentType::Data, sequence, payload);
    msg.header
        .add_header("conn_id".to_string(), conn_id.to_string());
    Ok(OutboundFrame {
        conn_id,
        bytes: msg.serialize()?,
        is_close: false,
    })
}

/// Build the `OutboundFrame` for a `Close` message for `conn_id`.
pub(crate) fn build_close_frame(conn_id: u32, sequence: u32) -> ZitiResult<OutboundFrame> {
    use crate::transport::protocol::{ContentType, ZitiMessage};

    let mut msg = ZitiMessage::new(ContentType::Close, sequence, Bytes::new());
    msg.header
        .add_header("conn_id".to_string(), conn_id.to_string());
    Ok(OutboundFrame {
        conn_id,
        bytes: msg.serialize()?,
        is_close: true,
    })
}

/// Main stream type for Ziti connections
///
/// `ZitiStream` provides a bidirectional communication channel over the Ziti network.
/// It implements the standard Rust async I/O traits (`AsyncRead` and `AsyncWrite`),
/// allowing it to be used with any code that expects these standard interfaces.
///
/// The stream handles the underlying Ziti protocol details transparently, presenting
/// a simple byte stream interface to applications.
///
/// # Examples
///
/// ## Basic usage with AsyncRead/AsyncWrite
///
/// ```rust,no_run
/// use ziti_sdk::Context;
/// use tokio::io::{AsyncReadExt, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let context = Context::from_file("identity.json").await?;
///     let mut stream = context.dial("echo-service").await?;
///
///     stream.write_all(b"Hello, Ziti!").await?;
///     stream.flush().await?;
///
///     let mut buffer = [0; 1024];
///     let n = stream.read(&mut buffer).await?;
///     println!("Received: {}", String::from_utf8_lossy(&buffer[..n]));
///
///     stream.close().await?;
///     Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct ZitiStream {
    backend: StreamBackend,
    read_buffer: VecDeque<u8>,
    closed: bool,
}

impl ZitiStream {
    /// Create a new ZitiStream from a WebSocket transport.
    ///
    /// Used by [`crate::connection::dial`] when each Ziti connection
    /// owns its own WebSocket to an edge router.
    pub fn from_transport(transport: WebSocketTransport) -> Self {
        Self {
            backend: StreamBackend::Direct(Box::new(transport)),
            read_buffer: VecDeque::new(),
            closed: false,
        }
    }

    /// Create a new channel-backed ZitiStream.
    ///
    /// Used by [`crate::connection::ZitiListener::accept`] for inbound
    /// connections multiplexed over a single edge-router WebSocket.
    pub(crate) fn from_channels(
        conn_id: u32,
        inbound_rx: mpsc::UnboundedReceiver<Bytes>,
        outbound_tx: mpsc::UnboundedSender<OutboundFrame>,
    ) -> Self {
        Self {
            backend: StreamBackend::Channel(ChannelBackend {
                conn_id,
                inbound_rx,
                outbound_tx,
                next_seq: 1,
            }),
            read_buffer: VecDeque::new(),
            closed: false,
        }
    }

    /// Check if the stream is closed.
    pub fn is_closed(&self) -> bool {
        self.closed
    }

    /// Gracefully close the stream and the underlying transport/conn.
    pub async fn close(&mut self) -> ZitiResult<()> {
        if self.closed {
            return Ok(());
        }
        match &mut self.backend {
            StreamBackend::Direct(transport) => transport.close().await?,
            StreamBackend::Channel(ch) => ch.send_close(),
        }
        self.closed = true;
        Ok(())
    }
}

impl AsyncRead for ZitiStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        let this = self.get_mut();

        loop {
            if !this.read_buffer.is_empty() {
                let n = std::cmp::min(buf.remaining(), this.read_buffer.len());
                if n > 0 {
                    let data: Vec<u8> = this.read_buffer.drain(..n).collect();
                    buf.put_slice(&data);
                }
                return Poll::Ready(Ok(()));
            }

            if this.closed {
                return Poll::Ready(Ok(()));
            }

            match &mut this.backend {
                StreamBackend::Direct(transport) => {
                    match Pin::new(transport.stream_mut()).poll_next(cx) {
                        Poll::Ready(Some(Ok(message))) => match message {
                            Message::Binary(data) => {
                                this.read_buffer.extend(data);
                            }
                            Message::Close(_) => {
                                this.closed = true;
                                return Poll::Ready(Ok(()));
                            }
                            Message::Ping(_) | Message::Pong(_) | Message::Frame(_) => {}
                            Message::Text(_) => {
                                return Poll::Ready(Err(io::Error::new(
                                    io::ErrorKind::InvalidData,
                                    "unexpected text message in Ziti stream",
                                )));
                            }
                        },
                        Poll::Ready(Some(Err(e))) => {
                            return Poll::Ready(Err(io::Error::other(format!(
                                "WebSocket receive failed: {}",
                                e
                            ))));
                        }
                        Poll::Ready(None) => {
                            this.closed = true;
                            return Poll::Ready(Ok(()));
                        }
                        Poll::Pending => return Poll::Pending,
                    }
                }
                StreamBackend::Channel(ch) => match ch.inbound_rx.poll_recv(cx) {
                    Poll::Ready(Some(data)) => {
                        this.read_buffer.extend(data);
                    }
                    Poll::Ready(None) => {
                        this.closed = true;
                        return Poll::Ready(Ok(()));
                    }
                    Poll::Pending => return Poll::Pending,
                },
            }
        }
    }
}

impl AsyncWrite for ZitiStream {
    fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        let this = self.get_mut();

        if this.closed {
            return Poll::Ready(Err(io::Error::new(
                io::ErrorKind::BrokenPipe,
                "stream is closed",
            )));
        }
        if buf.is_empty() {
            return Poll::Ready(Ok(0));
        }

        match &mut this.backend {
            StreamBackend::Direct(transport) => {
                match Pin::new(transport.stream_mut()).poll_ready(cx) {
                    Poll::Ready(Ok(())) => {
                        Pin::new(transport.stream_mut())
                            .start_send(Message::Binary(buf.to_vec().into()))
                            .map_err(|e| {
                                io::Error::other(format!("WebSocket send failed: {}", e))
                            })?;
                        Poll::Ready(Ok(buf.len()))
                    }
                    Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(format!(
                        "WebSocket sink error: {}",
                        e
                    )))),
                    Poll::Pending => Poll::Pending,
                }
            }
            StreamBackend::Channel(ch) => {
                let payload = Bytes::copy_from_slice(buf);
                let seq = ch.next_seq();
                let frame = build_data_frame(ch.conn_id, seq, payload).map_err(|e| {
                    io::Error::other(format!("Failed to frame outbound bytes: {}", e))
                })?;
                ch.outbound_tx
                    .send(frame)
                    .map_err(|_| io::Error::new(io::ErrorKind::BrokenPipe, "listener closed"))?;
                Poll::Ready(Ok(buf.len()))
            }
        }
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        let this = self.get_mut();
        match &mut this.backend {
            StreamBackend::Direct(transport) => {
                match Pin::new(transport.stream_mut()).poll_flush(cx) {
                    Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
                    Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(format!(
                        "WebSocket flush failed: {}",
                        e
                    )))),
                    Poll::Pending => Poll::Pending,
                }
            }
            // The unbounded mpsc has no flush concept; the demux task
            // drains it. There's nothing to wait on here.
            StreamBackend::Channel(_) => Poll::Ready(Ok(())),
        }
    }

    fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
        let this = self.get_mut();
        match &mut this.backend {
            StreamBackend::Direct(transport) => {
                match Pin::new(transport.stream_mut()).poll_close(cx) {
                    Poll::Ready(Ok(())) => {
                        this.closed = true;
                        Poll::Ready(Ok(()))
                    }
                    Poll::Ready(Err(e)) => Poll::Ready(Err(io::Error::other(format!(
                        "WebSocket close failed: {}",
                        e
                    )))),
                    Poll::Pending => Poll::Pending,
                }
            }
            StreamBackend::Channel(ch) => {
                if !this.closed {
                    ch.send_close();
                    this.closed = true;
                }
                Poll::Ready(Ok(()))
            }
        }
    }
}

impl Drop for ZitiStream {
    fn drop(&mut self) {
        // Best-effort: tell the demuxer to drop bookkeeping for this conn
        // so the peer learns the listener side hung up. The Direct backend
        // is owned end-to-end and needs no signaling.
        if !self.closed
            && let StreamBackend::Channel(ch) = &mut self.backend
        {
            ch.send_close();
            self.closed = true;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::transport::protocol::{ContentType, ZitiMessage};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    #[test]
    fn test_stream_buffer_operations() {
        let mut buffer = VecDeque::new();
        buffer.extend(b"test data");

        let data: Vec<u8> = buffer.drain(..4).collect();
        assert_eq!(data, b"test");

        let remaining: Vec<u8> = buffer.into_iter().collect();
        assert_eq!(remaining, b" data");
    }

    #[test]
    fn test_build_data_frame_includes_conn_id_header() {
        let frame = build_data_frame(42, 7, Bytes::from_static(b"hello")).unwrap();
        assert!(!frame.is_close);
        assert_eq!(frame.conn_id, 42);

        let parsed = ZitiMessage::deserialize(frame.bytes).unwrap();
        assert_eq!(parsed.content_type(), ContentType::Data);
        assert_eq!(parsed.sequence(), 7);
        assert_eq!(parsed.header.get_header("conn_id").unwrap(), "42");
        assert_eq!(parsed.payload().as_ref(), b"hello");
    }

    #[test]
    fn test_build_close_frame() {
        let frame = build_close_frame(13, 99).unwrap();
        assert!(frame.is_close);

        let parsed = ZitiMessage::deserialize(frame.bytes).unwrap();
        assert_eq!(parsed.content_type(), ContentType::Close);
        assert_eq!(parsed.header.get_header("conn_id").unwrap(), "13");
    }

    #[tokio::test]
    async fn channel_stream_reads_inbound_bytes() {
        let (in_tx, in_rx) = mpsc::unbounded_channel();
        let (out_tx, _out_rx) = mpsc::unbounded_channel();

        in_tx.send(Bytes::from_static(b"hello ")).unwrap();
        in_tx.send(Bytes::from_static(b"world")).unwrap();
        drop(in_tx);

        let mut stream = ZitiStream::from_channels(1, in_rx, out_tx);
        let mut got = Vec::new();
        stream.read_to_end(&mut got).await.unwrap();
        assert_eq!(got, b"hello world");
        assert!(stream.is_closed());
    }

    #[tokio::test]
    async fn channel_stream_writes_frame_data() {
        let (_in_tx, in_rx) = mpsc::unbounded_channel();
        let (out_tx, mut out_rx) = mpsc::unbounded_channel();

        let mut stream = ZitiStream::from_channels(5, in_rx, out_tx);
        stream.write_all(b"payload").await.unwrap();

        let frame = out_rx.recv().await.unwrap();
        assert_eq!(frame.conn_id, 5);
        let parsed = ZitiMessage::deserialize(frame.bytes).unwrap();
        assert_eq!(parsed.content_type(), ContentType::Data);
        assert_eq!(parsed.header.get_header("conn_id").unwrap(), "5");
        assert_eq!(parsed.payload().as_ref(), b"payload");
    }

    #[tokio::test]
    async fn channel_stream_close_emits_close_frame() {
        let (_in_tx, in_rx) = mpsc::unbounded_channel();
        let (out_tx, mut out_rx) = mpsc::unbounded_channel();

        let mut stream = ZitiStream::from_channels(9, in_rx, out_tx);
        stream.close().await.unwrap();
        assert!(stream.is_closed());

        let frame = out_rx.recv().await.unwrap();
        assert!(frame.is_close);
        let parsed = ZitiMessage::deserialize(frame.bytes).unwrap();
        assert_eq!(parsed.content_type(), ContentType::Close);
        assert_eq!(parsed.header.get_header("conn_id").unwrap(), "9");
    }
}