first addition ś of aes

This commit is contained in:
2025-07-26 21:28:03 +02:00
parent 3bcccc7620
commit e1d5a34e82
9 changed files with 507 additions and 38 deletions
+72 -15
View File
@@ -1,4 +1,4 @@
use pea_2_pea::SERVER_PORT;
use pea_2_pea::*;
use std::{
io::{Error, ErrorKind, Read, Write},
@@ -38,6 +38,10 @@ struct Cli {
fn main() -> std::io::Result<()> {
let cli = <Cli as clap::Parser>::parse();
if cli.network_id.len() > 0xff {
eprintln!("network id cannot have more then 255 charactes");
exit(7); // posix for E2BIG
}
{
let socket: UdpSocket = (|| -> std::io::Result<UdpSocket> {
match UdpSocket::bind("0.0.0.0:0") {
@@ -50,11 +54,10 @@ fn main() -> std::io::Result<()> {
socket.set_read_timeout(Some(Duration::new(10, 0)))?; // set timeout to 10 seconds
// send query request to get server public key
let server_port: u16 = (|| -> u16 {
match cli.registrar_port {
Some(port_proveded) => return port_proveded,
None => return pea_2_pea::SERVER_PORT,
None => return SERVER_PORT,
}
})();
@@ -63,34 +66,88 @@ fn main() -> std::io::Result<()> {
.parse()
.unwrap();
{
let mut query_byte: [u8; 1] = [0; 1];
query_byte[0] = pea_2_pea::ServerMethods::QUERY as u8;
match socket.send_to(&query_byte, &server_SocketAddr) {
let mut buf: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE];
let mut data_lenght;
loop {
match socket.send_to(&[ServerMethods::QUERY as u8], &server_SocketAddr) {
Ok(s) => {
#[cfg(debug_assertions)]
eprintln!("send {} bytes", s);
}
Err(e) => {
eprintln!("Error snding data: {}", e);
panic!("Error sending data: {}", e);
}
}
}
let mut buf: [u8; pea_2_pea::BUFFER_SIZE] = [0; pea_2_pea::BUFFER_SIZE];
loop {
match socket.recv_from(&mut buf) {
Ok((data_length, src)) => {}
Ok((data_length_recved, _src)) => {
data_lenght = data_length_recved;
if buf[0] != 0 {
continue;
}
break;
}
Err(e) if e.kind() == ErrorKind::WouldBlock || e.kind() == ErrorKind::TimedOut => {
// timedout
continue;
}
Err(e) => {
eprintln!("Error receiving data: {}", e);
std::process::exit(-4);
panic!("Error receiving data: {}", e);
}
}
break;
}
let mut public_sock_addr: Vec<u8> = buf[1..data_lenght].to_vec();
// register network
buf[0] = ServerMethods::REGISTER as u8;
buf[RegisterRequestDataPositions::ENCRYPTED as usize] = match cli.password {
Some(_) => true as u8,
None => false as u8,
};
buf[RegisterRequestDataPositions::ID_LEN as usize] = cli.network_id.len() as u8;
buf[RegisterRequestDataPositions::SOCKADDR_LEN as usize] =
server_SocketAddr.to_string().len() as u8;
buf[RegisterRequestDataPositions::DATA as usize
..RegisterRequestDataPositions::DATA as usize + cli.network_id.len()]
.copy_from_slice(cli.network_id.as_bytes());// store network id
match cli.password {
Some(s) => {},
None => {},// do nothig
}
buf[RegisterRequestDataPositions::DATA as usize + cli.network_id.len()..RegisterRequestDataPositions::DATA as usize + cli.network_id.len() + ]
match buf[0] {
x if x == ServerResponse::OK as u8 => {
eprintln!("network registered");
}
x if x == ServerResponse::GENERAL_ERROR as u8 => {
eprintln!(
"{}",
match std::str::from_utf8(&buf[1..data_lenght]) {
Ok(s) => s.to_string(),
Err(e) => {
panic!("id to utf-8 failed: {}", e);
}
}
)
}
x if x == ServerResponse::ID_EXISTS as u8 => {
panic!("network ID already exist try differnt one!");
}
_ => {
panic!("unknown responce from server code: 0x{:02x}", buf[0])
}
}
}
Ok(())