Skip to main content

UdpSocket

Struct UdpSocket 

Source
pub struct UdpSocket { /* private fields */ }
Available on crate feature udp only.
Expand description

A UDP socket with the guard in front of it.

let mut socket = net_io::UdpSocket::bind("0.0.0.0:5353")?;
let mut buf = [0u8; 1500];
loop {
    let datagram = socket.recv_from(&mut buf)?;
    if datagram.truncated {
        continue; // larger than we accept
    }
    // Replies are limited to the size of what the peer sent until it is verified.
    socket.send_to(&buf[..datagram.len], datagram.from)?;
}

Implementations§

Source§

impl UdpSocket

Source

pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<UdpSocket, NetError>

Binds with UdpOptions::new. The first resolved address that binds wins.

§Errors

AddrInUse, AddrNotAvailable, PermissionDenied, NoAddresses, Bind.

Source

pub fn bind_with<A: ToSocketAddrs>( addr: A, options: UdpOptions, ) -> Result<UdpSocket, NetError>

Binds with options.

§Errors

As UdpSocket::bind; InvalidConfig for an option out of range.

Source

pub fn recv_from(&mut self, buf: &mut [u8]) -> Result<Datagram, NetError>

Receives one datagram within the budgets. Datagrams over budget are read, dropped, and counted; ICMP error reports are counted and never end the socket. Under a flood the call returns within twice the read timeout.

§Errors

TimedOut { kind: Read } (only with a read timeout), Os.

Source

pub fn recv(&mut self, buf: &mut [u8]) -> Result<Datagram, NetError>

Receives from the connected peer (see UdpSocket::connect).

§Errors

As UdpSocket::recv_from.

Source

pub fn send_to(&mut self, buf: &[u8], to: SocketAddr) -> Result<usize, NetError>

Sends one datagram. To a source not marked verified, the bytes sent within the window never exceed factor times the bytes received from it; a source never heard from gets nothing (an empty datagram aside).

§Errors

Limit { which: Amplification } (nothing was sent), Os.

Source

pub fn send(&mut self, buf: &[u8]) -> Result<usize, NetError>

Sends to the connected peer, under the same guard as UdpSocket::send_to.

§Errors

Closed when not connected; as UdpSocket::send_to.

Source

pub fn connect(&mut self, to: SocketAddr) -> Result<(), NetError>

Fixes the peer: the kernel drops datagrams from other sources, and UdpSocket::send goes to it.

§Errors

Os.

Source

pub fn mark_verified(&mut self, peer: SocketAddr)

The protocol proved peer can receive (a cookie, a handshake): the guard no longer limits sends to it, and its table entry is kept.

Source

pub fn set_read_timeout( &mut self, timeout: Option<Duration>, ) -> Result<(), NetError>

How long a receive waits (None: forever, the default).

§Errors

InvalidConfig for a zero duration, Os.

Source

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

The bound address.

§Errors

Os when getsockname fails.

Source

pub fn stats(&self) -> UdpStats

Counters since the socket was bound.

Trait Implementations§

Source§

impl AsFd for UdpSocket

Available on Unix only.
Source§

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

Borrows the file descriptor. Read more
Source§

impl Debug for UdpSocket

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.