Skip to content

Commit

Permalink
Merge branch with list_connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
panhania committed Sep 28, 2023
2 parents 458c809 + 956ebe1 commit 7dca8f9
Show file tree
Hide file tree
Showing 11 changed files with 580 additions and 0 deletions.
123 changes: 123 additions & 0 deletions crates/ospect/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub mod macos;
#[cfg(target_os = "windows")]
pub mod windows;

#[cfg(target_family = "unix")]
pub mod unix;

mod sys {
#[cfg(target_os = "linux")]
pub use crate::net::linux::*;
Expand Down Expand Up @@ -411,6 +414,14 @@ pub enum Connection {

impl Connection {

/// Returns the local address of the connection.
pub fn local_addr(&self) -> std::net::SocketAddr {
match self {
Connection::Tcp(conn) => conn.local_addr(),
Connection::Udp(conn) => conn.local_addr(),
}
}

/// Returns the identifier of the process that owns the connection.
pub fn pid(&self) -> u32 {
match self {
Expand Down Expand Up @@ -541,6 +552,85 @@ pub fn connections(pid: u32) -> std::io::Result<impl Iterator<Item = std::io::Re
Ok(tcp.chain(udp))
}

/// Returns an iterator over IPv4 TCP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_tcp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV4>>> {
self::sys::all_tcp_v4_connections()
}

/// Returns an iterator over IPv6 TCP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_tcp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV6>>> {
self::sys::all_tcp_v6_connections()
}

/// Returns an iterator over IPv4 UDP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_udp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV4>>> {
self::sys::all_udp_v4_connections()
}

/// Returns an iterator over IPv6 UDP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_udp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV6>>> {
self::sys::all_udp_v6_connections()
}

/// Returns an iterator over TCP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_tcp_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnection>>> {
let v4 = all_tcp_v4_connections()?.map(|conn| conn.map(TcpConnection::V4));
let v6 = all_tcp_v6_connections()?.map(|conn| conn.map(TcpConnection::V6));

Ok(v4.chain(v6))
}

/// Returns an iterator over UDP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_udp_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnection>>> {
let v4 = all_udp_v4_connections()?.map(|conn| conn.map(UdpConnection::V4));
let v6 = all_udp_v6_connections()?.map(|conn| conn.map(UdpConnection::V6));

Ok(v4.chain(v6))
}

/// Returns an iterator over UDP connections of all processes.
///
/// # Errors
///
/// This function will fail if there was some kind of issue (e.g. insufficient
/// permissions to make certain system calls) during information collection.
pub fn all_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<Connection>>> {
let tcp = all_tcp_connections()?.map(|conn| conn.map(Connection::Tcp));
let udp = all_udp_connections()?.map(|conn| conn.map(Connection::Udp));

Ok(tcp.chain(udp))
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -631,4 +721,37 @@ mod tests {

assert_eq!(server_conn.pid(), std::process::id());
}

#[test]
fn all_connections_local_connections() {
use std::net::Ipv4Addr;

let tcp_server = std::net::TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
.unwrap();
let tcp_server_addr = tcp_server.local_addr()
.unwrap();

let udp_socket = std::net::UdpSocket::bind((Ipv4Addr::LOCALHOST, 0))
.unwrap();
let udp_socket_addr = udp_socket.local_addr()
.unwrap();

let mut conns = all_connections()
.unwrap()
.filter_map(Result::ok);

assert! {
conns.find(|conn| {
conn.pid() == std::process::id() &&
conn.local_addr() == tcp_server_addr
}).is_some()
}

assert! {
conns.find(|conn| {
conn.pid() == std::process::id() &&
conn.local_addr() == udp_socket_addr
}).is_some()
}
}
}
20 changes: 20 additions & 0 deletions crates/ospect/src/net/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ pub fn udp_v6_connections(pid: u32) -> std::io::Result<impl Iterator<Item = std:
self::conn::udp_v6(pid)
}

/// Returns an iterator over IPv4 TCP connections of all processes.
pub fn all_tcp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV4>>> {
crate::net::unix::all_tcp_v4_connections()
}

/// Returns an iterator over IPv6 TCP connections of all processes.
pub fn all_tcp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV6>>> {
crate::net::unix::all_tcp_v6_connections()
}

/// Returns an iterator over IPv4 UDP connections of all processes.
pub fn all_udp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV4>>> {
crate::net::unix::all_udp_v4_connections()
}

/// Returns an iterator over IPv6 UDP connections of all processes.
pub fn all_udp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV6>>> {
crate::net::unix::all_udp_v6_connections()
}

#[cfg(test)]
mod tests {

Expand Down
20 changes: 20 additions & 0 deletions crates/ospect/src/net/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ pub fn udp_v6_connections(pid: u32) -> std::io::Result<impl Iterator<Item = std:
conn::udp_v6(pid)
}

/// Returns an iterator over IPv4 TCP connections of all processes.
pub fn all_tcp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV4>>> {
crate::net::unix::all_tcp_v4_connections()
}

/// Returns an iterator over IPv6 TCP connections of all processes.
pub fn all_tcp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV6>>> {
crate::net::unix::all_tcp_v6_connections()
}

/// Returns an iterator over IPv4 UDP connections of all processes.
pub fn all_udp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV4>>> {
crate::net::unix::all_udp_v4_connections()
}

/// Returns an iterator over IPv6 UDP connections of all processes.
pub fn all_udp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV6>>> {
crate::net::unix::all_udp_v6_connections()
}

#[cfg(test)]
mod tests {

Expand Down
25 changes: 25 additions & 0 deletions crates/ospect/src/net/unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::*;

/// Returns an iterator over IPv4 TCP connections of all processes.
pub fn all_tcp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV4>>> {
let pids = crate::proc::ids()?;
Ok(pids.flat_map(|pid| crate::net::tcp_v4_connections(pid?)).flatten())
}

/// Returns an iterator over IPv6 TCP connections of all processes.
pub fn all_tcp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<TcpConnectionV6>>> {
let pids = crate::proc::ids()?;
Ok(pids.flat_map(|pid| crate::net::tcp_v6_connections(pid?)).flatten())
}

/// Returns an iterator over IPv4 UDP connections of all processes.
pub fn all_udp_v4_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV4>>> {
let pids = crate::proc::ids()?;
Ok(pids.flat_map(|pid| crate::net::udp_v4_connections(pid?)).flatten())
}

/// Returns an iterator over IPv6 UDP connections of all processes.
pub fn all_udp_v6_connections() -> std::io::Result<impl Iterator<Item = std::io::Result<UdpConnectionV6>>> {
let pids = crate::proc::ids()?;
Ok(pids.flat_map(|pid| crate::net::udp_v6_connections(pid?)).flatten())
}
2 changes: 2 additions & 0 deletions crates/rrg-proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ const PROTOS_V2: &'static [&'static str] = &[
"../../proto/rrg.proto",
"../../proto/rrg/blob.proto",
"../../proto/rrg/fs.proto",
"../../proto/rrg/net.proto",
"../../proto/rrg/os.proto",
"../../proto/rrg/startup.proto",
"../../proto/rrg/action/get_file_contents.proto",
"../../proto/rrg/action/get_file_metadata.proto",
"../../proto/rrg/action/get_filesystem_timeline.proto",
"../../proto/rrg/action/get_system_metadata.proto",
"../../proto/rrg/action/list_connections.proto",
];

fn main() {
Expand Down
Loading

0 comments on commit 7dca8f9

Please sign in to comment.