Send `warning` instead of `error` when we incounter bogus gossip
authorMatt Corallo <git@bluematt.me>
Fri, 7 Jan 2022 20:30:50 +0000 (20:30 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 11 Jan 2022 19:48:20 +0000 (19:48 +0000)
lightning/src/ln/peer_handler.rs
lightning/src/ln/wire.rs

index f6369c62f70837ec0d0341552b6ca5cee33ba9d2..86cd344ce352dea1544f27d894a94813a6ca3930 100644 (file)
@@ -902,26 +902,31 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref, CMH: Deref> P
                                                                                        Ok(x) => x,
                                                                                        Err(e) => {
                                                                                                match e {
-                                                                                                       msgs::DecodeError::UnknownVersion => return Err(PeerHandleError { no_connection_possible: false }),
-                                                                                                       msgs::DecodeError::UnknownRequiredFeature => {
+                                                                                                       (msgs::DecodeError::UnknownRequiredFeature, _) => {
                                                                                                                log_gossip!(self.logger, "Got a channel/node announcement with an unknown required feature flag, you may want to update!");
                                                                                                                continue;
                                                                                                        }
-                                                                                                       msgs::DecodeError::InvalidValue => {
+                                                                                                       (msgs::DecodeError::UnsupportedCompression, _) => {
+                                                                                                               log_gossip!(self.logger, "We don't support zlib-compressed message fields, sending a warning and ignoring message");
+                                                                                                               self.enqueue_message(peer, &msgs::WarningMessage { channel_id: [0; 32], data: "Unsupported message compression: zlib".to_owned() });
+                                                                                                               continue;
+                                                                                                       }
+                                                                                                       (_, Some(ty)) if is_gossip_msg(ty) => {
+                                                                                                               log_gossip!(self.logger, "Got an invalid value while deserializing a gossip message");
+                                                                                                               self.enqueue_message(peer, &msgs::WarningMessage { channel_id: [0; 32], data: "Unreadable/bogus gossip message".to_owned() });
+                                                                                                               continue;
+                                                                                                       }
+                                                                                                       (msgs::DecodeError::UnknownVersion, _) => return Err(PeerHandleError { no_connection_possible: false }),
+                                                                                                       (msgs::DecodeError::InvalidValue, _) => {
                                                                                                                log_debug!(self.logger, "Got an invalid value while deserializing message");
                                                                                                                return Err(PeerHandleError { no_connection_possible: false });
                                                                                                        }
-                                                                                                       msgs::DecodeError::ShortRead => {
+                                                                                                       (msgs::DecodeError::ShortRead, _) => {
                                                                                                                log_debug!(self.logger, "Deserialization failed due to shortness of message");
                                                                                                                return Err(PeerHandleError { no_connection_possible: false });
                                                                                                        }
-                                                                                                       msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
-                                                                                                       msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
-                                                                                                       msgs::DecodeError::UnsupportedCompression => {
-                                                                                                               log_gossip!(self.logger, "We don't support zlib-compressed message fields, sending a warning and ignoring message");
-                                                                                                               self.enqueue_message(peer, &msgs::WarningMessage { channel_id: [0; 32], data: "Unsupported message compression: zlib".to_owned() });
-                                                                                                               continue;
-                                                                                                       }
+                                                                                                       (msgs::DecodeError::BadLengthDescriptor, _) => return Err(PeerHandleError { no_connection_possible: false }),
+                                                                                                       (msgs::DecodeError::Io(_), _) => return Err(PeerHandleError { no_connection_possible: false }),
                                                                                                }
                                                                                        }
                                                                                };
index 2c77fd46b8d92f8730e19d65448665f2fd86c710..d113acbfc7d4d5e50a17d2bea7012f67fa845285 100644 (file)
@@ -119,15 +119,20 @@ impl<T> Message<T> where T: core::fmt::Debug + Type {
 /// # Errors
 ///
 /// Returns an error if the message payload code not be decoded as the specified type.
-pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(
-       buffer: &mut R,
-       custom_reader: H,
-) -> Result<Message<T>, msgs::DecodeError>
-where
+pub(crate) fn read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, custom_reader: H)
+-> Result<Message<T>, (msgs::DecodeError, Option<u16>)> where
+       T: core::fmt::Debug + Type + Writeable,
+       H::Target: CustomMessageReader<CustomMessage = T>,
+{
+       let message_type = <u16 as Readable>::read(buffer).map_err(|e| (e, None))?;
+       do_read(buffer, message_type, custom_reader).map_err(|e| (e, Some(message_type)))
+}
+
+fn do_read<R: io::Read, T, H: core::ops::Deref>(buffer: &mut R, message_type: u16, custom_reader: H)
+-> Result<Message<T>, msgs::DecodeError> where
        T: core::fmt::Debug + Type + Writeable,
        H::Target: CustomMessageReader<CustomMessage = T>,
 {
-       let message_type = <u16 as Readable>::read(buffer)?;
        match message_type {
                msgs::Init::TYPE => {
                        Ok(Message::Init(Readable::read(buffer)?))