From: Matt Corallo Date: Thu, 22 Jul 2021 15:25:13 +0000 (+0000) Subject: Add a new `WarningMessage` message to send and receive warnings X-Git-Tag: v0.0.105~41^2~5 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=f676f5585f46eefbababdc19a5a90d5e0c77f0ca;p=rust-lightning Add a new `WarningMessage` message to send and receive warnings --- diff --git a/fuzz/src/msg_targets/gen_target.sh b/fuzz/src/msg_targets/gen_target.sh index 2e1334841..0e930cfa1 100755 --- a/fuzz/src/msg_targets/gen_target.sh +++ b/fuzz/src/msg_targets/gen_target.sh @@ -43,4 +43,5 @@ GEN_TEST QueryShortChannelIds test_msg "" GEN_TEST ReplyChannelRange test_msg "" GEN_TEST ErrorMessage test_msg_hole ", 32, 2" +GEN_TEST WarningMessage test_msg_hole ", 32, 2" GEN_TEST ChannelUpdate test_msg_hole ", 108, 1" diff --git a/fuzz/src/msg_targets/mod.rs b/fuzz/src/msg_targets/mod.rs index af70c58e3..8acb690b0 100644 --- a/fuzz/src/msg_targets/mod.rs +++ b/fuzz/src/msg_targets/mod.rs @@ -28,4 +28,5 @@ pub mod msg_node_announcement; pub mod msg_query_short_channel_ids; pub mod msg_reply_channel_range; pub mod msg_error_message; +pub mod msg_warning_message; pub mod msg_channel_update; diff --git a/fuzz/src/msg_targets/msg_warning_message.rs b/fuzz/src/msg_targets/msg_warning_message.rs new file mode 100644 index 000000000..f033b6fd6 --- /dev/null +++ b/fuzz/src/msg_targets/msg_warning_message.rs @@ -0,0 +1,27 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +// This file is auto-generated by gen_target.sh based on msg_target_template.txt +// To modify it, modify msg_target_template.txt and run gen_target.sh instead. + +use lightning::ln::msgs; + +use msg_targets::utils::VecWriter; +use utils::test_logger; + +#[inline] +pub fn msg_warning_message_test(data: &[u8], _out: Out) { + test_msg_hole!(msgs::WarningMessage, data, 32, 2); +} + +#[no_mangle] +pub extern "C" fn msg_warning_message_run(data: *const u8, datalen: usize) { + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_hole!(msgs::WarningMessage, data, 32, 2); +} diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 39c00105f..1335a04c4 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -80,12 +80,29 @@ pub struct Init { /// 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, } @@ -1515,6 +1532,32 @@ impl Readable for ErrorMessage { } } +impl Writeable for WarningMessage { + fn write(&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: &mut R) -> Result { + Ok(Self { + channel_id: Readable::read(r)?, + data: { + let mut sz: usize = ::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()) { + Ok(s) => s, + Err(_) => return Err(DecodeError::InvalidValue), + } + } + }) + } +} + impl Writeable for UnsignedNodeAnnouncement { fn write(&self, w: &mut W) -> Result<(), io::Error> { self.features.write(w)?; @@ -2405,6 +2448,17 @@ mod tests { 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 {