-
Notifications
You must be signed in to change notification settings - Fork 690
[WIP] Netlink bits #202
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
Closed
Closed
[WIP] Netlink bits #202
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ use std::{fmt, hash, mem, net, ptr}; | |
use std::ffi::OsStr; | ||
use std::path::Path; | ||
use std::os::unix::ffi::OsStrExt; | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
use ::sys::socket::addr::netlink::NetlinkAddr; | ||
|
||
// TODO: uncomment out IpAddr functions: rust-lang/rfcs#988 | ||
|
||
|
@@ -15,12 +17,24 @@ use std::os::unix::ffi::OsStrExt; | |
* | ||
*/ | ||
|
||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | ||
#[repr(C)] | ||
pub struct sockaddr_nl { | ||
pub nl_family: sa_family_t, | ||
nl_pad: libc::c_ushort, | ||
pub nl_pid: u32, | ||
pub nl_groups: u32 | ||
} | ||
|
||
#[repr(i32)] | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | ||
pub enum AddressFamily { | ||
Unix = consts::AF_UNIX, | ||
Inet = consts::AF_INET, | ||
Inet6 = consts::AF_INET6, | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
Netlink = consts::AF_NETLINK, | ||
} | ||
|
||
#[derive(Copy)] | ||
|
@@ -450,7 +464,9 @@ impl fmt::Display for UnixAddr { | |
#[derive(Copy)] | ||
pub enum SockAddr { | ||
Inet(InetAddr), | ||
Unix(UnixAddr) | ||
Unix(UnixAddr), | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
Netlink(NetlinkAddr) | ||
} | ||
|
||
impl SockAddr { | ||
|
@@ -462,11 +478,18 @@ impl SockAddr { | |
Ok(SockAddr::Unix(try!(UnixAddr::new(path)))) | ||
} | ||
|
||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
pub fn new_netlink(pid: u32, groups: u32) -> SockAddr { | ||
SockAddr::Netlink(NetlinkAddr::new(pid, groups)) | ||
} | ||
|
||
pub fn family(&self) -> AddressFamily { | ||
match *self { | ||
SockAddr::Inet(InetAddr::V4(..)) => AddressFamily::Inet, | ||
SockAddr::Inet(InetAddr::V6(..)) => AddressFamily::Inet6, | ||
SockAddr::Unix(..) => AddressFamily::Unix, | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
SockAddr::Netlink(..) => AddressFamily::Netlink, | ||
} | ||
} | ||
|
||
|
@@ -479,6 +502,8 @@ impl SockAddr { | |
SockAddr::Inet(InetAddr::V4(ref addr)) => (mem::transmute(addr), mem::size_of::<libc::sockaddr_in>() as libc::socklen_t), | ||
SockAddr::Inet(InetAddr::V6(ref addr)) => (mem::transmute(addr), mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t), | ||
SockAddr::Unix(UnixAddr(ref addr, len)) => (mem::transmute(addr), (len + mem::size_of::<libc::sa_family_t>()) as libc::socklen_t), | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
SockAddr::Netlink(NetlinkAddr(ref sa)) => (mem::transmute(sa), mem::size_of::<sockaddr_nl>() as libc::socklen_t), | ||
} | ||
} | ||
} | ||
|
@@ -492,6 +517,10 @@ impl PartialEq for SockAddr { | |
(SockAddr::Unix(ref a), SockAddr::Unix(ref b)) => { | ||
a == b | ||
} | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
(SockAddr::Netlink(ref a), SockAddr::Netlink(ref b)) => { | ||
a == b | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
@@ -505,6 +534,8 @@ impl hash::Hash for SockAddr { | |
match *self { | ||
SockAddr::Inet(ref a) => a.hash(s), | ||
SockAddr::Unix(ref a) => a.hash(s), | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
SockAddr::Netlink(ref a) => a.hash(s), | ||
} | ||
} | ||
} | ||
|
@@ -520,6 +551,43 @@ impl fmt::Display for SockAddr { | |
match *self { | ||
SockAddr::Inet(ref inet) => inet.fmt(f), | ||
SockAddr::Unix(ref unix) => unix.fmt(f), | ||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
SockAddr::Netlink(ref nl) => nl.fmt(f), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(any(target_os = "linux", target_os = "android"))] | ||
pub mod netlink { | ||
use ::sys::socket::addr::{AddressFamily,sockaddr_nl}; | ||
use libc::sa_family_t; | ||
use std::fmt; | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | ||
pub struct NetlinkAddr(pub sockaddr_nl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should the wrapped field be |
||
|
||
impl NetlinkAddr { | ||
pub fn new(pid: u32, groups: u32) -> NetlinkAddr { | ||
NetlinkAddr(sockaddr_nl { | ||
nl_family: AddressFamily::Netlink as sa_family_t, | ||
nl_pad: 0, | ||
nl_pid: pid, | ||
nl_groups: groups, | ||
}) | ||
} | ||
|
||
pub fn pid(&self) -> u32 { | ||
self.0.nl_pid | ||
} | ||
|
||
pub fn groups(&self) -> u32 { | ||
self.0.nl_groups | ||
} | ||
} | ||
|
||
impl fmt::Display for NetlinkAddr { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "pid: {} groups: {}", self.pid(), self.groups()) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use this from libc?
http://rust-lang-nursery.github.io/libc/x86_64-unknown-linux-gnu/libc/struct.sockaddr_nl.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(sorry for the derive suggestion, should have realised this was present)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also just realised this would mean none of those impls would be present. unclear if they're required or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libc already defines
Clone
andCopy
for most or all of its structs (see s! macro: http://rust-lang-nursery.github.io/libc/x86_64-unknown-linux-gnu/src/libc/macros.rs.html#42). So unlessPartialEq
,Eq
,Debug
andHash
are necessary, it should be fine to use the libc version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're required by SockAddr: https://github.com/polachok/nix-rust/blob/netlink/src/sys/socket/addr.rs#L510
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they make sense for the raw struct in our code, they would probably make sense for the corresponding raw struct in libc. Would you create a PR for libc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just created rust-lang/libc#159 for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR was rejected, with rationale:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it still makes sense to use the struct from libc and implement the required traits as in:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(above patch should
git apply
cleanly on your PR)