monocle 1.6.1

A commandline application to search, parse, and process BGP information in public sources.
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
//! Watch lens: live BGP message streams (RIPE RIS Live).
//!
//! Normalizes a live RIS Live websocket feed into a stream of `BgpElem`s, so
//! the same filter grammar, output formats, and MRT recording used by
//! `monocle parse` apply to live data. See `src/bin/commands/watch.rs` for the
//! CLI wiring.
//!
//! # Filter pushdown
//!
//! RIS Live supports server-side subscription filters (`RisSubscribe`). Watch
//! pushes down `host`, `prefix`, `peer`, and `require` to reduce traffic, but
//! pushdown is only an optimization: RIS selects whole UPDATE messages while
//! elements expand per prefix, and some predicates (e.g. origin ASN with
//! AS_SET origins) have no equivalent server-side pattern. Every element-level
//! predicate also runs client-side with bgpkit-parser semantics, so watch
//! matches exactly what `monocle parse` would match.

use std::net::IpAddr;
use std::str::FromStr;
use std::time::Duration;

use anyhow::{anyhow, Result};
use bgpkit_parser::parser::filter::Filterable;
use bgpkit_parser::{parse_ris_live_message_raw, BgpElem, RisSubscribe, RisSubscribeType};
use ipnet::IpNet;
use serde::{Deserialize, Serialize};

/// Public RIS Live websocket endpoint (TLS).
pub const RIS_LIVE_URL: &str = "wss://ris-live.ripe.net/v1/ws/?client=monocle";

/// Reconnect backoff after an abnormal websocket close or IO error.
pub const RECONNECT_BACKOFF: Duration = Duration::from_secs(5);

/// Live-stream filters for the watch command.
///
/// A subset of [`crate::lens::parse::ParseFilters`] dimensions that have a
/// meaningful live interpretation, with the same option names and semantics.
/// Time-window filters are intentionally absent: a live stream has no archive
/// window; use `monocle parse` or `monocle search` for historical ranges.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "cli", derive(clap::Args))]
pub struct WatchFilters {
    /// Filter by origin AS Number(s), comma-separated. Prefix with ! to exclude.
    #[cfg_attr(feature = "cli", clap(short = 'o', long, value_delimiter = ','))]
    #[serde(default)]
    pub origin_asn: Vec<String>,

    /// Filter by network prefix(es), comma-separated. Prefix with ! to exclude.
    #[cfg_attr(feature = "cli", clap(short = 'p', long, value_delimiter = ','))]
    #[serde(default)]
    pub prefix: Vec<String>,

    /// Include super-prefixes when filtering
    #[cfg_attr(feature = "cli", clap(short = 's', long))]
    #[serde(default)]
    pub include_super: bool,

    /// Include sub-prefixes when filtering
    #[cfg_attr(feature = "cli", clap(short = 'S', long))]
    #[serde(default)]
    pub include_sub: bool,

    /// Filter by peer IP address(es)
    #[cfg_attr(feature = "cli", clap(short = 'j', long))]
    #[serde(default)]
    pub peer_ip: Vec<IpAddr>,

    /// Filter by peer ASN(s), comma-separated. Prefix with ! to exclude.
    #[cfg_attr(feature = "cli", clap(short = 'J', long, value_delimiter = ','))]
    #[serde(default)]
    pub peer_asn: Vec<String>,

    /// Filter by BGP community value(s), comma-separated (`A:B` or `A:B:C`).
    /// Each part can be a number or `*` wildcard (e.g., `*:100`, `13335:*`, `57866:104:31`).
    /// Prefix with ! to exclude.
    #[cfg_attr(
        feature = "cli",
        clap(
            short = 'C',
            long = "community",
            visible_alias = "communities",
            value_delimiter = ','
        )
    )]
    #[serde(default)]
    pub communities: Vec<String>,

    /// Filter by elem type: announce (a) or withdraw (w)
    #[cfg_attr(feature = "cli", clap(short = 'm', long, value_enum))]
    #[serde(default)]
    pub elem_type: Option<crate::lens::parse::ParseElemType>,

    /// Filter by AS path regex string
    #[cfg_attr(feature = "cli", clap(short = 'a', long))]
    #[serde(default)]
    pub as_path: Option<String>,
}

