Skip to content

Commit 285cb27

Browse files
committed
Ignore NetAdress read errors
1 parent 04b677c commit 285cb27

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

lightning/src/routing/gossip.rs

+70-11
Original file line numberDiff line numberDiff line change
@@ -1012,14 +1012,73 @@ pub struct NodeAnnouncementInfo {
10121012
pub announcement_message: Option<NodeAnnouncement>
10131013
}
10141014

1015-
impl_writeable_tlv_based!(NodeAnnouncementInfo, {
1016-
(0, features, required),
1017-
(2, last_update, required),
1018-
(4, rgb, required),
1019-
(6, alias, required),
1020-
(8, announcement_message, option),
1021-
(10, addresses, vec_type),
1022-
});
1015+
impl Writeable for NodeAnnouncementInfo {
1016+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1017+
write_tlv_fields!(writer, {
1018+
(0, self.features, required),
1019+
(2, self.last_update, required),
1020+
(4, self.rgb, required),
1021+
(6, self.alias, required),
1022+
(8, self.announcement_message, option),
1023+
(10, self.addresses, vec_type),
1024+
});
1025+
1026+
Ok(())
1027+
}
1028+
}
1029+
1030+
// A wrapper allowing for the optional deseralization of `Vec<NetAddress>`. Utilizing this is
1031+
// necessary to maintain compatibility with previous serializations of `NetAddress` that have an
1032+
// invalid hostname set. In this case, we simply ignore such invalid entries.
1033+
struct NetAddressVecDeserWrapper(Vec<NetAddress>);
1034+
1035+
impl Readable for NetAddressVecDeserWrapper {
1036+
fn read<R: io::Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
1037+
let mut values = Vec::new();
1038+
loop {
1039+
let mut track_read = ::util::ser::ReadTrackingReader::new(&mut reader);
1040+
match MaybeReadable::read(&mut track_read) {
1041+
Ok(Some(v)) => { values.push(v); },
1042+
Ok(None) => { },
1043+
Err(DecodeError::InvalidValue) => { },
1044+
Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,
1045+
Err(e) => return Err(e),
1046+
}
1047+
}
1048+
Ok(Self(values))
1049+
}
1050+
}
1051+
1052+
1053+
impl Readable for NodeAnnouncementInfo {
1054+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
1055+
init_tlv_field_var!(features, required);
1056+
init_tlv_field_var!(last_update, required);
1057+
init_tlv_field_var!(rgb, required);
1058+
init_tlv_field_var!(alias, required);
1059+
init_tlv_field_var!(announcement_message, option);
1060+
let mut addresses = NetAddressVecDeserWrapper(Vec::new());
1061+
1062+
read_tlv_fields!(reader, {
1063+
(0, features, required),
1064+
(2, last_update, required),
1065+
(4, rgb, required),
1066+
(6, alias, required),
1067+
(8, announcement_message, option),
1068+
(10, addresses, required),
1069+
});
1070+
1071+
Ok(NodeAnnouncementInfo{
1072+
features: init_tlv_based_struct_field!(features, required),
1073+
last_update: init_tlv_based_struct_field!(last_update, required),
1074+
rgb: init_tlv_based_struct_field!(rgb, required),
1075+
alias: init_tlv_based_struct_field!(alias, required),
1076+
announcement_message: init_tlv_based_struct_field!(announcement_message, option),
1077+
addresses: addresses.0,
1078+
})
1079+
}
1080+
}
1081+
10231082

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

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

11081167
impl MaybeReadable for NodeAnnouncementInfoDeserWrapper {
11091168
fn read<R: io::Read>(reader: &mut R) -> Result<Option<Self>, DecodeError> {
11101169
match ::util::ser::Readable::read(reader) {
11111170
Ok(node_announcement) => Ok(Some(Self(node_announcement))),
1112-
Err(DecodeError::InvalidValue) => Ok(None),
1171+
Err(DecodeError::ShortRead) => Ok(None),
11131172
Err(err) => Err(err),
11141173
}
11151174
}

0 commit comments

Comments
 (0)