Skip to main content

UdpSocketBuilder

Struct UdpSocketBuilder 

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

A UDP socket to set up before it binds: what only a socket not bound yet takes (SO_REUSEPORT among sockets that share a port, IPV6_V6ONLY, a device), and the rest before the first datagram.

use nuclei::net::UdpSocketBuilder;

let builder = UdpSocketBuilder::new_v4()?;
builder.set_reuseaddr(true)?;
builder.set_recv_buffer_size(256 * 1024)?;
let socket = builder.bind("127.0.0.1:0".parse().unwrap())?;
let addr = socket.local_addr()?;
socket.send_to(b"hi", addr).await?;
let mut buf = [0u8; 2];
assert_eq!(socket.recv_from(&mut buf).await?, (2, addr));

Implementations§

Source§

impl UdpSocketBuilder

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 UdpSocketBuilder

Source

pub fn new_v4() -> Result<UdpSocketBuilder>

A new IPv4 UDP socket.

§Errors

When the system has no socket to give.

Source

pub fn new_v6() -> Result<UdpSocketBuilder>

A new IPv6 UDP socket.

§Errors

When the system has no socket to give.

Source

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

The address the socket is bound to; before it binds, the unspecified one on unix, and an error (InvalidInput) on Windows, as std’s local_addr answers there.

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<UdpSocket>

Binds the socket to addr, and makes it a UdpSocket.

§Errors

As bind(2): the address is in use (shared only by sockets that all set SO_REUSEPORT, or on Windows SO_REUSEADDR), or not this host’s.

Trait Implementations§

Source§

impl AsFd for UdpSocketBuilder

Available on Unix only.
Source§

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

Borrows the file descriptor. Read more
Source§

impl AsRawFd for UdpSocketBuilder

Available on Unix only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for UdpSocketBuilder

Source§

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

Formats the value using the given formatter. 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.