pub struct SockAddr { /* private fields */ }
Expand description
The address of a socket.
SockAddr
s may be constructed directly to and from the standard library
SocketAddr
, SocketAddrV4
, and SocketAddrV6
types.
Implementations
sourceimpl SockAddr
impl SockAddr
sourcepub const unsafe fn new(storage: sockaddr_storage, len: socklen_t) -> SockAddr
pub const unsafe fn new(storage: sockaddr_storage, 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
use std::io;
use std::mem;
use std::os::unix::io::AsRawFd;
use socket2::{SockAddr, Socket, Domain, Type};
let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
// Initialise a `SocketAddr` byte calling `getsockname(2)`.
let mut addr_storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
let mut len = mem::size_of_val(&addr_storage) as libc::socklen_t;
// The `getsockname(2)` system call will intiliase `storage` for
// us, setting `len` to the correct length.
let res = unsafe {
libc::getsockname(
socket.as_raw_fd(),
(&mut addr_storage as *mut libc::sockaddr_storage).cast(),
&mut len,
)
};
if res == -1 {
return Err(io::Error::last_os_error());
}
let address = unsafe { SockAddr::new(addr_storage, len) };
sourcepub unsafe fn init<F, T>(init: F) -> Result<(T, SockAddr)> where
F: FnOnce(*mut sockaddr_storage, *mut socklen_t) -> Result<T>,
pub unsafe fn init<F, T>(init: F) -> Result<(T, SockAddr)> where
F: FnOnce(*mut sockaddr_storage, *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
use std::io;
use std::os::unix::io::AsRawFd;
use socket2::{SockAddr, Socket, Domain, Type};
let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
// Initialise a `SocketAddr` byte calling `getsockname(2)`.
let (_, address) = unsafe {
SockAddr::init(|addr_storage, len| {
// The `getsockname(2)` system call will intiliase `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(())
}
})
}?;
sourcepub const fn family(&self) -> sa_family_t
pub const fn family(&self) -> sa_family_t
Returns this address’s family.
sourcepub fn as_socket(&self) -> Option<SocketAddr>
pub fn as_socket(&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
.
sourcepub fn as_socket_ipv4(&self) -> Option<SocketAddrV4>
pub fn as_socket_ipv4(&self) -> Option<SocketAddrV4>
Returns this address as a SocketAddrV4
if it is in the AF_INET
family.
sourcepub fn as_socket_ipv6(&self) -> Option<SocketAddrV6>
pub fn as_socket_ipv6(&self) -> Option<SocketAddrV6>
Returns this address as a SocketAddrV6
if it is in the AF_INET6
family.
sourceimpl SockAddr
impl SockAddr
Trait Implementations
sourceimpl From<SocketAddr> for SockAddr
impl From<SocketAddr> for SockAddr
sourcefn from(addr: SocketAddr) -> SockAddr
fn from(addr: SocketAddr) -> SockAddr
Performs the conversion.
sourceimpl From<SocketAddrV4> for SockAddr
impl From<SocketAddrV4> for SockAddr
sourcefn from(addr: SocketAddrV4) -> SockAddr
fn from(addr: SocketAddrV4) -> SockAddr
Performs the conversion.
sourceimpl From<SocketAddrV6> for SockAddr
impl From<SocketAddrV6> for SockAddr
sourcefn from(addr: SocketAddrV6) -> SockAddr
fn from(addr: SocketAddrV6) -> SockAddr
Performs the conversion.
Auto Trait Implementations
impl RefUnwindSafe for SockAddr
impl Send for SockAddr
impl Sync for SockAddr
impl Unpin for SockAddr
impl UnwindSafe for SockAddr
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more