#![allow(unsafe_code)]
use crate::imp::c;
use crate::imp::net::ext::{
in6_addr_s6_addr, in_addr_s_addr, sockaddr_in6_new, sockaddr_in6_sin6_scope_id,
sockaddr_in6_sin6_scope_id_mut,
};
use crate::net::ip::{IpAddr, Ipv4Addr, Ipv6Addr};
use core::cmp::Ordering;
use core::hash;
use core::mem;
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
pub enum SocketAddr {
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
V4(#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] SocketAddrV4),
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
V6(#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))] SocketAddrV6),
}
#[derive(Copy)]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
pub struct SocketAddrV4 {
pub(crate) inner: c::sockaddr_in,
}
#[derive(Copy)]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
pub struct SocketAddrV6 {
pub(crate) inner: c::sockaddr_in6,
}
impl SocketAddr {
#[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))]
#[must_use]
pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
match ip {
IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
}
}
#[cfg(const_raw_ptr_deref)]
#[must_use]
#[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn ip(&self) -> IpAddr {
match *self {
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
}
}
#[cfg(not(const_raw_ptr_deref))]
#[must_use]
#[cfg_attr(staged_api, stable(feature = "ip_addr", since = "1.7.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub fn ip(&self) -> IpAddr {
match *self {
SocketAddr::V4(ref a) => IpAddr::V4(*a.ip()),
SocketAddr::V6(ref a) => IpAddr::V6(*a.ip()),
}
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_ip(&mut self, new_ip: IpAddr) {
match (self, new_ip) {
(&mut SocketAddr::V4(ref mut a), IpAddr::V4(new_ip)) => a.set_ip(new_ip),
(&mut SocketAddr::V6(ref mut a), IpAddr::V6(new_ip)) => a.set_ip(new_ip),
(self_, new_ip) => *self_ = Self::new(new_ip, self_.port()),
}
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn port(&self) -> u16 {
match *self {
SocketAddr::V4(ref a) => a.port(),
SocketAddr::V6(ref a) => a.port(),
}
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_port(&mut self, new_port: u16) {
match *self {
SocketAddr::V4(ref mut a) => a.set_port(new_port),
SocketAddr::V6(ref mut a) => a.set_port(new_port),
}
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "sockaddr_checker", since = "1.16.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn is_ipv4(&self) -> bool {
matches!(*self, SocketAddr::V4(_))
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "sockaddr_checker", since = "1.16.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn is_ipv6(&self) -> bool {
matches!(*self, SocketAddr::V6(_))
}
}
impl SocketAddrV4 {
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[must_use]
pub fn new(ip: Ipv4Addr, port: u16) -> SocketAddrV4 {
SocketAddrV4 {
inner: c::sockaddr_in {
sin_family: c::AF_INET as c::sa_family_t,
sin_port: port.to_be(),
sin_addr: ip.inner,
..unsafe { mem::zeroed() }
},
}
}
#[cfg(const_raw_ptr_deref)]
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn ip(&self) -> &Ipv4Addr {
unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
}
#[cfg(not(const_raw_ptr_deref))]
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub fn ip(&self) -> &Ipv4Addr {
unsafe { &*(&self.inner.sin_addr as *const c::in_addr as *const Ipv4Addr) }
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_ip(&mut self, new_ip: Ipv4Addr) {
self.inner.sin_addr = new_ip.inner
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn port(&self) -> u16 {
u16::from_be(self.inner.sin_port)
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_port(&mut self, new_port: u16) {
self.inner.sin_port = new_port.to_be();
}
}
impl SocketAddrV6 {
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[must_use]
pub fn new(ip: Ipv6Addr, port: u16, flowinfo: u32, scope_id: u32) -> SocketAddrV6 {
SocketAddrV6 {
inner: sockaddr_in6_new(
c::AF_INET6 as c::sa_family_t,
port.to_be(),
flowinfo,
ip.inner,
scope_id,
),
}
}
#[cfg(const_raw_ptr_deref)]
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn ip(&self) -> &Ipv6Addr {
unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) }
}
#[cfg(not(const_raw_ptr_deref))]
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub fn ip(&self) -> &Ipv6Addr {
unsafe { &*(&self.inner.sin6_addr as *const c::in6_addr as *const Ipv6Addr) }
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_ip(&mut self, new_ip: Ipv6Addr) {
self.inner.sin6_addr = new_ip.inner
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn port(&self) -> u16 {
u16::from_be(self.inner.sin6_port)
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_port(&mut self, new_port: u16) {
self.inner.sin6_port = new_port.to_be();
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn flowinfo(&self) -> u32 {
self.inner.sin6_flowinfo
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_flowinfo(&mut self, new_flowinfo: u32) {
self.inner.sin6_flowinfo = new_flowinfo;
}
#[must_use]
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
#[cfg_attr(
staged_api,
rustc_const_unstable(feature = "const_socketaddr", issue = "82485")
)]
pub const fn scope_id(&self) -> u32 {
sockaddr_in6_sin6_scope_id(self.inner)
}
#[cfg_attr(staged_api, stable(feature = "sockaddr_setters", since = "1.9.0"))]
pub fn set_scope_id(&mut self, new_scope_id: u32) {
*sockaddr_in6_sin6_scope_id_mut(&mut self.inner) = new_scope_id;
}
}
#[cfg_attr(staged_api, stable(feature = "ip_from_ip", since = "1.16.0"))]
impl From<SocketAddrV4> for SocketAddr {
fn from(sock4: SocketAddrV4) -> SocketAddr {
SocketAddr::V4(sock4)
}
}
#[cfg_attr(staged_api, stable(feature = "ip_from_ip", since = "1.16.0"))]
impl From<SocketAddrV6> for SocketAddr {
fn from(sock6: SocketAddrV6) -> SocketAddr {
SocketAddr::V6(sock6)
}
}
#[cfg_attr(staged_api, stable(feature = "addr_from_into_ip", since = "1.17.0"))]
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
fn from(pieces: (I, u16)) -> SocketAddr {
SocketAddr::new(pieces.0.into(), pieces.1)
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl Clone for SocketAddrV4 {
fn clone(&self) -> SocketAddrV4 {
*self
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl Clone for SocketAddrV6 {
fn clone(&self) -> SocketAddrV6 {
*self
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl PartialEq for SocketAddrV4 {
fn eq(&self, other: &SocketAddrV4) -> bool {
self.inner.sin_port == other.inner.sin_port
&& in_addr_s_addr(self.inner.sin_addr) == in_addr_s_addr(other.inner.sin_addr)
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl PartialEq for SocketAddrV6 {
fn eq(&self, other: &SocketAddrV6) -> bool {
self.inner.sin6_port == other.inner.sin6_port
&& in6_addr_s6_addr(self.inner.sin6_addr) == in6_addr_s6_addr(self.inner.sin6_addr)
&& self.inner.sin6_flowinfo == other.inner.sin6_flowinfo
&& sockaddr_in6_sin6_scope_id(self.inner) == sockaddr_in6_sin6_scope_id(other.inner)
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl Eq for SocketAddrV4 {}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl Eq for SocketAddrV6 {}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl PartialOrd for SocketAddrV4 {
fn partial_cmp(&self, other: &SocketAddrV4) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl PartialOrd for SocketAddrV6 {
fn partial_cmp(&self, other: &SocketAddrV6) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl Ord for SocketAddrV4 {
fn cmp(&self, other: &SocketAddrV4) -> Ordering {
self.ip()
.cmp(other.ip())
.then(self.port().cmp(&other.port()))
}
}
#[cfg_attr(staged_api, stable(feature = "socketaddr_ordering", since = "1.45.0"))]
impl Ord for SocketAddrV6 {
fn cmp(&self, other: &SocketAddrV6) -> Ordering {
self.ip()
.cmp(other.ip())
.then(self.port().cmp(&other.port()))
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl hash::Hash for SocketAddrV4 {
fn hash<H: hash::Hasher>(&self, s: &mut H) {
(self.inner.sin_port, in_addr_s_addr(self.inner.sin_addr)).hash(s)
}
}
#[cfg_attr(staged_api, stable(feature = "rust1", since = "1.0.0"))]
impl hash::Hash for SocketAddrV6 {
fn hash<H: hash::Hasher>(&self, s: &mut H) {
(
self.inner.sin6_port,
&in6_addr_s6_addr(self.inner.sin6_addr),
self.inner.sin6_flowinfo,
sockaddr_in6_sin6_scope_id(self.inner),
)
.hash(s)
}
}