2025-06-10 06:12:18 -06:00
|
|
|
use std::sync::{
|
|
|
|
|
Arc,
|
|
|
|
|
atomic::{AtomicBool, Ordering},
|
2025-06-09 12:37:49 -06:00
|
|
|
};
|
|
|
|
|
|
2025-06-10 06:12:18 -06:00
|
|
|
use crate::networkers::{Connection, ProtocolLayer};
|
|
|
|
|
|
2025-06-09 12:37:49 -06:00
|
|
|
type Error = Box<dyn std::error::Error>;
|
|
|
|
|
|
|
|
|
|
// 4-Way Handshake Layer
|
2025-06-10 06:12:18 -06:00
|
|
|
pub struct HandshakeLayer {
|
|
|
|
|
inner: Box<dyn Connection>,
|
|
|
|
|
finished_handshake: Arc<AtomicBool>,
|
2025-06-09 12:37:49 -06:00
|
|
|
}
|
|
|
|
|
|
2025-06-10 06:12:18 -06:00
|
|
|
impl Connection for HandshakeLayer {
|
2025-06-09 12:37:49 -06:00
|
|
|
fn get_info(&self) -> String {
|
|
|
|
|
format!("handshake->{}", self.inner.get_info())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_alive(&self) -> bool {
|
|
|
|
|
self.inner.is_alive()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 05:44:54 -06:00
|
|
|
fn read(&mut self) -> Result<Vec<u8>, Error> {
|
2025-06-10 06:12:18 -06:00
|
|
|
if !self.finished_handshake.load(Ordering::Relaxed) {
|
2025-06-09 12:37:49 -06:00
|
|
|
return Err("NotComplete".into());
|
|
|
|
|
}
|
|
|
|
|
self.inner.read()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 05:44:54 -06:00
|
|
|
fn write(&mut self, data: &[u8]) -> Result<(), Error> {
|
2025-06-10 06:12:18 -06:00
|
|
|
if !self.finished_handshake.load(Ordering::Relaxed) {
|
2025-06-09 12:37:49 -06:00
|
|
|
return Err("NotComplete".into());
|
|
|
|
|
}
|
|
|
|
|
self.inner.write(data)
|
|
|
|
|
}
|
2025-06-10 06:12:18 -06:00
|
|
|
|
|
|
|
|
fn try_clone(&self) -> Result<Box<dyn Connection + Send + Sync>, crate::Error> {
|
|
|
|
|
Ok(Box::new(Self {
|
|
|
|
|
inner: self.inner.try_clone()?,
|
|
|
|
|
finished_handshake: Arc::clone(&self.finished_handshake.clone()),
|
|
|
|
|
}))
|
|
|
|
|
}
|
2025-06-09 12:37:49 -06:00
|
|
|
}
|
|
|
|
|
|
2025-06-10 06:12:18 -06:00
|
|
|
impl ProtocolLayer for HandshakeLayer {
|
|
|
|
|
fn new(inner: Box<dyn Connection>) -> Result<Self, Error> {
|
2025-06-09 12:37:49 -06:00
|
|
|
Ok(HandshakeLayer {
|
|
|
|
|
inner,
|
2025-06-10 06:12:18 -06:00
|
|
|
finished_handshake: Arc::new(AtomicBool::new(false)),
|
2025-06-09 12:37:49 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn initialize_client(&mut self) -> Result<(), Error> {
|
|
|
|
|
// Step 1: Client sends SYN
|
2025-06-12 05:44:54 -06:00
|
|
|
self.inner.write("SYN".as_bytes())?;
|
2025-06-09 12:37:49 -06:00
|
|
|
|
|
|
|
|
// Step 2: Client receives SYN-ACK
|
|
|
|
|
let response = self.inner.read()?;
|
2025-06-12 05:44:54 -06:00
|
|
|
if response != "SYN-ACK".as_bytes() {
|
|
|
|
|
return Err(format!("Expected SYN-ACK, got: {:?}", response).into());
|
2025-06-09 12:37:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: Client sends ACK
|
2025-06-12 05:44:54 -06:00
|
|
|
self.inner.write("ACK".as_bytes())?;
|
2025-06-09 12:37:49 -06:00
|
|
|
|
|
|
|
|
// Step 4: Client receives FIN (final confirmation)
|
|
|
|
|
let response = self.inner.read()?;
|
2025-06-12 05:44:54 -06:00
|
|
|
if response != "FIN".as_bytes() {
|
|
|
|
|
return Err(format!("Expected FIN, got: {:?}", response).into());
|
2025-06-09 12:37:49 -06:00
|
|
|
}
|
|
|
|
|
|
2025-06-10 06:12:18 -06:00
|
|
|
info!("Handshake complete!");
|
|
|
|
|
|
|
|
|
|
self.finished_handshake.swap(true, Ordering::Relaxed);
|
2025-06-09 12:37:49 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn initialize_server(&mut self) -> Result<(), Error> {
|
|
|
|
|
// Step 1: Server receives SYN
|
|
|
|
|
let request = self.inner.read()?;
|
2025-06-12 05:44:54 -06:00
|
|
|
if request != "SYN".as_bytes() {
|
|
|
|
|
return Err(format!("Expected SYN, got: {:?}", request).into());
|
2025-06-09 12:37:49 -06:00
|
|
|
}
|
|
|
|
|
// Step 2: Server sends SYN-ACK
|
2025-06-12 05:44:54 -06:00
|
|
|
self.inner.write("SYN-ACK".as_bytes())?;
|
2025-06-09 12:37:49 -06:00
|
|
|
|
|
|
|
|
// Step 3: Server receives ACK
|
|
|
|
|
let response = self.inner.read()?;
|
2025-06-12 05:44:54 -06:00
|
|
|
if response != "ACK".as_bytes() {
|
|
|
|
|
return Err(format!("Expected ACK, got: {:?}", response).into());
|
2025-06-09 12:37:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 4: Server sends FIN (final confirmation)
|
2025-06-12 05:44:54 -06:00
|
|
|
self.inner.write("FIN".as_bytes())?;
|
2025-06-10 06:12:18 -06:00
|
|
|
info!("Handshake complete!");
|
2025-06-09 12:37:49 -06:00
|
|
|
|
2025-06-10 06:12:18 -06:00
|
|
|
self.finished_handshake.swap(true, Ordering::Relaxed);
|
2025-06-09 12:37:49 -06:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|