Browse Source

Merge remote-tracking branch 'origin/master'

master
Andrew Onuchowski 1 year ago
parent
commit
d06aead691
1 changed files with 75 additions and 0 deletions
  1. 75
    0
      src/bin/message.rs

+ 75
- 0
src/bin/message.rs View File

@@ -0,0 +1,75 @@
const KEYLENGTH: u8 = 32;

pub enum PayloadType {
//0-31 system messages (plaintext)
Ping = 1,
Pong = 2,
Error = 3,
//32 - 63 Serverside messages (currently plaintext)
Init = 32,
Join = 33,
Exit = 34,
//64 -95 initial setup messages(plaintext)
DhSetup = 64,
DhReturn = 65,
//96 - 127 client-client setup messages(DH or old KEY encrypted)
AUTHORIZE = 96,
KeyCurrent = 97,
KeyNew = 98,
//128-159 main messages (shared KEY encrypted)
Msg = 128,
//160-255 reserved
}

struct Message {
// Eigenschaften der Klasse
src_id: u32,
dest_id: u32,
size: u16,
payload: Vec<u8>,
}

impl Message {
fn new(src_id: u32, dest_id: u32, size: u16, msg_type: PayloadType) -> Message {
let mut msg = Message {
src_id: src_id,
dest_id: dest_id,
size: size,
payload: Vec::with_capacity(size.into()),
};
msg.payload[0] = msg_type as u8;
msg
}

fn recive(input: Vec<u8>) -> Message {
let byte1 = input.get(0).expect("Fail to Read Byte 1");
let byte2 = input.get(1).expect("Fail to Read Byte 2");
let byte3 = input.get(2).expect("Fail to Read Byte 3");
let byte4 = input.get(3).expect("Fail to Read Byte 4");

let src_id: u32 =
(byte1 << 24) as u32 | (byte2 << 16) as u32 | (byte3 << 8) as u32 | (byte4 << 0) as u32;

let byte5 = input.get(5).expect("Fail to Read Byte 5");
let byte6 = input.get(6).expect("Fail to Read Byte 6");
let byte7 = input.get(7).expect("Fail to Read Byte 7");
let byte8 = input.get(8).expect("Fail to Read Byte 8");

let dest_id: u32 =
(byte5 << 24) as u32 | (byte6 << 16) as u32 | (byte7 << 8) as u32 | (byte8 << 0) as u32;

let byte9 = input.get(9).expect("Fail to Read byte 9");
let byte10 = input.get(10).expect("Fail to Read byte 10");

let size = (byte9 << 8) as u16 | (byte10 << 0) as u16;
assert!(size > 0, "Ungültige size größe size = {}", size);

Message {
src_id: src_id,
dest_id: dest_id,
size: size,
payload: (&input[11..]).to_vec(),
}
}
// Methoden der Klasse
}

Loading…
Cancel
Save