diff --git a/src/client/main.rs b/src/client/main.rs index d755e3d..00e57eb 100644 --- a/src/client/main.rs +++ b/src/client/main.rs @@ -1,6 +1,6 @@ mod net; mod types; -use pea_2_pea::{shared::crypto::derive_key_from_password, *}; +use pea_2_pea::*; use rand::RngCore; use std::{net::UdpSocket, process::exit, time::Duration}; @@ -104,6 +104,7 @@ fn main() -> std::io::Result<()> { &cli.password, ) { Ok(n) => { + eprintln!("Network exists joining it"); let _ = net::send_heartbeat( &mut buf, &server_SocketAddr, @@ -115,6 +116,7 @@ fn main() -> std::io::Result<()> { n } Err(e) if e.kind() == ServerResponse::ID_DOESNT_EXIST => { + eprintln!("Network does not exist creating it!"); let tmp_v_net: Network = Network::new( match cli.password { Some(_) => true, diff --git a/src/client/net.rs b/src/client/net.rs index 01fefa5..f050fa8 100644 --- a/src/client/net.rs +++ b/src/client/net.rs @@ -13,7 +13,7 @@ pub fn send_and_recv_with_retry( buf: &mut [u8; BUFFER_SIZE], send_buf: &[u8], dst: &SocketAddr, - socket: UdpSocket, + socket: &UdpSocket, retry_max: usize, ) -> Result<(usize, usize), ServerErrorResponses> { let mut retry_count: usize = 0; @@ -79,7 +79,7 @@ pub fn send_and_recv_with_retry( pub fn query_request( buf: &mut [u8; BUFFER_SIZE], dst: &SocketAddr, - socket: UdpSocket, + socket: &UdpSocket, ) -> Result { match send_and_recv_with_retry( buf, @@ -104,11 +104,11 @@ pub fn query_request( pub fn register_request( buf: &mut [u8; BUFFER_SIZE], dst: &SocketAddr, - socket: UdpSocket, + socket: &UdpSocket, network: &types::Network, public_sock_addr: &Box<[u8]>, - iv: [u8; SALT_AND_IV_SIZE as usize], -) -> Result<([u8; SALT_AND_IV_SIZE as usize], usize), ServerErrorResponses> { + iv: &[u8; SALT_AND_IV_SIZE as usize], +) -> Result { let mut send_buf: Box<[u8]> = vec![ 0u8; RegisterRequestDataPositions::DATA as usize @@ -127,7 +127,7 @@ pub fn register_request( send_buf[RegisterRequestDataPositions::IV as usize ..RegisterRequestDataPositions::IV as usize + SALT_AND_IV_SIZE as usize] - .copy_from_slice(&iv); // copy iv ad salt do the request + .copy_from_slice(iv); // copy iv ad salt do the request send_buf[RegisterRequestDataPositions::SALT as usize ..RegisterRequestDataPositions::SALT as usize + SALT_AND_IV_SIZE as usize] .copy_from_slice(&network.salt); @@ -141,7 +141,7 @@ pub fn register_request( .copy_from_slice(&public_sock_addr); match send_and_recv_with_retry(buf, &send_buf, dst, socket, STANDARD_RETRY_MAX) { - Ok((data_lenght, _)) => return Ok((iv, data_lenght)), + Ok((data_lenght, _)) => return Ok(data_lenght), Err(e) => return Err(e), } } @@ -149,9 +149,9 @@ pub fn register_request( pub fn get_request( buf: &mut [u8; BUFFER_SIZE], dst: &SocketAddr, - socket: UdpSocket, - network_id: String, - password: Option, + socket: &UdpSocket, + network_id: &String, + password: &Option, ) -> Result { let mut send_buf: Box<[u8]> = vec![0u8; GetRequestDataPositions::ID as usize + network_id.len()].into_boxed_slice(); @@ -269,13 +269,19 @@ pub fn get_request( offset += SALT_AND_IV_SIZE as usize + sock_addr_len as usize; } - return Ok(types::Network::new(encrypted, key, network_id, salt, peers)); + return Ok(types::Network::new( + encrypted, + key, + network_id.to_string(), + salt, + peers, + )); } pub fn send_heartbeat( buf: &mut [u8; BUFFER_SIZE], dst: &SocketAddr, - socket: UdpSocket, + socket: &UdpSocket, network: &types::Network, my_public_sock_addr: &Box<[u8]>, iv: &[u8; SALT_AND_IV_SIZE as usize], diff --git a/src/client/types.rs b/src/client/types.rs new file mode 100644 index 0000000..41a4cc0 --- /dev/null +++ b/src/client/types.rs @@ -0,0 +1,32 @@ +use pea_2_pea::*; +#[readonly::make] +pub struct Network { + #[readonly] + pub encrypted: bool, + #[readonly] + pub key: [u8; 32], + #[readonly] + pub net_id: String, + #[readonly] + pub salt: [u8; SALT_AND_IV_SIZE as usize], + #[readonly] + pub peers: Vec, +} + +impl Network { + pub fn new( + encrypted: bool, + key: [u8; 32], + net_id: String, + salt: [u8; SALT_AND_IV_SIZE as usize], + peers: Vec, + ) -> Self { + Network { + encrypted, + key, + net_id, + salt, + peers, + } + } +} diff --git a/src/lib.rs b/src/lib.rs index d2f0216..1c0bb59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ pub const SERVER_PORT: u16 = 3543; pub const BUFFER_SIZE: usize = 65535; pub const DEFAULT_TIMEOUT: u64 = 30; pub const VERSION: &str = "v0.1"; -pub const SALT_AND_IV_SIZE: u8 = 16; +pub const SALT_AND_IV_SIZE: usize = 16; pub const STANDARD_RETRY_MAX: usize = 10; #[repr(u8)] @@ -14,11 +14,14 @@ pub enum ServerMethods { GET = 2, HEARTBEAT = 3, // this also registers addtional clients } +#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[allow(non_camel_case_types)] +#[repr(u8)] pub enum ServerResponse { GENERAL_ERROR = 255, ID_EXISTS = 254, ID_DOESNT_EXIST = 253, // both error since sometimes it is the problem that the id exist and somethimes problem is that is doesn't + IO = 252, // had to place it here to avoid creating anther enum } #[allow(non_camel_case_types)] @@ -55,33 +58,51 @@ impl ServerErrorResponses { } } } +impl ServerErrorResponses { + pub fn kind(&self) -> ServerResponse { + match self { + ServerErrorResponses::GENERAL_ERROR(_) => ServerResponse::GENERAL_ERROR, + ServerErrorResponses::ID_EXISTS => ServerResponse::ID_EXISTS, + ServerErrorResponses::ID_DOESNT_EXIST => ServerResponse::ID_DOESNT_EXIST, + ServerErrorResponses::IO(_) => ServerResponse::IO, + } + } +} #[allow(non_camel_case_types)] +#[repr(usize)] pub enum RegisterRequestDataPositions { ENCRYPTED = 1, // this feeld should be 0 if not encrypted ID_LEN = 2, SOCKADDR_LEN = 3, SALT = 4, - IV = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::SALT as u8) as isize, - DATA = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::IV as u8) as isize, // after this there will be id and sockaddr in string or encrypted form after + IV = (SALT_AND_IV_SIZE as usize + RegisterRequestDataPositions::SALT as usize) as usize, + DATA = (SALT_AND_IV_SIZE as usize + RegisterRequestDataPositions::IV as usize) as usize, // after this there will be id and sockaddr in string or encrypted form after } #[allow(non_camel_case_types)] +#[repr(usize)] +pub enum GetRequestDataPositions { + ID = 1, // no need for len since id is the whoule rest of the packet +} + +#[allow(non_camel_case_types)] +#[repr(usize)] pub enum GetResponseDataPositions { ENCRYPTED = 1, // this feeld should be 0 if not encrypted - ID_LEN = 2, - NUM_OF_CLIENTS = 3, - SALT = 4, - CLIENTS = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::SALT as u8) as isize, + NUM_OF_CLIENTS = 2, + SALT = 3, + CLIENTS = (SALT_AND_IV_SIZE as usize + RegisterRequestDataPositions::SALT as usize) as usize, // after this there will be blocks of this sturcture: one byte size of sockaddr than there will be IV that is SALT_AND_IV_SIZE long and after that there will be sockaddr this repeats until the end of packet } #[allow(non_camel_case_types)] +#[repr(usize)] pub enum HeartBeatRequestDataPositions { ID_LEN = 1, SOCKADDR_LEN = 2, IV = 3, - DATA = (HeartBeatRequestDataPositions::IV as u8 + SALT_AND_IV_SIZE) as isize, // first ID than sockaddr + DATA = (HeartBeatRequestDataPositions::IV as usize + SALT_AND_IV_SIZE as usize) as usize, // first ID than sockaddr } pub mod shared; diff --git a/src/server/net.rs b/src/server/net.rs index 74a5951..3b50551 100644 --- a/src/server/net.rs +++ b/src/server/net.rs @@ -62,7 +62,15 @@ pub async fn handle_request( .find(|elem| elem.map(|s| &s.net_id == &net_id)) // find if id exists { Some(registration) => registration, - None => {let _ = socket.send_to(&[ServerResponse::ID_DOESNT_EXIST as u8], src); + None => {match socket.send_to(&[ServerResponse::ID_DOESNT_EXIST as u8], src){ + Ok(s) => { + #[cfg(debug_assertions)] + eprintln!("send {} bytes", s); + } + Err(e) => { + eprintln!("Error snding data: {}", e); + } + }; return; }, } diff --git a/src/server/types.rs b/src/server/types.rs index d6ed335..14e2c81 100644 --- a/src/server/types.rs +++ b/src/server/types.rs @@ -1,5 +1,4 @@ use pea_2_pea::*; -use std::ops::{Deref, DerefMut}; use std::sync::{Arc, atomic::Ordering}; #[derive(Clone)]