Skip to content

Commit

Permalink
Limit possible connection states for UdpNetEntry by introducing UdpState
Browse files Browse the repository at this point in the history
As suggested in prometheus#61.
  • Loading branch information
edigaryev committed Dec 26, 2019
1 parent 89f2bb0 commit 8a55ab4
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions 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,7 +147,7 @@ pub struct TcpNetEntry {
pub struct UdpNetEntry {
pub local_address: SocketAddr,
pub remote_address: SocketAddr,
pub state: TcpState,
pub state: UdpState,
pub rx_queue: u32,
pub tx_queue: u32,
pub inode: u32,
Expand Down Expand Up @@ -254,7 +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!(TcpState::from_u8(from_str!(u8, state, 16))),
state: expect!(UdpState::from_u8(from_str!(u8, state, 16))),
inode: from_str!(u32, inode),
});
}
Expand Down

0 comments on commit 8a55ab4

Please sign in to comment.