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

```rust

[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.asrawfd();

[cfg(windows)]

let fd = sock.asrawsocket();

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

// prints: // Ok( // SocketStat { // unreceived: 0, // unsent: 0, // connectioninfo: tcpconnectioninfo { // tcpistate: "ESTABLISHED", // tcpisndwscale: 8, // tcpircvwscale: 6, // tcpioptions: 7, // tcpiflags: 0, // tcpirto: 0, // tcpimaxseg: 1368, // tcpisndssthresh: 1073725440, // tcpisndcwnd: 4380, // tcpisndwnd: 60192, // tcpisndsbbytes: 0, // tcpircvwnd: 131328, // tcpirttcur: 79, // tcpisrtt: 79, // tcpirttvar: 39, // tcpitfo: 0, // tcpitxpackets: 0, // tcpitxbytes: 0, // tcpitxretransmitbytes: 0, // tcpirxpackets: 0, // tcpirxbytes: 0, // tcpirxoutoforderbytes: 0, // tcpitxretransmitpackets: 0, // }, // socketinfo: tcpsockinfo { // tcpsiini: insockinfo { // insifport: 80, // insilport: 52621, // insigencnt: 100950561, // insiflags: 8390720, // insiflow: 0, // insivflag: "IPV4", // insiipttl: 64, // rfu1: 0, // }, // tcpsistate: "ESTABLISHED", // tcpsitimer: [ // 0, // 0, // 7200079, // 0, // ], // tcpsimss: 1368, // tcpsiflags: 1140851680, // rfu1: 0, // tcpsitp: 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: * sysctl([CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_PCBLIST], ... * https://stackoverflow.com/questions/31263289/on-linux-mac-windows-is-it-possible-to-access-the-tcp-timestamp-and-or-rtt-in-u

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.