messages rewrite

This commit is contained in:
2026-01-07 23:34:44 +01:00
parent f51b8e999c
commit dc1767abe4
5 changed files with 276 additions and 38 deletions

View File

@@ -0,0 +1 @@
fn parse_received_datum(recevied_datum: Vec<u8>) {}

View File

@@ -1,5 +1,6 @@
mod cryptographic_signature; mod cryptographic_signature;
mod data; mod data;
mod datum_parsing;
mod message_handling; mod message_handling;
mod messages_channels; mod messages_channels;
mod messages_structure; mod messages_structure;

View File

@@ -16,29 +16,24 @@ pub enum EventType {
PeerHello, PeerHello,
} }
/*pub fn handle_recevied_message( const ID: usize = 4;
messages_list: &mut HashMap<i32, EventType>, const TYPE: usize = 5;
recevied_message: &Vec<u8>, const LENGTH: usize = 7;
crypto_pair: &CryptographicSignature, const EXTENSIONS: usize = 32;
socket_addr: &SocketAddr, const SIGNATURE: usize = 64;
senders: &MultipleSenders,
) { const PING: u8 = 0;
let message_id: [u8; 4] = recevied_message[0..4].try_into().expect("size error"); const OK: u8 = 128;
let id = i32::from_be_bytes(message_id); const ERROR: u8 = 129;
let eventtype = messages_list.get(&id); const HELLO: u8 = 1;
match eventtype { const HELLOREPLY: u8 = 130;
Some(EventType::ServerHelloReply) => { const ROOTREQUEST: u8 = 2;
registration::register_ip_addresses( const ROOTREPLY: u8 = 131;
&crypto_pair, const DATUMREQUEST: u8 = 3;
socket_addr.ip().to_string(), const NODATUM: u8 = 133;
&senders, const DATUM: u8 = 132;
messages_list, const NATTRAVERSALREQUEST: u8 = 4;
); const NATTRAVERSALREQUEST2: u8 = 5;
}
Some(_) => print!("Not implemented"),
None => print!("Message not found"),
}
}*/
pub fn handle_recevied_message( pub fn handle_recevied_message(
messages_list: &Arc<Mutex<HashMap<i32, EventType>>>, messages_list: &Arc<Mutex<HashMap<i32, EventType>>>,
@@ -120,3 +115,173 @@ pub fn handle_recevied_message(
} }
} }
} }
pub fn ping(received_message: Vec<u8>, socket_addr: String) {}
pub fn ok(received_message: Vec<u8>, socket_addr: String) {}
pub fn error(received_message: Vec<u8>, socket_addr: String) {}
pub fn hello(received_message: Vec<u8>, socket_addr: String) {}
pub fn hello_reply(received_message: Vec<u8>, socket_addr: String) {}
pub fn root_request(received_message: Vec<u8>, socket_addr: String) {}
pub fn root_reply(received_message: Vec<u8>, socket_addr: String) {}
pub fn datum_request(received_message: Vec<u8>, socket_addr: String) {}
pub fn no_datum(received_message: Vec<u8>, socket_addr: String) {}
pub fn datum(received_message: Vec<u8>, socket_addr: String) {}
pub fn parse_message(
received_message: Vec<u8>,
received_name: String,
id: i32,
username: String,
) -> Option<Vec<u8>> {
let id_bytes: [u8; 4] = received_message[0..ID]
.try_into()
.expect("Taille incorrecte");
let msgtype = received_message[ID];
let length_bytes: [u8; 2] = received_message[TYPE..LENGTH]
.try_into()
.expect("Taille incorrecte");
let msg_length = u16::from_be_bytes(length_bytes) as usize;
// verify signature
match msgtype {
HELLO | HELLOREPLY | ROOTREPLY | NODATUM | NATTRAVERSALREQUEST | NATTRAVERSALREQUEST2 => {
let peer_pubkey = tokio::runtime::Runtime::new()
.unwrap()
.block_on(get_peer_key(&received_name))
.expect("failed to retrieve public key");
let signature: [u8; SIGNATURE] = received_message
[LENGTH + msg_length..LENGTH + msg_length + SIGNATURE]
.try_into()
.expect("Taille incorrecte");
if !verify_signature(peer_pubkey, &received_message) {
println!(
"incorrect signature from given peer: {}, ignoring message of type {} with id {}",
&received_name, received_message[ID], id
);
return None;
}
}
_ => {}
}
match msgtype {
// PING
//
// envoie un OK
//
// OK
//
// si NATTRAVERSALREQUEST alors
//
// ERROR
//
// affiche un msg d'erreur
//
// HELLO
//
// envoie une hello reply
//
HELLO => {
let username_size = username.len();
let hello_handshake =
HandshakeMessage::helloReply(id as u32, username_size as u16 + 4, username.clone());
//HandshakeMessage::display(&hello_handshake);
let hello_handshake_serialized = hello_handshake.serialize();
return Some(hello_handshake_serialized);
}
// HELLOREPLY
//
// envoie un root request
//
// ROOTREQUEST
//
// envoie un root reply
//
// ROOTREPLY
//
// envoie un datum request
//
// DATUMREQUEST
//
// envoie le datum
//
// NODATUM
//
// affiche un msg d'erreur
//
// DATUM
//
// parcourt le directory recu ou le big directory et renvoie une DATUMREQUEST pour chaque
// directory ou big directory lu
//
// NATTRAVERSALREQUEST
//
// repond OK et envoie un NATTRAVERSALREQUEST2 au pair B
//
// NATTRAVERSALREQUEST2
//
// envoie OK à S puis envoie un ping à S
// PING
//
// envoie un OK
//
// OK
//
// si NATTRAVERSALREQUEST alors
//
// ERROR
//
// affiche un msg d'erreur
//
// HELLO
//
// envoie une hello reply
//
// HELLOREPLY
//
// envoie un root request
//
// ROOTREQUEST
//
// envoie un root reply
//
// ROOTREPLY
//
// envoie un datum request
//
// DATUMREQUEST
//
// envoie le datum
//
// NODATUM
//
// affiche un msg d'erreur
//
// DATUM
//
// parcourt le directory recu ou le big directory et renvoie une DATUMREQUEST pour chaque
// directory ou big directory lu
//
// NATTRAVERSALREQUEST
//
// repond OK et envoie un NATTRAVERSALREQUEST2 au pair B
//
// NATTRAVERSALREQUEST2
//
// envoie OK à S puis envoie un ping à S
_ => return None,
}
}

