netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! RPKI-to-Router client in the router role. Rust sends the Reset Query on connect, polls with
//! Serial Queries at the refresh interval and on Serial Notify, and answers Cache Reset with a
//! new Reset Query; the handler hears about each completed exchange.
pub mod actions;
use crate::client::llm_budget::call_llm_for_client;
use crate::llm::actions::client_trait::{Client, ClientActionResult};
use crate::logging::emit::Log;
use crate::protocol::{ConnectContext, Event};
use crate::server::rpki_rtr::codec::{self, Intervals, Packet, Pdu, Record};
use crate::state::client_handles::{ClientCommand, ClientSendOutcome};
use crate::state::{AccessLogOwner, ClientStatus};
pub use actions::RpkiRtrClientProtocol;
use anyhow::{bail, ensure, Context, Result};
use serde_json::{json, Value};
use std::net::SocketAddr;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::mpsc;

pub const EXCHANGE_TIMEOUT: Duration = Duration::from_secs(120);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(30);
const MAX_EXCHANGE_PDUS: u64 = 1_000_000;
const SAMPLE: usize = 256;

pub async fn connect(ctx: ConnectContext) -> Result<SocketAddr> {
    let p = ctx.startup_params.as_ref();
    let u64_param = |k: &str| {
        p.map(|p| p.get_optional_u64(k))
            .transpose()
            .map(Option::flatten)
    };
    let version = match u64_param("version")? {
        None | Some(1) => 1u8,
        Some(0) => 0,
        Some(v) => bail!("version must be 0 or 1, not {v}"),
    };
    let refresh = codec::seconds(
        u64_param("refresh_interval_secs")?,
        u64::from(codec::DEFAULT_REFRESH_SECONDS),
        86400,
        "refresh_interval_secs",
    )?;
    let exchange_timeout = codec::seconds(
        u64_param("exchange_timeout_secs")?,
        EXCHANGE_TIMEOUT.as_secs(),
        600,
        "exchange_timeout_secs",
    )?;
    let stream = tokio::time::timeout(
        CONNECT_TIMEOUT,
        tokio::net::TcpStream::connect(&ctx.remote_addr),
    )
    .await
    .context("RPKI-RTR connect deadline")??;
    let local = stream.local_addr()?;
    ctx.state
        .update_client_status(ctx.client_id, ClientStatus::Connected)
        .await;
    let external =
        crate::client::command_support::register_command_channel(&ctx.state, ctx.client_id).await;
    let (internal_tx, internal_rx) = mpsc::channel::<Value>(16);
    let (event_tx, mut event_rx) = mpsc::channel::<Event>(16);
    let events_ctx = ctx.clone();
    let dispatcher = tokio::spawn(async move {
        let protocol = RpkiRtrClientProtocol;
        while let Some(event) = event_rx.recv().await {
            let instruction = events_ctx
                .state
                .get_instruction_for_client(events_ctx.client_id)
                .await
                .unwrap_or_default();
            let memory = events_ctx
                .state
                .get_memory_for_client(events_ctx.client_id)
                .await
                .unwrap_or_default();
            match call_llm_for_client(
                &events_ctx.llm_client,
                &events_ctx.state,
                events_ctx.client_id.to_string(),
                &instruction,
                &memory,
                Some(&event),
                &protocol,
                &events_ctx.status_tx,
            )
            .await
            {
                Ok(result) => {
                    if let Some(memory) = result.memory_updates {
                        events_ctx
                            .state
                            .set_memory_for_client(events_ctx.client_id, memory)
                            .await;
                    }
                    for action in result.actions {
                        if internal_tx.send(action).await.is_err() {
                            return;
                        }
                    }
                }
                Err(e) => Log::new(Some(&events_ctx.status_tx))
                    .warn(format!("RPKI-RTR client handler: {e}")),
            }
        }
    });
    let dispatcher_abort = dispatcher.abort_handle();
    ctx.state
        .register_client_task(ctx.client_id, dispatcher)
        .await;
    let (reader, writer) = tokio::io::split(stream);
    // A dedicated reader keeps PDU reads cancel-safe against timers and commands.
    let (frame_tx, frame_rx) = mpsc::channel::<Result<Vec<u8>>>(64);
    let reader_task = tokio::spawn(read_frames(reader, frame_tx, exchange_timeout));
    ctx.state
        .register_client_task(ctx.client_id, reader_task)
        .await;
    let session_ctx = ctx.clone();
    let task = tokio::spawn(async move {
        let mut router = Router::new(version, refresh, exchange_timeout, writer);
        let result = router
            .run(&session_ctx, external, internal_rx, frame_rx, event_tx)
            .await;
        if result.is_err() {
            dispatcher_abort.abort();
        }
        let status = match result {
            Ok(()) => ClientStatus::Disconnected,
            Err(e) => {
                Log::new(Some(&session_ctx.status_tx)).warn(format!("RPKI-RTR client ended: {e}"));
                ClientStatus::Error(e.to_string())
            }
        };
        session_ctx
            .state
            .update_client_status(session_ctx.client_id, status)
            .await;
        session_ctx
            .state
            .remove_client_handle(session_ctx.client_id)
            .await;
        let _ = session_ctx.status_tx.send("__UPDATE_UI__".into());
    });
    ctx.state.register_client_task(ctx.client_id, task).await;
    Ok(local)
}

