Skip to content

Commit 05e2a7d

Browse files
committed
Support responding to Ping messages in peer_handler
1 parent 3377119 commit 05e2a7d

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/ln/msgs.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ pub struct Init {
132132
pub local_features: LocalFeatures,
133133
}
134134

135+
pub struct Ping {
136+
pub ponglen: u16,
137+
pub byteslen: u16,
138+
}
139+
140+
pub struct Pong {
141+
pub byteslen: u16,
142+
}
143+
135144
pub struct OpenChannel {
136145
pub chain_hash: Sha256dHash,
137146
pub temporary_channel_id: Uint256,
@@ -572,6 +581,54 @@ impl MsgEncodable for Init {
572581
}
573582
}
574583

584+
impl MsgDecodable for Ping {
585+
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
586+
if v.len() < 4 {
587+
return Err(DecodeError::WrongLength);
588+
}
589+
let ponglen = byte_utils::slice_to_be16(&v[0..2]);
590+
let byteslen = byte_utils::slice_to_be16(&v[2..4]);
591+
if v.len() < 4 + byteslen as usize {
592+
return Err(DecodeError::WrongLength);
593+
}
594+
Ok(Self {
595+
ponglen,
596+
byteslen,
597+
})
598+
}
599+
}
600+
impl MsgEncodable for Ping {
601+
fn encode(&self) -> Vec<u8> {
602+
let mut res = Vec::with_capacity(self.byteslen as usize + 2);
603+
res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
604+
res.resize(2 + self.byteslen as usize, 0);
605+
res
606+
}
607+
}
608+
609+
impl MsgDecodable for Pong {
610+
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
611+
if v.len() < 2 {
612+
return Err(DecodeError::WrongLength);
613+
}
614+
let byteslen = byte_utils::slice_to_be16(&v[0..2]);
615+
if v.len() < 2 + byteslen as usize {
616+
return Err(DecodeError::WrongLength);
617+
}
618+
Ok(Self {
619+
byteslen
620+
})
621+
}
622+
}
623+
impl MsgEncodable for Pong {
624+
fn encode(&self) -> Vec<u8> {
625+
let mut res = Vec::with_capacity(self.byteslen as usize + 2);
626+
res.extend_from_slice(&byte_utils::be16_to_array(self.byteslen));
627+
res.resize(2 + self.byteslen as usize, 0);
628+
res
629+
}
630+
}
631+
575632
impl MsgDecodable for OpenChannel {
576633
fn decode(v: &[u8]) -> Result<Self, DecodeError> {
577634
if v.len() < 2*32+6*8+4+2*2+6*33+1 {

src/ln/peer_handler.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,15 @@ impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
390390
17 => {
391391
// Error msg
392392
},
393-
18 => { }, // ping
394-
19 => { }, // pong
393+
394+
18 => {
395+
let msg = try_potential_decodeerror!(msgs::Ping::decode(&msg_data[2..]));
396+
let resp = msgs::Pong { byteslen: msg.ponglen };
397+
encode_and_send_msg!(resp, 19);
398+
},
399+
19 => {
400+
try_potential_decodeerror!(msgs::Pong::decode(&msg_data[2..]));
401+
},
395402

396403
// Channel control:
397404
32 => {

0 commit comments

Comments
 (0)