Struct SockAddr

struct SockAddr { ... }

The address of a socket.

SockAddrs may be constructed directly to and from the standard library SocketAddr, SocketAddrV4, and SocketAddrV6 types.

Implementations

impl SockAddr

unsafe const fn new(storage: SockAddrStorage, len: socklen_t) -> SockAddr

Create a SockAddr from the underlying storage and its length.

Safety

Caller must ensure that the address family and length match the type of storage address. For example if storage.ss_family is set to AF_INET the storage must be initialised as sockaddr_in, setting the content and length appropriately.

Examples

# fn main() -> std::io::Result<()> {
# #[cfg(unix)] {
use std::io;
use std::os::fd::AsRawFd;

use socket2::{SockAddr, SockAddrStorage, Socket, Domain, Type};

let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;

// Initialise a `SocketAddr` by calling `getsockname(2)`.
let mut addr_storage = SockAddrStorage::zeroed();
let mut len = addr_storage.size_of();

// The `getsockname(2)` system call will initialize `storage` for
// us, setting `len` to the correct length.
let res = unsafe {
    libc::getsockname(
        socket.as_raw_fd(),
        addr_storage.view_as(),
        &mut len,
    )
};
if res == -1 {
    return Err(io::Error::last_os_error());
}

let address = unsafe { SockAddr::new(addr_storage, len) };
# drop(address);
# }
# Ok(())
# }
unsafe fn try_init<F, T>(init: F) -> Result<(T, SockAddr)>
where
    F: FnOnce(*mut SockAddrStorage, *mut socklen_t) -> Result<T>

Initialise a SockAddr by calling the function init.

The type of the address storage and length passed to the function init is OS/architecture specific.

The address is zeroed before init is called and is thus valid to dereference and read from. The length initialised to the maximum length of the storage.

Safety

Caller must ensure that the address family and length match the type of storage address. For example if storage.ss_family is set to AF_INET the storage must be initialised as sockaddr_in, setting the content and length appropriately.

Examples

# fn main() -> std::io::Result<()> {
# #[cfg(unix)] {
use std::io;
use std::os::fd::AsRawFd;

use socket2::{SockAddr, Socket, Domain, Type};

let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;

// Initialise a `SocketAddr` by calling `getsockname(2)`.
let (_, address) = unsafe {
    SockAddr::try_init(|addr_storage, len| {
        // The `getsockname(2)` system call will initialize `storage` for
        // us, setting `len` to the correct length.
        if libc::getsockname(socket.as_raw_fd(), addr_storage.cast(), len) == -1 {
            Err(io::Error::last_os_error())
        } else {
            Ok(())
        }
    })
}?;
# drop(address);
# }
# Ok(())
# }
fn unix<P>(path: P) -> Result<SockAddr>
where
    P: AsRef<Path>

Constructs a SockAddr with the family AF_UNIX and the provided path.

Returns an error if the path is longer than SUN_LEN.

unsafe fn set_length(self: &mut Self, length: socklen_t)

Set the length of the address.

Safety

Caller must ensure that the address up to length bytes are properly initialised.

const fn family(self: &Self) -> sa_family_t

Returns this address's family.

const fn domain(self: &Self) -> Domain

Returns this address's Domain.

const fn len(self: &Self) -> socklen_t

Returns the size of this address in bytes.

const fn as_ptr(self: &Self) -> *const SockAddrStorage

Returns a raw pointer to the address.

const fn as_storage(self: Self) -> SockAddrStorage

Retuns the address as the storage.

const fn is_ipv4(self: &Self) -> bool

Returns true if this address is in the AF_INET (IPv4) family, false otherwise.

const fn is_ipv6(self: &Self) -> bool

Returns true if this address is in the AF_INET6 (IPv6) family, false otherwise.

fn is_unix(self: &Self) -> bool

Returns true if this address is of a unix socket (for local interprocess communication), i.e. it is from the AF_UNIX family, false otherwise.

fn as_socket(self: &Self) -> Option<SocketAddr>

Returns this address as a SocketAddr if it is in the AF_INET (IPv4) or AF_INET6 (IPv6) family, otherwise returns None.

fn as_socket_ipv4(self: &Self) -> Option<SocketAddrV4>

Returns this address as a SocketAddrV4 if it is in the AF_INET family.

fn as_socket_ipv6(self: &Self) -> Option<SocketAddrV6>

Returns this address as a SocketAddrV6 if it is in the AF_INET6 family.

impl SockAddr

fn is_unnamed(self: &Self) -> bool

Returns true if this address is an unnamed address from the AF_UNIX family (for local interprocess communication), false otherwise.

fn as_unix(self: &Self) -> Option<SocketAddr>

Returns this address as Unix SocketAddr if it is an AF_UNIX pathname address, otherwise returns None.

fn as_pathname(self: &Self) -> Option<&Path>

Returns this address as a Path reference if it is an AF_UNIX pathname address, otherwise returns None.

fn as_abstract_namespace(self: &Self) -> Option<&[u8]>

Returns this address as a slice of bytes representing an abstract address if it is an AF_UNIX abstract address, otherwise returns None.

Abstract addresses are a Linux extension, so this method returns None on all non-Linux platforms.

impl Clone for SockAddr

fn clone(self: &Self) -> SockAddr

impl Debug for SockAddr

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

impl Eq for SockAddr

impl Freeze for SockAddr

impl From for SockAddr

fn from(addr: SocketAddrV6) -> SockAddr

impl From for SockAddr

fn from(addr: SocketAddrV4) -> SockAddr

impl From for SockAddr

fn from(addr: SocketAddr) -> SockAddr

impl Hash for SockAddr

fn hash<H: std::hash::Hasher>(self: &Self, state: &mut H)

impl PartialEq for SockAddr

fn eq(self: &Self, other: &Self) -> bool

impl RefUnwindSafe for SockAddr

impl Send for SockAddr

impl Sync for SockAddr

impl Unpin for SockAddr

impl UnsafeUnpin for SockAddr

impl UnwindSafe for SockAddr

impl<T> Any for SockAddr

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for SockAddr

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for SockAddr

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

impl<T> CloneToUninit for SockAddr

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for SockAddr

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for SockAddr

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for SockAddr

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for SockAddr

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

impl<T, U> TryInto for SockAddr

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