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