@@ -21,16 +21,6 @@ lazy_static! {
2121 static ref LISTEN_REGEX : Regex = Regex :: new( r"(.*):(.*)" ) . unwrap( ) ;
2222}
2323
24- fn parse_ip_addr ( ip : & str ) -> IpAddr {
25- if let Ok ( v4addr) = ip. parse ( ) {
26- IpAddr :: V4 ( v4addr)
27- } else if let Ok ( v6addr) = ip. parse ( ) {
28- IpAddr :: V6 ( v6addr)
29- } else {
30- panic ! ( "{} is not a valid IP address" , ip)
31- }
32- }
33-
3424fn get_null_addr ( ip_type : & str ) -> & str {
3525 if ip_type. contains ( '4' ) {
3626 "0.0.0.0"
@@ -39,16 +29,12 @@ fn get_null_addr(ip_type: &str) -> &str {
3929 }
4030}
4131
42- #[ allow( clippy:: needless_return) ]
4332impl RawConnection {
4433 pub fn new ( raw_line : & str ) -> Option < RawConnection > {
34+ // Example row
35+ // com.apple 664 user 198u IPv4 0xeb179a6650592b8d 0t0 TCP 192.168.1.187:58535->1.2.3.4:443 (ESTABLISHED)
4536 let columns: Vec < & str > = raw_line. split_ascii_whitespace ( ) . collect ( ) ;
4637 if columns. len ( ) < 9 {
47- println ! (
48- "lsof's output string has {} columns, different than expected: {:#?}" ,
49- columns. len( ) ,
50- columns
51- ) ;
5238 return None ;
5339 }
5440 let process_name = columns[ 0 ] . replace ( "\\ x20" , " " ) ;
@@ -68,6 +54,10 @@ impl RawConnection {
6854 // let connection_state = columns[9];
6955 // If this socket is in a "connected" state
7056 if let Some ( caps) = CONNECTION_REGEX . captures ( connection_str) {
57+ // Example
58+ // 192.168.1.187:64230->0.1.2.3:5228
59+ // *:*
60+ // *:4567
7161 let local_ip = String :: from ( caps. get ( 1 ) . unwrap ( ) . as_str ( ) ) ;
7262 let local_port = String :: from ( caps. get ( 2 ) . unwrap ( ) . as_str ( ) ) ;
7363 let remote_ip = String :: from ( caps. get ( 3 ) . unwrap ( ) . as_str ( ) ) ;
@@ -110,23 +100,23 @@ impl RawConnection {
110100 }
111101
112102 pub fn get_protocol ( & self ) -> Protocol {
113- return Protocol :: from_str ( & self . protocol ) . unwrap ( ) ;
103+ Protocol :: from_str ( & self . protocol ) . unwrap ( )
114104 }
115105
116106 pub fn get_remote_ip ( & self ) -> IpAddr {
117- return parse_ip_addr ( & self . remote_ip ) ;
107+ self . remote_ip . parse ( ) . unwrap ( )
118108 }
119109
120110 pub fn get_remote_port ( & self ) -> u16 {
121- return self . remote_port . parse :: < u16 > ( ) . unwrap ( ) ;
111+ self . remote_port . parse :: < u16 > ( ) . unwrap ( )
122112 }
123113
124114 pub fn get_local_ip ( & self ) -> IpAddr {
125- return parse_ip_addr ( & self . local_ip ) ;
115+ self . local_ip . parse ( ) . unwrap ( )
126116 }
127117
128118 pub fn get_local_port ( & self ) -> u16 {
129- return self . local_port . parse :: < u16 > ( ) . unwrap ( ) ;
119+ self . local_port . parse :: < u16 > ( ) . unwrap ( )
130120 }
131121}
132122
0 commit comments