+use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::io::BufReader;
-use std::net::SocketAddr;
+use std::net::{IpAddr, SocketAddr};
use std::time::Instant;
use tokio::prelude::*;
use crate::printer::Printer;
use crate::datastore::{Store, AddressState, U64Setting, RegexSetting};
+use crate::bgp_client::BGPClient;
use crate::{START_SHUTDOWN, scan_node};
use regex::Regex;
-pub fn read(store: &'static Store, printer: &'static Printer) {
+pub fn read(store: &'static Store, printer: &'static Printer, bgp_client: Arc<BGPClient>) {
tokio::spawn(lines(BufReader::new(stdin())).for_each(move |line| {
macro_rules! err {
() => { {
"t" => store.set_u64(U64Setting::RunTimeout, try_parse_next_chunk!(u64)),
"v" => store.set_u64(U64Setting::MinProtocolVersion, try_parse_next_chunk!(u64)),
"w" => store.set_u64(U64Setting::WasGoodTimeout, try_parse_next_chunk!(u64)),
- "s" => store.set_regex(RegexSetting::SubverRegex, try_parse_next_chunk!(Regex)),
- "a" => scan_node(Instant::now(), try_parse_next_chunk!(SocketAddr)),
+ "s" => {
+ if line.len() < 3 || !line.starts_with("s ") {
+ err!();
+ }
+ store.set_regex(RegexSetting::SubverRegex, match line[2..].parse::<Regex>() {
+ Ok(res) => res,
+ Err(_) => err!(),
+ });
+ },
+ "a" => scan_node(Instant::now(), try_parse_next_chunk!(SocketAddr), true),
+ "b" => {
+ let ip = try_parse_next_chunk!(IpAddr);
+ printer.add_line(format!("ASN for {} is {}", ip, bgp_client.get_asn(ip)), false);
+ },
"r" => {
- match try_parse_next_chunk!(u8) {
- 0 => store.set_u64(U64Setting::RescanInterval(AddressState::Untested), try_parse_next_chunk!(u64)),
- 1 => store.set_u64(U64Setting::RescanInterval(AddressState::LowBlockCount), try_parse_next_chunk!(u64)),
- 2 => store.set_u64(U64Setting::RescanInterval(AddressState::HighBlockCount), try_parse_next_chunk!(u64)),
- 3 => store.set_u64(U64Setting::RescanInterval(AddressState::LowVersion), try_parse_next_chunk!(u64)),
- 4 => store.set_u64(U64Setting::RescanInterval(AddressState::BadVersion), try_parse_next_chunk!(u64)),
- 5 => store.set_u64(U64Setting::RescanInterval(AddressState::NotFullNode), try_parse_next_chunk!(u64)),
- 6 => store.set_u64(U64Setting::RescanInterval(AddressState::ProtocolViolation), try_parse_next_chunk!(u64)),
- 7 => store.set_u64(U64Setting::RescanInterval(AddressState::Timeout), try_parse_next_chunk!(u64)),
- 8 => store.set_u64(U64Setting::RescanInterval(AddressState::TimeoutDuringRequest), try_parse_next_chunk!(u64)),
- 9 => store.set_u64(U64Setting::RescanInterval(AddressState::Good), try_parse_next_chunk!(u64)),
- 10 => store.set_u64(U64Setting::RescanInterval(AddressState::WasGood), try_parse_next_chunk!(u64)),
- _ => err!(),
+ match AddressState::from_num(try_parse_next_chunk!(u8)) {
+ Some(state) => store.set_u64(U64Setting::RescanInterval(state), try_parse_next_chunk!(u64)),
+ None => err!(),
}
},
"q" => {
_ => err!(),
}
future::ok(())
- }).then(|_| {
- println!("Exiting");
+ }).then(move |_| {
+ printer.add_line("Shutting down...".to_string(), true);
future::ok(())
}));
}