/// Which filters were pushed down to the RIS Live subscription, for display.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushdownReport {
    /// RRC host filter (`--host`)
    pub host: Option<String>,
    /// Prefix subscriptions sent (one subscription per prefix)
    pub prefixes: Vec<String>,
    /// Peer IPs sent (one subscription per peer)
    pub peers: Vec<String>,
    /// `require` value sent (`announcements`/`withdrawals`)
    pub require: Option<String>,
}

/// A single RIS Live subscription to send after connecting.
///
/// `RisSubscribe` holds single `prefix`/`peer` values, so multi-value filters
/// need one subscription per combination; each combination is sent as its own
/// `ris_subscribe` on the same socket (RIS Live semantics: subscriptions add
/// up as OR).
#[derive(Debug)]
pub struct SubscriptionPlan {
    /// One subscription per prefix (or the single no-prefix subscription).
    pub subscriptions: Vec<RisSubscribe>,
    pub report: PushdownReport,
}

impl WatchFilters {
    /// Return true when no filter dimension is set, i.e. the subscription
    /// would request the full feed.
    pub fn is_empty(&self) -> bool {
        self.origin_asn.is_empty()
            && self.prefix.is_empty()
            && self.peer_ip.is_empty()
            && self.peer_asn.is_empty()
            && self.communities.is_empty()
            && self.elem_type.is_none()
            && self.as_path.is_none()
    }

    /// Build the server-side RIS Live subscription plan from these filters.
    ///
    /// `host` scopes every subscription to a single RRC. Only dimensions that
    /// reduce traffic without changing match semantics are pushed down:
    /// `host`, `prefix`, `peer`, and `require`:
    ///
    /// - Origin ASNs are NOT pushed down: the RIS `path` pattern `N$` does
    ///   not match AS_SET origins, so pushdown could discard updates that
    ///   client-side semantics accept.
    /// - Negative prefixes are NOT pushed down (no server-side negation);
    ///   they are enforced by the client-side filters.
    /// - Because `RisSubscribe` holds single `prefix`/`peer` values, the plan
    ///   carries one subscription per (prefix, peer) combination.
    /// - Both specificity flags are always set explicitly: RIS defaults
    ///   `moreSpecific` to true when omitted, which would silently widen an
    ///   exact-match `--prefix`.
    pub fn to_subscription_plan(&self, host: Option<&str>) -> Result<SubscriptionPlan> {
        let mut report = PushdownReport::default();
        if let Some(h) = host {
            report.host = Some(h.to_string());
        }

        let positive_prefixes: Vec<&String> = self
            .prefix
            .iter()
            .filter(|p| !p.trim_start().starts_with('!'))
            .collect();
        let mut prefix_nets = Vec::new();
        for value in &positive_prefixes {
            let net = IpNet::from_str(value.trim())
                .map_err(|e| anyhow!("invalid prefix '{}': {e}", value.trim()))?;
            prefix_nets.push(net);
            report.prefixes.push(value.trim().to_string());
        }

        for peer in &self.peer_ip {
            report.peers.push(peer.to_string());
        }

        if let Some(t) = &self.elem_type {
            report.require = Some(match t {
                crate::lens::parse::ParseElemType::A => "announcements".to_string(),
                crate::lens::parse::ParseElemType::W => "withdrawals".to_string(),
            });
        }

        // Cross product of prefix x peer (empty = absent dimension).
        let prefixes: Vec<Option<IpNet>> = if prefix_nets.is_empty() {
            vec![None]
        } else {
            prefix_nets.into_iter().map(Some).collect()
        };
        let peers: Vec<Option<IpAddr>> = if self.peer_ip.is_empty() {
            vec![None]
        } else {
            self.peer_ip.iter().copied().map(Some).collect()
        };

        let mut subscriptions = Vec::new();
        for pfx in &prefixes {
            for peer in &peers {
                // UPDATE messages only: watch emits UPDATE-derived elements,
                // so transporting OPEN/KEEPALIVE/NOTIFICATION frames is waste.
                let mut sub = RisSubscribe::new().data_type(RisSubscribeType::UPDATE);
                if let Some(h) = host {
                    sub = sub.host(h);
                }
                if let Some(net) = pfx {
                    sub = sub
                        .prefix(*net)
                        .more_specific(self.include_sub)
                        .less_specific(self.include_super);
                }
                if let Some(ip) = peer {
                    sub = sub.peer(*ip);
                }
                if let Some(req) = &report.require {
                    sub = sub.require(req);
                }
                subscriptions.push(sub);
            }
        }

        Ok(SubscriptionPlan {
            subscriptions,
            report,
        })
    }

