Docs.rs
  • socket2-0.5.10
    • socket2 0.5.10
    • Docs.rs crate page
    • MIT OR Apache-2.0
    • Links
    • Homepage
    • Repository
    • crates.io
    • Source
    • Owners
    • sfackler
    • Thomasdezeeuw
    • github:tokio-rs:socket2
    • Dependencies
      • libc ^0.2.171 normal
      • windows-sys ^0.52 normal
    • Versions
    • 100% of the crate is documented
  • Platform
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate socket2

socket20.5.10

  • All Items

Sections

  • Examples
    • Features

Crate Items

  • Structs
  • Enums

Crates

  • socket2

Crate socket2

Source
Expand description

Utilities for creating and using sockets.

The goal of this crate is to create and use a socket using advanced configuration options (those that are not available in the types in the standard library) without using any unsafe code.

This crate provides as direct as possible access to the system’s functionality for sockets, this means little effort to provide cross-platform utilities. It is up to the user to know how to use sockets when using this crate. If you don’t know how to create a socket using libc/system calls then this crate is not for you. Most, if not all, functions directly relate to the equivalent system call with no error handling applied, so no handling errors such as EINTR. As a result using this crate can be a little wordy, but it should give you maximal flexibility over configuration of sockets.

§Examples

use std::net::{SocketAddr, TcpListener};
use socket2::{Socket, Domain, Type};

// Create a TCP listener bound to two addresses.
let socket = Socket::new(Domain::IPV6, Type::STREAM, None)?;

socket.set_only_v6(false)?;
let address: SocketAddr = "[::1]:12345".parse().unwrap();
socket.bind(&address.into())?;
socket.listen(128)?;

let listener: TcpListener = socket.into();
// ...

§Features

This crate has a single feature all, which enables all functions even ones that are not available on all OSs.

Structs§

Domain
Specification of the communication domain for a socket.
MaybeUninitSlice
A version of IoSliceMut that allows the buffer to be uninitialised.
MsgHdr
Configuration of a sendmsg(2) system call.
MsgHdrMut
Configuration of a recvmsg(2) system call.
Protocol
Protocol specification used for creating sockets via Socket::new.
RecvFlagsNon-Redox
Flags for incoming messages.
SockAddr
The address of a socket.
SockRef
A reference to a Socket that can be used to configure socket types other than the Socket type itself.
Socket
Owned wrapper around a system socket.
TcpKeepalive
Configures a socket’s TCP keepalive parameters.
Type
Specification of communication semantics on a socket.

Enums§

InterfaceIndexOrAddress
A local interface specified by its index or an address assigned to it.

Results

