@@ -80,12 +80,29 @@ pub struct Init {
8080/// An error message to be sent or received from a peer
8181#[ derive( Clone , Debug , PartialEq ) ]
8282pub 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+
15181561impl 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