1 unstable release

0.1.0 Aug 22, 2019

#468 in Machine learning

Download history 23/week @ 2024-03-17 23/week @ 2024-03-24 58/week @ 2024-03-31 18/week @ 2024-04-07 16/week @ 2024-04-14 22/week @ 2024-04-21 17/week @ 2024-04-28 15/week @ 2024-05-05 20/week @ 2024-05-12 20/week @ 2024-05-19 22/week @ 2024-05-26 22/week @ 2024-06-02 14/week @ 2024-06-09 17/week @ 2024-06-16 19/week @ 2024-06-23 7/week @ 2024-06-30

59 downloads per month
Used in 4 crates (via tcp_typed)

MIT/Apache

24KB
451 lines

socketstat

Crates.io MIT / Apache 2.0 licensed Build Status

Docs

Get socket information and statistics.

Currently works on macOS only, PRs for other platforms welcome!

Example

#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawSocket;
use socketstat::socketstat;

let sock = std::net::TcpStream::connect("google.com:80").unwrap();

#[cfg(unix)]
let fd = sock.as_raw_fd();
#[cfg(windows)]
let fd = sock.as_raw_socket();

println!("{:#?}", socketstat(fd));

// prints:
//   Ok(
//       SocketStat {
//           unreceived: 0,
//           unsent: 0,
//           connection_info: tcp_connection_info {
//               tcpi_state: "ESTABLISHED",
//               tcpi_snd_wscale: 8,
//               tcpi_rcv_wscale: 6,
//               tcpi_options: 7,
//               tcpi_flags: 0,
//               tcpi_rto: 0,
//               tcpi_maxseg: 1368,
//               tcpi_snd_ssthresh: 1073725440,
//               tcpi_snd_cwnd: 4380,
//               tcpi_snd_wnd: 60192,
//               tcpi_snd_sbbytes: 0,
//               tcpi_rcv_wnd: 131328,
//               tcpi_rttcur: 79,
//               tcpi_srtt: 79,
//               tcpi_rttvar: 39,
//               tcpi_tfo: 0,
//               tcpi_txpackets: 0,
//               tcpi_txbytes: 0,
//               tcpi_txretransmitbytes: 0,
//               tcpi_rxpackets: 0,
//               tcpi_rxbytes: 0,
//               tcpi_rxoutoforderbytes: 0,
//               tcpi_txretransmitpackets: 0,
//           },
//           socket_info: tcp_sockinfo {
//               tcpsi_ini: in_sockinfo {
//                   insi_fport: 80,
//                   insi_lport: 52621,
//                   insi_gencnt: 100950561,
//                   insi_flags: 8390720,
//                   insi_flow: 0,
//                   insi_vflag: "IPV4",
//                   insi_ip_ttl: 64,
//                   rfu_1: 0,
//               },
//               tcpsi_state: "ESTABLISHED",
//               tcpsi_timer: [
//                   0,
//                   0,
//                   7200079,
//                   0,
//               ],
//               tcpsi_mss: 1368,
//               tcpsi_flags: 1140851680,
//               rfu_1: 0,
//               tcpsi_tp: 9662996336038732135,
//           },
//       },
//   )

Note

On macOS this calls:

  • getsockopt(fd, IPPROTO_TCP, TCP_CONNECTION_INFO, ...)
  • proc_pidfdinfo(getpid(), fd, PROC_PIDFDSOCKETINFO, ...)
  • ioctl(fd, FIONREAD, ...)
  • getsockopt(fd, SOL_SOCKET, SO_NWRITE, ...)

Other sources to explore:

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3MB
~56K SLoC