X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=dnsseed-rust;a=blobdiff_plain;f=src%2Fmain.rs;h=288cdd24d229168e09ee270e9fdb988b430cf23a;hp=96a74fd76c0501c025674bba0e8e04eeb0f8b4f2;hb=852e939be31574ea13b9f0da3056393d23d55753;hpb=ca19f8036d82bcc7a4100219bad3f16b9a91d69a diff --git a/src/main.rs b/src/main.rs index 96a74fd..288cdd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use bitcoin::hash_types::{BlockHash}; use bitcoin::network::constants::{Network, ServiceFlags}; use bitcoin::network::message::NetworkMessage; use bitcoin::network::message_blockdata::{GetHeadersMessage, Inventory}; -use bitcoin::util::hash::BitcoinHash; +//use bitcoin::util::hash::BitcoinHash; use printer::{Printer, Stat}; use peer::Peer; @@ -122,19 +122,8 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { Peer::new(node.clone(), unsafe { TOR_PROXY.as_ref().unwrap() }, Duration::from_secs(timeout), printer) }); tokio::spawn(peer.and_then(move |(mut write, read)| { - TimeoutStream::new_timeout(read, scan_time + Duration::from_secs(store.get_u64(U64Setting::RunTimeout))).map_err(move |err| { - match err { - bitcoin::consensus::encode::Error::UnrecognizedNetworkCommand(ref msg) => { - // If we got here, we hit one of the explicitly disallowed messages indicating - // a bogus "node". - let mut state_lock = err_peer_state.lock().unwrap(); - state_lock.msg = (format!("(bad msg type {})", msg), true); - state_lock.fail_reason = AddressState::EvilNode; - }, - _ => {}, - } - () - }).for_each(move |msg| { + TimeoutStream::new_timeout(read, scan_time + Duration::from_secs(store.get_u64(U64Setting::RunTimeout))) + .map_err(|_| ()).for_each(move |msg| { let mut state_lock = peer_state.lock().unwrap(); macro_rules! check_set_flag { ($recvd_flag: ident, $msg: expr) => { { @@ -179,6 +168,9 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { check_set_flag!(recvd_version, "version"); state_lock.node_services = ver.services.as_u64(); state_lock.msg = (format!("(subver: {})", safe_ua), false); + if let Err(_) = write.try_send(NetworkMessage::SendAddrV2) { + return future::err(()); + } if let Err(_) = write.try_send(NetworkMessage::Verack) { return future::err(()); } @@ -222,6 +214,23 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { } unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes(&addrs); }, + Some(NetworkMessage::AddrV2(addrs)) => { + if addrs.len() > 1000 { + state_lock.fail_reason = AddressState::ProtocolViolation; + state_lock.msg = (format!("due to oversized addr: {}", addrs.len()), true); + state_lock.recvd_addrs = false; + return future::err(()); + } + if addrs.len() > 10 { + if !state_lock.recvd_addrs { + if let Err(_) = write.try_send(NetworkMessage::GetData(vec![Inventory::WitnessBlock(state_lock.request.1)])) { + return future::err(()); + } + } + state_lock.recvd_addrs = true; + } + unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes_v2(&addrs); + }, Some(NetworkMessage::Block(block)) => { if block != state_lock.request.2 { state_lock.fail_reason = AddressState::ProtocolViolation; @@ -248,6 +257,14 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) { state_lock.msg = ("due to unrequested transaction".to_string(), true); return future::err(()); }, + Some(NetworkMessage::Unknown { command, .. }) => { + if command.as_ref() == "gnop" { + let mut state_lock = err_peer_state.lock().unwrap(); + state_lock.msg = (format!("(bad msg type {})", command), true); + state_lock.fail_reason = AddressState::EvilNode; + return future::err(()); + } + }, _ => {}, } future::ok(()) @@ -386,17 +403,17 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc) { if let Some(height) = header_map.get(&headers[0].prev_blockhash).cloned() { for i in 0..headers.len() { - let hash = headers[i].bitcoin_hash(); + let hash = headers[i].block_hash(); if i < headers.len() - 1 && headers[i + 1].prev_blockhash != hash { return future::err(()); } - header_map.insert(headers[i].bitcoin_hash(), height + 1 + (i as u64)); - height_map.insert(height + 1 + (i as u64), headers[i].bitcoin_hash()); + header_map.insert(headers[i].block_hash(), height + 1 + (i as u64)); + height_map.insert(height + 1 + (i as u64), headers[i].block_hash()); } let top_height = height + headers.len() as u64; *unsafe { HIGHEST_HEADER.as_ref().unwrap() }.lock().unwrap() - = (headers.last().unwrap().bitcoin_hash(), top_height); + = (headers.last().unwrap().block_hash(), top_height); printer.set_stat(printer::Stat::HeaderCount(top_height)); if top_height >= starting_height as u64 { @@ -419,7 +436,7 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc) { } }, Some(NetworkMessage::Block(block)) => { - let hash = block.header.bitcoin_hash(); + let hash = block.block_hash(); let header_map = unsafe { HEADER_MAP.as_ref().unwrap() }.lock().unwrap(); let height = *header_map.get(&hash).expect("Got loose block from trusted peer we coulnd't have requested"); if height == unsafe { HIGHEST_HEADER.as_ref().unwrap() }.lock().unwrap().1 - 216 { @@ -458,10 +475,10 @@ fn main() { unsafe { HEADER_MAP = Some(Box::new(Mutex::new(HashMap::with_capacity(600000)))) }; unsafe { HEIGHT_MAP = Some(Box::new(Mutex::new(HashMap::with_capacity(600000)))) }; - unsafe { HEADER_MAP.as_ref().unwrap() }.lock().unwrap().insert(genesis_block(Network::Bitcoin).bitcoin_hash(), 0); - unsafe { HEIGHT_MAP.as_ref().unwrap() }.lock().unwrap().insert(0, genesis_block(Network::Bitcoin).bitcoin_hash()); - unsafe { HIGHEST_HEADER = Some(Box::new(Mutex::new((genesis_block(Network::Bitcoin).bitcoin_hash(), 0)))) }; - unsafe { REQUEST_BLOCK = Some(Box::new(Mutex::new(Arc::new((0, genesis_block(Network::Bitcoin).bitcoin_hash(), genesis_block(Network::Bitcoin)))))) }; + unsafe { HEADER_MAP.as_ref().unwrap() }.lock().unwrap().insert(genesis_block(Network::Bitcoin).block_hash(), 0); + unsafe { HEIGHT_MAP.as_ref().unwrap() }.lock().unwrap().insert(0, genesis_block(Network::Bitcoin).block_hash()); + unsafe { HIGHEST_HEADER = Some(Box::new(Mutex::new((genesis_block(Network::Bitcoin).block_hash(), 0)))) }; + unsafe { REQUEST_BLOCK = Some(Box::new(Mutex::new(Arc::new((0, genesis_block(Network::Bitcoin).block_hash(), genesis_block(Network::Bitcoin)))))) }; let trt = tokio::runtime::Builder::new() .blocking_threads(2).core_threads(num_cpus::get().max(1) + 1)