Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

372: Implement Eq and Hash for socket2::SockAddr #374

Merged
merged 5 commits into from
Jan 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 192 additions & 38 deletions src/sockaddr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::hash::Hash;
use std::mem::{self, size_of, MaybeUninit};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::path::Path;
Expand Down Expand Up @@ -312,44 +313,197 @@ impl fmt::Debug for SockAddr {
}
}

#[test]
fn ipv4() {
use std::net::Ipv4Addr;
let std = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
let addr = SockAddr::from(std);
assert!(addr.is_ipv4());
assert_eq!(addr.family(), AF_INET as sa_family_t);
assert_eq!(addr.domain(), Domain::IPV4);
assert_eq!(addr.len(), size_of::<sockaddr_in>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V4(std)));
assert_eq!(addr.as_socket_ipv4(), Some(std));
assert!(addr.as_socket_ipv6().is_none());

let addr = SockAddr::from(SocketAddr::from(std));
assert_eq!(addr.family(), AF_INET as sa_family_t);
assert_eq!(addr.len(), size_of::<sockaddr_in>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V4(std)));
assert_eq!(addr.as_socket_ipv4(), Some(std));
assert!(addr.as_socket_ipv6().is_none());
impl PartialEq for SockAddr {
fn eq(&self, other: &Self) -> bool {
unsafe {
let these_bytes: &[u8] = any_as_u8_slice(&self.storage, self.len as usize);
let those_bytes: &[u8] = any_as_u8_slice(&other.storage, other.len as usize);
these_bytes == those_bytes
Thomasdezeeuw marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl Eq for SockAddr {}

impl Hash for SockAddr {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
unsafe {
let these_bytes: &[u8] = any_as_u8_slice(&self.storage, self.len as usize);
these_bytes.hash(state);
}
}
}

unsafe fn any_as_u8_slice<T: Sized>(p: &T, size: usize) -> &[u8] {
::std::slice::from_raw_parts((p as *const T) as *const u8, size)
}

#[test]
fn ipv6() {
use std::net::Ipv6Addr;
let std = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);
let addr = SockAddr::from(std);
assert!(addr.is_ipv6());
assert_eq!(addr.family(), AF_INET6 as sa_family_t);
assert_eq!(addr.domain(), Domain::IPV6);
assert_eq!(addr.len(), size_of::<sockaddr_in6>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V6(std)));
assert!(addr.as_socket_ipv4().is_none());
assert_eq!(addr.as_socket_ipv6(), Some(std));

let addr = SockAddr::from(SocketAddr::from(std));
assert_eq!(addr.family(), AF_INET6 as sa_family_t);
assert_eq!(addr.len(), size_of::<sockaddr_in6>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V6(std)));
assert!(addr.as_socket_ipv4().is_none());
assert_eq!(addr.as_socket_ipv6(), Some(std));
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn ipv4() {
use std::net::Ipv4Addr;
let std = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
let addr = SockAddr::from(std);
assert!(addr.is_ipv4());
assert_eq!(addr.family(), AF_INET as sa_family_t);
assert_eq!(addr.domain(), Domain::IPV4);
assert_eq!(addr.len(), size_of::<sockaddr_in>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V4(std)));
assert_eq!(addr.as_socket_ipv4(), Some(std));
assert!(addr.as_socket_ipv6().is_none());

let addr = SockAddr::from(SocketAddr::from(std));
assert_eq!(addr.family(), AF_INET as sa_family_t);
assert_eq!(addr.len(), size_of::<sockaddr_in>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V4(std)));
assert_eq!(addr.as_socket_ipv4(), Some(std));
assert!(addr.as_socket_ipv6().is_none());
}

#[test]
fn ipv6() {
use std::net::Ipv6Addr;
let std = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);
let addr = SockAddr::from(std);
assert!(addr.is_ipv6());
assert_eq!(addr.family(), AF_INET6 as sa_family_t);
assert_eq!(addr.domain(), Domain::IPV6);
assert_eq!(addr.len(), size_of::<sockaddr_in6>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V6(std)));
assert!(addr.as_socket_ipv4().is_none());
assert_eq!(addr.as_socket_ipv6(), Some(std));

let addr = SockAddr::from(SocketAddr::from(std));
assert_eq!(addr.family(), AF_INET6 as sa_family_t);
assert_eq!(addr.len(), size_of::<sockaddr_in6>() as socklen_t);
assert_eq!(addr.as_socket(), Some(SocketAddr::V6(std)));
assert!(addr.as_socket_ipv4().is_none());
assert_eq!(addr.as_socket_ipv6(), Some(std));
}

#[test]
fn ipv4_eq() {
use std::net::Ipv4Addr;

let std1 = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
let std2 = SocketAddrV4::new(Ipv4Addr::new(5, 6, 7, 8), 8765);

test_eq(
SockAddr::from(std1),
SockAddr::from(std1),
SockAddr::from(std2),
);
}

#[test]
fn ipv4_hash() {
use std::net::Ipv4Addr;

let std1 = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
let std2 = SocketAddrV4::new(Ipv4Addr::new(5, 6, 7, 8), 8765);

test_hash(
SockAddr::from(std1),
SockAddr::from(std1),
SockAddr::from(std2),
);
}

#[test]
fn ipv6_eq() {
use std::net::Ipv6Addr;

let std1 = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);
let std2 = SocketAddrV6::new(Ipv6Addr::new(3, 4, 5, 6, 7, 8, 9, 0), 7654, 13, 14);

test_eq(
SockAddr::from(std1),
SockAddr::from(std1),
SockAddr::from(std2),
);
}

#[test]
fn ipv6_hash() {
use std::net::Ipv6Addr;

let std1 = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);
let std2 = SocketAddrV6::new(Ipv6Addr::new(3, 4, 5, 6, 7, 8, 9, 0), 7654, 13, 14);

test_hash(
SockAddr::from(std1),
SockAddr::from(std1),
SockAddr::from(std2),
);
}

#[test]
fn ipv4_ipv6_eq() {
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;

let std1 = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
let std2 = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);

test_eq(
SockAddr::from(std1),
SockAddr::from(std1),
SockAddr::from(std2),
);

test_eq(
SockAddr::from(std2),
SockAddr::from(std2),
SockAddr::from(std1),
);
}

#[test]
fn ipv4_ipv6_hash() {
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;

let std1 = SocketAddrV4::new(Ipv4Addr::new(1, 2, 3, 4), 9876);
let std2 = SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9876, 11, 12);

test_hash(
SockAddr::from(std1),
SockAddr::from(std1),
SockAddr::from(std2),
);

test_hash(
SockAddr::from(std2),
SockAddr::from(std2),
SockAddr::from(std1),
);
}

#[allow(clippy::eq_op)] // allow a0 == a0 check
fn test_eq(a0: SockAddr, a1: SockAddr, b: SockAddr) {
assert!(a0 == a0);
assert!(a0 == a1);
assert!(a1 == a0);
assert!(a0 != b);
assert!(b != a0);
}

fn test_hash(a0: SockAddr, a1: SockAddr, b: SockAddr) {
assert!(calculate_hash(&a0) == calculate_hash(&a0));
assert!(calculate_hash(&a0) == calculate_hash(&a1));
// technically unequal values can have the same hash, in this case x != z and both have different hashes
assert!(calculate_hash(&a0) != calculate_hash(&b));
}

fn calculate_hash(x: &SockAddr) -> u64 {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;

let mut hasher = DefaultHasher::new();
x.hash(&mut hasher);
hasher.finish()
}
}