From: Matt Corallo Date: Mon, 13 Jan 2020 18:50:29 +0000 (-0500) Subject: Fix Feature endianness by swapping bytes on read/write. X-Git-Tag: v0.0.12~160^2~3 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=96d3de5cde06c6764213575702ff4a64c884a87e;p=rust-lightning 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. --- diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index cccbf83ac..0610c02c1 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,