Skip to main content

Crate tcp_stream

Crate tcp_stream 

Source
Expand description

A TcpStream with pluggable TLS support.

Wraps std::net::TcpStream in a TcpStream enum that can be upgraded to a TLS-encrypted stream via TcpStream::into_tls. Supported backends are rustls (default), native-tls, and OpenSSL, selected through feature flags. Async variants of the encrypted stream are provided via the futures-io traits when the futures feature is enabled.

§Feature flags

§Async runtime (pick exactly one)

FlagNotes
tokio (default)Requires a running Tokio runtime
smolUses the smol executor
async-global-executorUses async-global-executor

§TLS backend (pick at most one; rustls is the default)

FlagNotes
rustls (default)TLS via rustls
native-tlsTLS via the platform’s native library
opensslTLS via OpenSSL

§Rustls certificate store (only when rustls is active)

FlagNotes
rustls-platform-verifier (default)Uses the platform trust store
rustls-native-certsLoads native root certificates
rustls-webpki-roots-certsUses the webpki bundled root set

§Rustls crypto provider (at least one must be enabled)

FlagNotes
rustls--aws_lc_rs (default)Uses aws-lc-rs
rustls--ringUses ring (more portable)

§Miscellaneous

FlagNotes
futuresEnable futures-io async trait impls on the encrypted stream
vendored-opensslBuild a vendored OpenSSL (requires openssl feature)

§Example

To connect to a remote server:

use tcp_stream::{HandshakeError, TcpStream, TLSConfig};

use std::io::{self, Read, Write};

let mut stream = TcpStream::connect("www.rust-lang.org:443").unwrap();
stream.set_nonblocking(true).unwrap();

loop {
    if stream.try_connect().unwrap() {
        break;
    }
}

let mut stream = stream.into_tls("www.rust-lang.org", TLSConfig::default());

while let Err(HandshakeError::WouldBlock(mid_handshake)) = stream {
    stream = mid_handshake.handshake();
}

let mut stream = stream.unwrap();

while let Err(err) = stream.write_all(b"GET / HTTP/1.0\r\n\r\n") {
    if err.kind() != io::ErrorKind::WouldBlock {
        panic!("error: {:?}", err);
    }
}

while let Err(err) = stream.flush() {
    if err.kind() != io::ErrorKind::WouldBlock {
        panic!("error: {:?}", err);
    }
}

let mut res = vec![];
while let Err(err) = stream.read_to_end(&mut res) {
    if err.kind() != io::ErrorKind::WouldBlock {
        panic!("stream error: {:?}", err);
    }
}
println!("{}", String::from_utf8_lossy(&res));

Structs§

NativeTlsConnector
Reexport native-tls’s TlsConnector A builder for client-side TLS connections.
NativeTlsConnectorBuilder
Reexport native-tls’s TlsConnectorBuilder A builder for TlsConnectors.
OpensslConnector
Reexport openssl’s TlsConnector A type which wraps client-side streams in a TLS session.
OpensslMethod
Reexport openssl’s TlsConnector A type specifying the kind of protocol an SslContext will speak.
OwnedTLSConfig
Holds extra TLS configuration
RustlsConnector
Reexport rustls-connector’s TlsConnector A rustls TLS connector ready to perform TLS handshakes.
RustlsConnectorConfig
Reexport rustls-connector’s TlsConnector Configuration helper for RustlsConnector
TLSConfig
Holds extra TLS configuration

Enums§

AsyncTcpStream
Wrapper around plain or TLS async TCP streams
HandshakeError
An error returned while performing the handshake
Identity
Holds one of:
MidHandshakeTlsStream
A TLS stream which has been interrupted during the handshake
OwnedIdentity
Holds one of:
TcpStream
Wrapper around plain or TLS TCP streams

Type Aliases§

HandshakeResult
Holds either the TLS TcpStream result or the current handshake state
NativeTlsAsyncStream
An async TcpStream wrapped by native-tls
NativeTlsHandshakeError
A HandshakeError from native-tls
NativeTlsMidHandshakeTlsStream
A MidHandshakeTlsStream from native-tls
NativeTlsStream
A TcpStream wrapped by native-tls
OpensslAsyncStream
An async TcpStream wrapped by openssl
OpensslErrorStack
An ErrorStack from openssl
OpensslHandshakeError
A HandshakeError from openssl
OpensslMidHandshakeTlsStream
A MidHandshakeTlsStream from openssl
OpensslStream
A TcpStream wrapped by openssl
RustlsAsyncStream
An async TcpStream wrapped by rustls
RustlsHandshakeError
A HandshakeError from rustls-connector
RustlsMidHandshakeTlsStream
A MidHandshakeTlsStream from rustls-connector
RustlsStream
A TcpStream wrapped by rustls