client registrar communication works
This commit is contained in:
parent
72703aa46b
commit
81649bf2fd
@ -1,6 +1,6 @@
|
|||||||
mod net;
|
mod net;
|
||||||
mod types;
|
mod types;
|
||||||
use pea_2_pea::{shared::crypto::derive_key_from_password, *};
|
use pea_2_pea::*;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
|
|
||||||
use std::{net::UdpSocket, process::exit, time::Duration};
|
use std::{net::UdpSocket, process::exit, time::Duration};
|
||||||
@ -104,6 +104,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
&cli.password,
|
&cli.password,
|
||||||
) {
|
) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
|
eprintln!("Network exists joining it");
|
||||||
let _ = net::send_heartbeat(
|
let _ = net::send_heartbeat(
|
||||||
&mut buf,
|
&mut buf,
|
||||||
&server_SocketAddr,
|
&server_SocketAddr,
|
||||||
@ -115,6 +116,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
n
|
n
|
||||||
}
|
}
|
||||||
Err(e) if e.kind() == ServerResponse::ID_DOESNT_EXIST => {
|
Err(e) if e.kind() == ServerResponse::ID_DOESNT_EXIST => {
|
||||||
|
eprintln!("Network does not exist creating it!");
|
||||||
let tmp_v_net: Network = Network::new(
|
let tmp_v_net: Network = Network::new(
|
||||||
match cli.password {
|
match cli.password {
|
||||||
Some(_) => true,
|
Some(_) => true,
|
||||||
|
@ -13,7 +13,7 @@ pub fn send_and_recv_with_retry(
|
|||||||
buf: &mut [u8; BUFFER_SIZE],
|
buf: &mut [u8; BUFFER_SIZE],
|
||||||
send_buf: &[u8],
|
send_buf: &[u8],
|
||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: UdpSocket,
|
socket: &UdpSocket,
|
||||||
retry_max: usize,
|
retry_max: usize,
|
||||||
) -> Result<(usize, usize), ServerErrorResponses> {
|
) -> Result<(usize, usize), ServerErrorResponses> {
|
||||||
let mut retry_count: usize = 0;
|
let mut retry_count: usize = 0;
|
||||||
@ -79,7 +79,7 @@ pub fn send_and_recv_with_retry(
|
|||||||
pub fn query_request(
|
pub fn query_request(
|
||||||
buf: &mut [u8; BUFFER_SIZE],
|
buf: &mut [u8; BUFFER_SIZE],
|
||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: UdpSocket,
|
socket: &UdpSocket,
|
||||||
) -> Result<String, ServerErrorResponses> {
|
) -> Result<String, ServerErrorResponses> {
|
||||||
match send_and_recv_with_retry(
|
match send_and_recv_with_retry(
|
||||||
buf,
|
buf,
|
||||||
@ -104,11 +104,11 @@ pub fn query_request(
|
|||||||
pub fn register_request(
|
pub fn register_request(
|
||||||
buf: &mut [u8; BUFFER_SIZE],
|
buf: &mut [u8; BUFFER_SIZE],
|
||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: UdpSocket,
|
socket: &UdpSocket,
|
||||||
network: &types::Network,
|
network: &types::Network,
|
||||||
public_sock_addr: &Box<[u8]>,
|
public_sock_addr: &Box<[u8]>,
|
||||||
iv: [u8; SALT_AND_IV_SIZE as usize],
|
iv: &[u8; SALT_AND_IV_SIZE as usize],
|
||||||
) -> Result<([u8; SALT_AND_IV_SIZE as usize], usize), ServerErrorResponses> {
|
) -> Result<usize, ServerErrorResponses> {
|
||||||
let mut send_buf: Box<[u8]> = vec![
|
let mut send_buf: Box<[u8]> = vec![
|
||||||
0u8;
|
0u8;
|
||||||
RegisterRequestDataPositions::DATA as usize
|
RegisterRequestDataPositions::DATA as usize
|
||||||
@ -127,7 +127,7 @@ pub fn register_request(
|
|||||||
|
|
||||||
send_buf[RegisterRequestDataPositions::IV as usize
|
send_buf[RegisterRequestDataPositions::IV as usize
|
||||||
..RegisterRequestDataPositions::IV as usize + SALT_AND_IV_SIZE 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
|
send_buf[RegisterRequestDataPositions::SALT as usize
|
||||||
..RegisterRequestDataPositions::SALT as usize + SALT_AND_IV_SIZE as usize]
|
..RegisterRequestDataPositions::SALT as usize + SALT_AND_IV_SIZE as usize]
|
||||||
.copy_from_slice(&network.salt);
|
.copy_from_slice(&network.salt);
|
||||||
@ -141,7 +141,7 @@ pub fn register_request(
|
|||||||
.copy_from_slice(&public_sock_addr);
|
.copy_from_slice(&public_sock_addr);
|
||||||
|
|
||||||
match send_and_recv_with_retry(buf, &send_buf, dst, socket, STANDARD_RETRY_MAX) {
|
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),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,9 +149,9 @@ pub fn register_request(
|
|||||||
pub fn get_request(
|
pub fn get_request(
|
||||||
buf: &mut [u8; BUFFER_SIZE],
|
buf: &mut [u8; BUFFER_SIZE],
|
||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: UdpSocket,
|
socket: &UdpSocket,
|
||||||
network_id: String,
|
network_id: &String,
|
||||||
password: Option<String>,
|
password: &Option<String>,
|
||||||
) -> Result<types::Network, ServerErrorResponses> {
|
) -> Result<types::Network, ServerErrorResponses> {
|
||||||
let mut send_buf: Box<[u8]> =
|
let mut send_buf: Box<[u8]> =
|
||||||
vec![0u8; GetRequestDataPositions::ID as usize + network_id.len()].into_boxed_slice();
|
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;
|
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(
|
pub fn send_heartbeat(
|
||||||
buf: &mut [u8; BUFFER_SIZE],
|
buf: &mut [u8; BUFFER_SIZE],
|
||||||
dst: &SocketAddr,
|
dst: &SocketAddr,
|
||||||
socket: UdpSocket,
|
socket: &UdpSocket,
|
||||||
network: &types::Network,
|
network: &types::Network,
|
||||||
my_public_sock_addr: &Box<[u8]>,
|
my_public_sock_addr: &Box<[u8]>,
|
||||||
iv: &[u8; SALT_AND_IV_SIZE as usize],
|
iv: &[u8; SALT_AND_IV_SIZE as usize],
|
||||||
|
32
src/client/types.rs
Normal file
32
src/client/types.rs
Normal file
@ -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<std::net::SocketAddr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Network {
|
||||||
|
pub fn new(
|
||||||
|
encrypted: bool,
|
||||||
|
key: [u8; 32],
|
||||||
|
net_id: String,
|
||||||
|
salt: [u8; SALT_AND_IV_SIZE as usize],
|
||||||
|
peers: Vec<std::net::SocketAddr>,
|
||||||
|
) -> Self {
|
||||||
|
Network {
|
||||||
|
encrypted,
|
||||||
|
key,
|
||||||
|
net_id,
|
||||||
|
salt,
|
||||||
|
peers,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
src/lib.rs
37
src/lib.rs
@ -4,7 +4,7 @@ pub const SERVER_PORT: u16 = 3543;
|
|||||||
pub const BUFFER_SIZE: usize = 65535;
|
pub const BUFFER_SIZE: usize = 65535;
|
||||||
pub const DEFAULT_TIMEOUT: u64 = 30;
|
pub const DEFAULT_TIMEOUT: u64 = 30;
|
||||||
pub const VERSION: &str = "v0.1";
|
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;
|
pub const STANDARD_RETRY_MAX: usize = 10;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
@ -14,11 +14,14 @@ pub enum ServerMethods {
|
|||||||
GET = 2,
|
GET = 2,
|
||||||
HEARTBEAT = 3, // this also registers addtional clients
|
HEARTBEAT = 3, // this also registers addtional clients
|
||||||
}
|
}
|
||||||
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
|
#[repr(u8)]
|
||||||
pub enum ServerResponse {
|
pub enum ServerResponse {
|
||||||
GENERAL_ERROR = 255,
|
GENERAL_ERROR = 255,
|
||||||
ID_EXISTS = 254,
|
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
|
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)]
|
#[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)]
|
#[allow(non_camel_case_types)]
|
||||||
|
#[repr(usize)]
|
||||||
pub enum RegisterRequestDataPositions {
|
pub enum RegisterRequestDataPositions {
|
||||||
ENCRYPTED = 1, // this feeld should be 0 if not encrypted
|
ENCRYPTED = 1, // this feeld should be 0 if not encrypted
|
||||||
ID_LEN = 2,
|
ID_LEN = 2,
|
||||||
SOCKADDR_LEN = 3,
|
SOCKADDR_LEN = 3,
|
||||||
SALT = 4,
|
SALT = 4,
|
||||||
IV = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::SALT as u8) as isize,
|
IV = (SALT_AND_IV_SIZE as usize + RegisterRequestDataPositions::SALT as usize) as usize,
|
||||||
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
|
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)]
|
#[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 {
|
pub enum GetResponseDataPositions {
|
||||||
ENCRYPTED = 1, // this feeld should be 0 if not encrypted
|
ENCRYPTED = 1, // this feeld should be 0 if not encrypted
|
||||||
ID_LEN = 2,
|
NUM_OF_CLIENTS = 2,
|
||||||
NUM_OF_CLIENTS = 3,
|
SALT = 3,
|
||||||
SALT = 4,
|
CLIENTS = (SALT_AND_IV_SIZE as usize + RegisterRequestDataPositions::SALT as usize) as usize,
|
||||||
CLIENTS = (SALT_AND_IV_SIZE + RegisterRequestDataPositions::SALT as u8) as isize,
|
|
||||||
// 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
|
// 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)]
|
#[allow(non_camel_case_types)]
|
||||||
|
#[repr(usize)]
|
||||||
pub enum HeartBeatRequestDataPositions {
|
pub enum HeartBeatRequestDataPositions {
|
||||||
ID_LEN = 1,
|
ID_LEN = 1,
|
||||||
SOCKADDR_LEN = 2,
|
SOCKADDR_LEN = 2,
|
||||||
IV = 3,
|
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;
|
pub mod shared;
|
||||||
|
@ -62,7 +62,15 @@ pub async fn handle_request(
|
|||||||
.find(|elem| elem.map(|s| &s.net_id == &net_id)) // find if id exists
|
.find(|elem| elem.map(|s| &s.net_id == &net_id)) // find if id exists
|
||||||
{
|
{
|
||||||
Some(registration) => registration,
|
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;
|
return;
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use pea_2_pea::*;
|
use pea_2_pea::*;
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::sync::{Arc, atomic::Ordering};
|
use std::sync::{Arc, atomic::Ordering};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user