use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
use prelude::*;
-use core::{cmp, fmt};
+use core::fmt;
use core::fmt::Debug;
use io::{self, Read};
use io_extras::read_to_end;
pub struct Init {
/// The relevant features which the sender supports
pub features: InitFeatures,
+ /// The receipient's network address. This adds the option to report a remote IP address
+ /// back to a connecting peer using the init message. A node can decide to use that information
+ /// to discover a potential update to its public IPv4 address (NAT) and use
+ /// that for a node_announcement update message containing the new address.
+ pub remote_network_address: Option<NetAddress>,
}
/// An error message to be sent or received from a peer
#[derive(Clone, Debug, PartialEq)]
pub struct ErrorMessage {
- /// The channel ID involved in the error
+ /// The channel ID involved in the error.
+ ///
+ /// All-0s indicates a general error unrelated to a specific channel, after which all channels
+ /// with the sending peer should be closed.
pub channel_id: [u8; 32],
/// A possibly human-readable error description.
- /// The string should be sanitized before it is used (e.g. emitted to logs
- /// or printed to stdout). Otherwise, a well crafted error message may trigger a security
- /// vulnerability in the terminal emulator or the logging subsystem.
+ /// The string should be sanitized before it is used (e.g. emitted to logs or printed to
+ /// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in
+ /// the terminal emulator or the logging subsystem.
+ pub data: String,
+}
+
+/// A warning message to be sent or received from a peer
+#[derive(Clone, Debug, PartialEq)]
+pub struct WarningMessage {
+ /// The channel ID involved in the warning.
+ ///
+ /// All-0s indicates a warning unrelated to a specific channel.
+ pub channel_id: [u8; 32],
+ /// A possibly human-readable warning description.
+ /// The string should be sanitized before it is used (e.g. emitted to logs or printed to
+ /// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in
+ /// the terminal emulator or the logging subsystem.
pub data: String,
}
pub first_per_commitment_point: PublicKey,
/// Optionally, a request to pre-set the to-sender output's scriptPubkey for when we collaboratively close
pub shutdown_scriptpubkey: OptionalField<Script>,
+ /// The channel type that this channel will represent. If none is set, we derive the channel
+ /// type from the intersection of our feature bits with our counterparty's feature bits from
+ /// the Init message.
+ ///
+ /// This is required to match the equivalent field in [`OpenChannel::channel_type`].
+ pub channel_type: Option<ChannelTypeFeatures>,
}
/// A funding_created message to be sent or received from a peer
pub channel_id: [u8; 32],
/// The per-commitment point of the second commitment transaction
pub next_per_commitment_point: PublicKey,
+ /// If set, provides a short_channel_id alias for this channel. The sender will accept payments
+ /// to be forwarded over this SCID and forward them to this messages' recipient.
+ pub short_channel_id_alias: Option<u64>,
}
/// A shutdown message to be sent or received from a peer
/// The peer did something harmless that we weren't able to meaningfully process.
/// If the error is logged, log it at the given level.
IgnoreAndLog(logger::Level),
+ /// The peer provided us with a gossip message which we'd already seen. In most cases this
+ /// should be ignored, but it may result in the message being forwarded if it is a duplicate of
+ /// our own channel announcements.
+ IgnoreDuplicateGossip,
/// The peer did something incorrect. Tell them.
SendErrorMessage {
/// The message to send.
- msg: ErrorMessage
+ msg: ErrorMessage,
+ },
+ /// The peer did something incorrect. Tell them without closing any channels.
+ SendWarningMessage {
+ /// The message to send.
+ msg: WarningMessage,
+ /// The peer may have done something harmless that we weren't able to meaningfully process,
+ /// though we should still tell them about it.
+ /// If this event is logged, log it at the given level.
+ log_level: logger::Level,
},
}
/// Called when a connection is established with a peer. This can be used to
/// perform routing table synchronization using a strategy defined by the
/// implementor.
- fn sync_routing_table(&self, their_node_id: &PublicKey, init: &Init);
+ fn peer_connected(&self, their_node_id: &PublicKey, init: &Init);
/// Handles the reply of a query we initiated to learn about channels
/// for a given range of blocks. We can expect to receive one or more
/// replies to a single query.
pub(crate) pad: Vec<u8>,
}
}
-#[cfg(feature = "fuzztarget")]
+#[cfg(fuzzing)]
pub use self::fuzzy_internal_msgs::*;
-#[cfg(not(feature = "fuzztarget"))]
+#[cfg(not(fuzzing))]
pub(crate) use self::fuzzy_internal_msgs::*;
#[derive(Clone)]
DecodeError::InvalidValue => f.write_str("Nonsense bytes didn't map to the type they were interpreted as"),
DecodeError::ShortRead => f.write_str("Packet extended beyond the provided bytes"),
DecodeError::BadLengthDescriptor => f.write_str("A length descriptor in the packet didn't describe the later data correctly"),
- DecodeError::Io(ref e) => e.fmt(f),
+ DecodeError::Io(ref e) => fmt::Debug::fmt(e, f),
DecodeError::UnsupportedCompression => f.write_str("We don't support receiving messages with zlib-compressed fields"),
}
}
htlc_basepoint,
first_per_commitment_point,
shutdown_scriptpubkey
-}, {});
+}, {
+ (1, channel_type, option),
+});
impl_writeable_msg!(AnnouncementSignatures, {
channel_id,
impl_writeable_msg!(FundingLocked, {
channel_id,
next_per_commitment_point,
-}, {});
+}, {
+ (1, short_channel_id_alias, option),
+});
impl Writeable for Init {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
// global_features gets the bottom 13 bits of our features, and local_features gets all of
// our relevant feature bits. This keeps us compatible with old nodes.
self.features.write_up_to_13(w)?;
- self.features.write(w)
+ self.features.write(w)?;
+ encode_tlv_stream!(w, {
+ (3, self.remote_network_address, option)
+ });
+ Ok(())
}
}
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let global_features: InitFeatures = Readable::read(r)?;
let features: InitFeatures = Readable::read(r)?;
+ let mut remote_network_address: Option<NetAddress> = None;
+ decode_tlv_stream!(r, {
+ (3, remote_network_address, option)
+ });
Ok(Init {
features: features.or(global_features),
+ remote_network_address,
})
}
}
impl Writeable for OnionHopData {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
- // Note that this should never be reachable if Rust-Lightning generated the message, as we
- // check values are sane long before we get here, though its possible in the future
- // user-generated messages may hit this.
- if self.amt_to_forward > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
match self.format {
OnionHopDataFormat::Legacy { short_channel_id } => {
0u8.write(w)?;
});
},
OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => {
- if let Some(final_data) = payment_data {
- if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); }
- }
encode_varint_length_prefixed_tlv!(w, {
(2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required),
(4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required),
Ok(Self {
channel_id: Readable::read(r)?,
data: {
- let mut sz: usize = <u16 as Readable>::read(r)? as usize;
- let data = read_to_end(r)?;
- sz = cmp::min(data.len(), sz);
- match String::from_utf8(data[..sz as usize].to_vec()) {
+ let sz: usize = <u16 as Readable>::read(r)? as usize;
+ let mut data = Vec::with_capacity(sz);
+ data.resize(sz, 0);
+ r.read_exact(&mut data)?;
+ match String::from_utf8(data) {
+ Ok(s) => s,
+ Err(_) => return Err(DecodeError::InvalidValue),
+ }
+ }
+ })
+ }
+}
+
+impl Writeable for WarningMessage {
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+ self.channel_id.write(w)?;
+ (self.data.len() as u16).write(w)?;
+ w.write_all(self.data.as_bytes())?;
+ Ok(())
+ }
+}
+
+impl Readable for WarningMessage {
+ fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+ Ok(Self {
+ channel_id: Readable::read(r)?,
+ data: {
+ let sz: usize = <u16 as Readable>::read(r)? as usize;
+ let mut data = Vec::with_capacity(sz);
+ data.resize(sz, 0);
+ r.read_exact(&mut data)?;
+ match String::from_utf8(data) {
Ok(s) => s,
Err(_) => return Err(DecodeError::InvalidValue),
}
delayed_payment_basepoint: pubkey_4,
htlc_basepoint: pubkey_5,
first_per_commitment_point: pubkey_6,
- shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
+ shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: pubkey_1}, Network::Testnet).script_pubkey()) } else { OptionalField::Absent },
+ channel_type: None,
};
let encoded_value = accept_channel.encode();
let mut target_value = hex::decode("020202020202020202020202020202020202020202020202020202020202020212345678901234562334032891223698321446687011447600083a840000034d000c89d4c0bcc0bc031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f024d4b6cd1361032ca9bd2aeb9d900aa4d45d9ead80ac9423374c451a7254d076602531fe6068134503d2723133227c867ac8fa6c83c537e9a44c3c5bdbdcb1fe33703462779ad4aad39514614751a71085f2f10e1c7a593e4e030efb5b8721ce55b0b0362c0a046dacce86ddd0343c6d3c7c79c2208ba0d9c9cf24a6d046d21d21f90f703f006a18d5653c4edf5391ff23a61f03ff83d237e880ee61187fa9f379a028e0a").unwrap();
let funding_locked = msgs::FundingLocked {
channel_id: [2; 32],
next_per_commitment_point: pubkey_1,
+ short_channel_id_alias: None,
};
let encoded_value = funding_locked.encode();
let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f").unwrap();
fn encoding_init() {
assert_eq!(msgs::Init {
features: InitFeatures::from_le_bytes(vec![0xFF, 0xFF, 0xFF]),
+ remote_network_address: None,
}.encode(), hex::decode("00023fff0003ffffff").unwrap());
assert_eq!(msgs::Init {
features: InitFeatures::from_le_bytes(vec![0xFF]),
+ remote_network_address: None,
}.encode(), hex::decode("0001ff0001ff").unwrap());
assert_eq!(msgs::Init {
features: InitFeatures::from_le_bytes(vec![]),
+ remote_network_address: None,
}.encode(), hex::decode("00000000").unwrap());
+
+ let init_msg = msgs::Init { features: InitFeatures::from_le_bytes(vec![]),
+ remote_network_address: Some(msgs::NetAddress::IPv4 {
+ addr: [127, 0, 0, 1],
+ port: 1000,
+ }),
+ };
+ let encoded_value = init_msg.encode();
+ let target_value = hex::decode("000000000307017f00000103e8").unwrap();
+ assert_eq!(encoded_value, target_value);
+ assert_eq!(msgs::Init::read(&mut Cursor::new(&target_value)).unwrap(), init_msg);
}
#[test]
assert_eq!(encoded_value, target_value);
}
+ #[test]
+ fn encoding_warning() {
+ let error = msgs::WarningMessage {
+ channel_id: [2; 32],
+ data: String::from("rust-lightning"),
+ };
+ let encoded_value = error.encode();
+ let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
#[test]
fn encoding_ping() {
let ping = msgs::Ping {