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
//! BGP client protocol actions implementation

use crate::llm::actions::{
    client_trait::{Client, ClientActionResult},
    protocol_trait::Protocol,
    ActionDefinition, Parameter, ParameterDefinition,
};
use crate::protocol::{ConnectContext, EventType};
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;

/// BGP client connected event
///
/// Fires when the session reaches Established, i.e. after the peer's OPEN *and* its KEEPALIVE
/// have both been seen. It used to fire on the OPEN alone, which announced a session the peer
/// had not yet confirmed.
pub static BGP_CLIENT_CONNECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "bgp_connected",
        "BGP session with the peer reached Established (OPEN and KEEPALIVE both exchanged). \
         This client announces no routes; reply with wait_for_more to keep monitoring, or \
         send_notification to tear the session down.",
        json!({"type": "wait_for_more"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "remote_addr".to_string(),
            type_hint: "string".to_string(),
            description: "Remote BGP peer address".to_string(),
            required: true,
        },
        Parameter {
            name: "peer_as".to_string(),
            type_hint: "number".to_string(),
            description: "Peer AS number. Taken from the peer's four-octet-AS capability when \
                          it advertised one, so values above 65535 are reported truthfully."
                .to_string(),
            required: true,
        },
        Parameter {
            name: "peer_router_id".to_string(),
            type_hint: "string".to_string(),
            description: "Peer BGP router ID".to_string(),
            required: true,
        },
        Parameter {
            name: "hold_time".to_string(),
            type_hint: "number".to_string(),
            description: "Negotiated hold time in seconds (the smaller of the two proposals; \
                          0 means both timers are disabled)"
                .to_string(),
            required: true,
        },
        Parameter {
            name: "peer_supports_four_octet_as".to_string(),
            type_hint: "boolean".to_string(),
            description: "Whether the peer advertised RFC 6793 four-octet AS support".to_string(),
            required: true,
        },
    ])
    .with_actions(bgp_client_response_actions())
});

/// BGP UPDATE message received event
///
/// The body is decoded field by field. It used to be delivered as `update_data_hex`, a raw hex
/// blob no model can act on and which the root CLAUDE.md forbids in event data.
pub static BGP_CLIENT_UPDATE_RECEIVED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "bgp_update_received",
        "BGP UPDATE received from the peer, decoded into withdrawn_routes, nlri, origin, \
         next_hop, as_path and the full path_attributes list. This client announces no routes, \
         so there is no way to answer an UPDATE with one — but the session verbs do reach the \
         wire: reply with wait_for_more to keep monitoring, or send_notification / disconnect \
         to tear the peering down over what the peer just advertised.",
        json!({"type": "wait_for_more"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "nlri".to_string(),
            type_hint: "array".to_string(),
            description: "Announced prefixes in CIDR form, e.g. [\"10.0.0.0/24\"]".to_string(),
            required: true,
        },
        Parameter {
            name: "withdrawn_routes".to_string(),
            type_hint: "array".to_string(),
            description: "Withdrawn prefixes in CIDR form".to_string(),
            required: true,
        },
        Parameter {
            name: "next_hop".to_string(),
            type_hint: "string".to_string(),
            description: "NEXT_HOP attribute, or null when the UPDATE only withdraws".to_string(),
            required: false,
        },
        Parameter {
            name: "as_path".to_string(),
            type_hint: "array".to_string(),
            description: "AS_PATH as a list of AS numbers".to_string(),
            required: false,
        },
        Parameter {
            name: "origin".to_string(),
            type_hint: "string".to_string(),
            description: "ORIGIN attribute: IGP, EGP or Incomplete".to_string(),
            required: false,
        },
        Parameter {
            name: "path_attributes".to_string(),
            type_hint: "array".to_string(),
            description: "Every path attribute, each with its type_name and flags".to_string(),
            required: true,
        },
        Parameter {
            name: "end_of_rib".to_string(),
            type_hint: "boolean".to_string(),
            description: "True when this is an End-of-RIB marker (RFC 4724)".to_string(),
            required: true,
        },
        Parameter {
            name: "peer_as".to_string(),
            type_hint: "number".to_string(),
            description: "AS number of the peer that sent this UPDATE".to_string(),
            required: false,
        },
    ])
    .with_actions(bgp_client_response_actions())
});

/// BGP NOTIFICATION message received event
pub static BGP_CLIENT_NOTIFICATION_RECEIVED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "bgp_notification_received",
        "BGP NOTIFICATION received from the peer. RFC 4271 forbids answering one, so the \
         session closes regardless of what is returned and nothing is written to the socket.",
        json!({"type": "wait_for_more"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "error_code".to_string(),
            type_hint: "number".to_string(),
            description: "BGP error code (RFC 4271 section 6)".to_string(),
            required: true,
        },
        Parameter {
            name: "error_name".to_string(),
            type_hint: "string".to_string(),
            description: "Human-readable error name, e.g. \"OPEN Message Error\"".to_string(),
            required: true,
        },
        Parameter {
            name: "error_subcode".to_string(),
            type_hint: "number".to_string(),
            description: "BGP error subcode".to_string(),
            required: true,
        },
        Parameter {
            name: "error_subcode_name".to_string(),
            type_hint: "string".to_string(),
            description: "Human-readable subcode name, e.g. \"Bad Peer AS\"".to_string(),
            required: true,
        },
        Parameter {
            name: "peer_as".to_string(),
            type_hint: "number".to_string(),
            description: "AS number of the peer, if its OPEN was seen".to_string(),
            required: false,
        },
    ])
    .with_no_actions()
});

