Update locking

This commit is contained in:
Jett Chen 2024-01-25 01:07:40 +08:00 committed by Conrad Kramer
parent 0b46ac57b7
commit 079c4f676d

View file

@ -1,10 +1,11 @@
use std::{net::SocketAddr, sync::Arc}; use std::{net::SocketAddr, sync::Arc};
use std::time::Duration;
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use fehler::throws; use fehler::throws;
use ip_network::IpNetwork; use ip_network::IpNetwork;
use rand::random; use rand::random;
use tokio::{net::UdpSocket, sync::RwLock, task::JoinHandle}; use tokio::{net::UdpSocket, sync::RwLock, task::JoinHandle, time::timeout};
use tokio::io::AsyncWrite; use tokio::io::AsyncWrite;
use tun::tokio::TunInterface; use tun::tokio::TunInterface;
use crate::wireguard::noise::errors::WireGuardError; use crate::wireguard::noise::errors::WireGuardError;
@ -94,7 +95,7 @@ impl PeerPcb {
TunnResult::WriteToNetwork(packet) => { TunnResult::WriteToNetwork(packet) => {
tracing::debug!("WriteToNetwork: {:?}", packet); tracing::debug!("WriteToNetwork: {:?}", packet);
self.open_if_closed().await?; self.open_if_closed().await?;
socket.send(packet).await?; self.socket.read().await.as_ref().unwrap().send(packet).await?;
tracing::debug!("WriteToNetwork done"); tracing::debug!("WriteToNetwork done");
res_dat = &[]; res_dat = &[];
continue continue
@ -140,12 +141,9 @@ impl PeerPcb {
} }
pub async fn update_timers(&self, dst: &mut [u8]) -> Result<(), Error> { pub async fn update_timers(&self, dst: &mut [u8]) -> Result<(), Error> {
tracing::debug!("update timers called...");
match self.tunnel.write().await.update_timers(dst) { match self.tunnel.write().await.update_timers(dst) {
TunnResult::Done => {} TunnResult::Done => {}
TunnResult::Err(WireGuardError::ConnectionExpired) => { TunnResult::Err(WireGuardError::ConnectionExpired) => {
tracing::debug!("Connection expired, closing socket");
self.socket.write().await.take();
} }
TunnResult::Err(e) => { TunnResult::Err(e) => {
tracing::error!(message = "Update timers error", error = ?e) tracing::error!(message = "Update timers error", error = ?e)
@ -163,7 +161,6 @@ impl PeerPcb {
} }
_ => panic!("Unexpected result from update_timers"), _ => panic!("Unexpected result from update_timers"),
}; };
tracing::debug!("update timers exit...");
Ok(()) Ok(())
} }