|
|
|
@ -39,14 +39,14 @@ impl RecvOptions {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> { |
|
|
|
|
pub fn start(options: RecvOptions, verbose_output: bool) -> Result<(), Error> { |
|
|
|
|
let listener: net::TcpListener; |
|
|
|
|
match net::TcpListener::bind(options.listen_address) { |
|
|
|
|
Ok(l) => { |
|
|
|
|
listener = l; |
|
|
|
|
} |
|
|
|
|
Err(error) => { |
|
|
|
|
return Some(Error::new(format!("could not create TCP listener: {}", error).as_str())); |
|
|
|
|
return Err(Error::new(format!("could not create TCP listener: {}", error).as_str())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -63,7 +63,7 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
address = a; |
|
|
|
|
} |
|
|
|
|
Err(error) => { |
|
|
|
|
return Some(Error::new(format!("error accepting a new incoming connection: {}", error).as_str())); |
|
|
|
|
return Err(Error::new(format!("error accepting a new incoming connection: {}", error).as_str())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -83,7 +83,7 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
if verbose_output { |
|
|
|
|
println!("{} did not provide valid handshake packet. Dropping connection...", address); |
|
|
|
|
} |
|
|
|
|
connection.shutdown(net::Shutdown::Both); |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -92,7 +92,9 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
PacketType::Handshake => {
|
|
|
|
|
match handshake.from_bytes(incoming_packet.as_bytes()) { |
|
|
|
|
Some(error) => { |
|
|
|
|
return Some( |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
|
|
|
|
|
return Err( |
|
|
|
|
Error::new( |
|
|
|
|
format!("error constructing handshake from {}: {}", address, error.text).as_str() |
|
|
|
|
)); |
|
|
|
@ -108,7 +110,9 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
match send_packet(&mut connection, &packets::HandshakeAccept{}) { |
|
|
|
|
Ok(()) => {} |
|
|
|
|
Err(error) => { |
|
|
|
|
return Some( |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
|
|
|
|
|
return Err( |
|
|
|
|
Error::new(format!("error accepting handshake from {}: {}", address, error.text).as_str()) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
@ -122,7 +126,7 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
if verbose_output { |
|
|
|
|
println!("{} didn't send a handshake packet, but {:?} packet instead. Dropping connection...", address, incoming_packet.get_type()); |
|
|
|
|
} |
|
|
|
|
connection.shutdown(net::Shutdown::Both); |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -140,7 +144,9 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Err(_) => { |
|
|
|
|
return Some( |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
|
|
|
|
|
return Err( |
|
|
|
|
Error::new("could not clone connection") |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
@ -177,7 +183,9 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Err(error) => { |
|
|
|
|
return Some(Error::new( |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
|
|
|
|
|
return Err(Error::new( |
|
|
|
|
format!("error receiving a new packet from listener thread: {}", error).as_str() |
|
|
|
|
)); |
|
|
|
|
} |
|
|
|
@ -188,7 +196,7 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
if verbose_output { |
|
|
|
|
println!("{} shuts down the connection", address); |
|
|
|
|
} |
|
|
|
|
connection.shutdown(net::Shutdown::Both); |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
@ -203,10 +211,9 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
|
|
|
|
|
Some(error) => { |
|
|
|
|
// close connection and exit
|
|
|
|
|
send_packet(&mut connection, &packets::ConnectionShutdown{}); |
|
|
|
|
connection.shutdown(net::Shutdown::Both); |
|
|
|
|
packets::close_connection(&mut connection); |
|
|
|
|
|
|
|
|
|
return Some( |
|
|
|
|
return Err( |
|
|
|
|
Error::new(format!("could not get text packet: {}", error.text).as_str()) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
@ -217,5 +224,5 @@ pub fn start(options: RecvOptions, verbose_output: bool) -> Option<Error> {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return None; |
|
|
|
|
return Ok(()); |
|
|
|
|
} |
|
|
|
|