Cross-platform library for inspecting network sockets and owning processes.
- TCP and UDP socket inspection
- IPv4 and IPv6 support
- Optional process ownership information
- Iterator and query-based filtering API
- Optional
serdesupport for serializing socket data
- Linux
- macOS
- Windows
- FreeBSD
Add netsock to your dependencies:
[dependencies]
netsock = "0.8"Enable serde support if needed:
[dependencies]
netsock = { version = "0.8", features = ["serde"] }See examples and API docs for more details.
use netsock::family::AddressFamilyFlags;use netsock::get_sockets;use netsock::protocol::ProtocolFlags;use netsock::socket::ProtocolSocketInfo;fnmain(){let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;let proto_flags = ProtocolFlags::TCP | ProtocolFlags::UDP;matchget_sockets(af_flags, proto_flags){Ok(sockets) => {for socket in sockets {match socket.protocol_socket_info{ProtocolSocketInfo::Tcp(tcp_socket) => println!("[TCP] {}:{} -> {}:{} {:?} ({})",
tcp_socket.local_addr,
tcp_socket.local_port,
tcp_socket.remote_addr,
tcp_socket.remote_port,
tcp_socket.state,
socket.processes.len()),ProtocolSocketInfo::Udp(udp_socket) => println!("[UDP] {}:{} ({})",
udp_socket.local_addr,
udp_socket.local_port,
socket.processes.len()),}}}Err(err) => eprintln!("error: {err}"),}}use netsock::family::AddressFamilyFlags;use netsock::iter_sockets;use netsock::protocol::ProtocolFlags;use netsock::socket::{SocketIteratorExt,SocketQuery};fnmain() -> Result<(),Box<dyn std::error::Error>>{let query = SocketQuery::new().with_local_port(443);for socket initer_sockets(AddressFamilyFlags::all(),ProtocolFlags::TCP)?
.filter_by_query(query){println!("{:?}", socket?);}Ok(())}