Skip to content

Commit c2031bb

Browse files
committed
Update readme and don't expose Error directly
1 parent cbe66ec commit c2031bb

File tree

14 files changed

+56
-14
lines changed

14 files changed

+56
-14
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,24 @@ Cross-platform library to retrieve network sockets information.
88
Aims to be optimal by using low-level OS APIs instead of command line utilities.
99
Provides unified interface and returns data structures which may have additional fields depending on platform.
1010

11+
```toml
12+
# Cargo.toml
13+
[dependencies]
14+
netstat2 = "0.8"
15+
```
16+
1117
This is a fork based on the [netstat](https://crates.io/crates/netstat) crate by [ivxvm](https://github.com/ivxvm).
1218

1319
## Example
1420

1521
```rust
16-
use netstat2::*;
22+
use netstat2::{get_sockets_info, AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo};
1723

18-
fn main() {
24+
fn main() -> Result<(), Box<dyn std::error::Error>> {
1925
let af_flags = AddressFamilyFlags::IPV4 | AddressFamilyFlags::IPV6;
2026
let proto_flags = ProtocolFlags::TCP | ProtocolFlags::UDP;
21-
let sockets_info = get_sockets_info(af_flags, proto_flags).unwrap();
27+
let sockets_info = get_sockets_info(af_flags, proto_flags)?;
28+
2229
for si in sockets_info {
2330
match si.protocol_socket_info {
2431
ProtocolSocketInfo::Tcp(tcp_si) => println!(
@@ -36,6 +43,8 @@ fn main() {
3643
),
3744
}
3845
}
46+
47+
Ok(())
3948
}
4049
```
4150

src/integrations/linux/api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::integrations::linux::netlink_iterator::*;
22
use crate::integrations::linux::procfs::*;
3+
use crate::types::error::Error;
34
use crate::types::*;
45
use libc::*;
56

src/integrations/linux/ext/tcp_state_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl From<u8> for TcpState {
1414
9 => TcpState::LastAck,
1515
10 => TcpState::Listen,
1616
11 => TcpState::Closing,
17-
_ => panic!("Unknown TcpState!"),
17+
_ => TcpState::Unknown,
1818
}
1919
}
2020
}

src/integrations/linux/netlink_iterator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::integrations::linux::ffi::*;
22
use crate::types::*;
3+
use crate::types::error::*;
34
use libc::*;
45
use std;
56
use std::io;

src/integrations/osx/api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::integrations::osx::netstat::*;
2+
use crate::types::error::Error;
23
use crate::types::*;
34

45
/// Iterate through sockets information.

src/integrations/osx/netstat.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ use std::ptr;
1212
use std::{io, mem};
1313

1414
use crate::integrations::osx::ffi::libproc::*;
15+
use crate::types::error::Error;
1516
use crate::types::{AddressFamilyFlags, ProtocolFlags};
16-
use crate::{Error, UdpSocketInfo};
17-
use crate::{ProtocolSocketInfo, SocketInfo, TcpSocketInfo, TcpState};
18-
use bitflags::_core::result::Result::Err;
19-
use std::collections::hash_map::Entry;
20-
use std::collections::HashMap;
17+
use crate::{ProtocolSocketInfo, SocketInfo, TcpSocketInfo, TcpState, UdpSocketInfo};
2118

2219
pub type PID = c_int;
2320

src/integrations/shared_api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::integrations::*;
2+
use crate::types::error::Error;
23
use crate::types::*;
34

45
/// Retrieve sockets information as a vector.

src/integrations/windows/api.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::integrations::windows::ffi::*;
22
use crate::integrations::windows::socket_table_iterator::SocketTableIterator;
33
use crate::types::*;
4+
use crate::types::error::*;
45

56
/// Iterate through sockets information.
67
pub fn iterate_sockets_info(

src/integrations/windows/ext/tcp_state_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl From<ffi::DWORD> for TcpState {
1616
ffi::MIB_TCP_STATE_LAST_ACK => TcpState::LastAck,
1717
ffi::MIB_TCP_STATE_TIME_WAIT => TcpState::TimeWait,
1818
ffi::MIB_TCP_STATE_DELETE_TCB => TcpState::DeleteTcb,
19-
_ => panic!("Unknown TcpState!"),
19+
_ => TcpState::Unknown,
2020
}
2121
}
2222
}

src/integrations/windows/socket_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::integrations::windows::ffi::*;
22
use crate::integrations::windows::socket_table_extended::SocketTable;
33
use crate::types::*;
4+
use crate::types::error::*;
45
use std;
56
use std::net::{IpAddr, Ipv4Addr};
67

0 commit comments

Comments
 (0)