Skip to content

Commit

Permalink
Merge pull request prometheus#61 from edigaryev/net-udp-state
Browse files Browse the repository at this point in the history
UDP sockets have a state too
  • Loading branch information
eminence committed Dec 27, 2019
2 parents 2fd17ea + 8a55ab4 commit 744fd5c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ impl TcpState {
}
}

#[derive(Debug, PartialEq)]
pub enum UdpState {
Established = 1,
Close = 7,
}

impl UdpState {
pub fn from_u8(num: u8) -> Option<UdpState> {
match num {
0x01 => Some(UdpState::Established),
0x07 => Some(UdpState::Close),
_ => None,
}
}

pub fn to_u8(&self) -> u8 {
match self {
UdpState::Established => 0x01,
UdpState::Close => 0x07,
}
}
}

/// An entry in the TCP socket table
#[derive(Debug)]
pub struct TcpNetEntry {
Expand All @@ -124,6 +147,7 @@ pub struct TcpNetEntry {
pub struct UdpNetEntry {
pub local_address: SocketAddr,
pub remote_address: SocketAddr,
pub state: UdpState,
pub rx_queue: u32,
pub tx_queue: u32,
pub inode: u32,
Expand Down Expand Up @@ -238,7 +262,7 @@ pub fn read_udp_table<R: Read>(reader: BufReader<R>) -> ProcResult<Vec<UdpNetEnt
s.next();
let local_address = expect!(s.next(), "udp::local_address");
let rem_address = expect!(s.next(), "udp::rem_address");
s.next(); // skip state
let state = expect!(s.next(), "udp::st");
let mut tx_rx_queue = expect!(s.next(), "udp::tx_queue:rx_queue").splitn(2, ':');
let tx_queue: u32 = from_str!(u32, expect!(tx_rx_queue.next(), "udp::tx_queue"), 16);
let rx_queue: u32 = from_str!(u32, expect!(tx_rx_queue.next(), "udp::rx_queue"), 16);
Expand All @@ -253,6 +277,7 @@ pub fn read_udp_table<R: Read>(reader: BufReader<R>) -> ProcResult<Vec<UdpNetEnt
remote_address: parse_addressport_str(rem_address)?,
rx_queue,
tx_queue,
state: expect!(UdpState::from_u8(from_str!(u8, state, 16))),
inode: from_str!(u32, inode),
});
}
Expand Down

0 comments on commit 744fd5c

Please sign in to comment.