]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Support responding to Ping messages in peer_handler
authorMatt Corallo <git@bluematt.me>
Sat, 16 Jun 2018 23:39:21 +0000 (19:39 -0400)
committerMatt Corallo <git@bluematt.me>
Sat, 16 Jun 2018 23:46:49 +0000 (19:46 -0400)
src/ln/msgs.rs
src/ln/peer_handler.rs

index f0f1d4890e13370f83cc3c8c633da57f7b03ce20..869b5115f2ab5362a62b197d63ff684bc3ff3909 100644 (file)
@@ -132,6 +132,15 @@ pub struct Init {
        pub local_features: LocalFeatures,
 }
 
+pub struct Ping {
+       pub ponglen: u16,
+       pub byteslen: u16,
+}
+
+pub struct Pong {
+       pub byteslen: u16,
+}
+
 pub struct OpenChannel {
        pub chain_hash: Sha256dHash,
        pub temporary_channel_id: Uint256,
@@ -572,6 +581,54 @@ impl MsgEncodable for Init {
        }
 }
 
+impl MsgDecodable for Ping {
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 4 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let ponglen = byte_utils::slice_to_be16(&v[0..2]);
+               let byteslen = byte_utils::slice_to_be16(&v[2..4]);
+               if v.len() < 4 + byteslen as usize {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self {
+                       ponglen,
+                       byteslen,
+               })
+       }
+}
+impl MsgEncodable for Ping {
+       fn encode(&self) -> Vec<u8> {
+               let mut res = Vec::with_capacity(self.byteslen as usize + 2);
+               res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
+               res.resize(2 + self.byteslen as usize, 0);
+               res
+       }
+}
+
+impl MsgDecodable for Pong {
+       fn decode(v: &[u8]) -> Result<Self, DecodeError> {
+               if v.len() < 2 {
+                       return Err(DecodeError::WrongLength);
+               }
+               let byteslen = byte_utils::slice_to_be16(&v[0..2]);
+               if v.len() < 2 + byteslen as usize {
+                       return Err(DecodeError::WrongLength);
+               }
+               Ok(Self {
+                       byteslen
+               })
+       }
+}
+impl MsgEncodable for Pong {
+       fn encode(&self) -> Vec<u8> {
+               let mut res = Vec::with_capacity(self.byteslen as usize + 2);
+               res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
+               res.resize(2 + self.byteslen as usize, 0);
+               res
+       }
+}
+
 impl MsgDecodable for OpenChannel {
        fn decode(v: &[u8]) -> Result<Self, DecodeError> {
                if v.len() < 2*32+6*8+4+2*2+6*33+1 {
index 3c4196519c5cca1700e8c008e2af97f5d76cb81d..30900cab499a8407fb4c7029c76a2de75517276d 100644 (file)
@@ -390,8 +390,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
                                                                                        17 => {
                                                                                                // Error msg
                                                                                        },
-                                                                                       18 => { }, // ping
-                                                                                       19 => { }, // pong
+
+                                                                                       18 => {
+                                                                                               let msg = try_potential_decodeerror!(msgs::Ping::decode(&msg_data[2..]));
+                                                                                               let resp = msgs::Pong { byteslen: msg.ponglen };
+                                                                                               encode_and_send_msg!(resp, 19);
+                                                                                       },
+                                                                                       19 => {
+                                                                                               try_potential_decodeerror!(msgs::Pong::decode(&msg_data[2..]));
+                                                                                       },
 
                                                                                        // Channel control:
                                                                                        32 => {