Skip to content

Commit 745c791

Browse files
committed
Use sockaddr_nl from libc
1 parent 02cb854 commit 745c791

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/sys/socket/addr.rs

+31-21
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ use ::sys::socket::addr::netlink::NetlinkAddr;
1616
*
1717
*/
1818

19-
#[cfg(any(target_os = "linux", target_os = "android"))]
20-
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
21-
#[repr(C)]
22-
pub struct sockaddr_nl {
23-
pub nl_family: sa_family_t,
24-
nl_pad: libc::c_ushort,
25-
pub nl_pid: u32,
26-
pub nl_groups: u32
27-
}
28-
2919
#[repr(i32)]
3020
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
3121
pub enum AddressFamily {
@@ -502,7 +492,7 @@ impl SockAddr {
502492
SockAddr::Inet(InetAddr::V6(ref addr)) => (mem::transmute(addr), mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t),
503493
SockAddr::Unix(UnixAddr(ref addr, len)) => (mem::transmute(addr), (len + mem::size_of::<libc::sa_family_t>()) as libc::socklen_t),
504494
#[cfg(any(target_os = "linux", target_os = "android"))]
505-
SockAddr::Netlink(NetlinkAddr(ref sa)) => (mem::transmute(sa), mem::size_of::<sockaddr_nl>() as libc::socklen_t),
495+
SockAddr::Netlink(NetlinkAddr(ref sa)) => (mem::transmute(sa), mem::size_of::<libc::sockaddr_nl>() as libc::socklen_t),
506496
}
507497
}
508498
}
@@ -558,21 +548,41 @@ impl fmt::Display for SockAddr {
558548

559549
#[cfg(any(target_os = "linux", target_os = "android"))]
560550
pub mod netlink {
561-
use ::sys::socket::addr::{AddressFamily,sockaddr_nl};
562-
use libc::sa_family_t;
563-
use std::fmt;
551+
use ::sys::socket::addr::{AddressFamily};
552+
use libc::{sa_family_t, sockaddr_nl};
553+
use std::{fmt, mem};
554+
use std::hash::{Hash, Hasher};
564555

565-
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
556+
#[derive(Copy, Clone)]
566557
pub struct NetlinkAddr(pub sockaddr_nl);
567558

559+
// , PartialEq, Eq, Debug, Hash
560+
impl PartialEq for NetlinkAddr {
561+
fn eq(&self, other: &Self) -> bool {
562+
let (inner, other) = (self.0, other.0);
563+
(inner.nl_family, inner.nl_pid, inner.nl_groups) ==
564+
(other.nl_family, other.nl_pid, other.nl_groups)
565+
}
566+
}
567+
568+
impl Eq for NetlinkAddr {}
569+
570+
impl Hash for NetlinkAddr {
571+
fn hash<H: Hasher>(&self, s: &mut H) {
572+
let inner = self.0;
573+
(inner.nl_family, inner.nl_pid, inner.nl_groups).hash(s);
574+
}
575+
}
576+
577+
568578
impl NetlinkAddr {
569579
pub fn new(pid: u32, groups: u32) -> NetlinkAddr {
570-
NetlinkAddr(sockaddr_nl {
571-
nl_family: AddressFamily::Netlink as sa_family_t,
572-
nl_pad: 0,
573-
nl_pid: pid,
574-
nl_groups: groups,
575-
})
580+
let mut addr: sockaddr_nl = unsafe { mem::zeroed() };
581+
addr.nl_family = AddressFamily::Netlink as sa_family_t;
582+
addr.nl_pid = pid;
583+
addr.nl_groups = groups;
584+
585+
NetlinkAddr(addr)
576586
}
577587

578588
pub fn pid(&self) -> u32 {

0 commit comments

Comments
 (0)