Print stats about routing table size in UI
[dnsseed-rust] / src / bgp_client.rs
index a890dad9cd72b227c5948461eefab5d2979899c8..ed07bdc46989c4257311aed443319317da37be25 100644 (file)
@@ -20,7 +20,7 @@ use tokio::timer::Delay;
 
 use futures::sync::mpsc;
 
-use crate::printer::Printer;
+use crate::printer::{Printer, Stat};
 
 struct Route {
        path: Vec<u32>,
@@ -176,13 +176,24 @@ pub struct BGPClient {
 impl BGPClient {
        pub fn get_asn(&self, addr: IpAddr) -> u32 {
                let mut path_vecs = self.routes.lock().unwrap().get_route_attrs(addr).clone();
+               if path_vecs.is_empty() { return 0; }
+
                path_vecs.sort_unstable_by(|path_a, path_b| {
                        path_a.pref.cmp(&path_b.pref)
                                .then(path_b.path.len().cmp(&path_a.path.len()))
                                .then(path_b.med.cmp(&path_a.med))
                });
-               // TODO: Find last common ASN among all paths
-               *path_vecs.first().map(|route| route.path.last().unwrap_or(&0)).unwrap_or(&0)
+
+               let primary_route = path_vecs.pop().unwrap();
+               'asn_candidates: for asn in primary_route.path.iter().rev() {
+                       for secondary_route in path_vecs.iter() {
+                               if !secondary_route.path.contains(asn) {
+                                       continue 'asn_candidates;
+                               }
+                       }
+                       return *asn;
+               }
+               *primary_route.path.last().unwrap_or(&0)
        }
 
        pub fn disconnect(&self) {
@@ -257,7 +268,9 @@ impl BGPClient {
                                        }
                                        match bgp_msg {
                                                Message::Open(_) => {
-                                                       printer.add_line("Connected to BGP route provider".to_string(), true);
+                                                       client.routes.lock().unwrap().v4_table.clear();
+                                                       client.routes.lock().unwrap().v6_table.clear();
+                                                       printer.add_line("Connected to BGP route provider".to_string(), false);
                                                },
                                                Message::KeepAlive => {
                                                        let _ = sender.try_send(Message::KeepAlive);
@@ -274,6 +287,8 @@ impl BGPClient {
                                                                        route_table.announce(r, Arc::clone(&path_arc));
                                                                }
                                                        }
+                                                       printer.set_stat(Stat::V4RoutingTableSize(route_table.v4_table.len()));
+                                                       printer.set_stat(Stat::V6RoutingTableSize(route_table.v6_table.len()));
                                                },
                                                _ => {}
                                        }