Module net

Source
Expand description

Information about the networking layer.

This module corresponds to the /proc/net directory and contains various information about the networking layer.

§Example

Here’s an example that will print out all of the open and listening TCP sockets, and their corresponding processes, if know. This mimics the “netstat” utility, but for TCP only. You can run this example yourself with:

cargo run –example=netstat

let all_procs = procfs::process::all_processes().unwrap();

// build up a map between socket inodes and process stat info:
let mut map: HashMap<u64, Stat> = HashMap::new();
for p in all_procs {
    let Ok(process) = p else {
        // process vanished
        continue;
    };
    if let (Ok(stat), Ok(fds)) = (process.stat(), process.fd()) {
        for fd in fds {
            if let FDTarget::Socket(inode) = fd.unwrap().target {
                map.insert(inode, stat.clone());
            }
        }
    }
}

// get the tcp table
let tcp = procfs::net::tcp().unwrap();
let tcp6 = procfs::net::tcp6().unwrap();
println!("{:<26} {:<26} {:<15} {:<8} {}", "Local address", "Remote address", "State", "Inode", "PID/Program name");
for entry in tcp.into_iter().chain(tcp6) {
    // find the process (if any) that has an open FD to this entry's inode
    let local_address = format!("{}", entry.local_address);
    let remote_addr = format!("{}", entry.remote_address);
    let state = format!("{:?}", entry.state);
    if let Some(stat) = map.get(&entry.inode) {
        println!("{:<26} {:<26} {:<15} {:<12} {}/{}", local_address, remote_addr, state, entry.inode, stat.pid, stat.comm);
    } else {
        // We might not always be able to find the process associated with this socket
        println!("{:<26} {:<26} {:<15} {:<12} -", local_address, remote_addr, state, entry.inode);
    }
}

Structs§

ARPEntry
An entry in the ARP table
ARPFlags
Flags for ARP entries
ARPHardware
Hardware type for an ARP table entry.
ArpEntries
ARP table entries.
DeviceStatus
General statistics for a network interface/device
InterfaceDeviceStatus
Device status information for all network interfaces.
RouteEntries
A set of ipv4 routes.
RouteEntry
An entry in the ipv4 route table
Snmp
This struct holds the data needed for the IP, ICMP, TCP, and UDP management information bases for an SNMP agent.
Snmp6
This struct holds the data needed for the IP, ICMP, TCP, and UDP management information bases for an SNMP agent.
TcpNetEntries
TCP socket entries.
TcpNetEntry
An entry in the TCP socket table
UdpNetEntries
UDP socket entries.
UdpNetEntry
An entry in the UDP socket table
UnixNetEntries
Unix socket entries.
UnixNetEntry
An entry in the Unix socket table

Enums§

IpForwarding
The indication of whether this entity is acting as an IP gateway in respect to the forwarding of datagrams received by, but not addressed to, this entity. IP gateways forward datagrams. IP hosts do not (except those source-routed via the host).
TcpRtoAlgorithm
The algorithm used to determine the timeout value used for retransmitting unacknowledged octets.
TcpState
UdpState
UnixState

Functions§

arp
Reads the ARP table
dev_status
Returns basic network device statistics for all interfaces
route
Reads the ipv4 route table
snmp
Reads the network management information by Simple Network Management Protocol
snmp6
Reads the network management information of IPv6 by Simple Network Management Protocol
tcp
Reads the tcp socket table
tcp6
Reads the tcp6 socket table
udp
Reads the udp socket table
udp6
Reads the udp6 socket table
unix
Reads the unix socket table