]> git.bitcoin.ninja Git - dnsseed-rust/commitdiff
Use Option<NetworkMessage> so short buffer and unknown msg are diff
authorMatt Corallo <git@bluematt.me>
Mon, 26 Aug 2019 19:10:44 +0000 (15:10 -0400)
committerMatt Corallo <git@bluematt.me>
Mon, 26 Aug 2019 19:10:44 +0000 (15:10 -0400)
src/main.rs
src/peer.rs

index c86529e95295e9d8932d7e4acfbec59e8747bd0e..19dcb308f6a12144df21afcebd4afe69e7bc75d5 100644 (file)
@@ -108,7 +108,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                        }
                        state_lock.fail_reason = AddressState::TimeoutDuringRequest;
                        match msg {
-                               NetworkMessage::Version(ver) => {
+                               Some(NetworkMessage::Version(ver)) => {
                                        if ver.start_height < 0 || ver.start_height as u64 > state_lock.request.0 + 1008*2 {
                                                state_lock.fail_reason = AddressState::HighBlockCount;
                                                return future::err(());
@@ -142,18 +142,18 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                                return future::err(());
                                        }
                                },
-                               NetworkMessage::Verack => {
+                               Some(NetworkMessage::Verack) => {
                                        check_set_flag!(recvd_verack, "verack");
                                        if let Err(_) = write.try_send(NetworkMessage::Ping(state_lock.pong_nonce)) {
                                                return future::err(());
                                        }
                                },
-                               NetworkMessage::Ping(v) => {
+                               Some(NetworkMessage::Ping(v)) => {
                                        if let Err(_) = write.try_send(NetworkMessage::Pong(v)) {
                                                return future::err(())
                                        }
                                },
-                               NetworkMessage::Pong(v) => {
+                               Some(NetworkMessage::Pong(v)) => {
                                        if v != state_lock.pong_nonce {
                                                state_lock.fail_reason = AddressState::ProtocolViolation;
                                                state_lock.msg = ("due to invalid pong nonce".to_string(), true);
@@ -164,7 +164,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                                return future::err(());
                                        }
                                },
-                               NetworkMessage::Addr(addrs) => {
+                               Some(NetworkMessage::Addr(addrs)) => {
                                        if addrs.len() > 1000 {
                                                state_lock.fail_reason = AddressState::ProtocolViolation;
                                                state_lock.msg = (format!("due to oversized addr: {}", addrs.len()), true);
@@ -184,7 +184,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                        }
                                        unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes(&addrs);
                                },
-                               NetworkMessage::Block(block) => {
+                               Some(NetworkMessage::Block(block)) => {
                                        if block != state_lock.request.2 {
                                                state_lock.fail_reason = AddressState::ProtocolViolation;
                                                state_lock.msg = ("due to bad block".to_string(), true);
@@ -193,7 +193,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                        check_set_flag!(recvd_block, "block");
                                        return future::err(());
                                },
-                               NetworkMessage::Inv(invs) => {
+                               Some(NetworkMessage::Inv(invs)) => {
                                        for inv in invs {
                                                if inv.inv_type == InvType::Transaction {
                                                        state_lock.fail_reason = AddressState::EvilNode;
@@ -202,7 +202,7 @@ pub fn scan_node(scan_time: Instant, node: SocketAddr, manual: bool) {
                                                }
                                        }
                                },
-                               NetworkMessage::Tx(_) => {
+                               Some(NetworkMessage::Tx(_)) => {
                                        state_lock.fail_reason = AddressState::EvilNode;
                                        state_lock.msg = ("due to unrequested transaction".to_string(), true);
                                        return future::err(());
@@ -310,13 +310,13 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc<BGPClient>) {
                                return future::err(());
                        }
                        match msg {
-                               NetworkMessage::Version(ver) => {
+                               Some(NetworkMessage::Version(ver)) => {
                                        if let Err(_) = trusted_write.try_send(NetworkMessage::Verack) {
                                                return future::err(())
                                        }
                                        starting_height = ver.start_height;
                                },
-                               NetworkMessage::Verack => {
+                               Some(NetworkMessage::Verack) => {
                                        if let Err(_) = trusted_write.try_send(NetworkMessage::SendHeaders) {
                                                return future::err(());
                                        }
@@ -331,10 +331,10 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc<BGPClient>) {
                                                return future::err(());
                                        }
                                },
-                               NetworkMessage::Addr(addrs) => {
+                               Some(NetworkMessage::Addr(addrs)) => {
                                        unsafe { DATA_STORE.as_ref().unwrap() }.add_fresh_nodes(&addrs);
                                },
-                               NetworkMessage::Headers(headers) => {
+                               Some(NetworkMessage::Headers(headers)) => {
                                        if headers.is_empty() {
                                                return future::ok(());
                                        }
@@ -376,7 +376,7 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc<BGPClient>) {
                                                return future::err(())
                                        }
                                },
-                               NetworkMessage::Block(block) => {
+                               Some(NetworkMessage::Block(block)) => {
                                        let hash = block.header.bitcoin_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");
@@ -388,7 +388,7 @@ fn make_trusted_conn(trusted_sockaddr: SocketAddr, bgp_client: Arc<BGPClient>) {
                                                }
                                        }
                                },
-                               NetworkMessage::Ping(v) => {
+                               Some(NetworkMessage::Ping(v)) => {
                                        if let Err(_) = trusted_write.try_send(NetworkMessage::Pong(v)) {
                                                return future::err(())
                                        }
index 137d92dc311fea047dab956f0ba573c5f1caa7a0..aeeb2c2c582e7ba9c0546c5a08010468a6ef7fe1 100644 (file)
@@ -45,10 +45,10 @@ impl<'a> std::io::Read for BytesDecoder<'a> {
 
 struct MsgCoder<'a>(&'a Printer);
 impl<'a> codec::Decoder for MsgCoder<'a> {
-       type Item = NetworkMessage;
+       type Item = Option<NetworkMessage>;
        type Error = encode::Error;
 
-       fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result<Option<NetworkMessage>, encode::Error> {
+       fn decode(&mut self, bytes: &mut bytes::BytesMut) -> Result<Option<Option<NetworkMessage>>, encode::Error> {
                let mut decoder = BytesDecoder {
                        buf: bytes,
                        pos: 0
@@ -57,7 +57,7 @@ impl<'a> codec::Decoder for MsgCoder<'a> {
                        Ok(res) => {
                                decoder.buf.advance(decoder.pos);
                                if res.magic == Network::Bitcoin.magic() {
-                                       Ok(Some(res.payload))
+                                       Ok(Some(Some(res.payload)))
                                } else {
                                        Err(encode::Error::UnexpectedNetworkMagic {
                                                expected: Network::Bitcoin.magic(),
@@ -72,7 +72,7 @@ impl<'a> codec::Decoder for MsgCoder<'a> {
                                        //XXX(fixthese): self.0.add_line(format!("rust-bitcoin doesn't support {}!", msg), true);
                                        if msg == "gnop" {
                                                Err(e)
-                                       } else { Ok(None) }
+                                       } else { Ok(Some(None)) }
                                },
                                _ => {
                                        self.0.add_line(format!("Error decoding message: {:?}", e), true);
@@ -148,7 +148,7 @@ macro_rules! try_write_small {
 
 pub struct Peer {}
 impl Peer {
-       pub fn new(addr: SocketAddr, tor_proxy: &SocketAddr, timeout: Duration, printer: &'static Printer) -> impl Future<Error=(), Item=(mpsc::Sender<NetworkMessage>, impl Stream<Item=NetworkMessage, Error=encode::Error>)> {
+       pub fn new(addr: SocketAddr, tor_proxy: &SocketAddr, timeout: Duration, printer: &'static Printer) -> impl Future<Error=(), Item=(mpsc::Sender<NetworkMessage>, impl Stream<Item=Option<NetworkMessage>, Error=encode::Error>)> {
                let connect_timeout = Delay::new(Instant::now() + timeout.clone()).then(|_| {
                        future::err(std::io::Error::new(std::io::ErrorKind::TimedOut, "timeout reached"))
                });