netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
//! KNXnet/IP tunnelling client: one tunnel to a gateway, group writes and reads out, every
//! group telegram on the bus in as an event.
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::knx::wire::{self, Apci, Telegram};
use crate::state::client_handles::{ClientCommand, ClientSendOutcome};
use crate::state::{AccessLogOwner, ClientStatus};
pub use actions::KnxClientProtocol;
use anyhow::{bail, ensure, Context, Result};
use serde_json::{json, Value};
use std::collections::{HashMap, VecDeque};
use std::net::SocketAddr;
use std::time::Duration;
use tokio::net::UdpSocket;
use tokio::sync::mpsc;

pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
pub const ACK_TIMEOUT: Duration = Duration::from_secs(1);
pub const HEARTBEAT: Duration = Duration::from_secs(60);
pub const MAX_QUEUED: usize = 32;
pub const MAX_FOLLOWUP_DEPTH: usize = 8;

fn hpai_nat() -> Vec<u8> {
    vec![8, 0x01, 0, 0, 0, 0, 0, 0]
}

fn types(ctx: &ConnectContext) -> Result<HashMap<u16, String>> {
    let mut out = HashMap::new();
    if let Some(map) = ctx
        .startup_params
        .as_ref()
        .map(|p| p.get_optional_object("group_types"))
        .transpose()?
        .flatten()
    {
        for (ga, dpt) in map {
            let dpt = dpt.as_str().context("group_types values are DPT names")?;
            wire::dpt_main(dpt)?;
            out.insert(wire::parse_group(ga)?, dpt.to_string());
        }
    }
    Ok(out)
}

