netsock
Cross-platform library for network sockets information.
Features
- Retrieve information for TCP and UDP sockets
- Support for IPv4 and IPv6
- Fetch socket information with process info
Supported platform
Usage
Add netsock
to your dependencies
[dependencies]
netsock = "0.2"
For more details, see examples or doc.
Example
use netsock::family::AddressFamilyFlags;
use netsock::protocol::ProtocolFlags;
use netsock::socket::ProtocolSocketInfo;
use netsock::get_sockets;
fn main() {
let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
let proto_flags = ProtocolFlags::TCP | ProtocolFlags::UDP;
match get_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,
socket.processes,
tcp_socket.state
),
ProtocolSocketInfo::Udp(udp_socket) => println!(
"[UDP] {}:{} -> *:* {:?}",
udp_socket.local_addr, udp_socket.local_port, socket.processes
),
}
}
}
Err(e) => {
eprintln!("Error: {}", e);
}
}
}
Inspired by