Skip to content

Commit f676f55

Browse files
committed
Add a new WarningMessage message to send and receive warnings
1 parent 036ea11 commit f676f55

File tree

4 files changed

+87
-4
lines changed

4 files changed

+87
-4
lines changed

fuzz/src/msg_targets/gen_target.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ GEN_TEST QueryShortChannelIds test_msg ""
4343
GEN_TEST ReplyChannelRange test_msg ""
4444

4545
GEN_TEST ErrorMessage test_msg_hole ", 32, 2"
46+
GEN_TEST WarningMessage test_msg_hole ", 32, 2"
4647
GEN_TEST ChannelUpdate test_msg_hole ", 108, 1"

fuzz/src/msg_targets/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ pub mod msg_node_announcement;
2828
pub mod msg_query_short_channel_ids;
2929
pub mod msg_reply_channel_range;
3030
pub mod msg_error_message;
31+
pub mod msg_warning_message;
3132
pub mod msg_channel_update;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
// This file is auto-generated by gen_target.sh based on msg_target_template.txt
11+
// To modify it, modify msg_target_template.txt and run gen_target.sh instead.
12+
13+
use lightning::ln::msgs;
14+
15+
use msg_targets::utils::VecWriter;
16+
use utils::test_logger;
17+
18+
#[inline]
19+
pub fn msg_warning_message_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
20+
test_msg_hole!(msgs::WarningMessage, data, 32, 2);
21+
}
22+
23+
#[no_mangle]
24+
pub extern "C" fn msg_warning_message_run(data: *const u8, datalen: usize) {
25+
let data = unsafe { std::slice::from_raw_parts(data, datalen) };
26+
test_msg_hole!(msgs::WarningMessage, data, 32, 2);
27+
}

lightning/src/ln/msgs.rs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,29 @@ pub struct Init {
8080
/// An error message to be sent or received from a peer
8181
#[derive(Clone, Debug, PartialEq)]
8282
pub struct ErrorMessage {
83-
/// The channel ID involved in the error
83+
/// The channel ID involved in the error.
84+
///
85+
/// All-0s indicates a general error unrelated to a specific channel, after which all channels
86+
/// with the sending peer should be closed.
8487
pub channel_id: [u8; 32],
8588
/// A possibly human-readable error description.
86-
/// The string should be sanitized before it is used (e.g. emitted to logs
87-
/// or printed to stdout). Otherwise, a well crafted error message may trigger a security
88-
/// vulnerability in the terminal emulator or the logging subsystem.
89+
/// The string should be sanitized before it is used (e.g. emitted to logs or printed to
90+
/// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in
91+
/// the terminal emulator or the logging subsystem.
92+
pub data: String,
93+
}
94+
95+
/// A warning message to be sent or received from a peer
96+
#[derive(Clone, Debug, PartialEq)]
97+
pub struct WarningMessage {
98+
/// The channel ID involved in the warning.
99+
///
100+
/// All-0s indicates a warning unrelated to a specific channel.
101+
pub channel_id: [u8; 32],
102+
/// A possibly human-readable warning description.
103+
/// The string should be sanitized before it is used (e.g. emitted to logs or printed to
104+
/// stdout). Otherwise, a well crafted error message may trigger a security vulnerability in
105+
/// the terminal emulator or the logging subsystem.
89106
pub data: String,
90107
}
91108

@@ -1515,6 +1532,32 @@ impl Readable for ErrorMessage {
15151532
}
15161533
}
15171534

1535+
impl Writeable for WarningMessage {
1536+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
1537+
self.channel_id.write(w)?;
1538+
(self.data.len() as u16).write(w)?;
1539+
w.write_all(self.data.as_bytes())?;
1540+
Ok(())
1541+
}
1542+
}
1543+
1544+
impl Readable for WarningMessage {
1545+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1546+
Ok(Self {
1547+
channel_id: Readable::read(r)?,
1548+
data: {
1549+
let mut sz: usize = <u16 as Readable>::read(r)? as usize;
1550+
let data = read_to_end(r)?;
1551+
sz = cmp::min(data.len(), sz);
1552+
match String::from_utf8(data[..sz as usize].to_vec()) {
1553+
Ok(s) => s,
1554+
Err(_) => return Err(DecodeError::InvalidValue),
1555+
}
1556+
}
1557+
})
1558+
}
1559+
}
1560+
15181561
impl Writeable for UnsignedNodeAnnouncement {
15191562
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
15201563
self.features.write(w)?;
@@ -2405,6 +2448,17 @@ mod tests {
24052448
assert_eq!(encoded_value, target_value);
24062449
}
24072450

2451+
#[test]
2452+
fn encoding_warning() {
2453+
let error = msgs::WarningMessage {
2454+
channel_id: [2; 32],
2455+
data: String::from("rust-lightning"),
2456+
};
2457+
let encoded_value = error.encode();
2458+
let target_value = hex::decode("0202020202020202020202020202020202020202020202020202020202020202000e727573742d6c696768746e696e67").unwrap();
2459+
assert_eq!(encoded_value, target_value);
2460+
}
2461+
24082462
#[test]
24092463
fn encoding_ping() {
24102464
let ping = msgs::Ping {

0 commit comments

Comments
 (0)