Add option to configure the TCP ttl

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/39/head
trivernis 3 years ago
parent ae52798cd0
commit aedf7c4ba2
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -5,18 +5,27 @@ use std::net::SocketAddr;
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::{TcpListener, TcpStream}; use tokio::net::{TcpListener, TcpStream};
#[derive(Clone, Debug, Default)]
pub struct TcpOptions {
/// The time to live for the socket connection
pub ttl: Option<u32>,
}
#[async_trait] #[async_trait]
impl AsyncStreamProtocolListener for TcpListener { impl AsyncStreamProtocolListener for TcpListener {
type AddressType = SocketAddr; type AddressType = SocketAddr;
type RemoteAddressType = SocketAddr; type RemoteAddressType = SocketAddr;
type Stream = TcpStream; type Stream = TcpStream;
type ListenerOptions = (); type ListenerOptions = TcpOptions;
async fn protocol_bind( async fn protocol_bind(
address: Self::AddressType, address: Self::AddressType,
_: Self::ListenerOptions, options: Self::ListenerOptions,
) -> IPCResult<Self> { ) -> IPCResult<Self> {
let listener = TcpListener::bind(address).await?; let listener = TcpListener::bind(address).await?;
if let Some(ttl) = options.ttl {
listener.set_ttl(ttl)?;
}
Ok(listener) Ok(listener)
} }
@ -40,13 +49,16 @@ impl AsyncProtocolStreamSplit for TcpStream {
#[async_trait] #[async_trait]
impl AsyncProtocolStream for TcpStream { impl AsyncProtocolStream for TcpStream {
type AddressType = SocketAddr; type AddressType = SocketAddr;
type StreamOptions = (); type StreamOptions = TcpOptions;
async fn protocol_connect( async fn protocol_connect(
address: Self::AddressType, address: Self::AddressType,
_: Self::StreamOptions, options: Self::StreamOptions,
) -> IPCResult<Self> { ) -> IPCResult<Self> {
let stream = TcpStream::connect(address).await?; let stream = TcpStream::connect(address).await?;
if let Some(ttl) = options.ttl {
stream.set_ttl(ttl)?;
}
Ok(stream) Ok(stream)
} }

Loading…
Cancel
Save