Exit loop when response arrived

This commit is contained in:
clerie 2024-08-24 18:20:02 +02:00
parent 1ea4715b59
commit a6d6936499

View File

@ -92,22 +92,30 @@ async fn main() -> Result<()>{
sock.send_to(&msg_buf, remote_addr).await sock.send_to(&msg_buf, remote_addr).await
.context("Failed to send DHCPv6 INFORMATION-REQUEST message")?; .context("Failed to send DHCPv6 INFORMATION-REQUEST message")?;
loop { let response_msg = loop {
let mut recv_buf = [0; 1024]; 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")?; .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)) let recv_msg = Message::decode(&mut Decoder::new(&recv_buf))
.context("Unable to parse DHCPv6 response message")?; .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) { if log_enabled!(Level::Debug) {
debug!("Received packet: {}", hex::encode(&recv_buf[..len])); debug!("Packet contains transaction id: 0x{}", hex::encode(&msg.xid()));
}
println!("{}", &recv_msg);
break;
} }
if recv_msg.xid() == msg.xid() {
info!("Found reponse with matching transaction id");
break recv_msg;
} }
};
println!("{}", &response_msg);
Ok(()) Ok(())
} }