Skip to content

Commit 96d3de5

Browse files
committed
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.
1 parent 0ad8fde commit 96d3de5

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lightning/src/ln/msgs.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl FeatureContextInitNode for FeatureContextNode {}
105105
/// appears.
106106
pub struct Features<T: FeatureContext> {
107107
#[cfg(not(test))]
108+
/// Note that, for convinience, flags is LITTLE endian (despite being big-endian on the wire)
108109
flags: Vec<u8>,
109110
// Used to test encoding of diverse msgs
110111
#[cfg(test)]
@@ -265,18 +266,25 @@ impl Features<FeatureContextInit> {
265266
impl<T: FeatureContext> Writeable for Features<T> {
266267
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
267268
w.size_hint(self.flags.len() + 2);
268-
self.flags.write(w)
269+
(self.flags.len() as u16).write(w)?;
270+
for f in self.flags.iter().rev() { // We have to swap the endianness back to BE for writing
271+
f.write(w)?;
272+
}
273+
Ok(())
269274
}
270275
}
271276

272277
impl<R: ::std::io::Read, T: FeatureContext> Readable<R> for Features<T> {
273278
fn read(r: &mut R) -> Result<Self, DecodeError> {
279+
let mut flags: Vec<u8> = Readable::read(r)?;
280+
flags.reverse(); // Swap to big-endian
274281
Ok(Self {
275-
flags: Readable::read(r)?,
282+
flags,
276283
mark: PhantomData,
277284
})
278285
}
279286
}
287+
280288
/// An init message to be sent or received from a peer
281289
pub struct Init {
282290
pub(crate) features: InitFeatures,

0 commit comments

Comments
 (0)