now works

This commit is contained in:
Zane C. B-H 2019-07-24 02:28:12 -05:00
parent 38a270d9c2
commit 5362bcab28
2 changed files with 34 additions and 45 deletions

View File

@ -7,7 +7,7 @@ use Net::IP;
=head1 NAME
Net::Connection::Sort::port_f - Sorts the connections via the foreign host.
Net::Connection::Sort::port_f - Sorts the connections via the foreign port numerically.
=head1 VERSION
@ -20,6 +20,8 @@ our $VERSION = '0.0.0';
=head1 SYNOPSIS
This currently implements numeric sorting only. For non-numeric sorting using port_fa.
use Net::Connection::Sort::port_f;
use Net::Connection;
use Data::Dumper;
@ -28,12 +30,13 @@ our $VERSION = '0.0.0';
Net::Connection->new({
'foreign_host' => '3.3.3.3',
'local_host' => '4.4.4.4',
'foreign_port' => '22',
'foreign_port' => '23',
'local_port' => '11132',
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -43,27 +46,30 @@ our $VERSION = '0.0.0';
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
'local_host' => '6.6.6.6',
'foreign_port' => '22',
'foreign_port' => '80',
'local_port' => '11132',
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
Net::Connection->new({
'foreign_host' => '3.3.3.3',
'local_host' => '4.4.4.4',
'foreign_port' => '22',
'foreign_port' => '21',
'local_port' => '11132',
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
);
@ -124,33 +130,12 @@ sub sorter{
}
@objects=sort {
&helper( $a->foreign_host ) <=> &helper( $b->foreign_host )
$a->foreign_port <=> $b->foreign_port
} @objects;
return @objects;
}
=head2 helper
This is a internal function.
=cut
sub helper{
if (
( !defined($_[0]) ) ||
( $_[0] eq '*' ) ||
( $_[0] =~ /[g-zG-Z]/ )
){
return 0;
}
my $host=eval { Net::IP->new( $_[0] )->intip} ;
if (!defined( $host )){
return 0;
}
return $host;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>

View File

@ -9,22 +9,24 @@ my @objects=(
Net::Connection->new({
'foreign_host' => '3.3.3.3',
'local_host' => '4.4.4.4',
'foreign_port' => '22',
'foreign_port' => '1',
'local_port' => '11132',
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
'local_host' => '2.2.2.2',
'foreign_port' => '22',
'foreign_port' => '80',
'local_port' => '11132',
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -34,33 +36,35 @@ my @objects=(
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
Net::Connection->new({
'foreign_host' => '3.3.3.3',
'local_host' => '4.4.4.4',
'foreign_port' => '22',
'foreign_port' => '21',
'local_port' => '11132',
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
'ports' => 0,
}),
);
BEGIN {
use_ok( 'Net::Connection::Sort::host_f' ) || print "Bail out!\n";
use_ok( 'Net::Connection::Sort::port_f' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Sort::host_f $Net::Connection::Sort::host_f::VERSION, Perl $], $^X" );
diag( "Testing Net::Connection::Sort::port_f $Net::Connection::Sort::port_f::VERSION, Perl $], $^X" );
my $sorter;
my $worked=0;
eval{
$sorter=Net::Connection::Sort::host_f->new;
$sorter=Net::Connection::Sort::port_f->new;
$worked=1;
};
ok( $worked eq 1, 'sorter init') or die ('Net::Connection::Sort::host_f->new resulted in... '.$@);
ok( $worked eq 1, 'sorter init') or die ('Net::Connection::Sort::port_f->new resulted in... '.$@);
my @sorted;
$worked=0;
@ -70,9 +74,9 @@ eval{
};
ok( $worked eq 1, 'sort') or die ('Net::Connection::Sort::host_f->sorter(@objects) resulted in... '.$@);
ok( $sorted[0]->foreign_host eq '1.1.1.1', 'sort order 0') or die ('The first foreign host value was not 1.1.1.1');
ok( $sorted[3]->foreign_host eq '5.5.5.5', 'sort order 1') or die ('The last foreign host value was not 5.5.5.5');
ok( $sorted[2]->foreign_host eq '3.3.3.3', 'sort order 2') or die ('The middle foreign host value was not 3.3.3.3');
ok( $sorted[1]->foreign_host eq '3.3.3.3', 'sort order 2') or die ('The middle foreign host value was not 3.3.3.3');
ok( $sorted[0]->foreign_port eq '1', 'sort order 0') or die ('The first foreign port value was not 1');
ok( $sorted[1]->foreign_port eq '21', 'sort order 1') or die ('The last foreign port value was not 21');
ok( $sorted[2]->foreign_port eq '22', 'sort order 2') or die ('The middle foreign port value was not 22');
ok( $sorted[3]->foreign_port eq '80' , 'sort order 2') or die ('The middle foreign port value was not 80');
done_testing(7);