Skip to main content

TcpSocket

Struct TcpSocket 

Source
pub struct TcpSocket { /* private fields */ }
Expand description

A TCP socket to set up before it connects or listens.

use nuclei::net::TcpSocket;

let socket = TcpSocket::new_v4()?;
socket.set_reuseaddr(true)?;
socket.bind("127.0.0.1:0".parse().unwrap())?;
let addr = socket.local_addr()?;
let listener = socket.listen(128)?;

let client = TcpSocket::new_v4()?;
client.set_nodelay(true)?;
let stream = client.connect(addr).await?;
let (_peer, _) = listener.accept().await?;
assert!(stream.nodelay()?);

Implementations§

Source§

impl TcpSocket

Source

pub fn set_reuseaddr(&self, reuseaddr: bool) -> Result<()>

Sets SO_REUSEADDR: the socket may bind an address still held by a connection that is closing.

Source

pub fn reuseaddr(&self) -> Result<bool>

Whether SO_REUSEADDR is set.

Source

pub fn set_reuseport(&self, reuseport: bool) -> Result<()>

Sets SO_REUSEPORT: several sockets may bind the same address and port, and the system shares what comes among them.

Source

pub fn reuseport(&self) -> Result<bool>

Whether SO_REUSEPORT is set.

Source

pub fn set_send_buffer_size(&self, size: u32) -> Result<()>

Sets the size of the system’s send buffer (SO_SNDBUF); the system may round it (Linux doubles it).

Source

pub fn send_buffer_size(&self) -> Result<u32>

The size of the system’s send buffer (SO_SNDBUF).

Source

pub fn set_recv_buffer_size(&self, size: u32) -> Result<()>

Sets the size of the system’s receive buffer (SO_RCVBUF); the system may round it (Linux doubles it).

Source

pub fn recv_buffer_size(&self) -> Result<u32>

The size of the system’s receive buffer (SO_RCVBUF).

Source

pub fn set_tos(&self, tos: u32) -> Result<()>

Sets the type of service of the socket’s IPv4 packets (IP_TOS).

Source

pub fn tos(&self) -> Result<u32>

The type of service of the socket’s IPv4 packets (IP_TOS).

Source

pub fn set_tclass_v6(&self, tclass: u32) -> Result<()>

Sets the traffic class of the socket’s IPv6 packets (IPV6_TCLASS).

Source

pub fn tclass_v6(&self) -> Result<u32>

The traffic class of the socket’s IPv6 packets (IPV6_TCLASS).

Source

pub fn set_only_v6(&self, only_v6: bool) -> Result<()>

Sets IPV6_V6ONLY on an IPv6 socket: with true it takes IPv6 alone, else IPv4 too, through addresses mapped into IPv6.

§Errors

As setsockopt(2): an IPv4 socket has no such option, and a bound one may no longer change it.

Source

pub fn only_v6(&self) -> Result<bool>

Whether IPV6_V6ONLY is set.

§Errors

As getsockopt(2): an IPv4 socket has no such option.

Source

pub fn bind_device(&self, interface: Option<&[u8]>) -> Result<()>

Binds the socket to the network interface named interface (SO_BINDTODEVICE), or, with None, to none.

Source

pub fn device(&self) -> Result<Option<DeviceName>>

The network interface the socket is bound to, if any (SO_BINDTODEVICE).

Source§

impl TcpSocket

Source

pub fn new_v4() -> Result<TcpSocket>

A new IPv4 TCP socket.

§Errors

When the system has no socket to give.

Source

pub fn new_v6() -> Result<TcpSocket>

A new IPv6 TCP socket.

§Errors

When the system has no socket to give.

Source

pub fn set_keepalive(&self, keepalive: bool) -> Result<()>

Sets SO_KEEPALIVE: the system probes an idle connection.

Source

pub fn keepalive(&self) -> Result<bool>

Whether SO_KEEPALIVE is set.

Source

pub fn set_linger(&self, linger: Option<Duration>) -> Result<()>

Sets SO_LINGER: with Some(time) a close waits up to time (whole seconds) for unsent data; with None it returns at once and the system sends what is left.

Source

pub fn set_zero_linger(&self) -> Result<()>

Sets SO_LINGER to zero: a close drops unsent data and resets the connection.

Source

pub fn linger(&self) -> Result<Option<Duration>>

SO_LINGER: how long a close waits for unsent data, None if it does not wait.

Source

pub fn set_nodelay(&self, nodelay: bool) -> Result<()>

