199fff7efad7096b5071f9e4876496762b6d4e84
1 use std
::io
::{ MemWriter
, Acceptor
, Listener
, TcpStream
, IoResult
, IoError
, EndOfFile
};
2 use std
::io
::net
::tcp
::{ TcpAcceptor
, TcpListener
};
4 use packet
::{ Packet
, Command
, Answer
, Error
, ReadingResult
, PacketType
};
6 static DEFAULT_TIMEOUT
: Option
<u64> = Some(500); // [ms].
18 current_timestamp
: u64
22 pub fn new(interface
: &str, port
: u16) -> IoResult
<Server
> {
23 let mut acceptor
= try!(TcpListener
::bind(interface
, port
).listen());
26 acceptor
: acceptor
.clone()
31 for stream
in acceptor
.incoming() {
34 Ok(stream
) => spawn(proc() {
35 Server
::handle_client(EndPoint
{ socket
: stream
, current_timestamp
: 0 });
45 pub fn close(&mut self) -> IoResult
<()> {
46 self.acceptor
.close_accept()
49 fn handle_client(mut end_point
: EndPoint
) {
51 match end_point
.read() {
52 Ok(packet@Packet
{ t
: Command(..), .. }) => {
53 println!("[Server] Valid command received: {}", packet
);
54 let answer
= Answer(Packet
::random_packet_data([]));
55 match end_point
.send(answer
.clone()) {
57 println!("[Server] Answer sent: {}", answer
),
59 println!("[Server] Can't send the answer. Error: {}", e
)
62 // Socket has been closed.
63 Err(packet
::IOReadError(IoError
{ kind
: EndOfFile
, .. })) => {
64 println!("[Server] Connection closed: EOF");
68 println!("[Server] Error or invalid packet: {}", other
)
75 pub fn new(address
: &str, port
: u16) -> IoResult
<Client
> {
76 Ok(Client
{ end_point
: EndPoint
{
77 socket
: try!(TcpStream
::connect(address
, port
)),
82 pub fn close(&mut self) -> IoResult
<()> {
83 self.end_point
.close()
86 pub fn send(&mut self, packet
: PacketType
) {
89 println!("[Client] Sending: {}", packet
);
90 match self.end_point
.send_with_result(packet
) {
91 Ok(Ok(packet@Packet
{ t
: Answer(..), .. })) =>
92 println!("[Client] Command transmitted correctly, answer: {}", packet
),
94 println!("[Client] Command transmitted correctly, wrong answer: {}", packet
),
96 println!("[Client] Answer error: {}", e
),
98 println!("[Client] Can't send the packet. Error: {}", e
)
102 println!("[Client] Cannot send this type of packet: {}", other
)
106 /// Send some valid and not-valid packets to the server and check the result.
107 pub fn start_tests(address
: &str, port
: u16) {
108 let execute
= |f
: &mut |&mut Client
| -> bool
| -> bool
{
109 match Client
::new(address
, port
) {
110 Ok(ref mut client
) => (*f
)(client
),
112 println!("Unable to create a client. Error: {}", e
);
118 fn raw_packet(timestamp
: u64) -> Vec
<u8> {
119 let mut m
= MemWriter
::new();
120 match (Packet
{ t
: Command(Packet
::random_packet_data([42])), timestamp
: timestamp
}).write(&mut m
) {
126 let mut tests
= vec!(
127 |client
: &mut Client
| -> bool
{
128 println!("Send a valid packet...");
129 match client
.end_point
.send_with_result(Command(Packet
::random_packet_data([42]))) {
130 Ok(Ok(packet@Packet
{ t
: Answer(..), .. })) => true,
132 println!("Error: {}", other
);
138 |client
: &mut Client
| -> bool
{
139 println!("Send a packet with an unknown type...");
140 client
.end_point
.current_timestamp
+= 1;
141 let mut raw_packet
= raw_packet(client
.end_point
.current_timestamp
);
142 raw_packet
[2] = 0xEE; // Alter the type.
143 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
144 Ok(Err(packet
::IOReadError( IoError
{ kind
: TimedOut
, .. }))) => true, // OK: the server should not send any packet.
146 println!("Error: {}", other
);
152 |client
: &mut Client
| -> bool
{
153 println!("Send a packet with an old timestamp...");
154 client
.end_point
.current_timestamp
+= 1;
155 let raw_packet
= raw_packet(0);
156 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
157 Ok(Err(packet
::IOReadError( IoError
{ kind
: TimedOut
, .. }))) => true, // OK: the server should not send any packet.
159 println!("Error: {}", other
);
165 |client
: &mut Client
| -> bool
{
166 println!("Send a packet with altered crypted data...");
167 client
.end_point
.current_timestamp
+= 1;
168 let mut raw_packet
= raw_packet(client
.end_point
.current_timestamp
);
169 raw_packet
[11] = 0xDE;
170 raw_packet
[12] = 0xAD;
171 raw_packet
[13] = 0xBE;
172 raw_packet
[14] = 0xEF;
173 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
174 Ok(Ok(packet@Packet
{ t
: Error(packet
::AuthError
), .. })) => true,
176 println!("Error: {}", other
);
182 |client
: &mut Client
| -> bool
{
183 println!("Send a packet with too small data...");
184 let mut command
= Command(Packet
::new_packet_data(0, Vec
::from_elem(6, 0x00)));
185 match client
.end_point
.send_with_result(command
) {
186 Ok(Err(packet
::IOReadError( IoError
{ kind
: TimedOut
, .. }))) => true, // OK: the server should not send any packet.
188 println!("Error: {}", other
);
194 |client
: &mut Client
| -> bool
{
195 println!("Send a packet with too large data...");
196 let mut command
= Command(Packet
::new_packet_data(0, Vec
::from_elem(40, 0x00)));
197 match client
.end_point
.send_with_result(command
) {
198 Ok(Err(packet
::IOReadError( IoError
{ kind
: TimedOut
, .. }))) => true, // OK: the server should not send any packet.
200 println!("Error: {}", other
);
206 |client
: &mut Client
| -> bool
{
207 println!("Send a packet with wrong padding...");
208 client
.end_point
.current_timestamp
+= 1;
209 let mut m
= MemWriter
::new();
210 let raw_packet
= match (Packet
{ t
: Command(Packet
::random_packet_data([42])), timestamp
: client
.end_point
.current_timestamp
}).write_with_padding_fun(&mut m
, |_
, _
| -> u8 { 0 }) {
215 match client
.end_point
.send_raw_with_result(raw_packet
.as_slice()) {
216 Ok(Ok(packet@Packet
{ t
: Error(packet
::CryptError
), .. })) => true,
218 println!("Error: {}", other
);
225 let mut nb_test_passed
= 0;
226 for (i
, test
) in range(1, tests
.len()+1).zip(tests
.iter_mut()) {
227 println!("===== Test case #{}:", i
)
230 println!("===== Test passed");
232 println!("===== Test failed");
236 if nb_test_passed
== tests
.len() {
237 println!("All tests passed");
239 println!("#{} test(s) failed", tests
.len() - nb_test_passed
);
245 fn close(&mut self) -> IoResult
<()> {
246 try!(self.socket
.close_read());
247 try!(self.socket
.close_write());
251 /// Send a packet and wait for an answer synchronously.
252 fn send_with_result(&mut self, p
: PacketType
) -> IoResult
<ReadingResult
> {
259 /// Send arbitrary data and wait for an answer synchronously.
260 /// Do not increment the current timestamp.
261 fn send_raw_with_result(&mut self, p
: &[u8]) -> IoResult
<ReadingResult
> {
262 self.socket
.set_timeout(DEFAULT_TIMEOUT
);
263 match self.socket
.write(p
) {
269 fn send(&mut self, p
: PacketType
) -> IoResult
<()> {
270 self.socket
.set_timeout(DEFAULT_TIMEOUT
);
271 self.current_timestamp
+= 1;
272 match (Packet
{ t
: p
, timestamp
: self.current_timestamp
}).write(&mut self.socket
) {
273 Err(packet
::WriteIOError(e
)) => Err(e
),
278 fn read(&mut self) -> ReadingResult
{
279 fn send_error(ep
: &mut EndPoint
, error_type
: packet
::ErrorType
) {
280 match ep
.send(Error(error_type
)) {
281 Err(e
) => println!("Unable to send error packet: {}", e
),
286 self.socket
.set_timeout(DEFAULT_TIMEOUT
);
287 match Packet
::read(&mut self.socket
) {
289 if packet
.timestamp
<= self.current_timestamp
{
290 println!("Error, timestamp mismatch, current timestamp: {}, packet received: {}", self.current_timestamp
, packet
);
291 Err(packet
::InvalidTimestampError
)
293 self.current_timestamp
= packet
.timestamp
+ 1;
297 e @
Err(packet
::PaddingError
) => {
298 self.current_timestamp
+= 1;
299 send_error(self, packet
::CryptError
);
302 e @
Err(packet
::MACMismatchError
) => {
303 self.current_timestamp
+= 1;
304 send_error(self, packet
::AuthError
);