From b39ccf26b64ba71eaaa66a68d19f307f4d0705c4 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Tue, 17 Dec 2019 10:31:04 +0000 Subject: [PATCH] Allowing the two provider listeners to run in parallel rather than just concurrently --- Cargo.lock | 52 +++++++++++++++++++++++++++++++++++++ src/provider/mod.rs | 47 +++++++++++++++++++++++++-------- src/provider/storage/mod.rs | 5 ++++ 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd56c97e08..550fce94ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -797,6 +797,11 @@ name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazycell" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.65" @@ -892,6 +897,17 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mio-extras" +version = "2.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mio-named-pipes" version = "0.1.6" @@ -977,12 +993,16 @@ dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "curve25519-dalek 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "pem 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_distr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", "sfw-provider-requests 0.1.0", "sphinx 0.1.0", "tokio 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "ws 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1484,6 +1504,17 @@ dependencies = [ "sphinx 0.1.0", ] +[[package]] +name = "sha-1" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "sha2" version = "0.8.0" @@ -1949,6 +1980,23 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ws" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-extras 2.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ws2_32-sys" version = "0.2.1" @@ -2053,6 +2101,7 @@ dependencies = [ "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum keystream 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c33070833c9ee02266356de0c43f723152bd38bd96ddf52c82b3af10c9138b28" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" "checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" "checksum lioness 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4ae926706ba42c425c9457121178330d75e273df2e82e28b758faf3de3a9acb9" "checksum lock_api 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e57b3997725d2b60dbec1297f6c2e2957cc383db1cebd6be812163f969c7d586" @@ -2065,6 +2114,7 @@ dependencies = [ "checksum mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599" "checksum miniz_oxide 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6f3f74f726ae935c3f514300cc6773a0c9492abc5e972d42ba0c0ebb88757625" "checksum mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)" = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" +"checksum mio-extras 2.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" "checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" @@ -2126,6 +2176,7 @@ dependencies = [ "checksum serde_derive 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)" = "a8c6faef9a2e64b0064f48570289b4bf8823b7581f1d6157c1b52152306651d0" "checksum serde_json 1.0.44 (registry+https://github.com/rust-lang/crates.io-index)" = "48c575e0cc52bdd09b47f330f646cf59afc586e9c4e3ccd6fc1f625b8ea1dad7" "checksum serde_urlencoded 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "642dd69105886af2efd227f75a520ec9b44a820d65bc133a9131f7d229fd165a" +"checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" @@ -2178,4 +2229,5 @@ dependencies = [ "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum winreg 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b2986deb581c4fe11b621998a5e53361efe6b48a151178d0cd9eeffa4dc6acc9" +"checksum ws 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a2c47b5798ccc774ffb93ff536aec7c4275d722fd9c740c83cdd1af1f2d94" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/src/provider/mod.rs b/src/provider/mod.rs index 4fb98cf6c2..cdad1b83b0 100644 --- a/src/provider/mod.rs +++ b/src/provider/mod.rs @@ -10,6 +10,7 @@ use tokio::runtime::Runtime; use crate::provider::client_handling::{ClientProcessingData, ClientRequestProcessor}; use crate::provider::mix_handling::{MixPacketProcessor, MixProcessingData}; use crate::provider::storage::ClientStorage; +use futures::io::Error; mod client_handling; mod mix_handling; @@ -20,6 +21,32 @@ mod storage; const STORED_MESSAGE_FILENAME_LENGTH: usize = 16; const MESSAGE_RETRIEVAL_LIMIT: usize = 2; +#[derive(Debug)] +pub enum ProviderError { + TcpListenerBindingError, + TcpListenerConnectionError, + TcpListenerUnexpectedEof, + + TcpListenerUnknownError, +} + +impl From for ProviderError { + fn from(err: Error) -> Self { + use ProviderError::*; + match err.kind() { + io::ErrorKind::ConnectionRefused => TcpListenerConnectionError, + io::ErrorKind::ConnectionReset => TcpListenerConnectionError, + io::ErrorKind::ConnectionAborted => TcpListenerConnectionError, + io::ErrorKind::NotConnected => TcpListenerConnectionError, + + io::ErrorKind::AddrInUse => TcpListenerBindingError, + io::ErrorKind::AddrNotAvailable => TcpListenerBindingError, + io::ErrorKind::UnexpectedEof => TcpListenerUnexpectedEof, + _ => TcpListenerUnknownError, + } + } +} + pub struct ServiceProvider { mix_network_address: SocketAddr, client_network_address: SocketAddr, @@ -127,9 +154,9 @@ impl ServiceProvider { } } - async fn start_mixnet_listening(&self) -> Result<(), Box> { - let mut listener = tokio::net::TcpListener::bind(self.mix_network_address).await?; - let processing_data = MixProcessingData::new(self.secret_key, self.store_dir.clone()).add_arc_rwlock(); + async fn start_mixnet_listening(address: SocketAddr, secret_key: Scalar, store_dir: PathBuf) -> Result<(), ProviderError> { + let mut listener = tokio::net::TcpListener::bind(address).await?; + let processing_data = MixProcessingData::new(secret_key, store_dir).add_arc_rwlock(); loop { let (socket, _) = listener.accept().await?; @@ -142,9 +169,9 @@ impl ServiceProvider { } } - async fn start_client_listening(&self) -> Result<(), Box> { - let mut listener = tokio::net::TcpListener::bind(self.client_network_address).await?; - let processing_data = ClientProcessingData::new(self.store_dir.clone()).add_arc_rwlock(); + async fn start_client_listening(address: SocketAddr, store_dir: PathBuf) -> Result<(), ProviderError> { + let mut listener = tokio::net::TcpListener::bind(address).await?; + let processing_data = ClientProcessingData::new(store_dir).add_arc_rwlock(); loop { let (socket, _) = listener.accept().await?; @@ -157,19 +184,17 @@ impl ServiceProvider { } } - async fn start_listeners(&self) -> (Result<(), Box>, Result<(), Box>) { - futures::future::join(self.start_mixnet_listening(), self.start_client_listening()).await - } - pub fn start_listening(&self) -> Result<(), Box> { // Create the runtime, probably later move it to Provider struct itself? // TODO: figure out the difference between Runtime and Handle let mut rt = Runtime::new()?; // let mut h = rt.handle(); + let mix_future = rt.spawn(ServiceProvider::start_mixnet_listening(self.mix_network_address, self.secret_key, self.store_dir.clone())); + let client_future = rt.spawn(ServiceProvider::start_client_listening(self.client_network_address, self.store_dir.clone())); // Spawn the root task rt.block_on(async { - let future_results = self.start_listeners().await; + let future_results = futures::future::join(mix_future, client_future).await; assert!(future_results.0.is_ok() && future_results.1.is_ok()); }); diff --git a/src/provider/storage/mod.rs b/src/provider/storage/mod.rs index cead9e2db0..8cb7b3d077 100644 --- a/src/provider/storage/mod.rs +++ b/src/provider/storage/mod.rs @@ -66,6 +66,11 @@ impl ClientStorage { store_data.message, full_store_path ); + println!( + "string message: {:?}", + std::str::from_utf8(&store_data.message) + ); + // TODO: what to do with surbIDs?? // TODO: this should be called when client sends 'register' request!