/// Everything a BGP client handler can answer a session event with.
///
/// One list, used for both the async and the sync set. A client cannot narrow its vocabulary
/// per event even if it wanted to: `client_llm_action_set`
/// (`src/llm/actions/client_trait.rs`) offers the model `get_async_actions` ∪ `get_sync_actions`
/// ∪ the firing event's own actions, because `call_llm_for_client` serves both the initial
/// instruction and every network event through one entry point. Declaring the same list twice
/// is therefore redundant rather than load-bearing — but it is also what keeps the declaration
/// honest about what the model will actually be offered.
///
/// The corollary matters for [`BGP_CLIENT_NOTIFICATION_RECEIVED_EVENT`]: its `with_no_actions()`
/// does *not* stop these verbs being offered, so the read loop still has to log anything the
/// model returns for it as discarded rather than assume it cannot happen.
fn bgp_client_response_actions() -> Vec<ActionDefinition> {
    vec![
        ActionDefinition {
            name: "send_keepalive".to_string(),
            description: "Send a BGP KEEPALIVE to the peer. Routine keepalives are already \
                          sent automatically at a third of the negotiated hold time; use this \
                          only to send an extra one."
                .to_string(),
            parameters: vec![],
            example: json!({
                "type": "send_keepalive"
            }),
            log_template: None,
        },
        ActionDefinition {
            name: "send_notification".to_string(),
            description: "Send a BGP NOTIFICATION. RFC 4271 requires the peer to close the \
                          session on receipt, so this ends the peering."
                .to_string(),
            parameters: vec![
                Parameter {
                    name: "error_code".to_string(),
                    type_hint: "number".to_string(),
                    description: "BGP error code, 1-6 (1 Message Header, 2 OPEN, 3 UPDATE, \
                                  4 Hold Timer Expired, 5 FSM, 6 Cease)"
                        .to_string(),
                    required: true,
                },
                Parameter {
                    name: "error_subcode".to_string(),
                    type_hint: "number".to_string(),
                    description: "BGP error subcode (default 0 = unspecified)".to_string(),
                    required: false,
                },
            ],
            example: json!({
                "type": "send_notification",
                "error_code": 6,
                "error_subcode": 2
            }),
            log_template: None,
        },
        ActionDefinition {
            name: "disconnect".to_string(),
            description: "Close the session gracefully: send NOTIFICATION 6/2 (Cease / \
                          Administrative Shutdown), then close the TCP connection."
                .to_string(),
            parameters: vec![],
            example: json!({
                "type": "disconnect"
            }),
            log_template: None,
        },
        ActionDefinition {
            name: "wait_for_more".to_string(),
            description: "Do nothing and keep monitoring the session".to_string(),
            parameters: vec![],
            example: json!({
                "type": "wait_for_more"
            }),
            log_template: None,
        },
    ]
}

/// BGP client protocol action handler
pub struct BgpClientProtocol;

impl BgpClientProtocol {
    pub fn new() -> Self {
        Self
    }
}

impl Default for BgpClientProtocol {
    fn default() -> Self {
        Self::new()
    }
}