/// Header with no deadline (a cache may be silent until the next poll); body bounded.
async fn read_frames(
    mut reader: tokio::io::ReadHalf<tokio::net::TcpStream>,
    tx: mpsc::Sender<Result<Vec<u8>>>,
    body_deadline: Duration,
) {
    loop {
        let mut header = [0u8; 8];
        let frame = async {
            reader.read_exact(&mut header).await?;
            let len = u32::from_be_bytes([header[4], header[5], header[6], header[7]]) as usize;
            ensure!(
                (8..=codec::MAX_PDU_BYTES).contains(&len),
                "RPKI-RTR inbound PDU bound"
            );
            let mut bytes = vec![0u8; len];
            bytes[..8].copy_from_slice(&header);
            tokio::time::timeout(body_deadline, reader.read_exact(&mut bytes[8..]))
                .await
                .context("RPKI-RTR PDU body deadline")??;
            Ok(bytes)
        }
        .await;
        let end = frame.is_err();
        if tx.send(frame).await.is_err() || end {
            return;
        }
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Kind {
    Reset,
    Incremental,
}

struct Exchange {
    kind: Kind,
    started: tokio::time::Instant,
    session: Option<u16>,
    announced: u64,
    withdrawn: u64,
    pdus: u64,
    router_keys: u64,
    sample: Vec<Value>,
}

struct Router {
    version: u8,
    refresh: Duration,
    exchange_timeout: Duration,
    writer: tokio::io::WriteHalf<tokio::net::TcpStream>,
    session: Option<u16>,
    serial: Option<u32>,
    intervals: Option<Intervals>,
    exchange: Option<Exchange>,
    next_poll: tokio::time::Instant,
}

impl Router {
    fn new(
        version: u8,
        refresh: Duration,
        exchange_timeout: Duration,
        writer: tokio::io::WriteHalf<tokio::net::TcpStream>,
    ) -> Self {
        Self {
            version,
            refresh,
            exchange_timeout,
            writer,
            session: None,
            serial: None,
            intervals: None,
            exchange: None,
            next_poll: tokio::time::Instant::now() + refresh,
        }
    }

    async fn write(&mut self, pdu: Pdu) -> Result<usize> {
        codec::write_packet(
            &mut self.writer,
            &Packet {
                version: self.version,
                pdu,
            },
        )
        .await
    }

    async fn reset_query(&mut self) -> Result<usize> {
        let n = self.write(Pdu::ResetQuery).await?;
        self.begin(Kind::Reset);
        Ok(n)
    }

    async fn serial_query(&mut self) -> Result<usize> {
        let (Some(session), Some(serial)) = (self.session, self.serial) else {
            bail!("no synchronized session yet; send a Reset Query first");
        };
        let n = self.write(Pdu::SerialQuery { session, serial }).await?;
        self.begin(Kind::Incremental);
        Ok(n)
    }

    fn begin(&mut self, kind: Kind) {
        self.exchange = Some(Exchange {
            kind,
            started: tokio::time::Instant::now(),
            session: None,
            announced: 0,
            withdrawn: 0,
            pdus: 0,
            router_keys: 0,
            sample: Vec::new(),
        });
    }

    async fn fail(&mut self, code: u16, offending: &[u8], why: &str) -> anyhow::Error {
        let _ = codec::write_packet(
            &mut self.writer,
            &codec::error_report(self.version, code, offending, why),
        )
        .await;
        anyhow::anyhow!("RPKI-RTR {}: {why}", codec::error_name(code))
    }

    async fn run(
        &mut self,
        ctx: &ConnectContext,
        mut external: mpsc::Receiver<ClientCommand>,
        mut internal: mpsc::Receiver<Value>,
        mut frames: mpsc::Receiver<Result<Vec<u8>>>,
        events: mpsc::Sender<Event>,
    ) -> Result<()> {
        self.reset_query().await?;
        loop {
            let deadline = match &self.exchange {
                Some(e) => e.started + self.exchange_timeout,
                None => self.next_poll,
            };
            tokio::select! {
                frame = frames.recv() => {
                    let Some(frame) = frame else { return Ok(()) };
                    let bytes = match frame {
                        Ok(b) => b,
                        Err(e) if e.downcast_ref::<std::io::Error>().is_some_and(|io| io.kind() == std::io::ErrorKind::UnexpectedEof) => return Ok(()),
                        Err(e) => return Err(e),
                    };
                    if self.on_pdu(&bytes, &events).await? {
                        return Ok(());
                    }
                }
                _ = tokio::time::sleep_until(deadline) => {
                    if self.exchange.is_some() {
                        bail!("RPKI-RTR exchange did not reach End of Data within {}s", self.exchange_timeout.as_secs());
                    }
                    if self.serial.is_some() {
                        self.serial_query().await?;
                    } else {
                        self.reset_query().await?;
                    }
                }
                command = external.recv() => {
                    let Some(command) = command else { return Ok(()) };
                    let action = command.action.clone();
                    let (reply, end) = self.on_action(action.clone()).await;
                    ctx.state.record_access_log(AccessLogOwner::Client(ctx.client_id.as_u32()), "RPKI-RTR", None, "injected_action", action, vec![json!({"ok": reply.is_ok()})]).await;
                    crate::client::command_support::reply(command, reply);
                    if end { return Ok(()); }
                }
                action = internal.recv() => {
                    let Some(action) = action else { return Ok(()) };
                    let (reply, end) = self.on_action(action).await;
                    if let Err(e) = reply {
                        Log::new(Some(&ctx.status_tx)).warn(format!("RPKI-RTR client action refused: {e}"));
                    }
                    if end { return Ok(()); }
                }
            }
        }
    }

    async fn on_action(&mut self, action: Value) -> (Result<ClientSendOutcome>, bool) {
        let result = match RpkiRtrClientProtocol.execute_action(action) {
            Ok(r) => r,
            Err(e) => {
                return (
                    Ok(ClientSendOutcome::Rejected {
                        error: e.to_string(),
                    }),
                    false,
                )
            }
        };
        match result {
            ClientActionResult::Disconnect => {
                let _ = self.writer.shutdown().await;
                (Ok(ClientSendOutcome::Disconnected), true)
            }
            ClientActionResult::Custom { name, .. } => {
                if self.exchange.is_some() {
                    return (
                        Ok(ClientSendOutcome::Rejected {
                            error: "an RTR exchange is in progress; retry after End of Data".into(),
                        }),
                        false,
                    );
                }
                let sent = if name == "rpki_rtr_reset_query" {
                    self.reset_query().await
                } else {
                    self.serial_query().await
                };
                match sent {
                    Ok(n) => (Ok(ClientSendOutcome::Sent { bytes_sent: n }), false),
                    Err(e) if self.session.is_none() && name == "rpki_rtr_serial_query" => (
                        Ok(ClientSendOutcome::Rejected {
                            error: e.to_string(),
                        }),
                        false,
                    ),
                    Err(e) => (Err(e), true),
                }
            }
            _ => (
                Ok(ClientSendOutcome::Rejected {
                    error: "unsupported action".into(),
                }),
                false,
            ),
        }
    }

    /// Handle one PDU; `Ok(true)` ends the session cleanly.
    async fn on_pdu(&mut self, bytes: &[u8], events: &mpsc::Sender<Event>) -> Result<bool> {
        if bytes[0] != self.version {
            // An Error Report saying the cache does not speak our version arrives in its own.
            if bytes[1] == 10 {
                if let Ok(Packet {
                    pdu:
                        Pdu::ErrorReport {
                            code, diagnostic, ..
                        },
                    ..
                }) = Packet::decode(bytes)
                {
                    return self.error_event(code, &diagnostic, events).map(|_| true);
                }
            }
            return Err(self
                .fail(8, bytes, "PDU version differs from the session's")
                .await);
        }
        if bytes[1] == 9 && self.version == 1 {
            // Router Key (BGPsec): outside this client's scope, counted and ignored.
            if let Some(e) = self.exchange.as_mut() {
                e.router_keys += 1;
                e.pdus += 1;
            }
            return Ok(false);
        }
        let packet = match Packet::decode(bytes) {
            Ok(p) => p,
            Err(e) => {
                let code = if matches!(bytes[1], 0..=4 | 6..=8 | 10) {
                    0
                } else {
                    5
                };
                return Err(self.fail(code, bytes, &e.to_string()).await);
            }
        };
        match packet.pdu {
            Pdu::CacheResponse { session } => {
                let Some(exchange) = self.exchange.as_mut() else {
                    return Err(self.fail(0, bytes, "Cache Response without a query").await);
                };
                if exchange.session.is_some() {
                    return Err(self
                        .fail(0, bytes, "second Cache Response in one exchange")
                        .await);
                }
                if exchange.kind == Kind::Incremental && Some(session) != self.session {
                    return Err(self
                        .fail(0, bytes, "Cache Response changed the session id")
                        .await);
                }
                exchange.session = Some(session);
            }
            Pdu::Prefix(record) => {
                let Some(exchange) = self.exchange.as_mut().filter(|e| e.session.is_some()) else {
                    return Err(self.fail(0, bytes, "Prefix PDU outside an exchange").await);
                };
                if exchange.kind == Kind::Reset && !record.announcement {
                    return Err(self.fail(6, bytes, "withdrawal in a reset exchange").await);
                }
                exchange.pdus += 1;
                if exchange.pdus > MAX_EXCHANGE_PDUS {
                    return Err(self.fail(1, bytes, "exchange exceeds 1,000,000 PDUs").await);
                }
                if record.announcement {
                    exchange.announced += 1;
                } else {
                    exchange.withdrawn += 1;
                }
                if exchange.sample.len() < SAMPLE {
                    exchange.sample.push(record_json(&record));
                }
            }
            Pdu::EndOfData {
                session,
                serial,
                intervals,
            } => {
                let Some(exchange) = self.exchange.take().filter(|e| e.session == Some(session))
                else {
                    return Err(self
                        .fail(
                            0,
                            bytes,
                            "End of Data does not close an exchange of this session",
                        )
                        .await);
                };
                if exchange.kind == Kind::Incremental {
                    let old = self.serial.unwrap_or(serial);
                    if serial != old && !codec::serial_newer(serial, old) {
                        return Err(self
                            .fail(0, bytes, "End of Data serial went backwards")
                            .await);
                    }
                }
                self.session = Some(session);
                self.serial = Some(serial);
                if self.version == 1 {
                    self.intervals = Some(intervals);
                }
                let refresh = self
                    .intervals
                    .map(|i| Duration::from_secs(u64::from(i.refresh)))
                    .unwrap_or(self.refresh);
                self.next_poll = tokio::time::Instant::now() + refresh;
                let shown = self.intervals.unwrap_or(Intervals {
                    refresh: refresh.as_secs() as u32,
                    ..Intervals::default()
                });
                let truncated =
                    exchange.announced + exchange.withdrawn > exchange.sample.len() as u64;
                events
                    .try_send(Event::new(
                        &actions::SYNCHRONIZED_EVENT,
                        json!({
                            "kind": if exchange.kind == Kind::Reset { "reset" } else { "incremental" },
                            "session_id": session,
                            "serial": serial,
                            "announced": exchange.announced,
                            "withdrawn": exchange.withdrawn,
                            "records": exchange.sample,
                            "truncated": truncated,
                            "router_keys_ignored": exchange.router_keys,
                            "intervals": {"refresh": shown.refresh, "retry": shown.retry, "expire": shown.expire},
                        }),
                    ))
                    .context("RPKI-RTR event queue full; consumer stalled")?;
            }
            Pdu::CacheReset => {
                if !matches!(
                    self.exchange.as_ref().map(|e| (e.kind, e.session)),
                    Some((Kind::Incremental, None))
                ) {
                    return Err(self
                        .fail(0, bytes, "Cache Reset outside a pending Serial Query")
                        .await);
                }
                let previous = self.serial;
                self.session = None;
                self.serial = None;
                self.reset_query().await?;
                events
                    .try_send(Event::new(
                        &actions::CACHE_RESET_EVENT,
                        json!({"previous_serial": previous}),
                    ))
                    .context("RPKI-RTR event queue full")?;
            }
            Pdu::SerialNotify { session, serial } => {
                // Poll now unless an exchange is running or the notice is about another session.
                if self.exchange.is_none()
                    && Some(session) == self.session
                    && Some(serial) != self.serial
                {
                    self.serial_query().await?;
                }
            }
            Pdu::ErrorReport {
                code, diagnostic, ..
            } => {
                let end = self.error_event(code, &diagnostic, events)?;
                if code == 2 {
                    self.exchange = None;
                    let retry = self
                        .intervals
                        .map(|i| u64::from(i.retry))
                        .unwrap_or(u64::from(codec::DEFAULT_RETRY_SECONDS));
                    self.next_poll = tokio::time::Instant::now() + Duration::from_secs(retry);
                }
                return Ok(end);
            }
            Pdu::ResetQuery | Pdu::SerialQuery { .. } => {
                return Err(self.fail(3, bytes, "a cache does not send queries").await);
            }
        }
        Ok(false)
    }

    fn error_event(
        &mut self,
        code: u16,
        diagnostic: &str,
        events: &mpsc::Sender<Event>,
    ) -> Result<bool> {
        events
            .try_send(Event::new(
                &actions::ERROR_EVENT,
                json!({"code": code, "name": codec::error_name(code), "diagnostic": crate::utils::sanitize::strip_controls(diagnostic)}),
            ))
            .context("RPKI-RTR event queue full")?;
        Ok(code != 2)
    }
}

fn record_json(r: &Record) -> Value {
    json!({"prefix": r.prefix, "max_length": r.max_length, "asn": r.asn, "announcement": r.announcement})
}