diff options
author | Jake McGinty <me@jake.su> | 2018-02-14 15:36:02 +0000 |
---|---|---|
committer | Jake McGinty <me@jake.su> | 2018-02-14 15:36:02 +0000 |
commit | 956492a28c975d73113bdac0547cea46bc504f4b (patch) | |
tree | 377050c39f197408df9cc107cf1dad288ce2e141 /src/types.rs | |
parent | clear unused imports (diff) | |
download | wireguard-rs-956492a28c975d73113bdac0547cea46bc504f4b.tar.xz wireguard-rs-956492a28c975d73113bdac0547cea46bc504f4b.zip |
clippy knows all
Diffstat (limited to 'src/types.rs')
-rw-r--r-- | src/types.rs | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/src/types.rs b/src/types.rs index 0191ed7..c17cbb1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,4 @@ use base64; -use std::io::{Cursor, Read, Write}; -use byteorder::{LittleEndian, WriteBytesExt, ReadBytesExt}; use std::fmt::{self, Display, Formatter}; use std::net::{IpAddr, SocketAddr}; @@ -26,82 +24,3 @@ pub struct InterfaceInfo { pub pub_key: Option<[u8; 32]>, pub listen_port: Option<u16>, } - -pub enum Message { - HandshakeInitiation(HandshakeInitiationMessage), -// HandshakeResponse(HandshakeResponseMessage), -// Transport(TransportMessage), -// CookieReply(CookieReplyMessage), - Other(Vec<u8>) -} - -// TODO use TryFrom -impl<'a> From<&'a [u8]> for Message { - fn from(bytes: &'a [u8]) -> Self { - use self::Message::*; - let mut cursor = Cursor::new(bytes); - match cursor.read_u8().unwrap() { - 1 => HandshakeInitiation(HandshakeInitiationMessage::from(&bytes[4..])), - _ => Other((&bytes[4..]).to_owned()) - } - } -} - -impl From<Message> for Vec<u8> { - fn from(message: Message) -> Self { - use self::Message::*; - match message { - HandshakeInitiation(message) => { - let mut bytes = vec![1u8, 0, 0, 0]; - bytes.append(&mut message.into()); - bytes - }, - _ => unimplemented!() - } - } -} - -pub struct HandshakeInitiationMessage { - pub sender_i: u32, - pub payload: [u8; 76], - pub mac1: [u8; 16], - pub mac2: [u8; 16] -} - -impl HandshakeInitiationMessage { - pub fn new() -> Self { - HandshakeInitiationMessage { - sender_i: 0, - payload: [0u8; 76], - mac1: [0u8; 16], - mac2: [0u8; 16], - } - } -} - -impl<'a> From<&'a [u8]> for HandshakeInitiationMessage { - - fn from(bytes: &'a [u8]) -> Self { - let mut message = HandshakeInitiationMessage::new(); - let mut cursor = Cursor::new(bytes); - - message.sender_i = cursor.read_u32::<LittleEndian>().unwrap(); - cursor.read_exact(&mut message.payload[..]).unwrap(); - cursor.read_exact(&mut message.mac1[..]).unwrap(); - cursor.read_exact(&mut message.mac2[..]).unwrap(); - message - } -} - -impl From<HandshakeInitiationMessage> for Vec<u8> { - fn from(message: HandshakeInitiationMessage) -> Self { - let mut cursor = vec![]; - cursor.write_all(&[1u8, 0, 0, 0]).unwrap(); - cursor.write_u32::<LittleEndian>(message.sender_i).unwrap(); - cursor.write_all(&message.payload).unwrap(); - cursor.write_all(&message.mac1).unwrap(); - cursor.write_all(&message.mac2).unwrap(); - - cursor - } -}
\ No newline at end of file |