netsock/
socket.rs

1use crate::process::Process;
2use crate::state::TcpState;
3use std::net::IpAddr;
4
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8/// Represents general information about a socket, encompassing both protocol-specific details
9/// and process associations.
10#[derive(Clone, Debug, PartialEq)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub struct SocketInfo {
13    /// Holds protocol-specific information, either TCP or UDP.
14    pub protocol_socket_info: ProtocolSocketInfo,
15    /// Lists processes associated with the socket, providing a connection between the socket
16    /// and the processes utilizing it.
17    pub processes: Vec<Process>,
18    #[cfg(any(target_os = "linux", target_os = "android"))]
19    /// Represents the inode number of the socket on Linux or Android systems, offering a unique
20    /// identifier in the filesystem's context.
21    pub inode: u32,
22    #[cfg(any(target_os = "linux", target_os = "android"))]
23    /// Stores the owner's user ID (UID) for this socket, indicating who has the rights to manipulate it.
24    pub uid: u32,
25}
26
27/// Defines protocol-specific socket information, distinguishing between TCP and UDP protocols.
28#[derive(Clone, Debug, PartialEq)]
29#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
30pub enum ProtocolSocketInfo {
31    /// Contains TCP-specific information, encapsulating the state and local/remote endpoints.
32    Tcp(TcpSocketInfo),
33    /// Contains UDP-specific information, focusing on the local endpoint as UDP is connectionless.
34    Udp(UdpSocketInfo),
35}
36
37/// Provides detailed information specific to TCP sockets, including endpoint addresses and the connection state.
38#[derive(Clone, Debug, PartialEq)]
39#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
40pub struct TcpSocketInfo {
41    /// The local IP address of the TCP socket.
42    pub local_addr: IpAddr,
43    /// The local port number of the TCP socket.
44    pub local_port: u16,
45    /// The remote IP address this socket is connected to.
46    pub remote_addr: IpAddr,
47    /// The remote port number this socket is connected to.
48    pub remote_port: u16,
49    /// The current state of the TCP connection.
50    pub state: TcpState,
51}
52
53/// Provides information specific to UDP sockets, which primarily includes the local endpoint data.
54#[derive(Clone, Debug, PartialEq)]
55#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
56pub struct UdpSocketInfo {
57    /// The local IP address of the UDP socket.
58    pub local_addr: IpAddr,
59    /// The local port number of the UDP socket.
60    pub local_port: u16,
61}
62
63impl SocketInfo {
64    /// Retrieves the local IP address associated with this socket, applicable to both TCP and UDP.
65    pub fn local_addr(&self) -> IpAddr {
66        match &self.protocol_socket_info {
67            ProtocolSocketInfo::Tcp(s) => s.local_addr,
68            ProtocolSocketInfo::Udp(s) => s.local_addr,
69        }
70    }
71
72    /// Retrieves the local port associated with this socket, applicable to both TCP and UDP.
73    pub fn local_port(&self) -> u16 {
74        match &self.protocol_socket_info {
75            ProtocolSocketInfo::Tcp(s) => s.local_port,
76            ProtocolSocketInfo::Udp(s) => s.local_port,
77        }
78    }
79}