Reputation: 439
Trying to create a non blocking ssl stream:
use openssl::ssl::{SslMethod, SslConnector};
use std::io::{Read, Write};
use std::net::TcpStream;
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
let stream = TcpStream::connect("google.com:443").unwrap();
stream.set_nonblocking(true);
let mut stream = connector.connect("google.com", stream).unwrap();
But I got this error:
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value:WouldBlock(MidHandshakeSslStream { stream: SslStream { stream: TcpStream { addr:V4(10.137.0.17:55628), peer: V4(172.217.21.78:443), fd: 3 }, ssl: Ssl { state: "SSLv3/TLSwrite client hello", verify_result: X509VerifyResult { code: 0, error: "ok" } } }, error: Error { code: ErrorCode(2), cause: Some(Io(Os { code: 11, kind: WouldBlock, message: "Resource temporarily unavailable" })) } })', src/libcore/result.rs:1051:5
How can I create a non blocking ssl stream?
Upvotes: 1
Views: 1919
Reputation: 828
With tokio any sockets that are created with it are automatically configured as non-blocking.
I suggest checking out tokio-native-tls and looking at its examples.
It depends on native-tls, an abstraction over platform-specific TLS implementations.
Specifically, this crate uses SChannel on Windows (via the schannel crate), Secure Transport on macOS (via the security-framework crate), and OpenSSL (via the openssl crate) on all other platforms.
Unless you have a very specific use case, this may be just what you need.
Upvotes: 0
Reputation: 439
If a non blocking stream is needed but you don't want to add tokio as dependency a possible solution is:
use openssl::ssl::{SslMethod, SslConnector};
use std::io::{Read, Write};
use std::net::TcpStream;
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
let stream = TcpStream::connect("google.com:443").unwrap();
let mut stream = connector.connect("google.com", stream).unwrap();
let inner_stream = stream.get_ref();
inner_stream.set_nonblocking(true);
Upvotes: 1
Reputation: 13942
The tokio project has tokio-openssl
crate. You probably need to embrace the whole async/await
machinery and use that crate to do nonblocking openssl:
//# openssl = "0.10.25"
//# tokio = "0.2.0-alpha.6"
//# tokio-net = "0.2.0-alpha.6"
//# tokio-openssl = "0.4.0-alpha.6"
use openssl::ssl::{SslMethod, SslConnector};
use tokio::net::TcpStream;
use tokio::prelude::*;
use tokio_net::driver::Handle;
use tokio_openssl::connect;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let sslconf = SslConnector::builder(SslMethod::tls())?
.build()
.configure()?;
// The following 3 lines are equivalent to:
// let stream = TcpStream::connect("google.com:443").await?;
// It's just going to show that the socket is indeed nonblocking.
let stream = std::net::TcpStream::connect("google.com:443")?;
stream.set_nonblocking(true)?;
let stream = TcpStream::from_std(stream, &Handle::default())?;
let mut stream = connect(sslconf, "google.com", stream).await?;
stream.write_all(b"GET / HTTP/1.0\r\n\r\n").await?;
let mut res = vec![];
stream.read_to_end(&mut res).await?;
dbg!(String::from_utf8_lossy(&res));
Ok(())
}
Of course this also means that for now you'll have to use beta/nightly channel. It may or may not work out for your project.
Upvotes: 1