From a6d693649985fee91e64875a34a96ac8e664ce61 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 24 Aug 2024 18:20:02 +0200 Subject: [PATCH] Exit loop when response arrived --- src/main.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0f30f83..6da3be7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,22 +92,30 @@ async fn main() -> Result<()>{ sock.send_to(&msg_buf, remote_addr).await .context("Failed to send DHCPv6 INFORMATION-REQUEST message")?; - loop { + let response_msg = loop { let mut recv_buf = [0; 1024]; - let (len, recv_addr) = sock.recv_from(&mut recv_buf).await + let (recv_len, recv_addr) = sock.recv_from(&mut recv_buf).await .context("Failed to receive DHCPv6 response")?; + + if log_enabled!(Level::Debug) { + debug!("{:?} bytes received from {:?}", recv_len, recv_addr); + debug!("Received packet: {}", hex::encode(&recv_buf[..recv_len])); + } + let recv_msg = Message::decode(&mut Decoder::new(&recv_buf)) .context("Unable to parse DHCPv6 response message")?; - if recv_msg.xid() == msg.xid() { - info!("{:?} bytes received from {:?}", len, recv_addr); - if log_enabled!(Level::Debug) { - debug!("Received packet: {}", hex::encode(&recv_buf[..len])); - } - println!("{}", &recv_msg); - break; + if log_enabled!(Level::Debug) { + debug!("Packet contains transaction id: 0x{}", hex::encode(&msg.xid())); } - } + + if recv_msg.xid() == msg.xid() { + info!("Found reponse with matching transaction id"); + break recv_msg; + } + }; + + println!("{}", &response_msg); Ok(()) }