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
/*!
Provides parsing functions for [RIS-Live](https://ris-live.ripe.net/manual/) real-time
BGP message stream JSON data.

The main parsing function, [parse_ris_live_message] converts a JSON-formatted message string into a
vector of [BgpElem]s.

Here is an example parsing stream data from one collector:
```no_run
use serde_json::json;
use tungstenite::{connect, Message};
use ris_live_rs::error::ParserRisliveError;
use ris_live_rs::parse_ris_live_message;

const RIS_LIVE_URL: &str = "ws://ris-live.ripe.net/v1/ws/?client=rust-bgpkit-parser";

/// This is an example of subscribing to RIS-Live's streaming data.
///
/// For more RIS-Live details, check out their documentation at https://ris-live.ripe.net/manual/
fn main() {
    // connect to RIPE RIS Live websocket server
    let (mut socket, _response) =
        connect(RIS_LIVE_URL)
            .expect("Can't connect to RIS Live websocket server");

    // subscribe to messages from one collector
    let msg = json!({"type": "ris_subscribe", "data": null}).to_string();
    socket.write_message(Message::Text(msg)).unwrap();

    loop {
        let msg = socket.read_message().expect("Error reading message").to_string();
        if msg.is_empty() {
            continue
        }
        match parse_ris_live_message(msg.as_str()) {
            Ok(elems) => {
                for e in elems {
                    println!("{}", e);
                }
            }
            Err(error) => {
                if let ParserRisliveError::ElemEndOfRibPrefix = error {
                    println!("{:?}", &error);
                    println!("{}", msg);
                    continue
                }
                break;
            }
        }
    }
}
```
 */

use std::net::IpAddr;
use bgp_models::prelude::*;
use crate::error::ParserRisliveError;
use crate::messages::{RisLiveMessage, RisMessageEnum};
use crate::messages::ris_message::path_to_as_path;

pub mod error;
pub mod messages;

