diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-11-22 14:03:41 +0100 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-11-22 14:03:41 +0100 |
commit | 0f94a6118ce463192ef0bedfa11ed942c8d5686d (patch) | |
tree | 595026c77a8f115728eeabad2875c76255581a52 | |
parent | e810686e00d121a3ca71f141e9e297c9760f0fd7 (diff) |
add elapsed time option to transmissions
-rw-r--r-- | src/error.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 12 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/error.rs b/src/error.rs index ffb57fe..d3ed5d1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use std::{io, net, time}; +use std::{io, net, num, time}; use tokio::sync::watch; @@ -33,6 +33,8 @@ pub enum Error { Io(#[from] io::Error), #[error("system time monotonicity error: {0}")] SystemTime(#[from] time::SystemTimeError), + #[error("can't convert between integer sizes: {0}")] + TryFromInt(#[from] num::TryFromIntError), #[error("can't receive from tokio watch channel: {0}")] WatchRecv(#[from] watch::error::RecvError), diff --git a/src/main.rs b/src/main.rs index 8b949ab..707384e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,7 @@ struct Dhcp6 { lease: Option<PdConfig>, xid: [u8; 3], + xts: Instant, // Transaction timestamp. server_id: Vec<u8>, last_sent: Packet, iapd: IAPD, @@ -43,6 +44,7 @@ impl Dhcp6 { lease: lease.clone(), xid: [0; 3], + xts: Instant::now(), server_id: lease .clone() .map(|lease| lease.server_id) @@ -345,8 +347,14 @@ async fn send_dhcp6(dhcp6: &mut Dhcp6, sock: &UdpSocket, packet: Packet) { async fn do_send_dhcp6(dhcp6: &mut Dhcp6, sock: &UdpSocket, packet: Packet) -> Result<()> { if packet != dhcp6.last_sent { dhcp6.xid = rand::random(); + dhcp6.xts = Instant::now(); } + let elapsed = Instant::now() + .duration_since(dhcp6.xts) + .as_millis() + .try_into()?; + match packet { Packet::Solicit => { let mut solicit = Message::new_with_id(MessageType::Solicit, dhcp6.xid); @@ -362,6 +370,7 @@ async fn do_send_dhcp6(dhcp6: &mut Dhcp6, sock: &UdpSocket, packet: Packet) -> R OptionCode::DomainNameServers, ], })); + opts.insert(DhcpOption::ElapsedTime(elapsed)); opts.insert(DhcpOption::IAPD(IAPD { id: 1, t1: 0, @@ -407,6 +416,7 @@ async fn do_send_dhcp6(dhcp6: &mut Dhcp6, sock: &UdpSocket, packet: Packet) -> R OptionCode::DomainNameServers, ], })); + opts.insert(DhcpOption::ElapsedTime(elapsed)); opts.insert(DhcpOption::IAPD(IAPD { id: 1, t1: 0, @@ -452,6 +462,7 @@ async fn do_send_dhcp6(dhcp6: &mut Dhcp6, sock: &UdpSocket, packet: Packet) -> R OptionCode::DomainNameServers, ], })); + opts.insert(DhcpOption::ElapsedTime(elapsed)); opts.insert(DhcpOption::IAPD(IAPD { id: 1, t1: 0, @@ -496,6 +507,7 @@ async fn do_send_dhcp6(dhcp6: &mut Dhcp6, sock: &UdpSocket, packet: Packet) -> R OptionCode::DomainNameServers, ], })); + opts.insert(DhcpOption::ElapsedTime(elapsed)); opts.insert(DhcpOption::IAPD(IAPD { id: 1, t1: 0, |