use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tracing::{debug, error};
use crate::api::common::error::SecurityError;
use crate::api::server::security::SocketHandle;
use crate::dtls::transport::udp::UdpTransport;
pub async fn create_udp_transport(
socket: &SocketHandle,
mtu: usize,
) -> Result<UdpTransport, SecurityError> {
debug!("Creating UDP transport for DTLS");
match UdpTransport::new(socket.socket.clone(), mtu).await {
Ok(mut transport) => {
if let Err(e) = transport.start().await {
return Err(SecurityError::Configuration(format!(
"Failed to start DTLS transport: {}",
e
)));
}
debug!("DTLS transport started successfully");
Ok(transport)
}
Err(e) => Err(SecurityError::Configuration(format!(
"Failed to create DTLS transport: {}",
e
))),
}
}
pub async fn start_packet_handler(
socket: &SocketHandle,
handler: impl Fn(Vec<u8>, SocketAddr) -> Result<(), SecurityError> + Send + Sync + 'static,
) -> Result<(), SecurityError> {
debug!("Starting automatic DTLS packet handler task");
let socket_clone = socket.socket.clone();
tokio::spawn(async move {
let transport = match UdpTransport::new(socket_clone.clone(), 1500).await {
Ok(mut t) => {
if let Err(e) = t.start().await {
error!("Failed to start server transport for packet handler: {}", e);
return;
}
debug!("Server packet handler transport started successfully");
Arc::new(Mutex::new(t))
}
Err(e) => {
error!(
"Failed to create server transport for packet handler: {}",
e
);
return;
}
};
loop {
let receive_result = transport.lock().await.recv().await;
match receive_result {
Some((data, addr)) => {
debug!("Server received {} bytes from {}", data.len(), addr);
match handler(data.to_vec(), addr) {
Ok(_) => debug!("Server successfully processed client DTLS packet"),
Err(e) => debug!("Error processing client DTLS packet: {:?}", e),
}
}
None => {
tokio::time::sleep(Duration::from_millis(10)).await;
}
}
}
});
Ok(())
}
pub async fn capture_initial_packet(
socket: &SocketHandle,
timeout_secs: u64,
) -> Result<Option<(Vec<u8>, SocketAddr)>, SecurityError> {
debug!(
"Attempting to capture initial client packet with timeout of {} seconds",
timeout_secs
);
let mut buffer = vec![0u8; 2048];
match tokio::time::timeout(
Duration::from_secs(timeout_secs),
socket.socket.recv_from(&mut buffer),
)
.await
{
Ok(Ok((size, addr))) => {
debug!("Captured initial packet: {} bytes from {}", size, addr);
Ok(Some((buffer[..size].to_vec(), addr)))
}
Ok(Err(e)) => Err(SecurityError::HandshakeError(format!(
"Error receiving initial packet: {}",
e
))),
Err(_) => {
debug!("Timeout occurred while waiting for initial packet");
Ok(None)
}
}
}
pub async fn start_udp_transport(transport: &mut UdpTransport) -> Result<(), SecurityError> {
debug!("Starting UDP transport");
match transport.start().await {
Ok(_) => {
debug!("UDP transport started successfully");
Ok(())
}
Err(e) => Err(SecurityError::Configuration(format!(
"Failed to start DTLS transport: {}",
e
))),
}
}