websocat 4.0.0-alpha2

Command-line client for web sockets, like netcat/curl/socat for ws://.
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use std::{
    net::SocketAddr,
    sync::Mutex,
    task::{ready, Poll},
    time::Duration,
};

use crate::scenario_executor::{
    scenario::{callback_and_continue, ScenarioAccess},
    types::{DatagramRead, DatagramSocket, DatagramWrite},
    utils1::{HandleExt, SimpleErr, NEUTRAL_SOCKADDR4},
    utils2::{AddressOrFd, DefragmenterAddChunkResult},
};
use bytes::BytesMut;
use futures::future::OptionFuture;
use lru::LruCache;
use rhai::{Dynamic, Engine, FnPtr, NativeCallContext};
use tokio::{net::UdpSocket, sync::mpsc::error::TrySendError, time::Instant};
use tracing::{debug, debug_span, error, trace, warn, Instrument};

use crate::scenario_executor::types::Handle;
use std::sync::Arc;

use super::{
    types::{BufferFlag, PacketRead, PacketReadResult, PacketWrite, Task},
    utils1::RhResult,
    utils2::Defragmenter,
};
use crate::scenario_executor::utils1::TaskHandleExt2;

struct VolatileClientInfo {
    deadline: Option<Instant>,
    removal_notifier: Option<tokio::sync::oneshot::Sender<()>>,
    sink: tokio::sync::mpsc::Sender<bytes::Bytes>,
}

impl VolatileClientInfo {
    fn dead(&self) -> bool {
        self.removal_notifier.is_none()
    }

    fn terminate(&mut self) {
        if let Some(rn) = self.removal_notifier.take() {
            let _ = rn.send(());
        }
    }
}

struct ClientInfo {
    addr: SocketAddr,
    v: Mutex<VolatileClientInfo>,
}

async fn hangup_monitor(
    ci: Arc<ClientInfo>,
    mut removal_notifier: tokio::sync::oneshot::Receiver<()>,
) {
    debug!(addr=?ci.addr, "Started hangup monitor");
    loop {
        trace!("hgmon loop");
        let (timeout, has_timeout): (OptionFuture<_>, bool) = {
            let mut l = ci.v.lock().unwrap();
            if l.dead() {
                trace!("hgmon dead");
                return;
            }
            let deadline = l.deadline;
            let now = Instant::now();
            if let Some(ref deadl) = deadline {
                if now >= *deadl {
                    debug!("Hangup monitor expired based on timeout");
                    l.terminate();
                    return;
                }
            }
            drop(l);
            (
                deadline.map(|d| tokio::time::sleep_until(d)).into(),
                deadline.is_some(),
            )
        };

        let do_expire = tokio::select! {
            biased;
            _ret = &mut removal_notifier => {
                true
            }
            _ret = timeout, if has_timeout => {
                // we loop around and check if possible updated dateline is really passed
                false
            }
        };

        if do_expire {
            debug!("Hangup monitor expired based on removal notifier");
            return;
        }
    }
}

struct UdpSend {
    s: Arc<UdpSocket>,
    ci: Arc<ClientInfo>,
    defragmenter: Defragmenter,
    inhibit_send_errors: bool,
}

impl UdpSend {
    fn new(
        s: Arc<UdpSocket>,
        ci: Arc<ClientInfo>,
        inhibit_send_errors: bool,
        max_send_datagram_size: usize,
    ) -> Self {
        Self {
            s,
            ci,
            defragmenter: Defragmenter::new(max_send_datagram_size),
            inhibit_send_errors,
        }
    }
}

