diff --git a/Parse-Netstat-Search/lib/Parse/Netstat/Search.pm b/Parse-Netstat-Search/lib/Parse/Netstat/Search.pm index eee04eb..9b0c045 100644 --- a/Parse-Netstat-Search/lib/Parse/Netstat/Search.pm +++ b/Parse-Netstat-Search/lib/Parse/Netstat/Search.pm @@ -180,7 +180,7 @@ sub search{ $protocol_require=1; } if (defined( (keys(%{ $self->{states} }))[0] )){ - $protocol_require=1; + $state_require=1; } my $res_int=0; @@ -200,7 +200,9 @@ sub search{ my $port_meet=1; my $cidr_meet=1; my $protocol_meet=1; + my $protocol_search=lc( $protocol ); my $state_meet=1; + my $state_search=lc( $state ); # reset the meet checks if ( $port_require ) { @@ -220,8 +222,14 @@ sub search{ if ( $cidr_require && ( - ( Net::CIDR::cidrlookup( $foreign_host, @{ $self->{cidrs} } ) ) || - ( Net::CIDR::cidrlookup( $local_host, @{ $self->{cidrs} } ) ) + ( + ( $foreign_host ne '*' ) && + ( Net::CIDR::cidrlookup( $foreign_host, @{ $self->{cidrs} } ) ) + ) || + ( + ( $local_host ne '*' ) && + ( Net::CIDR::cidrlookup( $local_host, @{ $self->{cidrs} } ) ) + ) ) ) { $cidr_meet=1; @@ -241,7 +249,7 @@ sub search{ # check protocol to see if it is one that is required if ( $protocol_require && - defined( $self->{protocols}{$protocol} ) + defined( $self->{protocols}{$protocol_search} ) ){ $protocol_meet=1; } @@ -249,7 +257,7 @@ sub search{ # check state to see if it is one that is required if ( $state_require && - defined( $self->{states}{$state} ) + defined( $self->{states}{$state_search} ) ){ $state_meet=1; } @@ -301,7 +309,7 @@ Validating in is done by Net::CIDR::cidrvalidate. sub set_cidrs{ my $self=$_[0]; my @cidrs; - if ( defined( $_ ) ){ + if ( defined( $_[1] ) ){ @cidrs=@{ $_[1] }; } @@ -315,16 +323,18 @@ sub set_cidrs{ } #chueck each one - my $cidr=pop(@cidrs); - while ( defined( $cidr ) ){ + my $cidr_int=0; + while ( defined( $cidrs[$cidr_int] ) ){ + my $cidr=$cidrs[$cidr_int]; if ( ! Net::CIDR::cidrvalidate( $cidr ) ){ $self->{error}=1; $self->{errorString}='"'.$cidr.'" is not a valid CIDR according to Net::CIDR::cidrvalidate'; $self->warn; return undef; + } - $cidr=pop(@cidrs); + $cidr_int++; } $self->{cidrs}=\@cidrs; @@ -355,7 +365,7 @@ The ports can be either numeric or by name. sub set_ports{ my $self=$_[0]; my @ports; - if ( defined( $_ ) ){ + if ( defined( $_[1] ) ){ @ports=@{ $_[1] }; } @@ -425,7 +435,7 @@ Starting and trailing white space is removed. sub set_protocols{ my $self=$_[0]; my @protocols; - if ( defined( $_ ) ){ + if ( defined( $_[1] ) ){ @protocols=@{ $_[1] }; } @@ -483,7 +493,7 @@ Starting and trailing white space is removed. sub set_states{ my $self=$_[0]; my @states; - if ( defined( $_ ) ){ + if ( defined( $_[1] ) ){ @states=@{ $_[1] }; } diff --git a/Parse-Netstat-Search/t/search.t b/Parse-Netstat-Search/t/search.t index 5942a0d..e47542f 100644 --- a/Parse-Netstat-Search/t/search.t +++ b/Parse-Netstat-Search/t/search.t @@ -10,9 +10,9 @@ my $res=[ '0', '1', { 'active_conns'=>[ { - 'foriegn_host'=>'10.0.0.1', + 'foreign_host'=>'10.0.0.1', 'local_host'=>'10.0.0.2', - 'foriegn_port'=>'22222', + 'foreign_port'=>'22222', 'local_port'=>'22', 'sendq'=>'0', 'recvq'=>'0', @@ -20,9 +20,9 @@ my $res=[ '0', '1', 'proto' => 'tcp4', }, { - 'foriegn_host'=>'10.0.0.1', + 'foreign_host'=>'10.0.0.1', 'local_host'=>'10.0.0.2', - 'foriegn_port'=>'22', + 'foreign_port'=>'22', 'local_port'=>'2222', 'sendq'=>'0', 'recvq'=>'0', @@ -30,9 +30,9 @@ my $res=[ '0', '1', 'proto' => 'tcp4', }, { - 'foriegn_host'=>'10.0.0.1', + 'foreign_host'=>'10.0.0.1', 'local_host'=>'192.168.0.1', - 'foriegn_port'=>'22', + 'foreign_port'=>'22', 'local_port'=>'2222', 'sendq'=>'0', 'recvq'=>'0', @@ -40,9 +40,9 @@ my $res=[ '0', '1', 'proto' => 'tcp4', }, { - 'foriegn_host'=>'10.0.0.1', + 'foreign_host'=>'10.0.0.1', 'local_host'=>'10.0.0.2', - 'foriegn_port'=>'22', + 'foreign_port'=>'22', 'local_port'=>'2222', 'sendq'=>'0', 'recvq'=>'0', @@ -90,4 +90,21 @@ my $search=Parse::Netstat::Search->new; my @found=$search->search($res); ok( $#found eq '5', 'search, all') or diag('"'.$#found.'" number of returned connections for a empty search instead of "5"'); -done_testing(3); +# set a state and make sure returns only those +$search->set_states( ['LISTEN'] ); +@found=$search->search($res); +ok( $#found eq '1', 'search, LISTEN state') or diag('"'.$#found.'" number of returned connections for LISTEN state search instead of "2"'); +$search->set_states; +@found=$search->search($res); +ok( $#found eq '5', 'search, state reset') or diag('"'.$#found.'" number of returned connections for a empty search instead of "5"... failed to reset the states'); + +# makes sure searching based on CIDR works +# set a state and make sure returns only those +$search->set_cidrs( ['10.0.0.0/24'] ); +@found=$search->search($res); +ok( $#found eq '3', 'search, CIDR 1') or diag('"'.$#found.'" number of returned connections for CIDR 10.0.0.0/24 search instead of "3"'); +$search->set_cidrs( ['127.0.0.0/24'] ); +@found=$search->search($res); +ok( $#found eq '0', 'search, CIDR 2') or diag('"'.$#found.'" number of returned connections for CIDR 127.0.0.1/32 search instead of "0"'); + +done_testing(7);