Skip to content

Latest commit

History

77 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

netsock Crates.ioLicense

Cross-platform library for inspecting network sockets and owning processes.

Features

  • TCP and UDP socket inspection
  • IPv4 and IPv6 support
  • Optional process ownership information
  • Iterator and query-based filtering API
  • Optional serde support for serializing socket data

Supported Platforms

  • Linux
  • macOS
  • Windows
  • FreeBSD

Installation

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.

Basic Example

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}"),}}

Query Filtering Example

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(())}

Related Crates

About

Cross-platform library for network sockets information

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages