diff --git a/src/proto/server/constants.rs b/src/proto/server/constants.rs new file mode 100644 index 0000000..f1d3fa9 --- /dev/null +++ b/src/proto/server/constants.rs @@ -0,0 +1,8 @@ +pub const CODE_LOGIN: u32 = 1; +pub const CODE_SET_LISTEN_PORT: u32 = 2; +pub const CODE_CONNECT_TO_PEER: u32 = 18; +pub const CODE_ROOM_LIST: u32 = 64; +pub const CODE_PRIVILEGED_USERS: u32 = 69; +pub const CODE_PARENT_MIN_SPEED: u32 = 83; +pub const CODE_PARENT_SPEED_RATIO: u32 = 84; +pub const CODE_WISHLIST_INTERVAL: u32 = 104; diff --git a/src/proto/server/mod.rs b/src/proto/server/mod.rs new file mode 100644 index 0000000..2c951ec --- /dev/null +++ b/src/proto/server/mod.rs @@ -0,0 +1,6 @@ +mod constants; +mod request; +mod response; + +pub use self::request::*; +pub use self::response::*; diff --git a/src/proto/server/request.rs b/src/proto/server/request.rs new file mode 100644 index 0000000..2c148fa --- /dev/null +++ b/src/proto/server/request.rs @@ -0,0 +1,132 @@ +use std::io; + +use crypto::md5::Md5; +use crypto::digest::Digest; + +use super::constants::*; +use super::super::packet::Packet; + +trait WriteToPacket { + fn write_to_packet(&self, &mut Packet) -> io::Result<()>; +} + +/*================* + * SERVER REQUEST * + *================*/ + +#[derive(Debug)] +pub enum ServerRequest { + LoginRequest(LoginRequest), + RoomListRequest(RoomListRequest), + SetListenPortRequest(SetListenPortRequest), +} + +impl ServerRequest { + pub fn to_packet(&self) -> io::Result { + let (mut packet, request): (Packet, &WriteToPacket) = match *self { + ServerRequest::LoginRequest(ref request) => + (Packet::new(CODE_LOGIN), request), + + ServerRequest::RoomListRequest(ref request) => + (Packet::new(CODE_ROOM_LIST), request), + + ServerRequest::SetListenPortRequest(ref request) => + (Packet::new(CODE_SET_LISTEN_PORT), request), + }; + try!(request.write_to_packet(&mut packet)); + Ok(packet) + } +} + +fn md5_str(string: &str) -> String { + let mut hasher = Md5::new(); + hasher.input_str(string); + hasher.result_str() +} + +/*=======* + * LOGIN * + *=======*/ + +#[derive(Debug)] +pub struct LoginRequest { + username: String, + password: String, + major: u32, + minor: u32, +} + +impl LoginRequest { + pub fn new(username: &str, password: &str, major: u32, minor: u32) + -> Result { + if password.len() > 0 { + Ok(LoginRequest { + username: username.to_string(), + password: password.to_string(), + major: major, + minor: minor, + }) + } else { + Err("Empty password") + } + } +} + +impl WriteToPacket for LoginRequest { + fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> { + let userpass = String::new() + &self.username + &self.password; + let userpass_md5 = md5_str(&userpass); + + try!(packet.write_str(&self.username)); + try!(packet.write_str(&self.password)); + try!(packet.write_uint(self.major)); + try!(packet.write_str(&userpass_md5)); + try!(packet.write_uint(self.minor)); + + Ok(()) + } +} + +/*===========* + * ROOM LIST * + *===========*/ + +#[derive(Debug)] +pub struct RoomListRequest; + +impl RoomListRequest { + pub fn new() -> Self { + RoomListRequest + } +} + +impl WriteToPacket for RoomListRequest { + fn write_to_packet(&self, _: &mut Packet) -> io::Result<()> { + Ok(()) + } +} + +/*=================* + * SET LISTEN PORT * + *=================*/ + +#[derive(Debug)] +pub struct SetListenPortRequest { + port: u16, +} + +impl SetListenPortRequest { + fn new(port: u16) -> Self { + SetListenPortRequest { + port: port, + } + } +} + +impl WriteToPacket for SetListenPortRequest { + fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> { + try!(packet.write_uint(self.port as u32)); + Ok(()) + } +} + diff --git a/src/proto/server.rs b/src/proto/server/response.rs similarity index 71% rename from src/proto/server.rs rename to src/proto/server/response.rs index d070de8..bf939ee 100644 --- a/src/proto/server.rs +++ b/src/proto/server/response.rs @@ -1,53 +1,10 @@ use std::io; use std::net; -use crypto::md5::Md5; -use crypto::digest::Digest; +use super::constants::*; +use super::super::packet::Packet; -use super::Packet; - -const MAX_PORT: u32 = 1 << 16; - -const CODE_LOGIN: u32 = 1; -const CODE_SET_LISTEN_PORT: u32 = 2; -const CODE_CONNECT_TO_PEER: u32 = 18; -const CODE_ROOM_LIST: u32 = 64; -const CODE_PRIVILEGED_USERS: u32 = 69; -const CODE_PARENT_MIN_SPEED: u32 = 83; -const CODE_PARENT_SPEED_RATIO: u32 = 84; -const CODE_WISHLIST_INTERVAL: u32 = 104; - -trait WriteToPacket { - fn write_to_packet(&self, &mut Packet) -> io::Result<()>; -} - -/*================* - * SERVER REQUEST * - *================*/ - -#[derive(Debug)] -pub enum ServerRequest { - LoginRequest(LoginRequest), - RoomListRequest(RoomListRequest), - SetListenPortRequest(SetListenPortRequest), -} - -impl ServerRequest { - pub fn to_packet(&self) -> io::Result { - let (mut packet, request): (Packet, &WriteToPacket) = match *self { - ServerRequest::LoginRequest(ref request) => - (Packet::new(CODE_LOGIN), request), - - ServerRequest::RoomListRequest(ref request) => - (Packet::new(CODE_ROOM_LIST), request), - - ServerRequest::SetListenPortRequest(ref request) => - (Packet::new(CODE_SET_LISTEN_PORT), request), - }; - try!(request.write_to_packet(&mut packet)); - Ok(packet) - } -} +const MAX_PORT: u32 = (1 << 16) - 1; /*=================* * SERVER RESPONSE * @@ -118,12 +75,6 @@ impl ServerResponse { } } -fn md5_str(string: &str) -> String { - let mut hasher = Md5::new(); - hasher.input_str(string); - hasher.result_str() -} - /*=================* * CONNECT TO PEER * *=================*/ @@ -169,45 +120,6 @@ impl ConnectToPeerResponse { * LOGIN * *=======*/ -#[derive(Debug)] -pub struct LoginRequest { - username: String, - password: String, - major: u32, - minor: u32, -} - -impl LoginRequest { - pub fn new(username: &str, password: &str, major: u32, minor: u32) - -> Result { - if password.len() > 0 { - Ok(LoginRequest { - username: username.to_string(), - password: password.to_string(), - major: major, - minor: minor, - }) - } else { - Err("Empty password") - } - } -} - -impl WriteToPacket for LoginRequest { - fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> { - let userpass = String::new() + &self.username + &self.password; - let userpass_md5 = md5_str(&userpass); - - try!(packet.write_str(&self.username)); - try!(packet.write_str(&self.password)); - try!(packet.write_uint(self.major)); - try!(packet.write_str(&userpass_md5)); - try!(packet.write_uint(self.minor)); - - Ok(()) - } -} - #[derive(Debug)] pub enum LoginResponse { LoginOk { @@ -303,21 +215,6 @@ impl PrivilegedUsersResponse { * ROOM LIST * *===========*/ -#[derive(Debug)] -pub struct RoomListRequest; - -impl RoomListRequest { - pub fn new() -> Self { - RoomListRequest - } -} - -impl WriteToPacket for RoomListRequest { - fn write_to_packet(&self, _: &mut Packet) -> io::Result<()> { - Ok(()) - } -} - #[derive(Debug)] pub struct RoomListResponse { pub rooms: Vec<(String, u32)>, @@ -387,30 +284,6 @@ impl RoomListResponse { } } -/*=================* - * SET LISTEN PORT * - *=================*/ - -#[derive(Debug)] -pub struct SetListenPortRequest { - port: u16, -} - -impl SetListenPortRequest { - fn new(port: u16) -> Self { - SetListenPortRequest { - port: port, - } - } -} - -impl WriteToPacket for SetListenPortRequest { - fn write_to_packet(&self, packet: &mut Packet) -> io::Result<()> { - try!(packet.write_uint(self.port as u32)); - Ok(()) - } -} - /*===================* * WISHLIST INTERVAL * *===================*/ @@ -428,3 +301,4 @@ impl WishlistIntervalResponse { }) } } +