X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fmsgs.rs;h=6f4795c75e8a7413d6ce18fbd1021dfbf5b99fec;hb=321b19c4d96a49b39b5583d0f12fea249038f6ff;hp=974e30095109e3647e2acd006ce36bad03e9e928;hpb=f24bbd63cc72e32efaa23844f19f4a7952fb22cb;p=rust-lightning diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 974e3009..6f4795c7 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -32,8 +32,8 @@ use bitcoin::hash_types::{Txid, BlockHash}; use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; -use std::{cmp, fmt}; -use std::fmt::Debug; +use core::{cmp, fmt}; +use core::fmt::Debug; use std::io::Read; use util::events::MessageSendEventsProvider; @@ -63,6 +63,8 @@ pub enum DecodeError { /// Error from std::io Io(/// (C-not exported) as ErrorKind doesn't have a reasonable mapping ::std::io::ErrorKind), + /// The message included zlib-compressed values, which we don't support. + UnsupportedCompression, } /// An init message to be sent or received from a peer @@ -390,6 +392,17 @@ pub enum NetAddress { }, } impl NetAddress { + /// Gets the ID of this address type. Addresses in node_announcement messages should be sorted + /// by this. + pub(crate) fn get_id(&self) -> u8 { + match self { + &NetAddress::IPv4 {..} => { 1 }, + &NetAddress::IPv6 {..} => { 2 }, + &NetAddress::OnionV2 {..} => { 3 }, + &NetAddress::OnionV3 {..} => { 4 }, + } + } + /// Strict byte-length of address descriptor, 1-byte type not recorded fn len(&self) -> u16 { match self { @@ -469,6 +482,17 @@ impl Readable for Result { } } +impl Readable for NetAddress { + fn read(reader: &mut R) -> Result { + match Readable::read(reader) { + Ok(Ok(res)) => Ok(res), + Ok(Err(_)) => Err(DecodeError::UnknownVersion), + Err(e) => Err(e), + } + } +} + + /// The unsigned part of a node_announcement #[derive(Clone, Debug, PartialEq)] pub struct UnsignedNodeAnnouncement { @@ -942,6 +966,7 @@ impl fmt::Display for DecodeError { DecodeError::ShortRead => f.write_str("Packet extended beyond the provided bytes"), DecodeError::BadLengthDescriptor => f.write_str("A length descriptor in the packet didn't describe the later data correctly"), DecodeError::Io(ref e) => e.fmt(f), + DecodeError::UnsupportedCompression => f.write_str("We don't support receiving messages with zlib-compressed fields"), } } } @@ -1271,20 +1296,17 @@ impl Writeable for OnionHopData { (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)), (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)), (6, short_channel_id) - }); + }, { }); }, - OnionHopDataFormat::FinalNode { payment_data: Some(ref final_data) } => { - if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); } - encode_varint_length_prefixed_tlv!(w, { - (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)), - (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)), - (8, final_data) - }); - }, - OnionHopDataFormat::FinalNode { payment_data: None } => { + OnionHopDataFormat::FinalNode { ref payment_data } => { + if let Some(final_data) = payment_data { + if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); } + } encode_varint_length_prefixed_tlv!(w, { (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)), (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)) + }, { + (8, payment_data) }); }, } @@ -1623,17 +1645,18 @@ impl Readable for QueryShortChannelIds { fn read(r: &mut R) -> Result { let chain_hash: BlockHash = Readable::read(r)?; - // We expect the encoding_len to always includes the 1-byte - // encoding_type and that short_channel_ids are 8-bytes each let encoding_len: u16 = Readable::read(r)?; - if encoding_len == 0 || (encoding_len - 1) % 8 != 0 { - return Err(DecodeError::InvalidValue); - } + let encoding_type: u8 = Readable::read(r)?; // Must be encoding_type=0 uncompressed serialization. We do not // support encoding_type=1 zlib serialization. - let encoding_type: u8 = Readable::read(r)?; if encoding_type != EncodingType::Uncompressed as u8 { + return Err(DecodeError::UnsupportedCompression); + } + + // We expect the encoding_len to always includes the 1-byte + // encoding_type and that short_channel_ids are 8-bytes each + if encoding_len == 0 || (encoding_len - 1) % 8 != 0 { return Err(DecodeError::InvalidValue); } @@ -1735,17 +1758,18 @@ impl Readable for ReplyChannelRange { let number_of_blocks: u32 = Readable::read(r)?; let sync_complete: bool = Readable::read(r)?; - // We expect the encoding_len to always includes the 1-byte - // encoding_type and that short_channel_ids are 8-bytes each let encoding_len: u16 = Readable::read(r)?; - if encoding_len == 0 || (encoding_len - 1) % 8 != 0 { - return Err(DecodeError::InvalidValue); - } + let encoding_type: u8 = Readable::read(r)?; // Must be encoding_type=0 uncompressed serialization. We do not // support encoding_type=1 zlib serialization. - let encoding_type: u8 = Readable::read(r)?; if encoding_type != EncodingType::Uncompressed as u8 { + return Err(DecodeError::UnsupportedCompression); + } + + // We expect the encoding_len to always includes the 1-byte + // encoding_type and that short_channel_ids are 8-bytes each + if encoding_len == 0 || (encoding_len - 1) % 8 != 0 { return Err(DecodeError::InvalidValue); }