1- use libc:: { IPPROTO_TCP , IPPROTO_UDP } ;
21use sysctl:: Sysctl ;
32
4- use crate :: utils:: { check , is_ipv6 } ;
5- use crate :: FindProc ;
3+ use crate :: utils:: { is_ipv6 , pre_condition } ;
4+ use crate :: NetworkProtocol ;
65
76use std:: io;
87use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
@@ -15,28 +14,24 @@ const PROCPIDPATHINFOSIZE: usize = 1024;
1514const PROCCALLNUMPIDINFO : i32 = 0x2 ;
1615
1716static STRUCT_SIZE : AtomicUsize = AtomicUsize :: new ( 0 ) ;
18- const STRUCT_SIZE_SETTER : Once = Once :: new ( ) ;
19-
20- pub struct FindProcImpl ;
21-
22- impl FindProc for FindProcImpl {
23- fn resolve (
24- src : Option < std:: net:: SocketAddr > ,
25- dst : Option < std:: net:: SocketAddr > ,
26- proto : i32 ,
27- ) -> Option < String > {
28- if !check ( src, dst) {
29- return None ;
30- }
31- find_process_name ( src, dst, proto) . ok ( )
32- }
17+ static STRUCT_SIZE_SETTER : Once = Once :: new ( ) ;
18+
19+ pub fn find_process_name (
20+ src : Option < std:: net:: SocketAddr > ,
21+ dst : Option < std:: net:: SocketAddr > ,
22+ proto : NetworkProtocol ,
23+ ) -> Option < String > {
24+ find_process_name_inner ( src, dst, proto) . ok ( )
3325}
3426
35- fn find_process_name (
27+ fn find_process_name_inner (
3628 src : Option < std:: net:: SocketAddr > ,
3729 dst : Option < std:: net:: SocketAddr > ,
38- proto : i32 ,
30+ proto : NetworkProtocol ,
3931) -> Result < String , io:: Error > {
32+ if !pre_condition ( src, dst) {
33+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidInput , "Invalid input" ) ) ;
34+ }
4035 STRUCT_SIZE_SETTER . call_once ( || {
4136 let default = "" . to_string ( ) ;
4237 let ctl = sysctl:: Ctl :: new ( "kern.osrelease" ) . unwrap ( ) ;
@@ -53,14 +48,8 @@ fn find_process_name(
5348
5449 // see: https://github.com/apple-oss-distributions/xnu/blob/94d3b452840153a99b38a3a9659680b2a006908e/bsd/netinet/in_pcblist.c#L292
5550 let spath = match proto {
56- IPPROTO_TCP => "net.inet.tcp.pcblist_n" ,
57- IPPROTO_UDP => "net.inet.udp.pcblist_n" ,
58- _ => {
59- return Err ( io:: Error :: new (
60- io:: ErrorKind :: InvalidInput ,
61- "Invalid network" ,
62- ) )
63- }
51+ NetworkProtocol :: TCP => "net.inet.tcp.pcblist_n" ,
52+ NetworkProtocol :: UDP => "net.inet.udp.pcblist_n" ,
6453 } ;
6554
6655 let is_ipv4 = !is_ipv6 ( src, dst) ;
@@ -69,7 +58,12 @@ fn find_process_name(
6958 let value = ctl. value ( ) . unwrap ( ) ;
7059 let buf = value. as_struct ( ) . unwrap ( ) ;
7160 let struct_size = STRUCT_SIZE . load ( std:: sync:: atomic:: Ordering :: Relaxed ) ;
72- let item_size = struct_size + if proto == IPPROTO_TCP { 208 } else { 0 } ;
61+ let item_size = struct_size
62+ + if proto == NetworkProtocol :: TCP {
63+ 208
64+ } else {
65+ 0
66+ } ;
7367
7468 // see https://github.com/apple-oss-distributions/xnu/blob/94d3b452840153a99b38a3a9659680b2a006908e/bsd/netinet/in_pcb.h#L451
7569 // offset of flag is 44
@@ -144,7 +138,7 @@ fn find_process_name(
144138fn get_pid ( bytes : & [ u8 ] ) -> u32 {
145139 assert_eq ! ( bytes. len( ) , 4 ) ;
146140 let mut pid_bytes = [ 0 ; 4 ] ;
147- pid_bytes. copy_from_slice ( & bytes) ;
141+ pid_bytes. copy_from_slice ( bytes) ;
148142 if cfg ! ( target_endian = "big" ) {
149143 u32:: from_be_bytes ( pid_bytes)
150144 } else {
0 commit comments