//!
//! Note that if you go with such an architecture (instead of passing raw socket events to a
//! non-internet-facing system) you trust the frontend internet-facing system to not lie about the
-//! source node_id of the mssage, however this does allow you to significantly reduce bandwidth
+//! source node_id of the message, however this does allow you to significantly reduce bandwidth
//! between the systems as routing messages can represent a significant chunk of bandwidth usage
//! (especially for non-channel-publicly-announcing nodes). As an alternate design which avoids
//! this issue, if you have sufficient bidirectional bandwidth between your systems, you may send
//! track the network on the less-secure system.
use secp256k1::key::PublicKey;
-use secp256k1::{Secp256k1, Signature};
+use secp256k1::Signature;
use secp256k1;
use bitcoin::util::hash::Sha256dHash;
use bitcoin::blockdata::script::Script;
use std::io::Read;
use std::result::Result;
-use util::{byte_utils, events};
+use util::events;
use util::ser::{Readable, Writeable, Writer};
use ln::channelmanager::{PaymentPreimage, PaymentHash};
/// node_announcement included more than one address of a given type!
ExtraAddressesPerType,
/// 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),
}
/// An address which can be used to connect to a remote peer
-#[derive(Clone)]
+#[derive(PartialEq, Clone)]
pub enum NetAddress {
- /// An IPv4 address/port on which the peer is listenting.
+ /// An IPv4 address/port on which the peer is listening.
IPv4 {
/// The 4-byte IPv4 address
addr: [u8; 4],
- /// The port on which the node is listenting
+ /// The port on which the node is listening
port: u16,
},
- /// An IPv6 address/port on which the peer is listenting.
+ /// An IPv6 address/port on which the peer is listening.
IPv6 {
/// The 16-byte IPv6 address
addr: [u8; 16],
- /// The port on which the node is listenting
+ /// The port on which the node is listening
port: u16,
},
/// An old-style Tor onion address/port on which the peer is listening.
OnionV2 {
/// The bytes (usually encoded in base32 with ".onion" appended)
addr: [u8; 10],
- /// The port on which the node is listenting
+ /// The port on which the node is listening
port: u16,
},
/// A new-style Tor onion address/port on which the peer is listening.
checksum: u16,
/// The version byte, as defined by the Tor Onion v3 spec.
version: u8,
- /// The port on which the node is listenting
+ /// The port on which the node is listening
port: u16,
},
}
&NetAddress::OnionV3 {..} => { 4 },
}
}
+
+ /// Strict byte-length of address descriptor, 1-byte type not recorded
+ fn len(&self) -> u16 {
+ match self {
+ &NetAddress::IPv4 { .. } => { 6 },
+ &NetAddress::IPv6 { .. } => { 18 },
+ &NetAddress::OnionV2 { .. } => { 12 },
+ &NetAddress::OnionV3 { .. } => { 37 },
+ }
+ }
}
-#[derive(Clone)]
+impl Writeable for NetAddress {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ match self {
+ &NetAddress::IPv4 { ref addr, ref port } => {
+ 1u8.write(writer)?;
+ addr.write(writer)?;
+ port.write(writer)?;
+ },
+ &NetAddress::IPv6 { ref addr, ref port } => {
+ 2u8.write(writer)?;
+ addr.write(writer)?;
+ port.write(writer)?;
+ },
+ &NetAddress::OnionV2 { ref addr, ref port } => {
+ 3u8.write(writer)?;
+ addr.write(writer)?;
+ port.write(writer)?;
+ },
+ &NetAddress::OnionV3 { ref ed25519_pubkey, ref checksum, ref version, ref port } => {
+ 4u8.write(writer)?;
+ ed25519_pubkey.write(writer)?;
+ checksum.write(writer)?;
+ version.write(writer)?;
+ port.write(writer)?;
+ }
+ }
+ Ok(())
+ }
+}
+
+impl<R: ::std::io::Read> Readable<R> for Result<NetAddress, u8> {
+ fn read(reader: &mut R) -> Result<Result<NetAddress, u8>, DecodeError> {
+ let byte = <u8 as Readable<R>>::read(reader)?;
+ match byte {
+ 1 => {
+ Ok(Ok(NetAddress::IPv4 {
+ addr: Readable::read(reader)?,
+ port: Readable::read(reader)?,
+ }))
+ },
+ 2 => {
+ Ok(Ok(NetAddress::IPv6 {
+ addr: Readable::read(reader)?,
+ port: Readable::read(reader)?,
+ }))
+ },
+ 3 => {
+ Ok(Ok(NetAddress::OnionV2 {
+ addr: Readable::read(reader)?,
+ port: Readable::read(reader)?,
+ }))
+ },
+ 4 => {
+ Ok(Ok(NetAddress::OnionV3 {
+ ed25519_pubkey: Readable::read(reader)?,
+ checksum: Readable::read(reader)?,
+ version: Readable::read(reader)?,
+ port: Readable::read(reader)?,
+ }))
+ },
+ _ => return Ok(Err(byte)),
+ }
+ }
+}
+
+#[derive(PartialEq, Clone)]
// Only exposed as broadcast of node_announcement should be filtered by node_id
/// The unsigned part of a node_announcement
pub struct UnsignedNodeAnnouncement {
pub(crate) excess_address_data: Vec<u8>,
pub(crate) excess_data: Vec<u8>,
}
-#[derive(Clone)]
+#[derive(PartialEq, Clone)]
/// A node_announcement message to be sent or received from a peer
pub struct NodeAnnouncement {
pub(crate) signature: Signature,
/// transaction updates if they were pending.
#[derive(PartialEq, Clone)]
pub struct CommitmentUpdate {
- pub(crate) update_add_htlcs: Vec<UpdateAddHTLC>,
- pub(crate) update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
- pub(crate) update_fail_htlcs: Vec<UpdateFailHTLC>,
- pub(crate) update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
- pub(crate) update_fee: Option<UpdateFee>,
- pub(crate) commitment_signed: CommitmentSigned,
+ /// update_add_htlc messages which should be sent
+ pub update_add_htlcs: Vec<UpdateAddHTLC>,
+ /// update_fulfill_htlc messages which should be sent
+ pub update_fulfill_htlcs: Vec<UpdateFulfillHTLC>,
+ /// update_fail_htlc messages which should be sent
+ pub update_fail_htlcs: Vec<UpdateFailHTLC>,
+ /// update_fail_malformed_htlc messages which should be sent
+ pub update_fail_malformed_htlcs: Vec<UpdateFailMalformedHTLC>,
+ /// An update_fee message which should be sent
+ pub update_fee: Option<UpdateFee>,
+ /// Finally, the commitment_signed message which should be sent
+ pub commitment_signed: CommitmentSigned,
}
/// The information we received from a peer along the route of a payment we originated. This is
public_key: {
let mut buf = [0u8;33];
r.read_exact(&mut buf)?;
- PublicKey::from_slice(&Secp256k1::without_caps(), &buf)
+ PublicKey::from_slice(&buf)
},
hop_data: Readable::read(r)?,
hmac: Readable::read(r)?,
w.write_all(&self.rgb)?;
self.alias.write(w)?;
- let mut addr_slice = Vec::with_capacity(self.addresses.len() * 18);
let mut addrs_to_encode = self.addresses.clone();
addrs_to_encode.sort_unstable_by(|a, b| { a.get_id().cmp(&b.get_id()) });
addrs_to_encode.dedup_by(|a, b| { a.get_id() == b.get_id() });
- for addr in addrs_to_encode.iter() {
- match addr {
- &NetAddress::IPv4{addr, port} => {
- addr_slice.push(1);
- addr_slice.extend_from_slice(&addr);
- addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
- },
- &NetAddress::IPv6{addr, port} => {
- addr_slice.push(2);
- addr_slice.extend_from_slice(&addr);
- addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
- },
- &NetAddress::OnionV2{addr, port} => {
- addr_slice.push(3);
- addr_slice.extend_from_slice(&addr);
- addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
- },
- &NetAddress::OnionV3{ed25519_pubkey, checksum, version, port} => {
- addr_slice.push(4);
- addr_slice.extend_from_slice(&ed25519_pubkey);
- addr_slice.extend_from_slice(&byte_utils::be16_to_array(checksum));
- addr_slice.push(version);
- addr_slice.extend_from_slice(&byte_utils::be16_to_array(port));
- },
- }
+ let mut addr_len = 0;
+ for addr in &addrs_to_encode {
+ addr_len += 1 + addr.len();
+ }
+ (addr_len + self.excess_address_data.len() as u16).write(w)?;
+ for addr in addrs_to_encode {
+ addr.write(w)?;
}
- ((addr_slice.len() + self.excess_address_data.len()) as u16).write(w)?;
- w.write_all(&addr_slice[..])?;
w.write_all(&self.excess_address_data[..])?;
w.write_all(&self.excess_data[..])?;
Ok(())
r.read_exact(&mut rgb)?;
let alias: [u8; 32] = Readable::read(r)?;
- let addrlen: u16 = Readable::read(r)?;
+ let addr_len: u16 = Readable::read(r)?;
+ let mut addresses: Vec<NetAddress> = Vec::with_capacity(4);
let mut addr_readpos = 0;
- let mut addresses = Vec::with_capacity(4);
- let mut f: u8 = 0;
- let mut excess = 0;
+ let mut excess = false;
+ let mut excess_byte = 0;
loop {
- if addrlen <= addr_readpos { break; }
- f = Readable::read(r)?;
- match f {
- 1 => {
- if addresses.len() > 0 {
- return Err(DecodeError::ExtraAddressesPerType);
- }
- if addrlen < addr_readpos + 1 + 6 {
- return Err(DecodeError::BadLengthDescriptor);
- }
- addresses.push(NetAddress::IPv4 {
- addr: {
- let mut addr = [0; 4];
- r.read_exact(&mut addr)?;
- addr
+ if addr_len <= addr_readpos { break; }
+ match Readable::read(r) {
+ Ok(Ok(addr)) => {
+ match addr {
+ NetAddress::IPv4 { .. } => {
+ if addresses.len() > 0 {
+ return Err(DecodeError::ExtraAddressesPerType);
+ }
},
- port: Readable::read(r)?,
- });
- addr_readpos += 1 + 6
- },
- 2 => {
- if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
- return Err(DecodeError::ExtraAddressesPerType);
- }
- if addrlen < addr_readpos + 1 + 18 {
- return Err(DecodeError::BadLengthDescriptor);
- }
- addresses.push(NetAddress::IPv6 {
- addr: {
- let mut addr = [0; 16];
- r.read_exact(&mut addr)?;
- addr
+ NetAddress::IPv6 { .. } => {
+ if addresses.len() > 1 || (addresses.len() == 1 && addresses[0].get_id() != 1) {
+ return Err(DecodeError::ExtraAddressesPerType);
+ }
},
- port: Readable::read(r)?,
- });
- addr_readpos += 1 + 18
- },
- 3 => {
- if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
- return Err(DecodeError::ExtraAddressesPerType);
- }
- if addrlen < addr_readpos + 1 + 12 {
- return Err(DecodeError::BadLengthDescriptor);
- }
- addresses.push(NetAddress::OnionV2 {
- addr: {
- let mut addr = [0; 10];
- r.read_exact(&mut addr)?;
- addr
+ NetAddress::OnionV2 { .. } => {
+ if addresses.len() > 2 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 2) {
+ return Err(DecodeError::ExtraAddressesPerType);
+ }
+ },
+ NetAddress::OnionV3 { .. } => {
+ if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
+ return Err(DecodeError::ExtraAddressesPerType);
+ }
},
- port: Readable::read(r)?,
- });
- addr_readpos += 1 + 12
- },
- 4 => {
- if addresses.len() > 3 || (addresses.len() > 0 && addresses.last().unwrap().get_id() > 3) {
- return Err(DecodeError::ExtraAddressesPerType);
}
- if addrlen < addr_readpos + 1 + 37 {
+ if addr_len < addr_readpos + 1 + addr.len() {
return Err(DecodeError::BadLengthDescriptor);
}
- addresses.push(NetAddress::OnionV3 {
- ed25519_pubkey: Readable::read(r)?,
- checksum: Readable::read(r)?,
- version: Readable::read(r)?,
- port: Readable::read(r)?,
- });
- addr_readpos += 1 + 37
+ addr_readpos += (1 + addr.len()) as u16;
+ addresses.push(addr);
},
- _ => { excess = 1; break; }
+ Ok(Err(unknown_descriptor)) => {
+ excess = true;
+ excess_byte = unknown_descriptor;
+ break;
+ },
+ Err(DecodeError::ShortRead) => return Err(DecodeError::BadLengthDescriptor),
+ Err(e) => return Err(e),
}
}
let mut excess_data = vec![];
- let excess_address_data = if addr_readpos < addrlen {
- let mut excess_address_data = vec![0; (addrlen - addr_readpos) as usize];
- r.read_exact(&mut excess_address_data[excess..])?;
- if excess == 1 {
- excess_address_data[0] = f;
+ let excess_address_data = if addr_readpos < addr_len {
+ let mut excess_address_data = vec![0; (addr_len - addr_readpos) as usize];
+ r.read_exact(&mut excess_address_data[if excess { 1 } else { 0 }..])?;
+ if excess {
+ excess_address_data[0] = excess_byte;
}
excess_address_data
} else {
- if excess == 1 {
- excess_data.push(f);
+ if excess {
+ excess_data.push(excess_byte);
}
Vec::new()
};
-
+ r.read_to_end(&mut excess_data)?;
Ok(UnsignedNodeAnnouncement {
- features: features,
- timestamp: timestamp,
- node_id: node_id,
- rgb: rgb,
- alias: alias,
- addresses: addresses,
- excess_address_data: excess_address_data,
- excess_data: {
- r.read_to_end(&mut excess_data)?;
- excess_data
- },
+ features,
+ timestamp,
+ node_id,
+ rgb,
+ alias,
+ addresses,
+ excess_address_data,
+ excess_data,
})
}
}
fn encoding_channel_reestablish_with_secret() {
let public_key = {
let secp_ctx = Secp256k1::new();
- PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&secp_ctx, &hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
+ PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap())
};
let cr = msgs::ChannelReestablish {