View File

@@ -1,3 +1,45 @@
use crate::server_communication::generate_id;
const ID: usize = 4;
const TYPE: usize = 5;
const LENGTH: usize = 7;
const EXTENSIONS: usize = 32;
const SIGNATURE: usize = 64;
const PING: u8 = 0;
const OK: u8 = 128;
const ERROR: u8 = 129;
const HELLO: u8 = 1;
const HELLOREPLY: u8 = 130;
const ROOTREQUEST: u8 = 2;
const ROOTREPLY: u8 = 131;
const DATUMREQUEST: u8 = 3;
const NODATUM: u8 = 133;
const DATUM: u8 = 132;
const NATTRAVERSALREQUEST: u8 = 4;
const NATTRAVERSALREQUEST2: u8 = 5;
pub fn constructMessage(msgtype: u8, payload: Vec<u8>, id: &[u8]) {
match msgtype {
HELLO => {
let message: Vec<u8> = vec![0u8; LENGTH + EXTENSIONS + payload.len() + SIGNATURE];
message.extend_from_slice(id);
message.extend_from_slice(0);
let name_vec = username.trim_end_matches(char::from(0)).as_bytes().to_vec();
HandshakeMessage {
id: id,
msg_type: 1,
length: length,
extensions: 0,
name: name_vec,
signature: vec![0; 64],
}
}
}
}
pub struct UDPMessage { pub struct UDPMessage {
id: u32, id: u32,
msg_type: u8, msg_type: u8,
@@ -15,6 +57,22 @@ pub struct HandshakeMessage {
pub signature: Vec<u8>, pub signature: Vec<u8>,
} }
pub struct DatumRootRequest {
pub id: u32,
msg_type: u8,
length: u16,
hash: Vec<u8>,
}
pub struct DatumReply {
pub id: u32,
msg_type: u8,
length: u16,
hash: Vec<u8>,
value: Vec<u8>,
signature: Vec<u8>,
}
impl UDPMessage { impl UDPMessage {
pub fn ping(id: u32) -> UDPMessage { pub fn ping(id: u32) -> UDPMessage {
UDPMessage { UDPMessage {

43
todo.md
View File

@@ -1,16 +1,9 @@
# Todo : # Todo :
## peer discovery
- get rsquest to the uri /peers/
## registration with the server
- generation of the cryptographic key OK ## peer discovery
- put request to the uri (check if the peer is already connected) OK
- udp handshakes OK
- get request to the uri /peers/key to get the public key of a peer OK
- get request to the uri /peers/key/addresses OK
## handshake ## handshake
- handshake structure OK
- 5min timeout after handshake - 5min timeout after handshake
- matain connection every 4 min - matain connection every 4 min
@@ -22,14 +15,34 @@
- setting in gui to act as a relay - setting in gui to act as a relay
- chunk, directory, big, bigdirectory structures - chunk, directory, big, bigdirectory structures
fonctionnalités : ## fonctionnalités application :
s'enregistrer avec le serveur OK
rechercher un pair
generer une clé publique OK
rechercher les fichiers d'un pair rechercher les fichiers d'un pair
telechargement des fichiers telechargement des fichiers
choisir un dossier à partager choisir un dossier à partager
se deconnecter du réseau choisir le nombre de canaux
handshake server DOING
se deconnecter du réseau DOING
## autre :
socket ipv6
# FAIT :
- choisir un pseudo OK
- get rsquest to the uri /peers/ OK
- generation of the cryptographic key OK
- put request to the uri (check if the peer is already connected) OK
- get request to the uri /peers/key to get the public key of a peer OK
- get request to the uri /peers/key/addresses OK
- udp handshakes OK
- handshake structure OK
- s'enregistrer avec le serveur OK
- generer une clé publique OK
- verifier signature OK
- 2 channels -> un pour envoyer et un pour recevoir OK
2 channels -> un pour envoyer et un pour recevoir OK