X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain.rs;h=d887934d435e847af43d34a9c60c8d50f9d5e881;hb=HEAD;hp=9c6e68155f584252805a15240e044ca8b13b06c4;hpb=c06ea88b377bd782c96d74028a11d8595112f272;p=dnsseed-rust diff --git a/src/main.rs b/src/main.rs index 9c6e681..d887934 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,48 +41,6 @@ static mut TOR_PROXY: Option = None; pub static START_SHUTDOWN: AtomicBool = AtomicBool::new(false); static SCANNING: AtomicBool = AtomicBool::new(false); - -use std::alloc::{GlobalAlloc, Layout, System}; -use std::ptr; -use std::sync::atomic::AtomicUsize; - -// We keep track of all memory allocated by Rust code, refusing new allocations if it exceeds -// 1.75GB. -// -// Note that while Rust's std, in general, should panic in response to a null allocation, it -// is totally conceivable that some code will instead dereference this null pointer, which -// would violate our guarantees that Rust modules should never crash the entire application. -// -// In the future, as upstream Rust explores a safer allocation API (eg the Alloc API which -// returns Results instead of raw pointers, or redefining the GlobalAlloc API to allow -// panic!()s inside of alloc calls), we should switch to those, however these APIs are -// currently unstable. -const TOTAL_MEM_LIMIT_BYTES: usize = (1024 + 756) * 1024 * 1024; -static TOTAL_MEM_ALLOCD: AtomicUsize = AtomicUsize::new(0); -struct MemoryLimitingAllocator; -unsafe impl GlobalAlloc for MemoryLimitingAllocator { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - let len = layout.size(); - if len > TOTAL_MEM_LIMIT_BYTES { - return ptr::null_mut(); - } - if TOTAL_MEM_ALLOCD.fetch_add(len, Ordering::AcqRel) + len > TOTAL_MEM_LIMIT_BYTES { - TOTAL_MEM_ALLOCD.fetch_sub(len, Ordering::AcqRel); - return ptr::null_mut(); - } - System.alloc(layout) - } - - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - System.dealloc(ptr, layout); - TOTAL_MEM_ALLOCD.fetch_sub(layout.size(), Ordering::AcqRel); - } -} - -#[global_allocator] -static ALLOC: MemoryLimitingAllocator = MemoryLimitingAllocator; - - struct PeerState { request: Arc<(u64, BlockHash, Block)>, pong_nonce: u64, @@ -469,8 +427,8 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc) { } fn main() { - if env::args().len() != 5 { - println!("USAGE: dnsseed-rust datastore localPeerAddress tor_proxy_addr bgp_peer"); + if env::args().len() != 6 { + println!("USAGE: dnsseed-rust datastore localPeerAddress tor_proxy_addr bgp_peer bgp_peer_asn"); return; } @@ -495,13 +453,14 @@ fn main() { unsafe { TOR_PROXY = Some(tor_socks5_sockaddr); } let bgp_sockaddr: SocketAddr = args.next().unwrap().parse().unwrap(); + let bgp_peerasn: u32 = args.next().unwrap().parse().unwrap(); Store::new(path).and_then(move |store| { unsafe { DATA_STORE = Some(Box::new(store)) }; let store = unsafe { DATA_STORE.as_ref().unwrap() }; unsafe { PRINTER = Some(Box::new(Printer::new(store))) }; - let bgp_client = BGPClient::new(bgp_sockaddr, Duration::from_secs(60), unsafe { PRINTER.as_ref().unwrap() }); + let bgp_client = BGPClient::new(bgp_peerasn, bgp_sockaddr, Duration::from_secs(60), unsafe { PRINTER.as_ref().unwrap() }); make_trusted_conn(trusted_sockaddr, Arc::clone(&bgp_client)); reader::read(store, unsafe { PRINTER.as_ref().unwrap() }, bgp_client);