This commit is contained in:
Tiago Batista Cardoso
2026-01-24 19:47:15 +01:00
parent f8e3e46672
commit a3648c2116
9 changed files with 270 additions and 66 deletions

View File

@@ -1,19 +1,16 @@
use tokio::sync::oneshot;
use crate::{
NetworkEvent, NodeHash, P2PSharedData,
cryptographic_signature::{
CryptographicSignature, get_peer_key, sign_message, verify_signature,
},
NetworkEvent, NodeHash,
cryptographic_signature::{CryptographicSignature, get_peer_key, verify_signature},
datum_parsing::parse_received_datum,
messages_channels::MultipleSenders,
messages_structure::construct_message,
peers_refresh::HandshakeHistory,
registration,
server_communication::generate_id,
timestamp::Timestamp,
};
use std::{
collections::HashMap,
default,
net::{Ipv4Addr, SocketAddr},
};
use std::{
@@ -22,7 +19,7 @@ use std::{
};
// Types of messages that await for a response
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum EventType {
HelloThenRootRequest,
Hello,
@@ -31,6 +28,33 @@ pub enum EventType {
NatTraversal,
DatumRequest,
DatumRequestBig,
Unknown,
}
impl EventType {
pub fn to_string(&self) -> String {
match self {
EventType::HelloThenRootRequest => "HelloThenRootRequest".to_owned(),
EventType::Hello => "Hello".to_owned(),
EventType::RootRequest => "RootRequest".to_owned(),
EventType::Ping => "Ping".to_owned(),
EventType::NatTraversal => "NatTraversal".to_owned(),
EventType::DatumRequest => "DatumRequest".to_owned(),
EventType::Unknown => "Unknown".to_owned(),
EventType::DatumRequestBig => "DatumRequestBig".to_owned(),
}
}
pub fn from_msgtype(msgtype: u8) -> EventType {
match msgtype {
PING => EventType::Ping,
HELLO => EventType::Hello,
ROOTREQUEST => EventType::RootRequest,
NATTRAVERSALREQUEST => EventType::NatTraversal,
DATUMREQUEST => EventType::DatumRequest,
_ => EventType::Unknown,
}
}
}
const ID: usize = 4;
@@ -54,6 +78,7 @@ const NATTRAVERSALREQUEST2: u8 = 5;
pub fn handle_recevied_message(
messages_list: &Arc<Mutex<HashMap<i32, EventType>>>,
messages_received: &Arc<Mutex<HashMap<String, (EventType, Timestamp)>>>,
recevied_message: &Vec<u8>,
crypto_pair: &CryptographicSignature,
//socket_addr: &SocketAddr,
@@ -92,6 +117,7 @@ pub fn handle_recevied_message(
cmd_tx,
ip,
messages_list,
messages_received,
handhsake_history,
senders,
);
@@ -117,6 +143,7 @@ pub fn parse_message(
cmd_tx: crossbeam_channel::Sender<NetworkEvent>,
ip: SocketAddr,
messages_list: &Arc<Mutex<HashMap<i32, EventType>>>,
messages_received: &Arc<Mutex<HashMap<String, (EventType, Timestamp)>>>,
handhsake_history: Arc<HandshakeHistory>,
senders: &MultipleSenders,
) -> Option<Vec<u8>> {
@@ -128,6 +155,14 @@ pub fn parse_message(
let msgtype = received_message[ID];
messages_received
.lock()
.expect("couldnt lock received map")
.insert(
ip.to_string(),
(EventType::from_msgtype(msgtype), Timestamp::now()),
);
let length_bytes: [u8; 2] = received_message[TYPE..LENGTH]
.try_into()
.expect("Taille incorrecte");