impl PacketWrite for UdpSend {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut [u8],
        flags: super::types::BufferFlags,
    ) -> std::task::Poll<std::io::Result<()>> {
        trace!("poll_write");
        let this = self.get_mut();

        let data: &[u8] = match this.defragmenter.add_chunk(buf, flags) {
            DefragmenterAddChunkResult::DontSendYet => {
                return Poll::Ready(Ok(()));
            }
            DefragmenterAddChunkResult::Continunous(x) => x,
            DefragmenterAddChunkResult::SizeLimitExceeded(_x) => {
                warn!("Exceeded maximum allowed outgoing datagram size. Closing this session.");
                return Poll::Ready(Err(std::io::ErrorKind::InvalidData.into()));
            }
        };

        let inhibit_send_errors = this.inhibit_send_errors;

        let addr = this.ci.addr;

        {
            let v = this.ci.v.lock().unwrap();
            if v.dead() {
                return Poll::Ready(Err(std::io::ErrorKind::ConnectionAborted.into()));
            }
        }

        let ret = this.s.poll_send_to(cx, data, addr);

        match ready!(ret) {
            Ok(n) => {
                if n != data.len() {
                    warn!("short UDP send");
                }
            }
            Err(e) => {
                this.defragmenter.clear();
                if inhibit_send_errors {
                    warn!("Failed to send to UDP socket: {e}");
                } else {
                    return Poll::Ready(Err(e));
                }
            }
        }

        this.defragmenter.clear();
        Poll::Ready(Ok(()))
    }
}

struct UdpRecv {
    recv: tokio::sync::mpsc::Receiver<bytes::Bytes>,
    tag_as_text: bool,
}

impl PacketRead for UdpRecv {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut [u8],
    ) -> std::task::Poll<std::io::Result<PacketReadResult>> {
        trace!("poll_read");
        let this = self.get_mut();
        let flags = if this.tag_as_text {
            BufferFlag::Text.into()
        } else {
            Default::default()
        };

        let l;
        match ready!(this.recv.poll_recv(cx)) {
            Some(b) => {
                trace!(len = b.len(), "recv");
                if b.len() > buf.len() {
                    warn!("Incoming UDP datagram too big for a supplied buffer");
                    return Poll::Ready(Err(std::io::ErrorKind::InvalidInput.into()));
                }
                l = b.len();
                buf[..l].copy_from_slice(&b);
            }
            None => {
                debug!("conn abort");
                return Poll::Ready(Err(std::io::ErrorKind::ConnectionAborted.into()));
            }
        }

        Poll::Ready(Ok(PacketReadResult {
            flags,
            buffer_subset: 0..l,
        }))
    }
}

const fn default_max_send_datagram_size() -> usize {
    4096
}

