Skip to content

Commit

Permalink
Added as const function SockaddrIn6::ip
Browse files Browse the repository at this point in the history
Signed-off-by: carlosb1 <mcflurry0@gmail.com>
  • Loading branch information
carlosb1 committed Oct 4, 2023
1 parent 16a667d commit f8213aa
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,9 @@ impl SockaddrIn {
/// Returns the IP address associated with this socket address, in native
/// endian.
pub const fn ip(&self) -> net::Ipv4Addr {
net::Ipv4Addr::from(self.0.sin_addr.s_addr.to_ne_bytes())
let bytes = self.0.sin_addr.s_addr.to_ne_bytes();
let (a, b, c, d) = (bytes[0], bytes[1], bytes[2], bytes[3]);
Ipv4Addr::new(a, b, c, d)
}

/// Creates a new socket address from IPv4 octets and a port number.
Expand Down Expand Up @@ -1162,7 +1164,23 @@ impl SockaddrIn6 {

/// Returns the IP address associated with this socket address.
pub const fn ip(&self) -> net::Ipv6Addr {
net::Ipv6Addr::from(self.0.sin6_addr.s6_addr)
let bytes = self.0.sin6_addr.s6_addr;
let [a, b, c, d, e, f, g, h] =
unsafe { transmute::<_, [u16; 8]>(bytes) };

// Ipv6Addr::new() takes segments in native endian, convert them here
let [a, b, c, d, e, f, g, h] = [
u16::from_be(a),
u16::from_be(b),
u16::from_be(c),
u16::from_be(d),
u16::from_be(e),
u16::from_be(f),
u16::from_be(g),
u16::from_be(h),
];

Ipv6Addr::new(a, b, c, d, e, f, g, h)
}

/// Returns the port number associated with this socket address, in native
Expand Down

0 comments on commit f8213aa

Please sign in to comment.