Settings
Help
    struct
    socket2::Socket
    Owned wrapper around a system socket.
    extern crate
    socket2
    Utilities for creating and using sockets.
    method
    socket2::SockAddr::as_socket
    Returns this address as a SocketAddr if it is in the …
    method
    socket2::SockAddr::as_socket_ipv4
    Returns this address as a SocketAddrV4 if it is in the …
    method
    socket2::SockAddr::as_socket_ipv6
    Returns this address as a SocketAddrV6 if it is in the …
    struct
    socket2::SockRef
    A reference to a Socket that can be used to configure …
    method
    socket2::Socket::into_raw_fd
    Socket -> c_int
    method
    socket2::Socket::as_fd
    &Socket -> BorrowedFd
    method
    socket2::Socket::as_raw_fd
    &Socket -> c_int
    method
    socket2::Socket::mss
    &Socket -> Result<u32>
    Gets the value of the TCP_MAXSEG option on this socket.
    method
    socket2::Socket::tos
    &Socket -> Result<u32>
    Get the value of the IP_TOS option for this socket.
    method
    socket2::Socket::ttl
    &Socket -> Result<u32>
    Get the value of the IP_TTL option for this socket.
    method
    socket2::Socket::type
    &Socket -> Result<Type>
    Returns the Type of this socket by checking the SO_TYPE …
    method
    socket2::Socket::nodelay
    &Socket -> Result<bool>
    Get the value of the TCP_NODELAY option on this socket.
    method
    socket2::Socket::only_v6
    &Socket -> Result<bool>
    Get the value of the IPV6_V6ONLY option for this socket.
    method
    socket2::Socket::recv_tos
    &Socket -> Result<bool>
    Get the value of the IP_RECVTOS option for this socket.
    method
    socket2::Socket::broadcast
    &Socket -> Result<bool>
    Get the value of the SO_BROADCAST option for this socket.
    method
    socket2::Socket::keepalive
    &Socket -> Result<bool>
    Get the value of the SO_KEEPALIVE option on this socket.
    method
    socket2::Socket::peer_addr
    &Socket -> Result<SockAddr>
    Returns the socket address of the remote peer of this …
    method
    socket2::Socket::try_clone
    &Socket -> Result<Socket>
    Creates a new independently owned handle to the underlying …
    method
    socket2::Socket::local_addr
    &Socket -> Result<SockAddr>
    Returns the socket address of the local half of this …
    method
    socket2::Socket::reuse_port
    &Socket -> Result<bool>
    Get the value of the SO_REUSEPORT option on this socket.
    method
    socket2::Socket::nonblocking
    &Socket -> Result<bool>
    Returns true if this socket is set to nonblocking mode, …
    method
    socket2::Socket::peek_sender
    &Socket -> Result<SockAddr>
    Retrieve the sender for the data at the head of the …
    method
    socket2::Socket::reuse_address
    &Socket -> Result<bool>
    Get the value of the SO_REUSEADDR option on this socket.
    method
    socket2::Socket::keepalive_time
    &Socket -> Result<Duration>
    Get the value of the TCP_KEEPIDLE option on this socket.
    method
    socket2::Socket::recv_tclass_v6
    &Socket -> Result<bool>
    Get the value of the IPV6_RECVTCLASS option for this …
    method
    socket2::Socket::multicast_if_v4
    &Socket -> Result<Ipv4Addr>
    Get the value of the IP_MULTICAST_IF option for this …
    method
    socket2::Socket::multicast_if_v6
    &Socket -> Result<u32>
    Get the value of the IPV6_MULTICAST_IF option for this …
    method
    socket2::Socket::unicast_hops_v6
    &Socket -> Result<u32>
    Get the value of the IPV6_UNICAST_HOPS option for this …
    method
    socket2::Socket::multicast_ttl_v4
    &Socket -> Result<u32>
    Get the value of the IP_MULTICAST_TTL option for this …
    method
    socket2::Socket::recv_buffer_size
    &Socket -> Result<usize>
    Get value for the SO_RCVBUF option on this socket.
    method
    socket2::Socket::recv_hoplimit_v6
    &Socket -> Result<bool>
    Get the value of the IPV6_RECVHOPLIMIT option for this …
    method
    socket2::Socket::send_buffer_size
    &Socket -> Result<usize>
    Get the value of the SO_SNDBUF option on this socket.
    method
    socket2::Socket::keepalive_retries
    &Socket -> Result<u32>
    Get the value of the TCP_KEEPCNT option on this socket.
    method
    socket2::Socket::multicast_hops_v6
    &Socket -> Result<u32>
    Get the value of the IPV6_MULTICAST_HOPS option for this …
    method
    socket2::Socket::multicast_loop_v4
    &Socket -> Result<bool>
    Get the value of the IP_MULTICAST_LOOP option for this …
    method
    socket2::Socket::multicast_loop_v6
    &Socket -> Result<bool>
    Get the value of the IPV6_MULTICAST_LOOP option for this …
    method
    socket2::Socket::header_included_v4
    &Socket -> Result<bool>
    Get the value of the IP_HDRINCL option on this socket.
    method
    socket2::Socket::header_included_v6
    &Socket -> Result<bool>
    Get the value of the IP_HDRINCL option on this socket.
    method
    socket2::Socket::keepalive_interval
    &Socket -> Result<Duration>
    Get the value of the TCP_KEEPINTVL option on this socket.
    method
    socket2::Socket::out_of_band_inline
    &Socket -> Result<bool>
    Get value for the SO_OOBINLINE option on this socket.
    method
    socket2::Socket::header_included
    &Socket -> Result<bool>
    This method is deprecated, use …
    method
    socket2::Socket::flush
    &mut Socket -> Result<()>
    method
    socket2::Socket::linger
    &Socket -> Result<Option<Duration>>
    Get the value of the SO_LINGER option on this socket.
    method
    socket2::Socket::listen
    &Socket, c_int -> Result<()>
    Mark a socket as ready to accept incoming connection …
    method
    socket2::Socket::set_mss
    &Socket, u32 -> Result<()>
    Sets the value of the TCP_MAXSEG option on this socket.
    method
    socket2::Socket::set_tos
    &Socket, u32 -> Result<()>
    Set the value of the IP_TOS option for this socket.
    method
    socket2::Socket::set_ttl
    &Socket, u32 -> Result<()>
    Set the value of the IP_TTL option for this socket.
    method
    socket2::Socket::shutdown
    &Socket, Shutdown -> Result<()>
    Shuts down the read, write, or both halves of this …
    method
    socket2::Socket::take_error
    &Socket -> Result<Option<Error>>
    Get the value of the SO_ERROR option on this socket.
    method
    socket2::Socket::set_cloexec
    &Socket, bool -> Result<()>
    Sets CLOEXEC on the socket.
    method
    socket2::Socket::set_nodelay
    &Socket, bool -> Result<()>
    Set the value of the TCP_NODELAY option on this socket.
    method
    socket2::Socket::set_only_v6
    &Socket, bool -> Result<()>
    Set the value for the IPV6_V6ONLY option on this socket.
    method
    socket2::Socket::read_timeout
    &Socket -> Result<Option<Duration>>
    Get value for the SO_RCVTIMEO option on this socket.
    method
    socket2::Socket::set_recv_tos
    &Socket, bool -> Result<()>
    Set the value of the IP_RECVTOS option for this socket.
    method
    socket2::Socket::set_broadcast
    &Socket, bool -> Result<()>
    Set the value of the SO_BROADCAST option for this socket.
    method
    socket2::Socket::set_keepalive
    &Socket, bool -> Result<()>
    Set value for the SO_KEEPALIVE option on this socket.
    method
    socket2::Socket::set_nosigpipe
    &Socket, bool -> Result<()>
    Sets SO_NOSIGPIPE on the socket.
    method
    socket2::Socket::write_timeout
    &Socket -> Result<Option<Duration>>
    Get value for the SO_SNDTIMEO option on this socket.
    method
    socket2::Socket::set_reuse_port
    &Socket, bool -> Result<()>
    Set value for the SO_REUSEPORT option on this socket.
    method
    socket2::Socket::device_index_v4
    &Socket -> Result<Option<NonZeroU32>>
    Gets the value for IP_BOUND_IF option on this socket, i.e. …
    method
    socket2::Socket::device_index_v6
    &Socket -> Result<Option<NonZeroU32>>
    Gets the value for IPV6_BOUND_IF option on this socket, …
    method
    socket2::Socket::set_nonblocking
    &Socket, bool -> Result<()>
    Moves this socket into or out of nonblocking mode.
    method
    socket2::Socket::set_reuse_address
    &Socket, bool -> Result<()>
    Set value for the SO_REUSEADDR option on this socket.
    method
    socket2::Socket::set_recv_tclass_v6
    &Socket, bool -> Result<()>
    Set the value of the IPV6_RECVTCLASS option for this …
    method
    socket2::Socket::set_multicast_if_v6
    &Socket, u32 -> Result<()>
    Set the value of the IPV6_MULTICAST_IF option for this …
    method
    socket2::Socket::set_unicast_hops_v6
    &Socket, u32 -> Result<()>
    Set the value for the IPV6_UNICAST_HOPS option on this …
    method
    socket2::Socket::set_multicast_ttl_v4
    &Socket, u32 -> Result<()>
    Set the value of the IP_MULTICAST_TTL option for this …
    method
    socket2::Socket::set_recv_buffer_size
    &Socket, usize -> Result<()>
    Set value for the SO_RCVBUF option on this socket.
    method
    socket2::Socket::set_recv_hoplimit_v6
    &Socket, bool -> Result<()>
    Set the value of the IPV6_RECVHOPLIMIT option for this …
    method
    socket2::Socket::set_send_buffer_size
    &Socket, usize -> Result<()>
    Set value for the SO_SNDBUF option on this socket.
    method
    socket2::Socket::set_multicast_hops_v6
    &Socket, u32 -> Result<()>
    Set the value of the IPV6_MULTICAST_HOPS option for this …
    method
    socket2::Socket::set_multicast_loop_v4
    &Socket, bool -> Result<()>
    Set the value of the IP_MULTICAST_LOOP option for this …
    method
    socket2::Socket::set_multicast_loop_v6
    &Socket, bool -> Result<()>
    Set the value of the IPV6_MULTICAST_LOOP option for this …
    method
    socket2::Socket::set_header_included_v4
    &Socket, bool -> Result<()>
    Set the value of the IP_HDRINCL option on this socket.
    method
    socket2::Socket::set_header_included_v6
    &Socket, bool -> Result<()>
    Set the value of the IP_HDRINCL option on this socket.
    method
    socket2::Socket::set_out_of_band_inline
    &Socket, bool -> Result<()>
    Set value for the SO_OOBINLINE option on this socket.
    method
    socket2::Socket::device_index
    &Socket -> Result<Option<NonZeroU32>>
    This method is deprecated, use …
    method
    socket2::Socket::set_header_included
    &Socket, bool -> Result<()>
    This method is deprecated, use …
    method
    socket2::Socket::fmt
    &Socket, &mut Formatter -> Result
    method
    socket2::Socket::bind
    &Socket, &SockAddr -> Result<()>
    Binds this socket to the specified address.
    method
    socket2::Socket::accept
    &Socket -> Result<(Socket, SockAddr)>
    Accept a new incoming connection from this listener.
    method
    socket2::Socket::connect
    &Socket, &SockAddr -> Result<()>
    Initiate a connection on this socket to the specified …
    method
    socket2::Socket::accept_raw
    &Socket -> Result<(Socket, SockAddr)>
    Accept a new incoming connection from this listener.
    method
    socket2::Socket::set_linger
    &Socket, Option<Duration> -> Result<()>
    Set value for the SO_LINGER option on this socket.
    method
    socket2::Socket::set_read_timeout
    &Socket, Option<Duration> -> Result<()>
    Set value for the SO_RCVTIMEO option on this socket.
    method
    socket2::Socket::set_tcp_keepalive
    &Socket, &TcpKeepalive -> Result<()>
    Set parameters configuring TCP keepalive probes for this …
    method
    socket2::Socket::set_write_timeout
    &Socket, Option<Duration> -> Result<()>
    Set value for the SO_SNDTIMEO option on this socket.
    method
    socket2::Socket::set_multicast_if_v4
    &Socket, &Ipv4Addr -> Result<()>
    Set the value of the IP_MULTICAST_IF option for this …
    method
    socket2::Socket::bind_device_by_index_v4
    &Socket, Option<NonZeroU32> -> Result<()>
    Sets the value for IP_BOUND_IF option on this socket.
    method
    socket2::Socket::bind_device_by_index_v6
    &Socket, Option<NonZeroU32> -> Result<()>
    Sets the value for IPV6_BOUND_IF option on this socket.
    method
    socket2::Socket::bind_device_by_index
    &Socket, Option<NonZeroU32> -> Result<()>
    This method is deprecated, use …
    method
    socket2::Socket::send
    &Socket, &[u8] -> Result<usize>
    Sends data on the socket to a connected peer.
    method
    socket2::Socket::sendmsg
    &Socket, &MsgHdr, c_int -> Result<usize>
    Send a message on a socket using a message structure.
    method
    socket2::Socket::send_vectored
    &Socket, &[IoSlice] -> Result<usize>
    Send data to the connected peer. Returns the amount of …
    method
    socket2::Socket::connect_timeout
    &Socket, &SockAddr, Duration -> Result<()>
    Initiate a connection on this socket to the specified …
    method
    socket2::Socket::send_out_of_band
    &Socket, &[u8] -> Result<usize>
    Sends out-of-band (OOB) data on the socket to connected …
    method
    socket2::Socket::join_multicast_v6
    &Socket, &Ipv6Addr, u32 -> Result<()>
    Join a multicast group using IPV6_ADD_MEMBERSHIP option on …
    method
    socket2::Socket::leave_multicast_v6
    &Socket, &Ipv6Addr, u32 -> Result<()>
    Leave a multicast group using IPV6_DROP_MEMBERSHIP option …
    method
    socket2::Socket::write
    &mut Socket, &[u8] -> Result<usize>
    method
    socket2::Socket::recvmsg
    &Socket, &mut MsgHdrMut, c_int -> Result<usize>
    Receive a message from a socket using a message structure.
    method
    socket2::Socket::write_vectored
    &mut Socket, &[IoSlice] -> Result<usize>
    method
    socket2::Socket::send_with_flags
    &Socket, &[u8], c_int -> Result<usize>
    Identical to send but allows for specification of …
    method
    socket2::Socket::join_multicast_v4
    &Socket, &Ipv4Addr, &Ipv4Addr -> Result<()>
    Join a multicast group using IP_ADD_MEMBERSHIP option on …
    method
    socket2::Socket::leave_multicast_v4
    &Socket, &Ipv4Addr, &Ipv4Addr -> Result<()>
    Leave a multicast group using IP_DROP_MEMBERSHIP option on …
    method
    socket2::Socket::join_multicast_v4_n
    &Socket, &Ipv4Addr, &InterfaceIndexOrAddress -> Result<()>
    Join a multicast group using IP_ADD_MEMBERSHIP option on …
    method
    socket2::Socket::leave_multicast_v4_n
    &Socket, &Ipv4Addr, &InterfaceIndexOrAddress -> Result<()>
    Leave a multicast group using IP_DROP_MEMBERSHIP option on …
    method
    socket2::Socket::send_vectored_with_flags
    &Socket, &[IoSlice], c_int -> Result<usize>
    Identical to send_vectored but allows for specification of …
    method
    socket2::Socket::peek
    &Socket, &mut [MaybeUninit<u8>] -> Result<usize>
    Receives data on the socket from the remote adress to …
    method
    socket2::Socket::read
    &mut Socket, &mut [u8] -> Result<usize>
    method
    socket2::Socket::recv
    &Socket, &mut [MaybeUninit<u8>] -> Result<usize>
    Receives data on the socket from the remote address to …
    method
    socket2::Socket::send_to
    &Socket, &[u8], &SockAddr -> Result<usize>
    Sends data on the socket to the given address. On success, …
    method
    socket2::Socket::read_vectored
    &mut Socket, &mut [IoSliceMut] -> Result<usize>
    method
    socket2::Socket::recv_out_of_band
    &Socket, &mut [MaybeUninit<u8>] -> Result<usize>
    Receives out-of-band (OOB) data on the socket from the …
    method
    socket2::Socket::send_to_vectored
    &Socket, &[IoSlice], &SockAddr -> Result<usize>
    Send data to a peer listening on addr. Returns the amount …
    method
    socket2::Socket::sendfile
    &Socket, &F, usize, Option<NonZeroUsize> -> Result<usize>
    Copies data between a file and this socket using the …
    method
    socket2::Socket::join_ssm_v4
    &Socket, &Ipv4Addr, &Ipv4Addr, &Ipv4Addr -> Result<()>
    Join a multicast SSM channel using IP_ADD_SOURCE_MEMBERSHIP…
    method
    socket2::Socket::leave_ssm_v4
    &Socket, &Ipv4Addr, &Ipv4Addr, &Ipv4Addr -> Result<()>
    Leave a multicast group using IP_DROP_SOURCE_MEMBERSHIP …
    method
    socket2::Socket::recv_vectored
    &Socket, &mut [MaybeUninitSlice] -> Result<(usize, RecvFlags)>
    Receives data on the socket from the remote address to …
    method
    socket2::Socket::recv_with_flags
    &Socket, &mut [MaybeUninit<u8>], c_int -> Result<usize>
    Identical to recv but allows for specification of …
    method
    socket2::Socket::send_to_with_flags
    &Socket, &[u8], &SockAddr, c_int -> Result<usize>
    Identical to send_to but allows for specification of …
    method
    socket2::Socket::send_to_vectored_with_flags
    &Socket, &[IoSlice], &SockAddr, c_int -> Result<usize>
    Identical to send_to_vectored but allows for specification …
    method
    socket2::Socket::peek_from
    &Socket, &mut [MaybeUninit<u8>] -> Result<(usize, SockAddr)>
    Receives data from the socket, without removing it from …
    method
    socket2::Socket::recv_from
    &Socket, &mut [MaybeUninit<u8>] -> Result<(usize, SockAddr)>
    Receives data from the socket. On success, returns the …
    method
    socket2::Socket::recv_from_vectored
    &Socket, &mut [MaybeUninitSlice] -> Result<(usize, RecvFlags, SockAddr)>
    Receives data from the socket. Returns the amount of bytes …
    method
    socket2::Socket::recv_vectored_with_flags
    &Socket, &mut [MaybeUninitSlice], c_int -> Result<(usize, RecvFlags)>
    Identical to recv_vectored but allows for specification of …
    method
    socket2::Socket::recv_from_with_flags
    &Socket, &mut [MaybeUninit<u8>], c_int -> Result<(usize, SockAddr)>
    Identical to recv_from but allows for specification of …
    method
    socket2::Socket::recv_from_vectored_with_flags
    &Socket, &mut [MaybeUninitSlice], c_int -> Result<(usize, RecvFlags, SockAddr)>
    Identical to recv_from_vectored but allows for …
    method
    socket2::Socket::from
    TcpListener -> Socket
    method
    socket2::Socket::from_raw_fd
    c_int -> Socket
    method
    socket2::Socket::new
    Domain, Type, Option<Protocol> -> Result<Socket>
    Creates a new socket and sets common flags.
    method
    socket2::Socket::new_raw
    Domain, Type, Option<Protocol> -> Result<Socket>
    Creates a new socket ready to be configured.
    method
    socket2::Socket::pair
    Domain, Type, Option<Protocol> -> Result<(Socket, Socket)>
    Creates a pair of sockets which are connected to each …
    method
    socket2::Socket::pair_raw
    Domain, Type, Option<Protocol> -> Result<(Socket, Socket)>
    Creates a pair of sockets which are connected to each …
    method
    socket2::Socket::try_clone
    &Socket -> Result<Socket>
    Creates a new independently owned handle to the underlying …
    method
    socket2::Socket::accept
    &Socket -> Result<(Socket, SockAddr)>
    Accept a new incoming connection from this listener.
    method
    socket2::Socket::accept_raw
    &Socket -> Result<(Socket, SockAddr)>
    Accept a new incoming connection from this listener.