// Implement Protocol trait (common functionality)
impl Protocol for BgpClientProtocol {
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        bgp_client_response_actions()
    }
    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        bgp_client_response_actions()
    }
    fn get_event_types(&self) -> Vec<EventType> {
        vec![
            BGP_CLIENT_CONNECTED_EVENT.clone(),
            BGP_CLIENT_UPDATE_RECEIVED_EVENT.clone(),
            BGP_CLIENT_NOTIFICATION_RECEIVED_EVENT.clone(),
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "BGP"
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>BGP"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["bgp", "border gateway"]
    }
    fn description(&self) -> &'static str {
        "BGP routing client (query mode)"
    }
    fn example_prompt(&self) -> &'static str {
        "Connect to BGP peer at 192.168.1.1:179 and query routing table"
    }
    fn get_startup_parameters(&self) -> Vec<ParameterDefinition> {
        vec![
            ParameterDefinition {
                name: "local_as".to_string(),
                type_hint: "integer".to_string(),
                description: "Local BGP AS number (can be fake for monitoring)".to_string(),
                required: false,
                example: json!(65000),
                default: None,
            },
            ParameterDefinition {
                name: "router_id".to_string(),
                type_hint: "string".to_string(),
                description: "BGP router ID in IPv4 format".to_string(),
                required: false,
                example: json!("192.168.1.100"),
                default: None,
            },
            ParameterDefinition {
                name: "hold_time".to_string(),
                type_hint: "integer".to_string(),
                description: "BGP hold time in seconds (default 180)".to_string(),
                required: false,
                example: json!(180),
                default: None,
            },
        ]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{
            DevelopmentState, PrivilegeRequirement, ProtocolMetadataV2,
        };

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Experimental)
            .privilege_requirement(PrivilegeRequirement::None)
            .implementation(
                "BGP-4 query client (RFC 4271, RFC 6793) over netgauze-bgp-pkt, \
                             sharing src/server/bgp/wire.rs with the BGP server",
            )
            .llm_control("Session establishment, route monitoring")
            .e2e_testing("NetGet BGP server")
            .notes(
                "Query mode only, no active route announcement, no RIB. Four-octet ASNs \
                    are advertised and parsed. Keepalives go at a third of the negotiated hold \
                    time and the hold timer is enforced: a peer that sends nothing for the \
                    negotiated hold time earns NOTIFICATION 4/0 and the session closes, proven \
                    end to end in tests/client/bgp/hold_timer_test.rs. Never peered against a \
                    real BGP daemon.",
            )
            .build()
    }
    fn group_name(&self) -> &'static str {
        "VPN & Routing"
    }
    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        use serde_json::json;

        StartupExamples::new(
            // LLM mode: LLM controls BGP session and route monitoring
            json!({
                "type": "open_client",
                "remote_addr": "192.168.1.1:179",
                "base_stack": "bgp",
                "instruction": "Establish BGP session and monitor routing updates",
                "startup_params": {
                    "local_as": 65000,
                    "router_id": "192.168.1.100"
                }
            }),
            // Script mode: Code-based BGP update handling
            json!({
                "type": "open_client",
                "remote_addr": "192.168.1.1:179",
                "base_stack": "bgp",
                "startup_params": {
                    "local_as": 65000,
                    "router_id": "192.168.1.100"
                },
                "event_handlers": [{
                    "event_pattern": "bgp_update_received",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "<bgp_client_handler>"
                    }
                }]
            }),
            // Static mode: Fixed BGP keepalive response
            json!({
                "type": "open_client",
                "remote_addr": "192.168.1.1:179",
                "base_stack": "bgp",
                "startup_params": {
                    "local_as": 65000,
                    "router_id": "192.168.1.100",
                    "hold_time": 180
                },
                "event_handlers": [
                    {
                        "event_pattern": "bgp_connected",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "send_keepalive"
                            }]
                        }
                    },
                    {
                        "event_pattern": "bgp_update_received",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "send_keepalive"
                            }]
                        }
                    }
                ]
            }),
        )
    }
}

// Implement Client trait (client-specific functionality)
impl Client for BgpClientProtocol {
    fn connect(
        &self,
        ctx: ConnectContext,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<std::net::SocketAddr>> + Send>>
    {
        Box::pin(async move {
            use crate::client::bgp::BgpClient;
            BgpClient::connect_with_llm_actions(
                ctx.remote_addr,
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.client_id,
                ctx.startup_params,
            )
            .await
        })
    }
    /// Turn an action into bytes.
    ///
    /// Every message is built by [`crate::server::bgp::wire`], the same codec the BGP server
    /// uses, rather than by assembling a marker and a length by hand here.
    fn execute_action(&self, action: serde_json::Value) -> Result<ClientActionResult> {
        use crate::server::bgp::wire;

        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .context("Missing 'type' field in action")?;

        match action_type {
            "send_keepalive" => Ok(ClientActionResult::SendData(wire::encode_keepalive())),
            "send_notification" => {
                // No default. A NOTIFICATION ends the session, and inventing a Cease for an
                // action that forgot to say why would make an unusable answer look deliberate.
                let error_code_raw = action
                    .get("error_code")
                    .and_then(|v| v.as_u64())
                    .context("send_notification requires error_code (1-6, see RFC 4271 §6)")?;
                let error_code = u8::try_from(error_code_raw)
                    .ok()
                    .filter(|c| (1..=6).contains(c))
                    .with_context(|| {
                        format!("send_notification error_code must be 1-6, got {error_code_raw}")
                    })?;

                let error_subcode_raw = action
                    .get("error_subcode")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(0);
                let error_subcode = u8::try_from(error_subcode_raw).with_context(|| {
                    format!(
                        "send_notification error_subcode must be 0-255, got {error_subcode_raw}"
                    )
                })?;

                Ok(ClientActionResult::SendData(wire::encode_notification(
                    error_code,
                    error_subcode,
                    &[],
                )?))
            }
            // RFC 4271 §6.7: a peer that is going away says so. Closing the socket without a
            // NOTIFICATION leaves the peer to discover it from the TCP FIN. The description
            // has always promised the Cease; only now is it actually sent.
            "disconnect" => Ok(ClientActionResult::Multiple(vec![
                ClientActionResult::SendData(wire::encode_notification(
                    wire::ERR_CEASE,
                    2, // Administrative Shutdown
                    &[],
                )?),
                ClientActionResult::Disconnect,
            ])),
            "wait_for_more" => Ok(ClientActionResult::WaitForMore),
            _ => Err(anyhow::anyhow!(
                "Unknown BGP client action: {}",
                action_type
            )),
        }
    }
}