// simple macro to make the code look a bit nicer
macro_rules! unwrap_or_return {
    ( $e:expr, $msg_string:expr ) => {
        match $e {
            Ok(x) => x,
            Err(_) => return Err(ParserRisliveError::IncorrectJson($msg_string)),
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn compose_subscription_message(
    host: &str,
    msg_type: &Option<String>,
    require: &Option<String>,
    peer: &Option<String>,
    prefix: &Option<String>,
    path: &Option<String>,
    more_specific: &bool,
    less_specific: &bool,
) -> String {
    let mut options: Vec<String> = vec![];

    if host.to_lowercase().as_str() != "all" {
        options.push(format!("\"host\": \"{}\"", host))
    }

    if let Some(msg_type) = msg_type {
        options.push(format!("\"type\": \"{}\"", msg_type))
    }

    if let Some(require) = require {
        options.push(format!("\"require\": \"{}\"", require))
    }

    if let Some(peer) = peer {
        options.push(format!("\"peer\": \"{}\"", peer))
    }

    if let Some(prefix) = prefix {
        options.push(format!("\"prefix\": \"{}\"", prefix))
    }

    if let Some(path) = path {
        options.push(format!("\"path\": \"{}\"", path))
    }

    match more_specific {
        true => {
            options.push("\"moreSpecific\": true".to_string())
        }
        false => {
            options.push("\"moreSpecific\": false".to_string())
        }
    }

    match less_specific {
        true => {
            options.push("\"lessSpecific\": true".to_string())
        }
        false => {
            options.push("\"lessSpecific\": false".to_string())
        }
    }

    format!("{{\"type\": \"ris_subscribe\", \"data\":{{ {} }} }}", options.join(","))
}

/// This function parses one message and returns a result of a vector of [BgpElem]s or an error
pub fn parse_ris_live_message(msg_str: &str) -> Result<Vec<BgpElem>, ParserRisliveError> {

    let msg_string = msg_str.to_string();

    // parse RIS Live message to internal struct using serde.
    let msg: RisLiveMessage = match serde_json::from_str(msg_str) {
        Ok(m) => m,
        Err(_e) => return Err(ParserRisliveError::IncorrectJson(msg_string)),
    };

    match msg {
        RisLiveMessage::RisMessage(ris_msg) => {
            // we currently only handles the `ris_message` data type. other
            // types provides meta information, but reveals no BGP elements, and
            // thus for now will be ignored.

            if ris_msg.msg.is_none() {
                return Ok(vec![])
            }

            match ris_msg.msg.unwrap() {
                RisMessageEnum::UPDATE {
                    path,
                    community,
                    origin,
                    med,
                    aggregator,
                    announcements,
                } => {
                    let mut elems: Vec<BgpElem> = vec![];

                    let peer_ip = unwrap_or_return!(ris_msg.peer.parse::<IpAddr>(), msg_string);
                    let peer_asn = Asn::from(unwrap_or_return!(ris_msg.peer_asn.parse::<u32>(), msg_string));

                    // parse path
                    let as_path = path.map(path_to_as_path);

                    // parse community
                    let communities: Option<Vec<MetaCommunity>> = match community {
                        None => {None}
                        Some(cs) => {
                            let mut comms: Vec<MetaCommunity> = vec![];
                            for c in cs {
                                comms.push(MetaCommunity::Community(Community::Custom(Asn::from(c.0),c.1)));
                            }
                            Some(comms)
                        }
                    };

                    // parse origin
                    let bgp_origin = match origin {
                        None => {None}
                        Some(o) => {
                            Some(match o.as_str(){
                                "igp" | "IGP" => Origin::IGP,
                                "egp" | "EGP" => Origin::EGP,
                                "incomplete" | "INCOMPLETE" => Origin::INCOMPLETE,
                                other => {
                                    return Err(ParserRisliveError::ElemUnknownOriginType(other.to_string()))
                                }
                            })
                        }
                    };

                    // parse aggregator
                    let bgp_aggregator = match aggregator{
                        None => {(None, None)}
                        Some(aggr_str) => {
                            let parts = aggr_str.split(':').collect::<Vec<&str>>();
                            if parts.len()!=2 {
                                return Err(ParserRisliveError::ElemIncorrectAggregator(aggr_str))
                            }
                            let asn = Asn::from(unwrap_or_return!(parts[0].to_owned().parse::<u32>(), msg_string));
                            let ip = unwrap_or_return!(parts[1].to_owned().parse::<IpAddr>(), msg_string);
                            (Some(asn), Some(ip))
                        }
                    };

                    // parser announcements
                    if let Some(announcements) = announcements {
                        for announcement in announcements {
                            let nexthop = match announcement.next_hop.parse::<IpAddr>(){
                                Ok(a) => {a}
                                Err(_) => {
                                    return Err(ParserRisliveError::IncorrectJson(msg_string))
                                }
                            };
                            for prefix in &announcement.prefixes {
                                let p = match prefix.parse::<NetworkPrefix>(){
                                    Ok(net) => { net }
                                    Err(_) => {
                                        if prefix == "eor" {
                                            return Err(ParserRisliveError::ElemEndOfRibPrefix)
                                        }
                                        return Err(ParserRisliveError::ElemIncorrectPrefix(prefix.to_string()))
                                    }
                                };

                                elems.push(
                                    BgpElem{
                                        timestamp: ris_msg.timestamp,
                                        elem_type: ElemType::ANNOUNCE,
                                        peer_ip,
                                        peer_asn,
                                        prefix: p,
                                        next_hop: Some(nexthop),
                                        as_path: as_path.clone(),
                                        origin_asns: None,
                                        origin: bgp_origin,
                                        local_pref: None,
                                        med,
                                        communities: communities.clone(),
                                        atomic: None,
                                        aggr_asn: bgp_aggregator.0,
                                        aggr_ip: bgp_aggregator.1,
                                    }
                                );
                            }

                            if let Some(prefixes) = &announcement.withdrawals {
                                for prefix in prefixes {
                                    let p = match prefix.parse::<NetworkPrefix>(){
                                        Ok(net) => { net }
                                        Err(_) => {
                                            if prefix == "eor" {
                                                return Err(ParserRisliveError::ElemEndOfRibPrefix)
                                            }
                                            return Err(ParserRisliveError::ElemIncorrectPrefix(prefix.to_string()))
                                        }
                                    };
                                    elems.push(
                                        BgpElem{
                                            timestamp: ris_msg.timestamp,
                                            elem_type: ElemType::WITHDRAW,
                                            peer_ip,
                                            peer_asn,
                                            prefix: p,
                                            next_hop: None,
                                            as_path: None,
                                            origin_asns: None,
                                            origin: None,
                                            local_pref: None,
                                            med: None,
                                            communities: None,
                                            atomic: None,
                                            aggr_asn: None,
                                            aggr_ip: None,
                                        }
                                    );

                                }
                            }
                        }
                    }

                    Ok(elems)
                }
                _ => Ok(vec![]),
            }
        },
        _ => Ok(vec![]),
    }
}

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

    #[test]
    fn test_ris_live_msg() {
        let msg_str = r#"
        {"type": "ris_message","data":{"timestamp":1636247118.76,"peer":"2001:7f8:24::82","peer_asn":"58299","id":"20-5761-238131559","host":"rrc20","type":"UPDATE","path":[58299,49981,397666],"origin":"igp","announcements":[{"next_hop":"2001:7f8:24::82","prefixes":["2602:fd9e:f00::/40"]},{"next_hop":"fe80::768e:f8ff:fea6:b2c4","prefixes":["2602:fd9e:f00::/40"], "withdrawals": ["1.1.1.0/24", "8.8.8.0/24"]}],"raw":"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF005A02000000434001010040020E02030000E3BB0000C33D00061162800E2B00020120200107F8002400000000000000000082FE80000000000000768EF8FFFEA6B2C400282602FD9E0F"}}
        "#;
        let msg = parse_ris_live_message(&msg_str).unwrap();
        for elem in msg {
            println!("{}", elem);
        }
    }

    #[test]
    fn test_error_message() {
        let msg_str = r#"
        {"type": "ris_message","data":{"timestamp":1636342486.17,"peer":"37.49.237.175","peer_asn":"199524","id":"21-587-22045871","host":"rrc21","type":"UPDATE","path":[199524,1299,3356,13904,13904,13904,13904,13904,13904],"origin":"igp","aggregator":"65000:8.42.232.1","announcements":[{"next_hop":"37.49.237.175","prefixes":["64.68.236.0/22"]}]}}
        "#;
        let msg = parse_ris_live_message(&msg_str).unwrap();
        for elem in msg {
            println!("{}", elem);
        }
    }

    #[test]
    fn test_error_message_2() {
        let msg_str = r#"
        {"type": "ris_message","data":{"timestamp":1636339375.83,"peer":"37.49.236.1","peer_asn":"8218","id":"21-594-37970252","host":"rrc21"}}
        "#;
        let msg = parse_ris_live_message(&msg_str).unwrap();
        for elem in msg {
            println!("{}", elem);
        }
    }
}