97104c686e6118eec4a142a0a3ff174679208ef6
2 use std
::io
::{ MemWriter
, Acceptor
, Listener
, TcpStream
, IoResult
, IoError
, EndOfFile
};
3 use std
::io
::net
::tcp
::{ TcpAcceptor
, TcpListener
};
5 use packet
::{ Packet
, ReadingResult
, PacketType
};
6 use packet
::PacketType
::{ Command
, Answer
, Error
};
8 // Default timeout when waiting data on a stream (for instance: 'TcpStream::read').
9 static DEFAULT_TIMEOUT
: Option
<u64> = Some(500); // [ms].
12 acceptor
: TcpAcceptor
,
21 current_timestamp
: u64,
22 variant
: packet
::Variant
,
26 pub fn new(interface
: &str, port
: u16, variant
: packet
::Variant
) -> IoResult
<Server
> {
27 let mut acceptor
= try!(TcpListener
::bind((interface
, port
)).listen());
30 acceptor
: acceptor
.clone()
35 for stream
in acceptor
.incoming() {
37 Ok(stream
) => spawn(proc() {
38 Server
::handle_client(EndPoint
::new(stream
, variant
));
49 pub fn close(&mut self) -> IoResult
<()> {
50 self.acceptor
.close_accept()
53 fn handle_client(mut end_point
: EndPoint
) {
55 match end_point
.read() {
56 Ok(packet@Packet
{ t
: Command(..), .. }) => {
57 end_point
.print("Server", format!("Valid command received: {}", packet
));
58 let answer
= Answer(Packet
::random_packet_data(&[]));
59 match end_point
.send(answer
.clone()) {
61 end_point
.print("Server", format!("Answer sent: {}", answer
)),
63 end_point
.print("Server", format!("Can't send the answer. Error: {}", e
))
66 // Socket has been closed.
67 Err(packet
::ReadingError
::IO(IoError
{ kind
: EndOfFile
, .. })) => {
68 end_point
.print("Server", format!("Connection closed: EOF"));
72 end_point
.print("Server", format!("Error or invalid packet: {}", other
))
79 pub fn new(address
: &str, port
: u16, variant
: packet
::Variant
) -> IoResult
<Client
> {
80 Ok(Client
{ end_point
: EndPoint
{
81 socket
: try!(TcpStream
::connect((address
, port
))),
88 pub fn close(&mut self) -> IoResult
<()> {
89 self.end_point
.close()
92 pub fn send(&mut self, packet
: PacketType
) -> bool
{
95 self.end_point
.print("Client", format!("Sending: {}", packet
));
96 match self.end_point
.send_with_result(packet
) {
97 Ok(Ok(packet@Packet
{ t
: Answer(..), .. })) => {
98 self.end_point
.print("Client", format!("Command transmitted correctly, answer: {}", packet
));
102 self.end_point
.print("Client", format!("Command transmitted correctly, wrong answer: {}", packet
));
106 self.end_point
.print("Client", format!("Answer error: {}", e
));
110 self.end_point
.print("Client", format!("Can't send the packet. Error: {}", e
));
116 self.end_point
.print("Client", format!("Cannot send this type of packet: {}", other
));
122 /// Sends some valid and not-valid packets to the server and check the result.
123 /// For each test a new client is created.
124 pub fn start_tests(address
: &str, port
: u16, variant
: packet
::Variant
) {
125 let execute
= |f
: &mut |&mut Client
| -> bool
| -> bool
{
126 match Client
::new(address
, port
, variant
) {
127 Ok(ref mut client
) => (*f
)(client
),
129 println!("Unable to create a client. Error: {}", e
);
135 fn raw_packet(timestamp
: u64) -> Vec
<u8> {
136 let mut m
= MemWriter
::new();
137 match (Packet
{ t
: Command(Packet
::random_packet_data(&[42])), timestamp
: timestamp
}).write(&mut m
, packet
::Variant
::Weak
) {
143 let mut tests
= vec!(
145 |client
: &mut Client
| -> bool
{
146 println!("Sending a valid packet...");
147 client
.send(Command(Packet
::random_packet_data(&[42])))
150 |client
: &mut Client
| -> bool
{
151 println!("Sending a packet with an unknown type...");
152 client
.end_point
.current_timestamp
+= 1;
153 let mut raw_packet
= raw_packet(client
.end_point
.current_timestamp
);
154 raw_packet
[2] = 0xEE; // Alter the type.
155 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
156 Ok(Err(packet
::ReadingError
::IO( IoError
{ kind
: io
::TimedOut
, .. }))) => true, // OK: the server should not send any packet.
158 println!("Error: {}", other
);
164 |client
: &mut Client
| -> bool
{
165 println!("Sending a packet with an old timestamp...");
166 client
.end_point
.current_timestamp
+= 1;
167 let raw_packet
= raw_packet(0);
168 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
169 Ok(Err(packet
::ReadingError
::IO( IoError
{ kind
: io
::TimedOut
, .. }))) => true, // OK: the server should not send any packet.
171 println!("Error: {}", other
);
177 |client
: &mut Client
| -> bool
{
178 println!("Sending a packet with altered crypted data (do not alter the padding)...");
179 client
.end_point
.current_timestamp
+= 1;
180 let mut raw_packet
= raw_packet(client
.end_point
.current_timestamp
);
181 raw_packet
[11] = 0xDE;
182 raw_packet
[12] = 0xAD;
183 raw_packet
[13] = 0xBE;
184 raw_packet
[14] = 0xEF;
185 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
186 Ok(Ok(Packet
{ t
: Error(packet
::ErrorType
::Auth
), .. })) => true,
188 println!("Error: {}", other
);
194 |client
: &mut Client
| -> bool
{
195 println!("Sending a packet with too small data...");
196 let command
= Command(Packet
::new_packet_data(0, Vec
::from_elem(6, 0x00)));
197 match client
.end_point
.send_with_result(command
) {
198 Ok(Err(packet
::ReadingError
::IO( IoError
{ kind
: io
::TimedOut
, .. }))) => true, // OK: the server should not send any packet.
200 println!("Error: {}", other
);
206 |client
: &mut Client
| -> bool
{
207 println!("Sending a packet with too large data...");
208 let command
= Command(Packet
::new_packet_data(0, Vec
::from_elem(40, 0x00)));
209 match client
.end_point
.send_with_result(command
) {
210 Ok(Err(packet
::ReadingError
::IO( IoError
{ kind
: io
::TimedOut
, .. }))) => true, // OK: the server should not send any packet.
212 println!("Error: {}", other
);
218 |client
: &mut Client
| -> bool
{
219 println!("Sending a packet with wrong padding (all 0)...");
220 client
.end_point
.current_timestamp
+= 1;
221 let mut m
= MemWriter
::new();
222 let raw_packet
= match (Packet
{ t
: Command(Packet
::random_packet_data(&[42])), timestamp
: client
.end_point
.current_timestamp
}).write_with_padding_fun(&mut m
, packet
::Variant
::Weak
, |_
, _
| -> u8 { 0 }) {
227 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
228 Ok(Ok(Packet
{ t
: Error(packet
::ErrorType
::Crypt
), .. })) => true,
230 println!("Error: {}", other
);
237 let mut nb_test_passed
= 0;
238 for (i
, test
) in range(1, tests
.len()+1).zip(tests
.iter_mut()) {
239 println!("===== Test case #{}:", i
)
242 println!("===== Test passed");
244 println!("===== Test failed");
248 if nb_test_passed
== tests
.len() {
249 println!("All tests passed");
251 println!("#{} test(s) failed", tests
.len() - nb_test_passed
);
257 pub fn new(mut socket
: TcpStream
, variant
: packet
::Variant
) -> EndPoint
{
258 let _
= socket
.set_nodelay(true);
259 EndPoint
{ socket
: socket
, current_timestamp
: 0 , variant
: variant
}
263 fn close(&mut self) -> IoResult
<()> {
264 try!(self.socket
.close_read());
265 try!(self.socket
.close_write());
269 fn print(&self, prefix
: &str, s
: String
) {
270 println!("[{}] time: {}. {}", prefix
, self.current_timestamp
, s
);
273 /// Sends a packet and wait for an answer synchronously.
274 fn send_with_result(&mut self, p
: PacketType
) -> IoResult
<ReadingResult
> {
281 /// Sends arbitrary data and wait for an answer synchronously.
282 /// Doesn't increment the current timestamp.
283 pub fn send_raw_with_result(&mut self, p
: &[u8]) -> IoResult
<ReadingResult
> {
284 self.socket
.set_timeout(DEFAULT_TIMEOUT
);
285 match self.socket
.write(p
) {
291 fn send(&mut self, p
: PacketType
) -> IoResult
<()> {
292 self.socket
.set_timeout(DEFAULT_TIMEOUT
);
293 self.current_timestamp
+= 1;
294 match (Packet
{ t
: p
, timestamp
: self.current_timestamp
}).write(&mut self.socket
, self.variant
) {
295 Err(packet
::WritingError
::IO(e
)) => Err(e
),
300 fn read(&mut self) -> ReadingResult
{
301 fn send_error(ep
: &mut EndPoint
, error_type
: packet
::ErrorType
) {
302 if let Err(e
) = ep
.send(Error(error_type
)) {
303 println!("Unable to send error packet: {}", e
)
307 self.socket
.set_timeout(DEFAULT_TIMEOUT
);
308 match Packet
::read(&mut self.socket
, self.variant
) {
310 if packet
.timestamp
<= self.current_timestamp
{
311 println!("Error, timestamp mismatch, current timestamp: {}, packet received: {}", self.current_timestamp
, packet
);
312 Err(packet
::ReadingError
::InvalidTimestamp
)
314 self.current_timestamp
= packet
.timestamp
+ 1;
318 e @
Err(packet
::ReadingError
::Padding
) => {
319 self.current_timestamp
+= 1;
320 send_error(self, packet
::ErrorType
::Crypt
);
323 e @
Err(packet
::ReadingError
::MACMismatch
) => {
324 self.current_timestamp
+= 1;
325 send_error(self, packet
::ErrorType
::Auth
);