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

Handle unset PID #57

Merged
merged 2 commits into from
Oct 27, 2019
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
24 changes: 15 additions & 9 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ impl NlSocket {

/// Use this function to bind to a netlink ID and subscribe to groups. See netlink(7)
/// man pages for more information on netlink IDs and groups.
///
/// The pid parameter sets PID checking.
/// * `None` means checking is off.
/// * `Some(0)` turns checking on, but takes the PID from the first received message.
/// * `Some(pid)` uses the given PID.
pub fn bind(&mut self, pid: Option<u32>, groups: Option<Vec<u32>>) -> Result<(), io::Error> {
let mut nladdr = unsafe { zeroed::<libc::sockaddr_nl>() };
nladdr.nl_family = libc::c_int::from(AddrFamily::Netlink) as u16;
Expand Down Expand Up @@ -336,10 +341,15 @@ impl NlSocket {
Some(ref mut b) => Nlmsghdr::deserialize(b)?,
None => unreachable!(),
};
if self.pid.is_none() {
self.pid = Some(msg.nl_pid);
} else if self.pid != Some(msg.nl_pid) {
return Err(NlError::BadPid);
match self.pid {
// PID checking turned off.
None => (),
// No PID set yet, store the current one.
Some(0) => self.pid = Some(msg.nl_pid),
// PID check OK
Some(pid) if pid == msg.nl_pid => (),
// PID doesn't match
Some(_) => return Err(NlError::BadPid),
}
if self.seq.is_some() {
self.seq.map(|s| s + 1);
Expand All @@ -354,11 +364,7 @@ impl NlSocket {
pub fn recv_ack(&mut self) -> Result<(), NlError> {
if let Ok(ack) = self.recv_nl::<consts::Nlmsg, Nlmsgerr<consts::Nlmsg>>(None) {
if ack.nl_type == consts::Nlmsg::Error && ack.nl_payload.error == 0 {
if self.pid.is_none() {
self.pid = Some(ack.nl_pid);
} else if self.pid != Some(ack.nl_pid) {
return Err(NlError::BadPid);
}
// PID check done as part of recv_nl already
if let Some(seq) = self.seq {
if seq != ack.nl_seq {
return Err(NlError::BadSeq);
Expand Down