pub async fn connect(ctx: ConnectContext) -> Result<SocketAddr> {
    let types = types(&ctx)?;
    let gateway: SocketAddr = tokio::net::lookup_host(&ctx.remote_addr)
        .await?
        .find(SocketAddr::is_ipv4)
        .context("the gateway needs an IPv4 address")?;
    let bind: SocketAddr = if gateway.ip().is_loopback() {
        "127.0.0.1:0".parse()?
    } else {
        "0.0.0.0:0".parse()?
    };
    let socket = UdpSocket::bind(bind).await?;
    let local = socket.local_addr()?;
    let mut body = hpai_nat();
    body.extend(hpai_nat());
    body.extend([4, 0x04, 0x02, 0x00]);
    socket
        .send_to(&wire::frame(wire::CONNECT_REQUEST, &body), gateway)
        .await?;
    let (channel, address) = tokio::time::timeout(CONNECT_TIMEOUT, async {
        let mut buf = vec![0u8; wire::MAX_FRAME + 1];
        loop {
            let (n, from) = socket.recv_from(&mut buf).await?;
            if from != gateway {
                continue;
            }
            let Ok((wire::CONNECT_RESPONSE, b)) = wire::parse_frame(&buf[..n]) else {
                continue;
            };
            ensure!(b.len() >= 2, "short CONNECT_RESPONSE");
            ensure!(
                b[1] == wire::E_NO_ERROR,
                "the gateway refused the tunnel (status {:#04x})",
                b[1]
            );
            let crd = b.get(10..14).context("CONNECT_RESPONSE without a CRD")?;
            return Ok::<_, anyhow::Error>((b[0], u16::from_be_bytes([crd[2], crd[3]])));
        }
    })
    .await
    .context("no CONNECT_RESPONSE from the gateway")??;
    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, usize)>(16);
    let (event_tx, mut event_rx) = mpsc::channel::<(Event, usize)>(64);
    event_tx.try_send((
        Event::new(
            &actions::CONNECTED_EVENT,
            json!({"remote_addr": gateway.to_string(), "individual_address": wire::format_individual(address)}),
        ),
        0,
    ))?;
    let events_ctx = ctx.clone();
    let dispatcher = tokio::spawn(async move {
        while let Some((event, depth)) = 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),
                &KnxClientProtocol,
                &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, depth + 1)).await.is_err() {
                            return;
                        }
                    }
                }
                Err(e) => {
                    Log::new(Some(&events_ctx.status_tx)).warn(format!("KNX client handler: {e}"))
                }
            }
        }
    });
    let dispatcher_abort = dispatcher.abort_handle();
    ctx.state
        .register_client_task(ctx.client_id, dispatcher)
        .await;
    let session_ctx = ctx.clone();
    let task = tokio::spawn(async move {
        let mut s = Session {
            ctx: &session_ctx,
            socket,
            gateway,
            channel,
            address,
            types,
            seq_out: 0,
            seq_in: 0,
            pending: None,
            queue: VecDeque::new(),
            events: event_tx,
        };
        let result = s.run(external, internal_rx).await;
        let _ = s
            .socket
            .send_to(&disconnect_request(channel), gateway)
            .await;
        dispatcher_abort.abort();
        let status = match result {
            Ok(()) => ClientStatus::Disconnected,
            Err(e) => {
                Log::new(Some(&session_ctx.status_tx)).warn(format!("KNX 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)
}

fn disconnect_request(channel: u8) -> Vec<u8> {
    let mut body = vec![channel, 0];
    body.extend(hpai_nat());
    wire::frame(wire::DISCONNECT_REQUEST, &body)
}

struct Pending {
    seq: u8,
    bytes: Vec<u8>,
    deadline: tokio::time::Instant,
    retried: bool,
    caller: Option<ClientCommand>,
}

struct Session<'a> {
    ctx: &'a ConnectContext,
    socket: UdpSocket,
    gateway: SocketAddr,
    channel: u8,
    address: u16,
    types: HashMap<u16, String>,
    seq_out: u8,
    seq_in: u8,
    pending: Option<Pending>,
    queue: VecDeque<(Telegram, Option<ClientCommand>)>,
    events: mpsc::Sender<(Event, usize)>,
}

fn reply(caller: Option<ClientCommand>, outcome: ClientSendOutcome) {
    if let Some(c) = caller {
        crate::client::command_support::reply(c, Ok(outcome));
    }
}

impl Session<'_> {
    async fn run(
        &mut self,
        mut external: mpsc::Receiver<ClientCommand>,
        mut internal: mpsc::Receiver<(Value, usize)>,
    ) -> Result<()> {
        let log = Log::new(Some(&self.ctx.status_tx));
        let mut buf = vec![0u8; wire::MAX_FRAME + 1];
        let mut heartbeat = tokio::time::interval(HEARTBEAT);
        heartbeat.tick().await;
        loop {
            let deadline = self
                .pending
                .as_ref()
                .map(|p| p.deadline)
                .unwrap_or_else(|| tokio::time::Instant::now() + HEARTBEAT * 10);
            let (action, depth, caller) = tokio::select! {
                r = self.socket.recv_from(&mut buf) => {
                    let (n, from) = r?;
                    if from == self.gateway {
                        let frame = buf[..n].to_vec();
                        if !self.incoming(&frame).await? {
                            return Ok(());
                        }
                    }
                    continue;
                }
                _ = tokio::time::sleep_until(deadline), if self.pending.is_some() => {
                    let mut p = self.pending.take().expect("pending");
                    ensure!(!p.retried, "the gateway did not acknowledge request {} twice", p.seq);
                    self.socket.send_to(&p.bytes, self.gateway).await?;
                    p.retried = true;
                    p.deadline = tokio::time::Instant::now() + ACK_TIMEOUT;
                    self.pending = Some(p);
                    continue;
                }
                _ = heartbeat.tick() => {
                    let mut body = vec![self.channel, 0];
                    body.extend(hpai_nat());
                    self.socket.send_to(&wire::frame(wire::CONNECTIONSTATE_REQUEST, &body), self.gateway).await?;
                    continue;
                }
                c = external.recv() => match c {
                    Some(c) => (c.action.clone(), 0, Some(c)),
                    None => return Ok(()),
                },
                a = internal.recv() => match a {
                    Some((a, d)) => (a, d, None),
                    None => return Ok(()),
                },
            };
            match KnxClientProtocol.execute_action(action.clone()) {
                Ok(ClientActionResult::Disconnect) => {
                    reply(caller, ClientSendOutcome::Disconnected);
                    return Ok(());
                }
                Ok(_) => {}
                Err(e) => {
                    reply(
                        caller,
                        ClientSendOutcome::Rejected {
                            error: e.to_string(),
                        },
                    );
                    continue;
                }
            }
            if depth > MAX_FOLLOWUP_DEPTH {
                log.warn(format!(
                    "KNX client: handler chain stopped after {MAX_FOLLOWUP_DEPTH} follow-ups"
                ));
                continue;
            }
            let telegram = match self.telegram(&action) {
                Ok(t) => t,
                Err(e) => {
                    log.warn(format!("KNX action refused: {e:#}"));
                    reply(
                        caller,
                        ClientSendOutcome::Rejected {
                            error: format!("{e:#}"),
                        },
                    );
                    continue;
                }
            };
            if caller.is_some() {
                self.ctx
                    .state
                    .record_access_log(
                        AccessLogOwner::Client(self.ctx.client_id.as_u32()),
                        "KNX/IP",
                        None,
                        "injected_action",
                        action.clone(),
                        vec![],
                    )
                    .await;
            }
            if self.queue.len() >= MAX_QUEUED {
                reply(
                    caller,
                    ClientSendOutcome::Rejected {
                        error: "too many telegrams queued".into(),
                    },
                );
                continue;
            }
            self.queue.push_back((telegram, caller));
            self.send_next().await?;
        }
    }

    fn telegram(&self, action: &Value) -> Result<Telegram> {
        let ga = wire::parse_group(action["group_address"].as_str().unwrap_or_default())?;
        let (apci, data) = if action["type"] == "knx_group_read" {
            (Apci::Read, wire::Data::Small(0))
        } else {
            let dpt = action["dpt"]
                .as_str()
                .or(self.types.get(&ga).map(String::as_str))
                .context("dpt required: the address has no configured type")?;
            (Apci::Write, wire::encode(dpt, &action["value"])?)
        };
        Ok(Telegram {
            message_code: wire::L_DATA_REQ,
            source: self.address,
            destination: ga,
            apci,
            data,
        })
    }

    /// Send the next queued telegram if nothing awaits an ack.
    async fn send_next(&mut self) -> Result<()> {
        if self.pending.is_some() {
            return Ok(());
        }
        let Some((t, caller)) = self.queue.pop_front() else {
            return Ok(());
        };
        let seq = self.seq_out;
        self.seq_out = self.seq_out.wrapping_add(1);
        let bytes = wire::tunnelling(self.channel, seq, &wire::cemi(&t));
        self.socket.send_to(&bytes, self.gateway).await?;
        self.pending = Some(Pending {
            seq,
            bytes,
            deadline: tokio::time::Instant::now() + ACK_TIMEOUT,
            retried: false,
            caller,
        });
        Ok(())
    }

    /// Handle one frame from the gateway; `false` ends the session.
    async fn incoming(&mut self, frame: &[u8]) -> Result<bool> {
        let Ok((service, body)) = wire::parse_frame(frame) else {
            return Ok(true);
        };
        match service {
            wire::TUNNELLING_ACK if body.len() >= 4 && body[1] == self.channel => {
                if self.pending.as_ref().is_some_and(|p| p.seq == body[2]) {
                    let p = self.pending.take().expect("pending");
                    if body[3] == wire::E_NO_ERROR {
                        reply(
                            p.caller,
                            ClientSendOutcome::Sent {
                                bytes_sent: p.bytes.len(),
                            },
                        );
                    } else {
                        reply(
                            p.caller,
                            ClientSendOutcome::Rejected {
                                error: format!(
                                    "the gateway refused the telegram (status {:#04x})",
                                    body[3]
                                ),
                            },
                        );
                    }
                    self.send_next().await?;
                }
            }
            wire::TUNNELLING_REQUEST if body.len() >= 4 && body[1] == self.channel => {
                let seq = body[2];
                let fresh = seq == self.seq_in;
                if !fresh && seq != self.seq_in.wrapping_sub(1) {
                    return Ok(true);
                }
                self.socket
                    .send_to(
                        &wire::tunnelling_ack(self.channel, seq, wire::E_NO_ERROR),
                        self.gateway,
                    )
                    .await?;
                if !fresh {
                    return Ok(true);
                }
                self.seq_in = self.seq_in.wrapping_add(1);
                if let Ok(Some(t)) = wire::parse_cemi(&body[4..]) {
                    if t.message_code == wire::L_DATA_IND {
                        self.raise(&t)?;
                    }
                }
            }
            wire::CONNECTIONSTATE_RESPONSE if body.len() >= 2 => {
                ensure!(
                    body[1] == wire::E_NO_ERROR,
                    "the gateway lost the tunnel (status {:#04x})",
                    body[1]
                );
            }
            wire::DISCONNECT_REQUEST if body.first() == Some(&self.channel) => {
                self.socket
                    .send_to(
                        &wire::frame(wire::DISCONNECT_RESPONSE, &[self.channel, 0]),
                        self.gateway,
                    )
                    .await?;
                bail!("the gateway closed the tunnel");
            }
            _ => {}
        }
        Ok(true)
    }

    fn raise(&self, t: &Telegram) -> Result<()> {
        let dpt = self.types.get(&t.destination).cloned();
        let value = dpt
            .as_deref()
            .and_then(|d| wire::decode(d, &t.data).ok())
            .unwrap_or(Value::Null);
        let interpretations = if t.apci == Apci::Read {
            json!({})
        } else {
            wire::interpretations(&t.data)
        };
        self.events
            .try_send((
                Event::new(
                    &actions::TELEGRAM_EVENT,
                    json!({"kind": t.apci.name(), "source": wire::format_individual(t.source),
                           "destination": wire::format_group(t.destination), "dpt": dpt,
                           "value": value, "interpretations": interpretations}),
                ),
                0,
            ))
            .context("KNX event queue full; consumer stalled")
    }
}