@@ -3,6 +3,7 @@ use hickory_resolver::{
33} ;
44use local_ip_address:: list_afinet_netifas;
55use serde:: Serialize ;
6+ use snafu:: { ResultExt , Snafu } ;
67use std:: {
78 collections:: { BTreeSet , HashMap } ,
89 net:: IpAddr ,
@@ -11,6 +12,12 @@ use std::{
1112} ;
1213use tokio:: task:: JoinSet ;
1314
15+ #[ derive( Debug , Snafu ) ]
16+ pub enum Error {
17+ #[ snafu( display( "failed to list network interfaces" ) ) ]
18+ ListInterfaces { source : local_ip_address:: Error } ,
19+ }
20+
1421static GLOBAL_DNS_RESOLVER : LazyLock < Option < TokioResolver > > = LazyLock :: new ( || {
1522 let ( resolver_config, mut resolver_opts) = match read_system_conf ( ) {
1623 Ok ( conf) => conf,
@@ -42,44 +49,33 @@ pub struct SystemNetworkInfo {
4249
4350impl SystemNetworkInfo {
4451 #[ tracing:: instrument( name = "SystemNetworkInfo::collect" ) ]
45- pub async fn collect ( ) -> SystemNetworkInfo {
46- let interfaces = match list_afinet_netifas ( ) {
47- Ok ( netifs) => {
48- let mut interface_map = std:: collections:: HashMap :: new ( ) ;
52+ pub async fn collect ( ) -> Result < SystemNetworkInfo , Error > {
53+ let netifs = list_afinet_netifas ( ) . context ( ListInterfacesSnafu ) ?;
54+ let mut interfaces = HashMap :: new ( ) ;
4955
50- // Iterate over the network interfaces and group them by name
51- // An interface may appear multiple times if it has multiple IP addresses (e.g. IPv4 and IPv6)
52- for ( name, ip_addr) in netifs {
53- tracing:: info!(
54- network. interface. name = name,
55- network. interface. address = %ip_addr,
56- "found network interface"
57- ) ;
58- interface_map
59- . entry ( name)
60- . or_insert_with ( Vec :: new)
61- . push ( ip_addr) ;
62- }
63- interface_map
64- }
65- Err ( error) => {
66- tracing:: error!(
67- error = & error as & dyn std:: error:: Error ,
68- "failed to list network interfaces"
69- ) ;
70- HashMap :: new ( )
71- }
72- } ;
56+ // Iterate over the network interfaces and group them by name
57+ // An interface may appear multiple times if it has multiple IP addresses (e.g. IPv4 and IPv6)
58+ for ( name, ip_addr) in netifs {
59+ tracing:: info!(
60+ network. interface. name = name,
61+ network. interface. address = %ip_addr,
62+ "found network interface"
63+ ) ;
64+ interfaces
65+ . entry ( name)
66+ . or_insert_with ( Vec :: new)
67+ . push ( ip_addr) ;
68+ }
7369
7470 let ips: BTreeSet < IpAddr > = interfaces. values ( ) . flatten ( ) . copied ( ) . collect ( ) ;
7571 tracing:: info!( network. addresses. ip = ?ips, "ip addresses" ) ;
7672
7773 let Some ( resolver) = GLOBAL_DNS_RESOLVER . as_ref ( ) else {
78- return SystemNetworkInfo {
74+ return Ok ( SystemNetworkInfo {
7975 interfaces,
8076 reverse_lookups : HashMap :: new ( ) ,
8177 forward_lookups : HashMap :: new ( ) ,
82- } ;
78+ } ) ;
8379 } ;
8480
8581 let mut reverse_lookup_tasks = JoinSet :: new ( ) ;
@@ -139,10 +135,10 @@ impl SystemNetworkInfo {
139135 } )
140136 . collect ( ) ;
141137
142- SystemNetworkInfo {
138+ Ok ( SystemNetworkInfo {
143139 interfaces,
144140 reverse_lookups,
145141 forward_lookups,
146- }
142+ } )
147143 }
148144}
0 commit comments