Sets TCP_NODELAY: small writes go out at once, not gathered.

Source

pub fn nodelay(&self) -> Result<bool>

Whether TCP_NODELAY is set.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

The address the socket is bound to.

Source

pub fn take_error(&self) -> Result<Option<Error>>

The socket’s pending error (SO_ERROR), taken.

Source

pub fn bind(&self, addr: SocketAddr) -> Result<()>

Binds the socket to addr.

§Errors

As bind(2): the address is in use, or not this host’s.

Source

pub async fn connect(self, addr: SocketAddr) -> Result<TcpStream>

Connects the socket to addr, as a stream.

§Errors

As connect(2): refused, unreachable, or timed out.

Source

pub fn listen(self, backlog: u32) -> Result<TcpListener>

Makes the socket a listener, with room for backlog connections not accepted yet.

§Errors

As listen(2); a socket not bound gets an address of the system’s choosing on unix, and an error on Windows.

Source

pub fn from_std_stream(stream: TcpStream) -> TcpSocket

Takes std’s stream over as a socket to set up, connected or not.

Source§

impl TcpSocket

Source

pub fn set_keepalive_timing(&self, keepalive: TcpKeepalive) -> Result<()>

Turns keepalive probes on (SO_KEEPALIVE), timed as keepalive says (TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT; macOS calls the first TCP_KEEPALIVE).

§Errors

Unsupported where the system has no such options, InvalidInput for a value past what it takes, and what setsockopt(2) answers (a zero time or count).

Source

pub fn keepalive_timing(&self) -> Result<TcpKeepalive>

The keepalive’s timing: the system’s values where none was set.

§Errors

Unsupported where the system has no such options.

Source

pub fn set_user_timeout(&self, timeout: Option<Duration>) -> Result<()>

Sets TCP_USER_TIMEOUT (Linux): how long sent bytes may go unacknowledged before the connection is dropped, in whole milliseconds; None or zero leaves it to the system.

§Errors

Unsupported off Linux, InvalidInput for a time past what the system takes.

Source

pub fn user_timeout(&self) -> Result<Option<Duration>>

TCP_USER_TIMEOUT (Linux), None where the system’s own applies.

§Errors

Unsupported off Linux.

Source§

impl TcpSocket

Source

pub fn set_incoming_cpu(&self, cpu: u32) -> Result<()>

Sets SO_INCOMING_CPU (Linux): the CPU whose connections this listener takes among listeners sharing a port with SO_REUSEPORT, so each core accepts the connections its own queues received.

§Errors

Unsupported off Linux, InvalidInput for a CPU past what the system takes.

Source

pub fn incoming_cpu(&self) -> Result<Option<u32>>

SO_INCOMING_CPU (Linux): the CPU set, or the last to handle the socket’s packets; None before either.

§Errors

Unsupported off Linux.

Source

pub fn set_fastopen(&self, queue: u32) -> Result<()>

Sets TCP_FASTOPEN on a socket that will listen: connections may bring data with their handshake. Linux keeps up to queue such handshakes waiting; macOS, FreeBSD and Windows take it as on (above zero) or off.

§Errors

Unsupported where the system has no fast open, InvalidInput for a queue past what it takes, and what setsockopt(2) answers where the system turned fast open off.

Source

pub fn fastopen(&self) -> Result<u32>

TCP_FASTOPEN: the queue’s length on Linux, and 1 or 0 where the system takes it as on or off.

§Errors

Unsupported where the system has no fast open.

Source

pub fn set_defer_accept(&self, wait: Option<Duration>) -> Result<()>

Sets TCP_DEFER_ACCEPT (Linux) on a socket that will listen: a connection is handed to accept only once its first data came, or wait passed (whole seconds, which the kernel rounds up to its retransmission steps); None hands each over at once.

§Errors

Unsupported off Linux, InvalidInput for a time past what the system takes.

Source

pub fn defer_accept(&self) -> Result<Option<Duration>>

TCP_DEFER_ACCEPT (Linux): how long a connection with no data waits, as the kernel rounded it; None if connections are handed over at once.

§Errors

Unsupported off Linux.

Trait Implementations§

Source§

impl AsFd for TcpSocket

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl AsRawFd for TcpSocket

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for TcpSocket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromRawFd for TcpSocket

Source§

unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket

§Safety

fd is an open TCP socket the caller owns and hands over.

Source§

impl IntoRawFd for TcpSocket

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IoObject for T
where T: AsFd,

Source§

fn raw_io(&self) -> i32

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.