    /// Compile the complete client-side element filters.
    ///
    /// Delegates to [`crate::lens::parse::ParseFilters`] for both validation
    /// and conversion, so input semantics and matching cannot drift from
    /// `monocle parse`. Call before opening a recording file or subscribing;
    /// invalid values fail here, before any connection is made.
    pub fn compile_client_filters(&self) -> Result<Vec<bgpkit_parser::parser::filter::Filter>> {
        self.to_parse_filters()?.to_filters()
    }

    /// Project these filters onto a `ParseFilters` value for validation and
    /// conversion. Time-window fields stay unset: a live stream has no
    /// archive window.
    pub fn to_parse_filters(&self) -> Result<crate::lens::parse::ParseFilters> {
        let filters = crate::lens::parse::ParseFilters {
            origin_asn: self.origin_asn.clone(),
            prefix: self.prefix.clone(),
            include_super: self.include_super,
            include_sub: self.include_sub,
            peer_ip: self.peer_ip.clone(),
            peer_asn: self.peer_asn.clone(),
            communities: self.communities.clone(),
            elem_type: self.elem_type.clone(),
            as_path: self.as_path.clone(),
            ..Default::default()
        };
        filters.validate()?;
        Ok(filters)
    }

    /// Apply client-side filters to a parsed element.
    pub fn matches(
        &self,
        elem: &BgpElem,
        filters: &[bgpkit_parser::parser::filter::Filter],
    ) -> bool {
        elem.match_filters(filters)
    }
}

/// A parsed live websocket frame.
#[derive(Debug)]
pub enum LiveFrame {
    /// A data frame carrying zero or more elements plus the RRC host.
    Data(LiveMessage),
    /// `ris_subscribe_ok`: the server accepted a subscription.
    SubscribeOk,
    /// `ris_error` with its message text.
    Error(String),
    /// Any other control frame (e.g. `ris_rrc_list`, `pong`).
    Other,
}

/// A parsed live data message.
#[derive(Debug)]
pub struct LiveMessage {
    pub host: Option<String>,
    pub elems: Vec<BgpElem>,
}

/// Classify and parse a raw RIS Live websocket text frame.
///
/// Data frames are parsed from the raw BGP message bytes (the subscription
/// requests `includeRaw`), so every attribute the `BgpElem` model carries is
/// populated — including large communities, which the JSON-projected parser
/// drops. Attributes outside the `BgpElem` model (e.g. ORIGINATOR_ID,
/// CLUSTER_LIST) are still not represented downstream; recordings are
/// `BgpElem`-faithful (exactly what filtering and output see), not
/// byte-faithful to the original UPDATE. Element parse failures are returned
/// as `Err` so the caller can report them instead of silently dropping live
/// updates.
pub fn parse_live_frame(msg_str: &str) -> Result<LiveFrame> {
    match extract_string_field(msg_str, "type").as_deref() {
        Some("ris_subscribe_ok") => return Ok(LiveFrame::SubscribeOk),
        Some("ris_error") => {
            return Ok(LiveFrame::Error(
                extract_string_field(msg_str, "message").unwrap_or_else(|| "unknown error".into()),
            ))
        }
        // Data frames parse through the raw-bytes parser even when the raw
        // member is unexpectedly absent: the parser reports the missing
        // payload as an error instead of silently dropping an update.
        Some("ris_message") => {}
        Some(_) => return Ok(LiveFrame::Other),
        None => {}
    }

    let host = extract_host(msg_str);
    match parse_ris_live_message_raw(msg_str) {
        Ok(elems) => Ok(LiveFrame::Data(LiveMessage { host, elems })),
        Err(e) => Err(anyhow!("failed to parse RIS Live message: {e}")),
    }
}

