]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Implement Writer/Reader
authorYuntai Kyong <yuntai.kyong@gmail.com>
Sat, 1 Sep 2018 07:18:56 +0000 (16:18 +0900)
committerMatt Corallo <git@bluematt.me>
Wed, 12 Sep 2018 14:18:02 +0000 (10:18 -0400)
with additional variants in DecodeError

fuzz/fuzz_targets/channel_target.rs
fuzz/fuzz_targets/router_target.rs
src/ln/msgs.rs
src/ln/peer_handler.rs
src/util/mod.rs
src/util/ser.rs [new file with mode: 0644]

index 7b3b7df1ce2ec3ddcc039212e92352bddef8b246..d2a4fbca94a2aa24cdeaf6beec5e9f7e2c01172c 100644 (file)
@@ -131,6 +131,8 @@ pub fn do_test(data: &[u8]) {
                                        msgs::DecodeError::ExtraAddressesPerType => return,
                                        msgs::DecodeError::BadLengthDescriptor => return,
                                        msgs::DecodeError::ShortRead => panic!("We picked the length..."),
+                                       msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
+                                       msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
                                }
                        }
                }
@@ -154,6 +156,8 @@ pub fn do_test(data: &[u8]) {
                                                msgs::DecodeError::ExtraAddressesPerType => return,
                                                msgs::DecodeError::BadLengthDescriptor => return,
                                                msgs::DecodeError::ShortRead => panic!("We picked the length..."),
+                                               msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
+                                               msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
                                        }
                                }
                        }
index 6476f812fb34dd2243ea1a585e6753eb44b3a52b..4669f1f8c4afe86be15ba2cacc24be95deacc0d0 100644 (file)
@@ -131,6 +131,8 @@ pub fn do_test(data: &[u8]) {
                                        msgs::DecodeError::ExtraAddressesPerType => return,
                                        msgs::DecodeError::BadLengthDescriptor => return,
                                        msgs::DecodeError::ShortRead => panic!("We picked the length..."),
+                                       msgs::DecodeError::InvalidValue => panic!("Writeable not used yet..."),
+                                       msgs::DecodeError::Io(_) => panic!("Writeable not used yet..."),
                                }
                        }
                }
index 1d0ea1c637235c02774fe3e111238c6a25ed15d4..795d117318ff01c39f5ff8e5e31643789188de80 100644 (file)
@@ -43,6 +43,10 @@ pub enum DecodeError {
        /// A length descriptor in the packet didn't describe the later data correctly
        /// (currently only generated in node_announcement)
        BadLengthDescriptor,
+       /// Error from std::io
+       Io(::std::io::Error),
+       /// Invalid value found when decoding
+       InvalidValue,
 }
 pub trait MsgDecodable: Sized {
        fn decode(v: &[u8]) -> Result<Self, DecodeError>;
@@ -519,6 +523,8 @@ impl Error for DecodeError {
                        DecodeError::ShortRead => "Packet extended beyond the provided bytes",
                        DecodeError::ExtraAddressesPerType => "More than one address of a single type",
                        DecodeError::BadLengthDescriptor => "A length descriptor in the packet didn't describe the later data correctly",
+                       DecodeError::Io(ref e) => e.description(),
+                       DecodeError::InvalidValue => "Invalid value in the bytes",
                }
        }
 }
@@ -534,6 +540,16 @@ impl fmt::Debug for HandleError {
        }
 }
 
+impl From<::std::io::Error> for DecodeError {
+       fn from(e: ::std::io::Error) -> Self {
+               if e.kind() == ::std::io::ErrorKind::UnexpectedEof {
+                       DecodeError::ShortRead
+               } else {
+                       DecodeError::Io(e)
+               }
+       }
+}
+
 macro_rules! secp_pubkey {
        ( $ctx: expr, $slice: expr ) => {
                match PublicKey::from_slice($ctx, $slice) {
index 8a4228851b7a7e3981e698ccc6cd05f3cb3e5d3b..5d5641bbe55eb75ff7c89749de1d51882901ecaa 100644 (file)
@@ -363,6 +363,9 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                                        continue;
                                                                                                },
                                                                                                msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::InvalidValue => return Err(PeerHandleError{ no_connection_possible: false }),
+                                                                                               msgs::DecodeError::InvalidLength => return Err(PeerHandleError{ no_connection_possible: false }),
                                                                                        }
                                                                                }
                                                                        };
