Skip to content

Commit

Permalink
Ignore NetAdress read errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jul 21, 2022
1 parent 04b677c commit 285cb27
Showing 1 changed file with 70 additions and 11 deletions.
81 changes: 70 additions & 11 deletions lightning/src/routing/gossip.rs
Expand Up @@ -1012,14 +1012,73 @@ pub struct NodeAnnouncementInfo {
pub announcement_message: Option<NodeAnnouncement>
}

impl_writeable_tlv_based!(NodeAnnouncementInfo, {
(0, features, required),
(2, last_update, required),
(4, rgb, required),
(6, alias, required),
(8, announcement_message, option),
(10, addresses, vec_type),
});
impl Writeable for NodeAnnouncementInfo {
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_tlv_fields!(writer, {
(0, self.features, required),
(2, self.last_update, required),
(4, self.rgb, required),
(6, self.alias, required),
(8, self.announcement_message, option),
(10, self.addresses, vec_type),
});

Ok(())
}
}

// A wrapper allowing for the optional deseralization of `Vec<NetAddress>`. Utilizing this is
// necessary to maintain compatibility with previous serializations of `NetAddress` that have an
// invalid hostname set. In this case, we simply ignore such invalid entries.
struct NetAddressVecDeserWrapper(Vec<NetAddress>);

impl Readable for NetAddressVecDeserWrapper {
fn read<R: io::Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
let mut values = Vec::new();
loop {
let mut track_read = ::util::ser::ReadTrackingReader::new(&mut reader);
match MaybeReadable::read(&mut track_read) {
Ok(Some(v)) => { values.push(v); },
Ok(None) => { },
Err(DecodeError::InvalidValue) => { },
Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,
Err(e) => return Err(e),
}
}
Ok(Self(values))
}
}


impl Readable for NodeAnnouncementInfo {
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
init_tlv_field_var!(features, required);
init_tlv_field_var!(last_update, required);
init_tlv_field_var!(rgb, required);
init_tlv_field_var!(alias, required);
init_tlv_field_var!(announcement_message, option);
let mut addresses = NetAddressVecDeserWrapper(Vec::new());

read_tlv_fields!(reader, {
(0, features, required),
(2, last_update, required),
(4, rgb, required),
(6, alias, required),
(8, announcement_message, option),
(10, addresses, required),
});

Ok(NodeAnnouncementInfo{
features: init_tlv_based_struct_field!(features, required),
last_update: init_tlv_based_struct_field!(last_update, required),
rgb: init_tlv_based_struct_field!(rgb, required),
alias: init_tlv_based_struct_field!(alias, required),
announcement_message: init_tlv_based_struct_field!(announcement_message, option),
addresses: addresses.0,
})
}
}


/// A user-defined name for a node, which may be used when displaying the node in a graph.
///
Expand Down Expand Up @@ -1101,15 +1160,15 @@ impl Writeable for NodeInfo {
}

// A wrapper allowing for the optional deseralization of `NodeAnnouncementInfo`. Utilizing this is
// necessary to maintain compatibility with previous serializations of `NodeAnnouncementInfo` that have an
// invalid hostname set. In this case, we simply ignore the error and continue reading the `NodeInfo`.
// necessary to maintain compatibility with previous serializations of `NetAddress` that have an
// invalid hostname set.
struct NodeAnnouncementInfoDeserWrapper(NodeAnnouncementInfo);

impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
match ::util::ser::Readable::read(reader) {
Ok(node_announcement) => Ok(Some(Self(node_announcement))),
Err(DecodeError::InvalidValue) => Ok(None),
Err(DecodeError::ShortRead) => Ok(None),
Err(err) => Err(err),
}
}
Expand Down

0 comments on commit 285cb27

Please sign in to comment.