X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmsgs.rs;h=85f3026eafa7930d705b9d7cc1b1b8fed3517b39;hb=c59468a88996cbb663a2487b77066a48db87e3b2;hp=70bf19f41e0b337dda57b9ca94c02b3b2f8005b8;hpb=cd5a11fe0d56fc6520fe94bad2cb5ea461608442;p=rust-lightning diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 70bf19f4..85f3026e 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -29,7 +29,7 @@ use std::io::Read; use std::result::Result; use util::events; -use util::ser::{Readable, Writeable, Writer}; +use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt}; use ln::channelmanager::{PaymentPreimage, PaymentHash}; @@ -39,15 +39,14 @@ pub enum DecodeError { /// A version byte specified something we don't know how to handle. /// Includes unknown realm byte in an OnionHopData packet UnknownVersion, - /// Unknown feature mandating we fail to parse message + /// Unknown feature mandating we fail to parse message (eg TLV with an even, unknown type) UnknownRequiredFeature, /// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0 - /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc + /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, TLV was + /// syntactically incorrect, etc InvalidValue, /// Buffer too short ShortRead, - /// node_announcement included more than one address of a given type! - ExtraAddressesPerType, /// A length descriptor in the packet didn't describe the later data correctly BadLengthDescriptor, /// Error from std::io @@ -56,7 +55,10 @@ pub enum DecodeError { /// An init message to be sent or received from a peer pub struct Init { + #[cfg(not(feature = "fuzztarget"))] pub(crate) features: InitFeatures, + #[cfg(feature = "fuzztarget")] + pub features: InitFeatures, } /// An error message to be sent or received from a peer @@ -139,9 +141,10 @@ pub struct FundingSigned { /// A funding_locked message to be sent or received from a peer #[derive(Clone, PartialEq)] +#[allow(missing_docs)] pub struct FundingLocked { - pub(crate) channel_id: [u8; 32], - pub(crate) next_per_commitment_point: PublicKey, + pub channel_id: [u8; 32], + pub next_per_commitment_point: PublicKey, } /// A shutdown message to be sent or received from a peer @@ -299,6 +302,9 @@ impl NetAddress { &NetAddress::OnionV3 { .. } => { 37 }, } } + + /// The maximum length of any address descriptor, not including the 1-byte type + pub(crate) const MAX_LEN: u16 = 37; } impl Writeable for NetAddress { @@ -331,9 +337,9 @@ impl Writeable for NetAddress { } } -impl Readable for Result { - fn read(reader: &mut R) -> Result, DecodeError> { - let byte = >::read(reader)?; +impl Readable for Result { + fn read(reader: &mut R) -> Result, DecodeError> { + let byte = ::read(reader)?; match byte { 1 => { Ok(Ok(NetAddress::IPv4 { @@ -570,7 +576,7 @@ pub trait ChannelMessageHandler : events::MessageSendEventsProvider + Send + Syn fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool); /// Handle a peer reconnecting, possibly generating channel_reestablish message(s). - fn peer_connected(&self, their_node_id: &PublicKey); + fn peer_connected(&self, their_node_id: &PublicKey, msg: &Init); /// Handle an incoming channel_reestablish message from the given peer. fn handle_channel_reestablish(&self, their_node_id: &PublicKey, msg: &ChannelReestablish); @@ -594,30 +600,36 @@ pub trait RoutingMessageHandler : Send + Sync { fn handle_htlc_fail_channel_update(&self, update: &HTLCFailChannelUpdate); /// Gets a subset of the channel announcements and updates required to dump our routing table /// to a remote node, starting at the short_channel_id indicated by starting_point and - /// including batch_amount entries. - fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, ChannelUpdate, ChannelUpdate)>; + /// including the batch_amount entries immediately higher in numerical value than starting_point. + fn get_next_channel_announcements(&self, starting_point: u64, batch_amount: u8) -> Vec<(ChannelAnnouncement, Option, Option)>; /// Gets a subset of the node announcements required to dump our routing table to a remote node, - /// starting at the node *after* the provided publickey and including batch_amount entries. + /// starting at the node *after* the provided publickey and including batch_amount entries + /// immediately higher (as defined by ::cmp) than starting_point. /// If None is provided for starting_point, we start at the first node. fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec; -} - -pub(crate) struct OnionRealm0HopData { - pub(crate) short_channel_id: u64, - pub(crate) amt_to_forward: u64, - pub(crate) outgoing_cltv_value: u32, - // 12 bytes of 0-padding + /// Returns whether a full sync should be requested from a peer. + fn should_request_full_sync(&self, node_id: &PublicKey) -> bool; } mod fuzzy_internal_msgs { // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize // them from untrusted input): - use super::OnionRealm0HopData; + pub(crate) enum OnionHopDataFormat { + Legacy { // aka Realm-0 + short_channel_id: u64, + }, + NonFinalNode { + short_channel_id: u64, + }, + FinalNode, + } + pub struct OnionHopData { - pub(crate) realm: u8, - pub(crate) data: OnionRealm0HopData, - pub(crate) hmac: [u8; 32], + pub(crate) format: OnionHopDataFormat, + pub(crate) amt_to_forward: u64, + pub(crate) outgoing_cltv_value: u32, + // 12 bytes of 0-padding for Legacy format } pub struct DecodedOnionErrorPacket { @@ -667,7 +679,6 @@ impl Error for DecodeError { DecodeError::UnknownRequiredFeature => "Unknown required feature preventing decode", DecodeError::InvalidValue => "Nonsense bytes didn't map to the type they were interpreted as", DecodeError::ShortRead => "Packet extended beyond the provided bytes", - DecodeError::ExtraAddressesPerType => "More than one address of a single type", DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly", DecodeError::Io(ref e) => e.description(), } @@ -708,9 +719,9 @@ impl Writeable for OptionalField