fn extract_string_field(msg_str: &str, field: &str) -> Option<String> {
    // Cheap scans avoid a full serde pass per frame and tolerate outer-shape
    // changes. Frames are small, so two scans are not a hotspot.
    let marker = format!("\"{field}\"");
    let idx = msg_str.find(&marker)?;
    let rest = &msg_str[idx + marker.len()..];
    let colon = rest.find(':')?;
    let after = rest[colon + 1..].trim_start();
    let quote = after.find('"')?;
    let value = &after[quote + 1..];
    let end = value.find('"')?;
    Some(value[..end].to_string())
}

fn extract_host(msg_str: &str) -> Option<String> {
    extract_string_field(msg_str, "host")
}

#[cfg(test)]
mod tests {
    use super::*;
    use bgpkit_parser::RisLiveClientMessage;

    #[test]
    fn test_empty_filters_report_full_feed() {
        let filters = WatchFilters::default();
        assert!(filters.is_empty());
    }

    #[test]
    fn test_origin_not_pushed_down() {
        let filters = WatchFilters {
            origin_asn: vec!["2906".to_string()],
            ..Default::default()
        };
        let plan = filters.to_subscription_plan(None).unwrap();
        assert_eq!(plan.subscriptions.len(), 1);
        assert!(!plan.subscriptions[0].to_json_string().contains("path"));
        // but the client-side filter carries it
        let client = filters.compile_client_filters().unwrap();
        assert!(!client.is_empty());
    }

    #[test]
    fn test_pushdown_prefix_and_host() {
        let filters = WatchFilters {
            prefix: vec!["1.1.1.0/24".to_string()],
            include_sub: true,
            ..Default::default()
        };
        let plan = filters.to_subscription_plan(Some("rrc00")).unwrap();
        assert_eq!(plan.report.host.as_deref(), Some("rrc00"));
        assert_eq!(plan.report.prefixes, vec!["1.1.1.0/24".to_string()]);
        let json = plan.subscriptions[0].to_json_string();
        assert!(json.contains("rrc00"));
        assert!(json.contains("1.1.1.0/24"));
        assert!(json.contains("\"moreSpecific\":true"));
    }

    #[test]
    fn test_prefix_specificity_set_explicitly() {
        // RIS defaults moreSpecific=true when omitted; exact-match --prefix
        // must send moreSpecific=false explicitly.
        let filters = WatchFilters {
            prefix: vec!["1.1.1.0/24".to_string()],
            ..Default::default()
        };
        let plan = filters.to_subscription_plan(None).unwrap();
        let json = plan.subscriptions[0].to_json_string();
        assert!(json.contains("\"moreSpecific\":false"));
        assert!(json.contains("\"lessSpecific\":false"));
    }

    #[test]
    fn test_multi_prefix_multi_peer_expands_subscriptions() {
        let filters = WatchFilters {
            prefix: vec!["1.1.1.0/24".to_string(), "8.8.8.0/24".to_string()],
            peer_ip: vec!["192.0.2.1".parse().unwrap(), "192.0.2.2".parse().unwrap()],
            ..Default::default()
        };
        let plan = filters.to_subscription_plan(None).unwrap();
        assert_eq!(plan.subscriptions.len(), 4);
        assert_eq!(plan.report.prefixes.len(), 2);
        assert_eq!(plan.report.peers.len(), 2);
        let jsons: Vec<String> = plan
            .subscriptions
            .iter()
            .map(|s| s.to_json_string())
            .collect();
        assert!(jsons
            .iter()
            .any(|j| j.contains("1.1.1.0/24") && j.contains("192.0.2.1")));
        assert!(jsons
            .iter()
            .any(|j| j.contains("8.8.8.0/24") && j.contains("192.0.2.2")));
    }

    #[test]
    fn test_negative_prefix_client_side_only() {
        let filters = WatchFilters {
            prefix: vec!["!1.1.1.0/24".to_string()],
            ..Default::default()
        };
        let plan = filters.to_subscription_plan(None).unwrap();
        assert!(plan.report.prefixes.is_empty());
        // enforced client-side instead
        assert!(!filters.compile_client_filters().unwrap().is_empty());
    }

