Skip to content

Commit 0adc9e7

Browse files
committed
Ignore connections that fail parsing instead of panicking on BSD
- Tentative fix for #217
1 parent 94ada5d commit 0adc9e7

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/os/lsof.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ pub(crate) fn get_open_sockets() -> OpenSockets {
88
let connections = get_connections();
99

1010
for raw_connection in connections {
11+
let Some(ip) = raw_connection.get_local_ip() else {
12+
continue;
13+
};
14+
let Some(port) = raw_connection.get_local_port() else {
15+
continue;
16+
};
17+
let Some(protocol) = raw_connection.get_protocol() else {
18+
continue;
19+
};
1120
open_sockets.insert(
12-
LocalSocket {
13-
ip: raw_connection.get_local_ip(),
14-
port: raw_connection.get_local_port(),
15-
protocol: raw_connection.get_protocol(),
16-
},
21+
LocalSocket { ip, port, protocol },
1722
raw_connection.process_name.clone(),
1823
);
1924
}

src/os/lsof_utils.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ impl RawConnection {
103103
}
104104
}
105105

106-
pub fn get_protocol(&self) -> Protocol {
107-
Protocol::from_str(&self.protocol).unwrap()
106+
pub fn get_protocol(&self) -> Option<Protocol> {
107+
Protocol::from_str(&self.protocol)
108108
}
109109

110-
pub fn get_local_ip(&self) -> IpAddr {
111-
self.local_ip.parse().unwrap()
110+
pub fn get_local_ip(&self) -> Option<IpAddr> {
111+
self.local_ip.parse().ok()
112112
}
113113

114-
pub fn get_local_port(&self) -> u16 {
115-
self.local_port.parse::<u16>().unwrap()
114+
pub fn get_local_port(&self) -> Option<u16> {
115+
self.local_port.parse::<u16>().ok()
116116
}
117117
}
118118

@@ -203,7 +203,7 @@ com.apple 590 etoledom 204u IPv4 0x28ffb9c04111253f 0t0 TCP 192.168.1.
203203
}
204204
fn test_raw_connection_parse_local_port(raw_output: &str) {
205205
let connection = RawConnection::new(raw_output).unwrap();
206-
assert_eq!(connection.get_local_port(), 1111);
206+
assert_eq!(connection.get_local_port(), Some(1111));
207207
}
208208

209209
#[test]
@@ -216,7 +216,7 @@ com.apple 590 etoledom 204u IPv4 0x28ffb9c04111253f 0t0 TCP 192.168.1.
216216
}
217217
fn test_raw_connection_parse_protocol(raw_line: &str) {
218218
let connection = RawConnection::new(raw_line).unwrap();
219-
assert_eq!(connection.get_protocol(), Protocol::Udp);
219+
assert_eq!(connection.get_protocol(), Some(Protocol::Udp));
220220
}
221221

222222
#[test]

0 commit comments

Comments
 (0)