aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeerserverDE <himbeerserverde@gmail.com>2023-03-24 16:55:34 +0100
committerHimbeerserverDE <himbeerserverde@gmail.com>2023-03-24 16:55:34 +0100
commit371f8bdcaeefe35944c427cd14561b7cdcb2095d (patch)
tree84e186f71dedf1e575b3ac412f34ea14ca5fdd5a
parent0ae3d73709ac6fb7d15686310fbaf71fb00464d5 (diff)
fix delayed packet transmission when lost connection is reconnected
-rw-r--r--src/client.rs39
-rw-r--r--src/error.rs2
2 files changed, 36 insertions, 5 deletions
diff --git a/src/client.rs b/src/client.rs
index 1a0ac03..6328889 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -62,6 +62,7 @@ impl Client {
magic_number: rand::random(),
error: String::new(),
ip_config: IpConfig::default(),
+ connected: false,
})),
})
}
@@ -81,10 +82,18 @@ impl Client {
thread::spawn(move || {
for buf in &*ip_rx.lock().unwrap() {
match buf {
- Some(buf) => match self.send_ipv4(&buf) {
- Ok(_) => {}
- Err(e) => println!("ip transmission failed: {}", e),
- },
+ Some(buf) => {
+ while let Err(e) = self.send_ipv4(&buf) {
+ match e {
+ Error::NoSession => {}
+ Error::Disconnected => {}
+ _ => {
+ println!("ip transmission failed: {}", e);
+ break;
+ }
+ }
+ }
+ }
None => return,
}
}
@@ -180,6 +189,14 @@ impl Client {
self.inner.write().unwrap().ip_config = ip_config;
}
+ pub fn is_connected(&self) -> bool {
+ self.inner.read().unwrap().connected
+ }
+
+ fn set_connected(&self, connected: bool) {
+ self.inner.write().unwrap().connected = connected;
+ }
+
fn new_discovery_packet(&self, buf: &mut [u8]) -> Result<()> {
let local_mac = self.inner.read().unwrap().socket.mac_address();
@@ -231,12 +248,16 @@ impl Client {
self.new_ppp_packet(Protocol::Ipv4, buf)
}
- pub fn send_ipv4(&self, buf: &[u8]) -> Result<()> {
+ fn send_ipv4(&self, buf: &[u8]) -> Result<()> {
match self.state() {
State::Session(_) => {}
_ => return Err(Error::NoSession),
}
+ if !self.is_connected() {
+ return Err(Error::Disconnected);
+ }
+
let mut dgram = Vec::new();
dgram.resize(14 + 6 + 2 + buf.len(), 0);
@@ -695,6 +716,11 @@ impl Client {
tx.send(ip_config)?;
println!("ip configuration acknowledged by peer, options: {:?}", opts);
+
+ // Wait, otherwise the peer may discard our data packets.
+ thread::sleep(Duration::from_secs(1));
+ self.set_connected(true);
+
println!(
"ip session opened, addr={}, rtr={}, dns1={}, dns2={}",
ip_config.addr, ip_config.rtr, ip_config.dns1, ip_config.dns2
@@ -852,6 +878,8 @@ impl Client {
}
if self.state() == State::Terminated {
+ self.set_connected(false);
+
self.inner.write().unwrap().socket.close();
let why = self.why_terminated();
@@ -878,4 +906,5 @@ struct ClientRef {
magic_number: u32,
error: String,
ip_config: IpConfig,
+ connected: bool,
}
diff --git a/src/error.rs b/src/error.rs
index f9587f4..b45ec93 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -45,6 +45,8 @@ pub enum Error {
MissingPrimaryDns,
#[error("peer did not send us a secondary DNS server")]
MissingSecondaryDns,
+ #[error("no ip connection")]
+ Disconnected,
#[error("io error")]
Io(#[from] io::Error),
#[error("failed to convert string from UTF-8")]