    #[test]
    fn test_elem_type_pushdown_and_client_filter() {
        let filters = WatchFilters {
            elem_type: Some(crate::lens::parse::ParseElemType::A),
            ..Default::default()
        };
        let plan = filters.to_subscription_plan(None).unwrap();
        assert_eq!(plan.report.require.as_deref(), Some("announcements"));
        // elem type also re-checked client-side (mixed UPDATEs)
        assert!(!filters.compile_client_filters().unwrap().is_empty());
    }

    #[test]
    fn test_invalid_origin_rejected_at_compile() {
        let filters = WatchFilters {
            origin_asn: vec!["bogus".to_string()],
            ..Default::default()
        };
        assert!(filters.compile_client_filters().is_err());
    }

    #[test]
    fn test_invalid_prefix_rejected_at_compile() {
        let filters = WatchFilters {
            prefix: vec!["not-a-prefix".to_string()],
            ..Default::default()
        };
        assert!(filters.compile_client_filters().is_err());
    }

    #[test]
    fn test_mixed_positive_negative_origin_rejected() {
        let filters = WatchFilters {
            origin_asn: vec!["13335".to_string(), "!15169".to_string()],
            ..Default::default()
        };
        assert!(filters.compile_client_filters().is_err());
    }

    #[test]
    fn test_community_spec_or_semantics() {
        // multiple communities must yield ONE filter with OR semantics,
        // matching parse behavior, not one filter per value (AND).
        let filters = WatchFilters {
            communities: vec!["13335:100".to_string(), "15169:*".to_string()],
            ..Default::default()
        };
        let compiled = filters.compile_client_filters().unwrap();
        // one filter per dimension, OR within the community value
        assert_eq!(compiled.len(), 1);
    }

    #[test]
    fn test_mixed_community_negation_rejected() {
        let filters = WatchFilters {
            communities: vec!["13335:100".to_string(), "!15169:1".to_string()],
            ..Default::default()
        };
        assert!(filters.compile_client_filters().is_err());
    }

    #[test]
    fn test_extract_host() {
        let frame = r#"{"type":"ris_message","data":{"timestamp":1700000000,"peer":"1.1.1.1","host":"rrc10","path":[1,2,3],"announcements":[]}}"#;
        assert_eq!(extract_host(frame).as_deref(), Some("rrc10"));
        assert!(extract_host("garbage").is_none());
    }

    #[test]
    fn test_data_frame_parses_elements_and_host() {
        // Real RIS Live frame (rrc10 UPDATE, includeRaw) from
        // bgpkit-parser's ris_live_raw_full example.
        let frame = r#"{"type":"ris_message","data":{"timestamp":1636245154.8,"peer":"2001:7f8:b:100:1d1:a520:1333:74","peer_asn":"201333","id":"10-183678-175313836","host":"rrc10","type":"UPDATE","raw":"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0086020000006F4001010040021202040003127500001A6A000000AE00004FF980040400000000C008081A6A001E1A6A3840800E4100020120200107F8000B010001D1A52013330074FE800000000000000217A3FFFEFE290500302A0E97C70000302A0E97C600FE302A10CC4217B7302A10CC421FEB"}}"#;
        match parse_live_frame(frame).unwrap() {
            LiveFrame::Data(msg) => {
                assert_eq!(msg.host.as_deref(), Some("rrc10"));
                assert!(!msg.elems.is_empty());
                let elem = &msg.elems[0];
                assert_eq!(elem.peer_asn.to_u32(), 201333);
                assert!(elem.prefix.prefix.addr().is_ipv6());
            }
            other => panic!("expected data frame, got {other:?}"),
        }
    }

    #[test]
    fn test_control_frames_classified() {
        let ok = parse_live_frame(r#"{"type":"ris_subscribe_ok","data":{}}"#).unwrap();
        assert!(matches!(ok, LiveFrame::SubscribeOk));

        let err = parse_live_frame(r#"{"type":"ris_error","data":{"message":"bad subscription"}}"#)
            .unwrap();
        match err {
            LiveFrame::Error(msg) => assert_eq!(msg, "bad subscription"),
            other => panic!("expected error frame, got {other:?}"),
        }

        let other = parse_live_frame(r#"{"type":"pong","data":null}"#).unwrap();
        assert!(matches!(other, LiveFrame::Other));
    }
}