index 571a4dbed1010049c2cd7590df55397a221af39c..308825855bd934f46e314477bb432cdda2fb7aa1 100644 (file)
@@ -21,4 +21,11 @@ pub(crate) mod test_utils;
 #[macro_use]
 pub(crate) mod macro_logger;
 
+#[cfg(feature = "fuzztarget")]
+#[macro_use]
+pub mod ser;
+#[cfg(not(feature = "fuzztarget"))]
+#[macro_use]
+pub(crate) mod ser;
+
 pub mod logger;
diff --git a/src/util/ser.rs b/src/util/ser.rs
new file mode 100644 (file)
index 0000000..0a247d4
--- /dev/null
@@ -0,0 +1,327 @@
+use std::result::Result;
+use std::io::{Read, Write};
+use std::collections::HashMap;
+use std::hash::Hash;
+use std::mem;
+
+use secp256k1::{Secp256k1, Signature};
+use secp256k1::key::PublicKey;
+use bitcoin::util::hash::Sha256dHash;
+use bitcoin::blockdata::script::Script;
+use std::marker::Sized;
+use ln::msgs::DecodeError;
+
+use util::byte_utils::{be64_to_array, be32_to_array, be16_to_array, slice_to_be16, slice_to_be32, slice_to_be64};
+
+const MAX_BUF_SIZE: usize = 16 * 1024;
+
+pub struct Writer<W> { writer: W }
+pub struct Reader<R> { reader: R }
+
+pub trait Writeable<W: Write> {
+       fn write(&self, writer: &mut Writer<W>) -> Result<(), DecodeError>;
+}
+
+pub trait Readable<R>
+       where Self: Sized,
+             R: Read
+{
+       fn read(reader: &mut Reader<R>) -> Result<Self, DecodeError>;
+}
+
+impl<W: Write> Writer<W> {
+       pub fn new(writer: W) -> Writer<W> {
+               return Writer { writer }
+       }
+       pub fn into_inner(self) -> W { self.writer }
+       pub fn get_ref(&self) -> &W { &self.writer }
+       fn write_u64(&mut self, v: u64) -> Result<(), DecodeError> {
+               Ok(self.writer.write_all(&be64_to_array(v))?)
+       }
+       fn write_u32(&mut self, v: u32) -> Result<(), DecodeError> {
+               Ok(self.writer.write_all(&be32_to_array(v))?)
+       }
+       fn write_u16(&mut self, v: u16) -> Result<(), DecodeError> {
+               Ok(self.writer.write_all(&be16_to_array(v))?)
+       }
+       fn write_u8(&mut self, v: u8) -> Result<(), DecodeError> {
+               Ok(self.writer.write_all(&[v])?)
+       }
+       fn write_bool(&mut self, v: bool) -> Result<(), DecodeError> {
+               Ok(self.writer.write_all(&[if v {1} else {0}])?)
+       }
+       pub fn write_all(&mut self, v: &[u8]) -> Result<(), DecodeError> {
+               Ok(self.writer.write_all(v)?)
+       }
+}
+
+impl<R: Read> Reader<R> {
+       pub fn new(reader: R) -> Reader<R> {
+               return Reader { reader }
+       }
+       pub fn into_inner(self) -> R { self.reader }
+       pub fn get_ref(&self) -> &R { &self.reader }
+
+       fn read_u64(&mut self) -> Result<u64, DecodeError> {
+               let mut buf = [0; 8];
+               self.reader.read_exact(&mut buf)?;
+               Ok(slice_to_be64(&buf))
+       }
+
+       fn read_u32(&mut self) -> Result<u32, DecodeError> {
+               let mut buf = [0; 4];
+               self.reader.read_exact(&mut buf)?;
+               Ok(slice_to_be32(&buf))
+       }
+
+       fn read_u16(&mut self) -> Result<u16, DecodeError> {
+               let mut buf = [0; 2];
+               self.reader.read_exact(&mut buf)?;
+               Ok(slice_to_be16(&buf))
+       }
+
+       fn read_u8(&mut self) -> Result<u8, DecodeError> {
+               let mut buf = [0; 1];
+               self.reader.read_exact(&mut buf)?;
+               Ok(buf[0])
+       }
+       fn read_bool(&mut self) -> Result<bool, DecodeError> {
+               let mut buf = [0; 1];
+               self.reader.read_exact(&mut buf)?;
+               if buf[0] != 0 && buf[0] != 1 {
+                       return Err(DecodeError::InvalidValue);
+               }
+               Ok(buf[0] == 1)
+       }
+       pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), DecodeError> {
+               Ok(self.reader.read_exact(buf)?)
+       }
+       pub fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, DecodeError> {
+               Ok(self.reader.read_to_end(buf)?)
+       }
+}
+
+macro_rules! impl_writeable_primitive {
+       ($val_type:ty, $meth_write:ident, $meth_read:ident) => {
+               impl<W:Write> Writeable<W> for $val_type {
+                       #[inline]
+                       fn write(&self, writer: &mut Writer<W>) -> Result<(), DecodeError> {
+                               writer.$meth_write(*self)
+                       }
+               }
+               impl<R:Read> Readable<R> for $val_type {
+                       #[inline]
+                       fn read(reader: &mut Reader<R>) -> Result<$val_type, DecodeError> {
+                               reader.$meth_read()
+                       }
+               }
+       }
+}
+
+impl_writeable_primitive!(u64, write_u64, read_u64);
+impl_writeable_primitive!(u32, write_u32, read_u32);
+impl_writeable_primitive!(u16, write_u16, read_u16);
+impl_writeable_primitive!(u8, write_u8, read_u8);
+impl_writeable_primitive!(bool, write_bool, read_bool);
+
+// u8 arrays
+macro_rules! impl_array {
+       ( $size:expr ) => (
+               impl<W> Writeable<W> for [u8; $size]
+                       where W: Write
+               {
+                       #[inline]
+                       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+                               w.write_all(self)?;
+                               Ok(())
+                       }
+               }
+
+               impl<R> Readable<R> for [u8; $size]
+                       where R: Read
+               {
+                       #[inline]
+                       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+                               let mut buf = [0u8; $size];
+                               r.read_exact(&mut buf)?;
+                               Ok(buf)
+                       }
+               }
+       );
+}
+
+//TODO: performance issue with [u8; size] with impl_array!()
+impl_array!(32); // for channel id & hmac
+impl_array!(33); // for PublicKey
+impl_array!(64); // for Signature
+impl_array!(1300); // for OnionPacket.hop_data
+
+// HashMap
+impl<W, K, V> Writeable<W> for HashMap<K, V>
+       where W: Write,
+             K: Writeable<W> + Eq + Hash,
+             V: Writeable<W>
+{
+       #[inline]
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+       (self.len() as u16).write(w)?;
+               for (key, value) in self.iter() {
+                       key.write(w)?;
+                       value.write(w)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R, K, V> Readable<R> for HashMap<K, V>
+       where R: Read,
+             K: Readable<R> + Eq + Hash,
+             V: Readable<R>
+{
+       #[inline]
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+               let len: u16 = Readable::read(r)?;
+               let mut ret = HashMap::with_capacity(len as usize);
+               for _ in 0..len {
+                               ret.insert(K::read(r)?, V::read(r)?);
+               }
+               Ok(ret)
+       }
+}
+
+// Vectors
+impl<W: Write, T: Writeable<W>> Writeable<W> for Vec<T> {
+       #[inline]
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+               let byte_size = (self.len() as usize)
+                               .checked_mul(mem::size_of::<T>())
+                               .ok_or(DecodeError::BadLengthDescriptor)?;
+               if byte_size > MAX_BUF_SIZE {
+                               return Err(DecodeError::BadLengthDescriptor);
+               }
+               (self.len() as u16).write(w)?;
+               // performance with Vec<u8>
+               for e in self.iter() {
+                       e.write(w)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R: Read, T: Readable<R>> Readable<R> for Vec<T> {
+       #[inline]
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+                       let len: u16 = Readable::read(r)?;
+                       let byte_size = (len as usize)
+                                       .checked_mul(mem::size_of::<T>())
+                                       .ok_or(DecodeError::BadLengthDescriptor)?;
+                       if byte_size > MAX_BUF_SIZE {
+                                       return Err(DecodeError::BadLengthDescriptor);
+                       }
+                       let mut ret = Vec::with_capacity(len as usize);
+                       for _ in 0..len { ret.push(T::read(r)?); }
+                       Ok(ret)
+       }
+}
+
+impl<W: Write> Writeable<W> for Script {
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+               self.to_bytes().to_vec().write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for Script {
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+               let len = <u16 as Readable<R>>::read(r)? as usize;
+               let mut buf = vec![0; len];
+               r.read_exact(&mut buf)?;
+               Ok(Script::from(buf))
+       }
+}
+
+impl<W: Write> Writeable<W> for Option<Script> {
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+               if let &Some(ref script) = self {
+                       script.write(w)?;
+               }
+               Ok(())
+       }
+}
+
+impl<R: Read> Readable<R> for Option<Script> {
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+               match <u16 as Readable<R>>::read(r) {
+                       Ok(len) => {
+                               let mut buf = vec![0; len as usize];
+                               r.read_exact(&mut buf)?;
+                               Ok(Some(Script::from(buf)))
+                       },
+                       Err(DecodeError::ShortRead) => Ok(None),
+                       Err(e) => Err(e)
+               }
+       }
+}
+
+impl<W: Write> Writeable<W> for PublicKey {
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+               self.serialize().write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for PublicKey {
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+               let buf: [u8; 33] = Readable::read(r)?;
+               match PublicKey::from_slice(&Secp256k1::without_caps(), &buf) {
+                       Ok(key) => Ok(key),
+                       Err(_) => return Err(DecodeError::BadPublicKey),
+               }
+       }
+}
+
+impl<W: Write> Writeable<W> for Sha256dHash {
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+               self.as_bytes().write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for Sha256dHash {
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+               let buf: [u8; 32] = Readable::read(r)?;
+               Ok(From::from(&buf[..]))
+       }
+}
+
+impl<W: Write> Writeable<W> for Signature {
+       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+               self.serialize_compact(&Secp256k1::without_caps()).write(w)
+       }
+}
+
+impl<R: Read> Readable<R> for Signature {
+       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+               let buf: [u8; 64] = Readable::read(r)?;
+               match Signature::from_compact(&Secp256k1::without_caps(), &buf) {
+                       Ok(sig) => Ok(sig),
+                       Err(_) => return Err(DecodeError::BadSignature),
+               }
+       }
+}
+
+macro_rules! impl_writeable {
+       ($st:ident, {$($field:ident),*}) => {
+               impl<W: ::std::io::Write> Writeable<W> for $st {
+                       fn write(&self, w: &mut Writer<W>) -> Result<(), DecodeError> {
+                               $( self.$field.write(w)?; )*
+                               Ok(())
+                       }
+               }
+
+               impl<R: ::std::io::Read> Readable<R> for $st {
+                       fn read(r: &mut Reader<R>) -> Result<Self, DecodeError> {
+                               Ok(Self {
+                                       $($field: Readable::read(r)?),*
+                               })
+                       }
+               }
+       }
+}