//@ Create a single Datagram Socket that is bound to a UDP port,
//@ typically for connecting to a specific UDP endpoint
fn udp_server(
    ctx: NativeCallContext,
    opts: Dynamic,
    //@ Called once after the port is bound
    when_listening: FnPtr,
    //@ Called when new client is sending us datagrams
    on_accept: FnPtr,
) -> RhResult<Handle<Task>> {
    let original_span = tracing::Span::current();
    let span = debug_span!(parent: original_span, "udp_server");
    let the_scenario = ctx.get_scenario()?;
    debug!(parent: &span, "node created");
    #[derive(serde::Deserialize)]
    struct Opts {
        //@ Specify address to bind the socket to.
        bind: Option<SocketAddr>,

        //@ Inherited file descriptor to accept connections from
        fd: Option<i32>,

        //@ Inherited file named (`LISTEN_FDNAMES``) descriptor to accept connections from
        named_fd: Option<String>,

        //@ Skip socket type check when using `fd`.
        #[serde(default)]
        fd_force: bool,

        //@ Mark the conection as closed when this number
        //@ of milliseconds elapse without a new datagram
        //@ from associated peer address
        timeout_ms: Option<u64>,

        //@ Maximum number of simultaneously connected clients.
        //@ If exceed, stale clients (based on the last received datagram) will be hung up.
        max_clients: Option<usize>,

        //@ Buffer size for receiving UDP datagrams.
        //@ Default is 4096 bytes.
        buffer_size: Option<usize>,

        //@ Queue length for distributing received UDP datagrams among spawned DatagramSocekts
        //@ Defaults to 1.
        qlen: Option<usize>,

        //@ Tag incoming UDP datagrams to be sent as text WebSocket messages
        //@ instead of binary.
        //@ Note that Websocat does not check for UTF-8 correctness and may
        //@ send non-compiant text WebSocket messages.
        #[serde(default)]
        tag_as_text: bool,

        //@ In case of one slow client handler, delay incoming UDP datagrams
        //@ instead of dropping them
        #[serde(default)]
        backpressure: bool,

        //@ Do not exit if `sendto` returned an error.
        #[serde(default)]
        inhibit_send_errors: bool,

        //@ Default defragmenter buffer limit
        #[serde(default = "default_max_send_datagram_size")]
        max_send_datagram_size: usize,
    }
    let opts: Opts = rhai::serde::from_dynamic(&opts)?;
    //span.record("addr", field::display(opts.addr));

    let mut lru: LruCache<SocketAddr, Arc<ClientInfo>> = match opts.max_clients {
        None => LruCache::unbounded(),
        Some(0) => return Err(ctx.err("max_clients cannot be 0")),
        Some(n) => LruCache::new(std::num::NonZeroUsize::new(n).unwrap()),
    };

    let buffer_size = opts.buffer_size.unwrap_or(4096);

    let qlen = opts.qlen.unwrap_or(1);

    let backpressure = opts.backpressure;

    if buffer_size == 0 {
        return Err(ctx.err("Invalid buffer_size 0"));
    }

    let a = AddressOrFd::interpret(&ctx, &span, opts.bind, opts.fd, opts.named_fd, None)?;

    Ok(async move {
        debug!("node started");
        let mut buf = BytesMut::new();
    
        let mut clients_add_events: usize = 0;


        let mut address_to_report = NEUTRAL_SOCKADDR4;

        let s = match a {
            AddressOrFd::Addr(a) => {
                address_to_report = a;
                UdpSocket::bind(a).await?
            }
            #[cfg(not(unix))]
            AddressOrFd::Fd(..) | AddressOrFd::NamedFd(..) => {
                error!("Inheriting listeners from parent processes is not supported outside UNIX platforms");
                anyhow::bail!("Unsupported feature");
            }
            #[cfg(unix)]
            AddressOrFd::Fd(_) | AddressOrFd::NamedFd(_) => {
                use super::unix1::{listen_from_fd, listen_from_fd_named, ListenFromFdType};
    
                let force_addr = opts.fd_force.then_some(ListenFromFdType::Udp);
                let assert_addr = Some(ListenFromFdType::Udp);
                let ret = match a {
                    AddressOrFd::Addr(_) => unreachable!(),
                    AddressOrFd::Fd(fd) => unsafe { listen_from_fd(fd, force_addr, assert_addr) },
                    AddressOrFd::NamedFd(ref fd) => unsafe {
                        listen_from_fd_named(fd, force_addr, assert_addr)
                    },
                };
                ret?.unwrap_udp()
            }
        };

        if address_to_report.port() == 0 {
            if let Ok(a) = s.local_addr() {
                address_to_report = a;
            } else {
                warn!("Failed to obtain actual listening port");
            }
        }

        callback_and_continue::<(SocketAddr,)>(
            the_scenario.clone(),
            when_listening,
            (address_to_report,),
        )
        .await;

        let s = Arc::new(s);

        'main_loop: loop {
            trace!("loop");
            if clients_add_events == 1024 && opts.max_clients.unwrap_or(4096) >= 4096 {
                debug!("vacuum");
                let mut ctr = 0;
                let dead_clients = Vec::from_iter(
                    lru.iter()
                        .filter(|x| x.1.v.lock().unwrap().dead())
                        .map(|x| *x.0),
                );
                for x in dead_clients {
                    if lru.pop(&x).is_some() {
                        ctr += 1;
                    }
                }
                if ctr > 0 {
                    debug!("Vacuumed {ctr} stale entries");
                }
                clients_add_events = 0;
            }

            buf.reserve(buffer_size.saturating_sub(buf.capacity()));

            let (b, from_addr) = match s.recv_buf_from(&mut buf).await {
                Ok((n, from_addr)) => {
                    trace!(n, %from_addr, "recv");
                    let b = buf.split_to(n).freeze();
                    (b, from_addr)
                }
                Err(e) => {
                    error!("Error receiving from udp: {e}");
                    tokio::time::sleep(std::time::Duration::from_millis(20)).await;
                    continue 'main_loop;
                }
            };

            let ci :&Arc<ClientInfo> = 'obtaining_entry: loop {
                trace!("lookup");
                break match lru.get(&from_addr) {
                    None => {
                        trace!("not found");
                        clients_add_events += 1;
                        let (tx, rx) = tokio::sync::mpsc::channel(qlen);
                        let (tx2, rx2) = tokio::sync::oneshot::channel();
                        let ci = Arc::new(ClientInfo {
                            addr: from_addr,
                            v: Mutex::new(VolatileClientInfo {
                                deadline: None,
                                removal_notifier: Some(tx2),
                                sink: tx,
                            }),
                        });
                        {
                            assert!(!ci.v.lock().unwrap().dead());
                        }
                        

                        let ci2 = ci.clone();
                        let ci3 = ci.clone();
                        if let Some((_, evicted)) = lru.push(from_addr, ci) {
                            debug!(peeraddr=%evicted.addr, "evicting");
                            let mut ev = evicted.v.lock().unwrap();
                            ev.terminate();
                        }

                        let udp_send = UdpSend::new(s.clone(), ci2, opts.inhibit_send_errors, opts.max_send_datagram_size);
                        let udp_recv = UdpRecv {
                            recv: rx,
                            tag_as_text: opts.tag_as_text,
                        };
                        let hangup =
                            Some(Box::pin(hangup_monitor(ci3, rx2)) as super::types::Hangup);
                        let socket = DatagramSocket {
                            read: Some(DatagramRead {
                                src: Box::pin(udp_recv),
                            }),
                            write: Some(DatagramWrite {
                                snk: Box::pin(udp_send),
                            }),
                            close: hangup,
                            fd: None,
                        };


                        let the_scenario = the_scenario.clone();
                        let on_accept = on_accept.clone();
                        tokio::spawn(async move {
                            let newspan = debug_span!("udp_accept", from=%from_addr);
                            debug!("accepted");
                            callback_and_continue::<(Handle<DatagramSocket>, SocketAddr)>(
                                the_scenario,
                                on_accept,
                                (Some(socket).wrap(), from_addr),
                            )
                            .instrument(newspan)
                            .await;
                        });

                        lru.get(&from_addr).unwrap()
                    }
                    Some(x) => {
                        let dead = { x.v.lock().unwrap().dead() };
                        trace!(dead, "found");
                        if dead {
                            lru.pop(&from_addr);
                            continue 'obtaining_entry;
                        }
                        x
                    }
                };
            };

            let mut send_debt = None;
            {
                let mut v = ci.v.lock().unwrap();
                if v.dead() {
                    warn!("A rare case of a dropped incoming datagram because of timer expiration in an unfortunate moment.");
                    continue 'main_loop;
                }
                if let Some(tmo) = opts.timeout_ms {
                    let deadline = Instant::now() + Duration::from_millis(tmo);
                    v.deadline = Some(deadline);
                }

                match v.sink.try_send(b) {
                    Ok(()) => (),
                    Err(TrySendError::Closed(_)) => {
                        v.terminate();
                    }
                    Err(TrySendError::Full(b)) => {
                        if backpressure {
                            send_debt = Some((v.sink.clone(), b));
                            
                        } else {
                            debug!(peer_addr=%from_addr, "dropping a datagram due to handler being too slow")
                        }
                    }
                }
            }
            if let Some((sink2, b)) = send_debt {
                debug!(peer_addr=%from_addr, "buffer full, sending later");
                match sink2.send(b).await {
                    Ok(()) => (),
                    Err(_) => {
                        let mut vv = ci.v.lock().unwrap();
                        vv.terminate();
                    }
                }
            }
        }
    }
    .instrument(span)
    .wrap())
}

pub fn register(engine: &mut Engine) {
    engine.register_fn("udp_server", udp_server);
}