Skip to content

Commit

Permalink
Merge pull request #5 from chengyuhui/linux-uid
Browse files Browse the repository at this point in the history
Add UID to Linux and Android targets, along with some convenience functions
  • Loading branch information
ohadravid authored Nov 18, 2020
2 parents 70f05a4 + dddb455 commit 1a1530e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/integrations/linux/netlink_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ unsafe fn parse_diag_msg(
}),
associated_pids: Vec::with_capacity(0),
inode: diag_msg.inode,
uid: diag_msg.uid,
},
IPPROTO_UDP => SocketInfo {
protocol_socket_info: ProtocolSocketInfo::Udp(UdpSocketInfo {
Expand All @@ -161,6 +162,7 @@ unsafe fn parse_diag_msg(
}),
associated_pids: Vec::with_capacity(0),
inode: diag_msg.inode,
uid: diag_msg.uid,
},
_ => return Err(Error::UnknownProtocol(protocol)),
};
Expand Down
22 changes: 22 additions & 0 deletions src/types/socket_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ pub struct SocketInfo {
/// Identifiers of processes associated with this socket.
pub associated_pids: Vec<u32>,
#[cfg(any(target_os = "linux", target_os = "android"))]
/// Inode number of this socket.
pub inode: u32,
#[cfg(any(target_os = "linux", target_os = "android"))]
/// Owner UID of this socket.
pub uid: u32,
}

/// Protocol-specific socket information.
Expand Down Expand Up @@ -37,3 +41,21 @@ pub struct UdpSocketInfo {
pub local_addr: IpAddr,
pub local_port: u16,
}

impl SocketInfo {
/// Local address of this socket.
pub fn local_addr(&self) -> IpAddr {
match &self.protocol_socket_info {
ProtocolSocketInfo::Tcp(s) => s.local_addr,
ProtocolSocketInfo::Udp(s) => s.local_addr,
}
}

/// Local port of this socket.
pub fn local_port(&self) -> u16 {
match &self.protocol_socket_info {
ProtocolSocketInfo::Tcp(s) => s.local_port,
ProtocolSocketInfo::Udp(s) => s.local_port,
}
}
}

0 comments on commit 1a1530e

Please sign in to comment.