From 96d3de5cde06c6764213575702ff4a64c884a87e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 13 Jan 2020 13:50:29 -0500 Subject: [PATCH] Fix Feature endianness by swapping bytes on read/write. The spec is a bit mum on feature endianness, so I suppose it falls under the "everything is big endian unless otherwise specified" clause, but we were treating it as little. --- lightning/src/ln/msgs.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index cccbf83a..0610c02c 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -105,6 +105,7 @@ impl FeatureContextInitNode for FeatureContextNode {} /// appears. pub struct Features { #[cfg(not(test))] + /// Note that, for convinience, flags is LITTLE endian (despite being big-endian on the wire) flags: Vec, // Used to test encoding of diverse msgs #[cfg(test)] @@ -265,18 +266,25 @@ impl Features { impl Writeable for Features { fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { w.size_hint(self.flags.len() + 2); - self.flags.write(w) + (self.flags.len() as u16).write(w)?; + for f in self.flags.iter().rev() { // We have to swap the endianness back to BE for writing + f.write(w)?; + } + Ok(()) } } impl Readable for Features { fn read(r: &mut R) -> Result { + let mut flags: Vec = Readable::read(r)?; + flags.reverse(); // Swap to big-endian Ok(Self { - flags: Readable::read(r)?, + flags, mark: PhantomData, }) } } + /// An init message to be sent or received from a peer pub struct Init { pub(crate) features: InitFeatures, -- 2.30.2