Skip to content

Commit 1a1530e

Browse files
authored
Merge pull request #5 from chengyuhui/linux-uid
Add UID to Linux and Android targets, along with some convenience functions
2 parents 70f05a4 + dddb455 commit 1a1530e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/integrations/linux/netlink_iterator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ unsafe fn parse_diag_msg(
153153
}),
154154
associated_pids: Vec::with_capacity(0),
155155
inode: diag_msg.inode,
156+
uid: diag_msg.uid,
156157
},
157158
IPPROTO_UDP => SocketInfo {
158159
protocol_socket_info: ProtocolSocketInfo::Udp(UdpSocketInfo {
@@ -161,6 +162,7 @@ unsafe fn parse_diag_msg(
161162
}),
162163
associated_pids: Vec::with_capacity(0),
163164
inode: diag_msg.inode,
165+
uid: diag_msg.uid,
164166
},
165167
_ => return Err(Error::UnknownProtocol(protocol)),
166168
};

src/types/socket_info.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ pub struct SocketInfo {
99
/// Identifiers of processes associated with this socket.
1010
pub associated_pids: Vec<u32>,
1111
#[cfg(any(target_os = "linux", target_os = "android"))]
12+
/// Inode number of this socket.
1213
pub inode: u32,
14+
#[cfg(any(target_os = "linux", target_os = "android"))]
15+
/// Owner UID of this socket.
16+
pub uid: u32,
1317
}
1418

1519
/// Protocol-specific socket information.
@@ -37,3 +41,21 @@ pub struct UdpSocketInfo {
3741
pub local_addr: IpAddr,
3842
pub local_port: u16,
3943
}
44+
45+
impl SocketInfo {
46+
/// Local address of this socket.
47+
pub fn local_addr(&self) -> IpAddr {
48+
match &self.protocol_socket_info {
49+
ProtocolSocketInfo::Tcp(s) => s.local_addr,
50+
ProtocolSocketInfo::Udp(s) => s.local_addr,
51+
}
52+
}
53+
54+
/// Local port of this socket.
55+
pub fn local_port(&self) -> u16 {
56+
match &self.protocol_socket_info {
57+
ProtocolSocketInfo::Tcp(s) => s.local_port,
58+
ProtocolSocketInfo::Udp(s) => s.local_port,
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)