lots of assorted logic cleanup also correct some test data

This commit is contained in:
Zane C. B-H 2019-02-20 04:01:16 -06:00
parent b9b7771ce5
commit e64265a39a
2 changed files with 48 additions and 21 deletions

View File

@ -180,7 +180,7 @@ sub search{
$protocol_require=1; $protocol_require=1;
} }
if (defined( (keys(%{ $self->{states} }))[0] )){ if (defined( (keys(%{ $self->{states} }))[0] )){
$protocol_require=1; $state_require=1;
} }
my $res_int=0; my $res_int=0;
@ -200,7 +200,9 @@ sub search{
my $port_meet=1; my $port_meet=1;
my $cidr_meet=1; my $cidr_meet=1;
my $protocol_meet=1; my $protocol_meet=1;
my $protocol_search=lc( $protocol );
my $state_meet=1; my $state_meet=1;
my $state_search=lc( $state );
# reset the meet checks # reset the meet checks
if ( $port_require ) { if ( $port_require ) {
@ -220,8 +222,14 @@ sub search{
if ( if (
$cidr_require && $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; $cidr_meet=1;
@ -241,7 +249,7 @@ sub search{
# check protocol to see if it is one that is required # check protocol to see if it is one that is required
if ( if (
$protocol_require && $protocol_require &&
defined( $self->{protocols}{$protocol} ) defined( $self->{protocols}{$protocol_search} )
){ ){
$protocol_meet=1; $protocol_meet=1;
} }
@ -249,7 +257,7 @@ sub search{
# check state to see if it is one that is required # check state to see if it is one that is required
if ( if (
$state_require && $state_require &&
defined( $self->{states}{$state} ) defined( $self->{states}{$state_search} )
){ ){
$state_meet=1; $state_meet=1;
} }
@ -301,7 +309,7 @@ Validating in is done by Net::CIDR::cidrvalidate.
sub set_cidrs{ sub set_cidrs{
my $self=$_[0]; my $self=$_[0];
my @cidrs; my @cidrs;
if ( defined( $_ ) ){ if ( defined( $_[1] ) ){
@cidrs=@{ $_[1] }; @cidrs=@{ $_[1] };
} }
@ -315,16 +323,18 @@ sub set_cidrs{
} }
#chueck each one #chueck each one
my $cidr=pop(@cidrs); my $cidr_int=0;
while ( defined( $cidr ) ){ while ( defined( $cidrs[$cidr_int] ) ){
my $cidr=$cidrs[$cidr_int];
if ( ! Net::CIDR::cidrvalidate( $cidr ) ){ if ( ! Net::CIDR::cidrvalidate( $cidr ) ){
$self->{error}=1; $self->{error}=1;
$self->{errorString}='"'.$cidr.'" is not a valid CIDR according to Net::CIDR::cidrvalidate'; $self->{errorString}='"'.$cidr.'" is not a valid CIDR according to Net::CIDR::cidrvalidate';
$self->warn; $self->warn;
return undef; return undef;
} }
$cidr=pop(@cidrs); $cidr_int++;
} }
$self->{cidrs}=\@cidrs; $self->{cidrs}=\@cidrs;
@ -355,7 +365,7 @@ The ports can be either numeric or by name.
sub set_ports{ sub set_ports{
my $self=$_[0]; my $self=$_[0];
my @ports; my @ports;
if ( defined( $_ ) ){ if ( defined( $_[1] ) ){
@ports=@{ $_[1] }; @ports=@{ $_[1] };
} }
@ -425,7 +435,7 @@ Starting and trailing white space is removed.
sub set_protocols{ sub set_protocols{
my $self=$_[0]; my $self=$_[0];
my @protocols; my @protocols;
if ( defined( $_ ) ){ if ( defined( $_[1] ) ){
@protocols=@{ $_[1] }; @protocols=@{ $_[1] };
} }
@ -483,7 +493,7 @@ Starting and trailing white space is removed.
sub set_states{ sub set_states{
my $self=$_[0]; my $self=$_[0];
my @states; my @states;
if ( defined( $_ ) ){ if ( defined( $_[1] ) ){
@states=@{ $_[1] }; @states=@{ $_[1] };
} }

View File

@ -10,9 +10,9 @@ my $res=[ '0', '1',
{ {
'active_conns'=>[ 'active_conns'=>[
{ {
'foriegn_host'=>'10.0.0.1', 'foreign_host'=>'10.0.0.1',
'local_host'=>'10.0.0.2', 'local_host'=>'10.0.0.2',
'foriegn_port'=>'22222', 'foreign_port'=>'22222',
'local_port'=>'22', 'local_port'=>'22',
'sendq'=>'0', 'sendq'=>'0',
'recvq'=>'0', 'recvq'=>'0',
@ -20,9 +20,9 @@ my $res=[ '0', '1',
'proto' => 'tcp4', 'proto' => 'tcp4',
}, },
{ {
'foriegn_host'=>'10.0.0.1', 'foreign_host'=>'10.0.0.1',
'local_host'=>'10.0.0.2', 'local_host'=>'10.0.0.2',
'foriegn_port'=>'22', 'foreign_port'=>'22',
'local_port'=>'2222', 'local_port'=>'2222',
'sendq'=>'0', 'sendq'=>'0',
'recvq'=>'0', 'recvq'=>'0',
@ -30,9 +30,9 @@ my $res=[ '0', '1',
'proto' => 'tcp4', 'proto' => 'tcp4',
}, },
{ {
'foriegn_host'=>'10.0.0.1', 'foreign_host'=>'10.0.0.1',
'local_host'=>'192.168.0.1', 'local_host'=>'192.168.0.1',
'foriegn_port'=>'22', 'foreign_port'=>'22',
'local_port'=>'2222', 'local_port'=>'2222',
'sendq'=>'0', 'sendq'=>'0',
'recvq'=>'0', 'recvq'=>'0',
@ -40,9 +40,9 @@ my $res=[ '0', '1',
'proto' => 'tcp4', 'proto' => 'tcp4',
}, },
{ {
'foriegn_host'=>'10.0.0.1', 'foreign_host'=>'10.0.0.1',
'local_host'=>'10.0.0.2', 'local_host'=>'10.0.0.2',
'foriegn_port'=>'22', 'foreign_port'=>'22',
'local_port'=>'2222', 'local_port'=>'2222',
'sendq'=>'0', 'sendq'=>'0',
'recvq'=>'0', 'recvq'=>'0',
@ -90,4 +90,21 @@ my $search=Parse::Netstat::Search->new;
my @found=$search->search($res); 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"'); 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);