Compare commits

...

29 Commits

Author SHA1 Message Date
Zane C. B-H 883693ee46 note the use of All and bump to 0.5.0 2021-02-17 23:14:41 -06:00
Zane C. B-H 6023e296a6 now uses All if the array is empty 2021-02-17 22:28:15 -06:00
Zane C. B-H 612d686546 bump to 0.5.0 2021-02-17 22:22:50 -06:00
Zane C. B-H 203d0c7c7d port matching change 2021-02-17 22:10:01 -06:00
Zane C. B-H e289ff3ddb note changes, bump version, and now require 0.2.0 of NC 2019-08-12 05:53:20 -05:00
Zane C. B-H d51a0d91e7 PctMem works now 2019-08-11 22:51:47 -05:00
Zane C. B-H 63764f9a50 add PctMem 2019-08-11 22:36:41 -05:00
Zane C. B-H 43795818ec now works with non-int numbers 2019-08-11 06:05:16 -05:00
Zane C. B-H e84ce182a0 add PctCPU support 2019-08-11 05:50:13 -05:00
Zane C. B-H 6e8c8f0dcd update the manifest 2019-08-11 05:04:26 -05:00
Zane C. B-H f71b83f6cc now requires the newest version of Net::Connection and add WChan 2019-08-11 05:02:37 -05:00
Zane C. B-H f6103c5bd0 note various changes to 0.3.0, bump version, assorted doc cleanup 2019-08-09 07:46:24 -05:00
Zane C. B-H 724476cd0b add a second missing depend 2019-08-09 06:39:07 -05:00
Zane C. B-H 51fc444a92 add a missing dependency 2019-08-09 06:33:15 -05:00
Zane C. B-H ac843c2ee9 Proc::ProcessTable->table returns a array ref and not a array 2019-08-07 06:04:33 -05:00
Zane C. B-H ddf4ab43b1 add the command match 2019-08-07 05:42:02 -05:00
Zane C. B-H f9d87b2c12 correct a test file name and add tests for PID 2019-08-07 04:05:30 -05:00
Zane C. B-H 9b1f568591 add PID support 2019-08-07 04:02:20 -05:00
Zane C. B-H f86e6ee2d1 ready to release 0.2.0 2019-08-06 05:30:38 -05:00
Zane C. B-H c43094df14 add username support and test it 2019-08-06 05:19:53 -05:00
Zane C. B-H 19b6bb5021 switch UID from foreach to while so it does not reference 2019-08-06 04:37:06 -05:00
Zane C. B-H be6a7cb8cf UID stuff is all good 2019-08-06 04:06:02 -05:00
Zane C. B-H 86fa94fc79 misc POD fixes, manifest update, start work on UID 2019-08-06 03:42:57 -05:00
Zane C. B-H 9100bd3f05 misc cleanup and add some regex specific examples 2019-08-06 02:18:56 -05:00
Zane C. B-H 6db5bfbe34 all works nicely now 2019-08-06 02:17:38 -05:00
Zane C. B-H 1593c53230 RegexPTR should be done... start work on the test 2019-08-06 01:54:30 -05:00
Zane C. B-H 182031912d copy PTR to RegexPTR in prep to add suppport for it 2019-08-06 01:06:21 -05:00
Zane C. B-H f2a92f8937 note the changes 2019-07-31 01:51:49 -05:00
Zane C. B-H b1c35defe0 ports now respects * 2019-07-31 01:48:13 -05:00
30 changed files with 2982 additions and 25 deletions

View File

@ -1,4 +1,24 @@
Revision history for Net-Connection-Match
0.5.0 2021-02-17:23:30
- Add All.
- Use all if no checks are specified.
0.4.0 2019-08-12/05:50
- Add PctCPU, PctMem, and WChan.
0.3.0 2019-08-09/07:40
- Add a missing depend for Error::Helper.
- Add support for regexp command searches.
0.2.0 2019-08-06/05:30
- Misc. POD fixes.
- Add RegexPTR support.
- Add UID support.
- Add username support.
0.1.0 2019-07-31/02:00
- Ports can now match '*'.
0.0.0 2019-07-17/05:15
- Initial release.

View File

@ -1,10 +1,16 @@
Changes
lib/Net/Connection/Match.pm
lib/Net/Connection/Match/All.pm
lib/Net/Connection/Match/CIDR.pm
lib/Net/Connection/Match/Command.pm
lib/Net/Connection/Match/PctCPU.pm
lib/Net/Connection/Match/Ports.pm
lib/Net/Connection/Match/Protos.p
lib/Net/Connection/Match/PTR.pmm
lib/Net/Connection/Match/Protos.pm
lib/Net/Connection/Match/PTR.pm
lib/Net/Connection/Match/RegexPTR.pm
lib/Net/Connection/Match/States.pm
lib/Net/Connection/Match/UID.pm
lib/Net/Connection/Match/WChan.pm
Makefile.PL
MANIFEST This list of files
README
@ -14,11 +20,22 @@ t/02-load.t
t/03-load.t
t/04-load.t
t/05-load.t
t/06-load.t
t/07-load.t
t/08-load.t
t/09-load.t
t/10-load.t
t/11-load.t
t/12-load.t
t/13-load.t
t/14-load.t
t/CIDR.t
t/PTR.t
t/Protos.t
t/Ports.t
t/RegexPTR.t
t/States.t
t/manifest.t
t/pod-coverage.t
t/pod.t
t/UID.t

View File

@ -19,8 +19,10 @@ WriteMakefile(
},
PREREQ_PM => {
'Net::CIDR'=>'0.20',
'Net::Connection'=>'0.0.0',
'Net::Connection'=>'0.2.0',
'Net::DNS'=>'1.20',
'Proc::ProcessTable' => '0.59',
'Error::Helper' => '1.0.0',
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
clean => { FILES => 'Net-Connection-Match-*' },

View File

@ -6,10 +6,16 @@ object mathes a series of checks.
Currently can do matching based off of the following.
* CIDR
* Command
* PctCPU
* Ports
* Protocol
* State
* RegexPTR
* PTR
* UID
* Username
* WChan
use Net::Connection::Match;
use Net::Connection;

View File

@ -11,11 +11,11 @@ Net::Connection::Match - Runs a stack of checks to match Net::Connection objects
=head1 VERSION
Version 0.0.0
Version 0.5.0
=cut
our $VERSION = '0.0.0';
our $VERSION = '0.5.0';
=head1 SYNOPSIS
@ -78,7 +78,8 @@ our $VERSION = '0.0.0';
This initializes a new check object.
It takes one value and thht is a hash ref with the key checks.
This is a array of hashes.
This is a array of hashes. If the array is empty, it will default
to using the All test.
If new fails, it will die.
@ -123,7 +124,12 @@ sub new{
}
# Will never match anything.
if ( ! defined $args{checks}[0] ){
die ('Nothing in the checks array');
$args{checks}[0] = {
type => 'All',
invert => 0,
args => {}
};
}
if ( ref( %{ $args{checks}[0] } ) eq 'HASH' ){
die ('The first item in the checks array is not a hash');
@ -144,9 +150,6 @@ sub new{
};
bless $self;
# will hold the created check objects
my @checks;
# Loads up each check or dies if it fails to.
my $check_int=0;
while( defined( $args{checks}[$check_int] ) ){

View File

@ -0,0 +1,164 @@
package Net::Connection::Match::All;
use 5.006;
use strict;
use warnings;
=head1 NAME
Net::Connection::Match::All - Matches everything.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::All;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
pid=>0,
};
my $conn=Net::Connection->new( $connection_args );
my $checker=Net::Connection::Match::All->new;
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
None taken.
my $checker=Net::Connection::Match::All->new( );
=cut
sub new{
my $self = {
};
bless $self;
return $self;
}
=head2 match
Returns 1.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
return 1;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,268 @@
package Net::Connection::Match::Command;
use 5.006;
use strict;
use warnings;
use Proc::ProcessTable;
=head1 NAME
Net::Connection::Match::Command - Check if the process command matches fix regexp for a Net::Connection object.
=head1 VERSION
Version 0.1.0
=cut
our $VERSION = '0.1.0';
=head1 SYNOPSIS
use Net::Connection::Match::Command;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
pid=>0,
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
commands=>[
'kernel',
],
);
my $checker=Net::Connection::Match::Command->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
The key in the hash ref commands is a array
that holds regular expressions to match against
the command line or fname(in case of blank command
line). At least one value must be defined.
If the new method fails, it dies.
my %args=(
commands=>[
'kernel',
],
);
my $checker=Net::Connection::Match::Command->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{commands} ) ){
die ('No commands key specified in the argument hash');
}
if ( ref( \$args{commands} ) eq 'ARRAY' ){
die ('The commands key is not a array');
}
if ( ! defined $args{commands}[0] ){
die ('Nothing defined in the commands array');
}
my $self = {
commands=>$args{commands},
};
bless $self;
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $conn_pid=$object->pid;
# don't bother proceeding, the object won't match ever
# as it does not have a PID
if ( ! defined( $conn_pid ) ){
return 0;
}
my $loop=0;
my $command;
if ( ! defined( $object->proc ) ){
# go through each proc and look for a matching pid
my $proctable=Proc::ProcessTable->new;
my $procs=$proctable->table;
my $proc_int=0;
my $loop=1;
while (
$loop &&
defined( $procs->[$proc_int] )
){
if ( $conn_pid eq $procs->[$proc_int]->{pid} ){
$command=$procs->[$proc_int]->cmndline;
# '' means it is a kernel process
if ( $command =~ /^$/ ){
$command='['.$procs->[$proc_int]->fname.']';
}
# exit the loop as we found it
$loop=0;
}
$proc_int++;
}
}else{
$command=$object->proc;
}
# likely a dead connection that is handing around...
# or disappeared since grabbing the connection list
# and starting processing
if ( !defined( $command ) ){
return 0;
}
# check each command regex and see if any of them match
foreach my $regex ( @{ $self->{commands} } ){
if ( $command =~ /$regex/ ){
return 1;
}
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,266 @@
package Net::Connection::Match::PID;
use 5.006;
use strict;
use warnings;
=head1 NAME
Net::Connection::Match::PID - Check if the PID of a connection matches.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::PID;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
pid=>0,
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
pids=>[
0,
'>1000',
],
);
my $checker=Net::Connection::Match::PID->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
It takes a hash reference with one key. One key is required and
that is 'pids', which is a array of pids to match.
The PID values can be prefixed with the equalities below for doing
additional comparisons.
<
<=
>
>=
Atleast one PID must be specified.
If the new method fails, it dies.
my %args=(
pids=>[
0,
'>1000',
],
);
my $checker=Net::Connection::Match::PID->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{pids} ) ){
die ('No pids key specified in the argument hash');
}
if ( ref( \$args{pids} ) eq 'ARRAY' ){
die ('The pids key is not a array');
}
if ( ! defined $args{pids}[0] ){
die ('Nothing defined in the pids array');
}
my $self = {
pids=>$args{pids},
};
bless $self;
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $conn_pid=$object->pid;
# don't bother proceeding, the object won't match ever
# as it does not have a PID
if ( ! defined( $conn_pid ) ){
return 0;
}
# use while as foreach will reference the value
my $pid_int=0;
while (defined( $self->{pids}[$pid_int] )){
my $pid=$self->{pids}[$pid_int];
if (
( $pid =~ /^[0-9]+$/ ) &&
( $pid eq $conn_pid )
){
return 1;
}elsif( $pid =~ /^\<\=[0-9]+$/ ){
$pid=~s/^\<\=//;
if ( $conn_pid <= $pid ){
return 1;
}
}elsif( $pid =~ /^\<[0-9]+$/ ){
$pid=~s/^\<//;
if ( $conn_pid < $pid ){
return 1;
}
}elsif( $pid =~ /^\>\=[0-9]+$/ ){
$pid=~s/^\>\=//;
if ( $conn_pid >= $pid ){
return 1;
}
}elsif( $pid =~ /^\>[0-9]+$/ ){
$pid=~s/^\>//;
if ( $conn_pid > $pid ){
return 1;
}
}
$pid_int++;
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -11,11 +11,11 @@ Net::Connection::Match::PTR - Runs a PTR check against a Net::Connection object.
=head1 VERSION
Version 0.0.0
Version 0.0.1
=cut
our $VERSION = '0.0.0';
our $VERSION = '0.0.1';
=head1 SYNOPSIS
@ -52,7 +52,7 @@ our $VERSION = '0.0.0';
],
);
my $checker=Net::Connection::Match::Ports->new( \%args );
my $checker=Net::Connection::Match::PTR->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
@ -76,23 +76,23 @@ This intiates the object.
],
);
my $checker=Net::Connection::Match::Ports->new( \%args );
my $checker=Net::Connection::Match::PTR->new( \%args );
=head3 args
Atleast one of the following need used.
=keys ptrs
=head4 ptrs
This is a array of PTRs to match in for either foreign
or local side.
=keys fptrs
=head4 fptrs
This is a array of PTRs to match in for the foreign side.
=keys lptrs
=head4 lptrs
This is a array of PTRs to match in for the local side.

View File

@ -0,0 +1,301 @@
package Net::Connection::Match::PctCPU;
use 5.006;
use strict;
use warnings;
use Proc::ProcessTable;
=head1 NAME
Net::Connection::Match::PctCPU - Check if the pctcpu of a process matches for the process that has the connection.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::PctCPU;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
pid=>0,
pctcpu=>'5.03',
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
pctcpus=>[
'>1',
],
);
my $checker=Net::Connection::Match::PctCPU->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
It takes a hash reference with one key. One key is required and
that is 'pctcpus', which is a array of pctcpu value.
The pctcpu values can be prefixed with the equalities below for doing
additional comparisons.
<
<=
>
>=
Atleast one must be specified.
If the new method fails, it dies.
my %args=(
pctcpus=>[
'>1',
],
);
my $checker=Net::Connection::Match::PctCPU->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{pctcpus} ) ){
die ('No pctcpus key specified in the argument hash');
}
if ( ref( \$args{pctcpus} ) eq 'ARRAY' ){
die ('The pctcpus key is not a array');
}
if ( ! defined $args{pctcpus}[0] ){
die ('Nothing defined in the pctcpus array');
}
my $self = {
pctcpus=>$args{pctcpus},
};
bless $self;
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $conn_pid=$object->pid;
# don't bother proceeding, the object won't match ever
# as it does not have a PID
if ( ! defined( $conn_pid ) ){
return 0;
}
my $loop=0;
my $pctcpu;
if ( ! defined( $object->proc ) ){
# go through each proc and look for a matching pid
my $proctable=Proc::ProcessTable->new;
my $procs=$proctable->table;
my $proc_int=0;
my $loop=1;
while (
$loop &&
defined( $procs->[$proc_int] )
){
if ( $conn_pid eq $procs->[$proc_int]->{pid} ){
$pctcpu=$procs->[$proc_int]->{pctcpu};
# exit the loop as we found it
$loop=0;
}
$proc_int++;
}
}else{
$pctcpu=$object->pctcpu;
}
# likely a dead connection that is handing around...
# or disappeared since grabbing the connection list
# and starting processing
if ( !defined( $pctcpu ) ){
return 0;
}
# use while as foreach will reference the value
my $pctcpu_int=0;
while (defined( $self->{pctcpus}[$pctcpu_int] )){
my $value=$self->{pctcpus}[$pctcpu_int];
if (
( $value =~ /^[0-9.]+$/ ) &&
( $value eq $pctcpu )
){
return 1;
}elsif( $value =~ /^\<\=[0-9.]+$/ ){
$value=~s/^\<\=//;
if ( $value <= $pctcpu ){
return 1;
}
}elsif( $value =~ /^\<[0-9.]+$/ ){
$value=~s/^\<//;
if ( $pctcpu < $value ){
return 1;
}
}elsif( $value =~ /^\>\=[0-9.]+$/ ){
$value=~s/^\>\=//;
if ( $pctcpu >= $value ){
return 1;
}
}elsif( $value =~ /^\>[0-9.]+$/ ){
$value=~s/^\>//;
if ( $pctcpu > $value ){
return 1;
}
}
$pctcpu_int++;
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,311 @@
package Net::Connection::Match::PctMem;
use 5.006;
use strict;
use warnings;
use Proc::ProcessTable;
=head1 NAME
Net::Connection::Match::PctMem - Check if the pctmem of a process matches for the process that has the connection.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::PctMem;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
pid=>0,
pctmem=>'5.03',
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
pctmems=>[
'>1',
],
);
my $checker=Net::Connection::Match::PctMem->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
It takes a hash reference with one key. One key is required and
that is 'pctmems', which is a array of pctmem value.
The pctmem values can be prefixed with the equalities below for doing
additional comparisons.
<
<=
>
>=
Atleast one must be specified.
If the new method fails, it dies.
my %args=(
pctmems=>[
'>1',
],
);
my $checker=Net::Connection::Match::PctMem->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{pctmems} ) ){
die ('No pctcpus key specified in the argument hash');
}
if ( ref( \$args{pctmems} ) eq 'ARRAY' ){
die ('The pctmems key is not a array');
}
if ( ! defined $args{pctmems}[0] ){
die ('Nothing defined in the pctmems array');
}
my $self = {
pctmems=>$args{pctmems},
};
bless $self;
if ( $^O =~ /bsd/ ){
my $physmem=`/sbin/sysctl -a hw.physmem`;
chomp( $physmem );
$physmem=~s/^.*\: //;
$self->{physmem}=$physmem;
}
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $conn_pid=$object->pid;
# don't bother proceeding, the object won't match ever
# as it does not have a PID
if ( ! defined( $conn_pid ) ){
return 0;
}
my $loop=0;
my $pctmem;
if ( ! defined( $object->proc ) ){
# go through each proc and look for a matching pid
my $proctable=Proc::ProcessTable->new;
my $procs=$proctable->table;
my $proc_int=0;
my $loop=1;
while (
$loop &&
defined( $procs->[$proc_int] )
){
if ( $conn_pid eq $procs->[$proc_int]->{pid} ){
if ($^O =~ /bsd/){
$pctmem= (( $procs->[ $proc_int ]->{rssize} * 1024 * 4 ) / $self->{physmem}) * 100;
}else{
$pctmem=$procs->[$proc_int]->{pctmem};
}
# exit the loop as we found it
$loop=0;
}
$proc_int++;
}
}else{
$pctmem=$object->pctmem;
}
# likely a dead connection that is handing around...
# or disappeared since grabbing the connection list
# and starting processing
if ( !defined( $pctmem ) ){
return 0;
}
# use while as foreach will reference the value
my $pctmem_int=0;
while (defined( $self->{pctmems}[$pctmem_int] )){
my $value=$self->{pctmems}[$pctmem_int];
if (
( $value =~ /^[0-9.]+$/ ) &&
( $value eq $pctmem )
){
return 1;
}elsif( $value =~ /^\<\=[0-9.]+$/ ){
$value=~s/^\<\=//;
if ( $value <= $pctmem ){
return 1;
}
}elsif( $value =~ /^\<[0-9.]+$/ ){
$value=~s/^\<//;
if ( $pctmem < $value ){
return 1;
}
}elsif( $value =~ /^\>\=[0-9.]+$/ ){
$value=~s/^\>\=//;
if ( $pctmem >= $value ){
return 1;
}
}elsif( $value =~ /^\>[0-9.]+$/ ){
$value=~s/^\>//;
if ( $pctmem > $value ){
return 1;
}
}
$pctmem_int++;
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -10,11 +10,11 @@ Net::Connection::Match::Ports - Runs a basic port check against a Net::Connectio
=head1 VERSION
Version 0.0.0
Version 0.1.0
=cut
our $VERSION = '0.0.0';
our $VERSION = '0.1.0';
=head1 SYNOPSIS
@ -43,6 +43,7 @@ our $VERSION = '0.0.0';
],
fports=>[
'http',
'*',
],
);
@ -63,7 +64,7 @@ possible keys defined. The possible keys are 'ports'(which
matches either side), 'lports'(which matches the local side),
and 'fports'(which matches the foreign side).
The value of each key is a array with either port numbers or
The value of each key is a array with either port numbers, '*', or
names. If names are given, getservbyname will be called. If
it errors for any of them, it will die.
@ -128,7 +129,7 @@ sub new{
my $ports_int=0;
if ( defined( $args{ports} ) ){
while (defined( $args{ports}[$ports_int] )) {
if ( $args{ports}[$ports_int] =~ /^[0-9]+$/ ){
if ( $args{ports}[$ports_int] =~ /^[0-9\*]+$/ ){
$self->{ports}{ $args{ports}[$ports_int] }= $args{ports}[$ports_int];
}else{
my $port_number=(getservbyname( $args{ports}[$ports_int] , '' ))[2];
@ -150,6 +151,8 @@ sub new{
while (defined( $args{lports}[$ports_int] )) {
if ( $args{lports}[$ports_int] =~ /^[0-9]+$/ ){
$self->{lports}{ $args{lports}[$ports_int] }= $args{lports}[$ports_int];
}elsif( $args{lports}[$ports_int] =~ /^\*$/ ){
$self->{lports}{'*'}='*';
}else{
my $port_number=(getservbyname( $args{lports}[$ports_int] , '' ))[2];
@ -170,6 +173,8 @@ sub new{
while (defined( $args{fports}[$ports_int] )) {
if ( $args{fports}[$ports_int] =~ /^[0-9]+$/ ){
$self->{fports}{ $args{fports}[$ports_int] }= $args{fports}[$ports_int];
}elsif( $args{fports}[$ports_int] =~ /^\*$/ ){
$self->{fports}{'*'}='*';
}else{
my $port_number=(getservbyname( $args{fports}[$ports_int] , '' ))[2];
@ -217,13 +222,13 @@ sub match{
my $fport=$object->foreign_port;
# If either are non-numeric, resolve them if possible
if ( $lport !~ /^[0-9]+$/ ){
if ( $lport !~ /^[0-9\*]+$/ ){
my $lport_number=(getservbyname( $lport , '' ))[2];
if ( defined( $lport_number ) ){
$lport=$lport_number;
}
}
if ( $fport !~ /^[0-9]+$/ ){
if ( $fport !~ /^[0-9\*]+$/ ){
my $fport_number=(getservbyname( $fport , '' ))[2];
if ( defined( $fport_number ) ){
$fport=$fport_number;

View File

@ -6,7 +6,7 @@ use warnings;
=head1 NAME
Net::Connection::Match::Protos - Runs a basic state check against a Net::Connection object.
Net::Connection::Match::Protos - Runs a protocol check against a Net::Connection object.
=head1 VERSION
@ -91,7 +91,7 @@ sub new{
};
bless $self;
# make sure each cidr is valid before returning it
# lc everything for easier matching later
my $protos_int=0;
while( defined( $args{protos}[$protos_int] ) ){
$self->{protos}[$protos_int]=lc( $args{protos}[$protos_int] );

View File

@ -0,0 +1,326 @@
package Net::Connection::Match::RegexPTR;
use 5.006;
use strict;
use warnings;
use Net::DNS;
=head1 NAME
Net::Connection::Match::RegexPTR - Runs a PTR check against a Net::Connection object using regular expressions.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::RegexPTR;
use Net::Connection;
# The *_ptr feilds do not need populated.
# If left undef, they will be resulved using Net::DNS::Resolver
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
foreign_ptr=>'foo.foo',
local_host=>'10.0.0.2',
local_port=>'12322',
local_ptr=>'foo.bar',
proto=>'tcp4',
state=>'ESTABLISHED',
};
my $conn=Net::Connection->new( $connection_args );
# All three don't need specified, but
# Atleast one of them must be and must not be a empty array.
my %args=(
ptrs=>[
'foo',
],
lptrs=>[
'bar',
],
fptrs=>[
'f[oa]o',
],
);
my $checker=Net::Connection::Match::RegexPTR->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
my %args=(
ptrs=>[
'foo.bar',
],
lptrs=>[
'a.foo.bar',
],
fptrs=>[
'b.foo.bar',
],
);
my $checker=Net::Connection::Match::RegexPTR->new( \%args );
=head3 args
Atleast one of the following need used.
=head4 ptrs
This is a array of PTRs to match in for either foreign
or local side.
=head4 fptrs
This is a array of PTRs to match in for the foreign side.
=head4 lptrs
This is a array of PTRs to match in for the local side.
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if (
( ! defined( $args{ptrs} ) ) &&
( ! defined( $args{fptrs} ) ) &&
( ! defined( $args{lptrs} ) )
){
die ('No [fl]ptrs key specified in the argument hash');
}
if (
(
defined( $args{ptrs} ) &&
( ! defined( $args{ptrs}[0] ) )
) &&
(
defined( $args{lptrs} ) &&
( ! defined( $args{lptrs}[0] ) )
) &&
(
defined( $args{fptrs} ) &&
( ! defined( $args{fptrs}[0] ) )
)
){
die ('No ports defined in the in any of the [fl]ptrs array');
}
my $self = {
ptrs=>[],
lptrs=>[],
fptrs=>[],
resolver=>Net::DNS::Resolver->new,
};
bless $self;
if ( defined( $args{ptrs}[0] ) ){
$self->{ptrs}=$args{ptrs};
}
if ( defined( $args{lptrs}[0] ) ){
$self->{lptrs}=$args{lptrs};
}
if ( defined( $args{fptrs}[0] ) ){
$self->{fptrs}=$args{fptrs};
}
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
If the *_ptr feilds for the object are undef, L<Net::DNS::Resolver>
will be used for resolving the address.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $l_ptr=$object->local_ptr;
my $f_ptr=$object->foreign_ptr;
if ( defined( $l_ptr ) ){
# If we have one, convert it to lower case for easier processing.
$l_ptr=lc( $l_ptr )
}else{
# We don't have it. Uppercase default will prevent it from being matched.
$l_ptr='NOTFOUND';
# See if we can look it up.
my $answer=$self->{resolver}->search( $object->local_host );
if ( defined( $answer->{answer}[0] ) &&
( ref( $answer->{answer}[0] ) eq 'Net::DNS::RR::PTR' )
){
$l_ptr=lc($answer->{answer}[0]->ptrdname);
}
}
if ( defined( $f_ptr ) ){
# If we have one, convert it to lower case for easier processing.
$f_ptr=lc( $f_ptr )
}else{
# We don't have it. Uppercase default will prevent it from being matched.
$f_ptr='NOTFOUND';
# See if we can look it up.
my $answer=$self->{resolver}->search( $object->foreign_host );
if ( defined( $answer->{answer}[0] ) &&
( ref( $answer->{answer}[0] ) eq 'Net::DNS::RR::PTR' )
){
$f_ptr=lc($answer->{answer}[0]->ptrdname);
}
}
foreach my $regex ( @{ $self->{ptrs} } ){
if ( $l_ptr =~ /$regex/ ){
return 1;
}
if ( $f_ptr =~ /$regex/ ){
return 1;
}
}
foreach my $regex ( @{ $self->{lptrs} } ){
if ( $l_ptr =~ /$regex/ ){
return 1;
}
}
foreach my $regex ( @{ $self->{fptrs} } ){
if ( $f_ptr =~ /$regex/ ){
return 1;
}
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,267 @@
package Net::Connection::Match::UID;
use 5.006;
use strict;
use warnings;
=head1 NAME
Net::Connection::Match::UID - Check if the UID of a connection matches.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::UID;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
uid=>0,
username=>'root',
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
uids=>[
0,
'>1000',
],
);
my $checker=Net::Connection::Match::UID->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
It takes a hash reference with one key. One key is required and
that is 'uids', which is a array of UIDs to match.
The UID values can be prefixed with the equalities below for doing
additional comparisons.
<
<=
>
>=
Atleast one UID must be specified.
If the new method fails, it dies.
my %args=(
uids=>[
0,
'>1000',
],
);
my $checker=Net::Connection::Match::UID->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{uids} ) ){
die ('No uids key specified in the argument hash');
}
if ( ref( \$args{uids} ) eq 'ARRAY' ){
die ('The uids key is not a array');
}
if ( ! defined $args{uids}[0] ){
die ('Nothing defined in the uids array');
}
my $self = {
uids=>$args{uids},
};
bless $self;
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $conn_uid=$object->uid;
# don't bother proceeding, the object won't match ever
# as it does not have a UID
if ( ! defined( $conn_uid ) ){
return 0;
}
# use while as foreach will reference the value
my $uid_int=0;
while (defined( $self->{uids}[$uid_int] )){
my $uid=$self->{uids}[$uid_int];
if (
( $uid =~ /^[0-9]+$/ ) &&
( $uid eq $conn_uid )
){
return 1;
}elsif( $uid =~ /^\<\=[0-9]+$/ ){
$uid=~s/^\<\=//;
if ( $conn_uid <= $uid ){
return 1;
}
}elsif( $uid =~ /^\<[0-9]+$/ ){
$uid=~s/^\<//;
if ( $conn_uid < $uid ){
return 1;
}
}elsif( $uid =~ /^\>\=[0-9]+$/ ){
$uid=~s/^\>\=//;
if ( $conn_uid >= $uid ){
return 1;
}
}elsif( $uid =~ /^\>[0-9]+$/ ){
$uid=~s/^\>//;
if ( $conn_uid > $uid ){
return 1;
}
}
$uid_int++;
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,228 @@
package Net::Connection::Match::Username;
use 5.006;
use strict;
use warnings;
=head1 NAME
Net::Connection::Match::Username - Runs a username check against a Net::Connection object.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::Username;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
usernames=>[
'root',
'slapd',
],
);
my $checker=Net::Connection::Match::Username->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
It takes a hash reference with one key. One key is required and
that is 'usernames', which is a array of usernames to match.
Atleast one protocol must be present.
If the new method fails, it dies.
my %args=(
usernames=>[
'root',
'slapd',
],
);
my $checker=Net::Connection::Match::Username->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{usernames} ) ){
die ('No usernames key specified in the argument hash');
}
if ( ref( \$args{usernames} ) eq 'ARRAY' ){
die ('The usernames key is not a array');
}
if ( ! defined $args{usernames}[0] ){
die ('No states defined in the protos array');
}
my $self = {
usernames=>$args{usernames},
};
bless $self;
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
# does not have a user.... we won't match
if ( ! defined( $object->username ) ){
return 0;
}
# check for matches
foreach my $username ( @{ $self->{usernames} } ){
if ( $username eq $object->username ){
return 1;
}
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,266 @@
package Net::Connection::Match::WChan;
use 5.006;
use strict;
use warnings;
use Proc::ProcessTable;
=head1 NAME
Net::Connection::Match::WChan - Check if the process wait channel matches fix regexp for a Net::Connection object.
=head1 VERSION
Version 0.0.0
=cut
our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::WChan;
use Net::Connection;
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
pid=>0,
wchan=>'sigwait',
};
my $conn=Net::Connection->new( $connection_args );
my %args=(
wchanss=>[
'sleep',
'sigwait',
],
);
my $checker=Net::Connection::Match::WChan->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
}
=head1 METHODS
=head2 new
This intiates the object.
The key in the hash ref commands is a array
that holds regular expressions to match against
the wait channel.
If the new method fails, it dies.
my %args=(
wchans=>[
'kernel',
],
);
my $checker=Net::Connection::Match::WChan->new( \%args );
=cut
sub new{
my %args;
if(defined($_[1])){
%args= %{$_[1]};
};
# run some basic checks to make sure we have the minimum stuff required to work
if ( ! defined( $args{wchans} ) ){
die ('No wchans key specified in the argument hash');
}
if ( ref( \$args{wchans} ) eq 'ARRAY' ){
die ('The wchans key is not a array');
}
if ( ! defined $args{wchans}[0] ){
die ('Nothing defined in the commands array');
}
my $self = {
wchans=>$args{wchans},
};
bless $self;
return $self;
}
=head2 match
Checks if a single Net::Connection object matches the stack.
One argument is taken and that is a Net::Connection object.
The returned value is a boolean.
if ( $checker->match( $conn ) ){
print "The connection matches.\n";
}
=cut
sub match{
my $self=$_[0];
my $object=$_[1];
if ( !defined( $object ) ){
return 0;
}
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $conn_pid=$object->pid;
# don't bother proceeding, the object won't match ever
# as it does not have a PID
if ( ! defined( $conn_pid ) ){
return 0;
}
my $loop=0;
my $wchan;
if ( ! defined( $object->proc ) ){
# go through each proc and look for a matching pid
my $proctable=Proc::ProcessTable->new;
my $procs=$proctable->table;
my $proc_int=0;
my $loop=1;
while (
$loop &&
defined( $procs->[$proc_int] )
){
if ( $conn_pid eq $procs->[$proc_int]->{pid} ){
$wchan=$procs->[$proc_int]->wchan;
# exit the loop as we found it
$loop=0;
}
$proc_int++;
}
}else{
$wchan=$object->wchan;
}
# likely a dead connection that is handing around...
# or disappeared since grabbing the connection list
# and starting processing
if ( !defined( $wchan ) ){
return 0;
}
# check each command regex and see if any of them match
foreach my $regex ( @{ $self->{wchans} } ){
if ( $wchan =~ /$regex/ ){
return 1;
}
}
return 0;
}
=head1 AUTHOR
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-net-connection-match at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Match>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Net::Connection::Match
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker (report bugs here)
L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Match>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Net-Connection-Match>
=item * CPAN Ratings
L<https://cpanratings.perl.org/d/Net-Connection-Match>
=item * Search CPAN
L<https://metacpan.org/release/Net-Connection-Match>
=back
=head1 ACKNOWLEDGEMENTS
=head1 LICENSE AND COPYRIGHT
Copyright 2019 Zane C. Bowers-Hadley.
This program is free software; you can redistribute it and/or modify it
under the terms of the the Artistic License (2.0). You may obtain a
copy of the full license at:
L<http://www.perlfoundation.org/artistic_license_2_0>
Any use, modification, and distribution of the Standard or Modified
Versions is governed by this Artistic License. By using, modifying or
distributing the Package, you accept this license. Do not use, modify,
or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made
by someone other than you, you are nevertheless required to ensure that
your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service
mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge
patent license to make, have made, use, offer to sell, sell, import and
otherwise transfer the Package with respect to any patent claims
licensable by the Copyright Holder that are necessarily infringed by the
Package. If you institute patent litigation (including a cross-claim or
counterclaim) against any party alleging that the Package constitutes
direct or contributory patent infringement, then this Artistic License
to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=cut
1; # End of Net::Connection::Match

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::RegexPTR' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::RegexPTR $Net::Connection::Match::RegexPTR::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::UID' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::UID $Net::Connection::Match::UID::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::Username' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::Username $Net::Connection::Match::Username::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::PID' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::PID $Net::Connection::Match::PID::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::Command' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::Command $Net::Connection::Match::Command::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::WChan' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::WChan $Net::Connection::Match::WChan::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::PctCPU' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::PctCPU $Net::Connection::Match::PctCPU::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::PctMem' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::PctMem $Net::Connection::Match::PctMem::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::All' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::All $Net::Connection::Match::All::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,86 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::PID' ) || print "Bail out!\n";
}
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'LISTEN',
pid=>0,
};
my %args=(
pids=>[
'0',
'>1000',
],
);
my $checker;
# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::PID->new();
$worked=1;
};
ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
# makes sure we can init with good args
$worked=0;
eval{
$checker=Net::Connection::Match::PID->new( \%args );
$worked=1;
};
ok( $worked eq '1', 'init check') or diag('Calling Net::Connection::Match::PID->new resulted in... '.$@);
# make sure it will not accept null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'undef match check') or diag('match accepted undefined input');
# make sure it will not accept a improper ref type
$returned=1;
eval{
$returned=$checker->match($checker);
};
ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
# Create a connection with a matching pid and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'pid match check') or diag('Failed to match a matching good pid');
# Create a connection with a matching pid greater than 1000 protocol and make sure it does not match
$connection_args->{pid}='1001';
$conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'pid match check 2') or diag('Failed to match a good pid');
# Create a connection with a matching pid greater than 1000 protocol and make sure it does not match
$connection_args->{pid}='900';
$conn=Net::Connection->new( $connection_args );
$returned=1;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'pid non-match check') or diag('Matched a pid that it should not of');
done_testing(8);

View File

@ -0,0 +1,137 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::RegexPTR' ) || print "Bail out!\n";
}
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'LISTEN',
local_ptr=>'foo.bar',
foreign_ptr=>'test',
};
my %args=(
ptrs=>[
'foo',
],
);
my %largs=(
lptrs=>[
'bar',
],
);
my %fargs=(
fptrs=>[
'bar',
],
);
my $checker;
# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::RegexPTR->new();
$worked=1;
};
ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
# makes sure we can init with general good args
$worked=0;
eval{
$checker=Net::Connection::Match::RegexPTR->new( \%args );
$worked=1;
};
ok( $worked eq '1', 'init check, general') or diag('Calling Net::Connection::Match::RegexPTR->new resulted in... '.$@);
# make sure it will not accept null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'proto undef check') or diag('match accepted undefined input');
# make sure it will not accept a improper ref type
$returned=1;
eval{
$returned=$checker->match($checker);
};
ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
# make sure the general PTR check works, testing local
$returned=0;
my $conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'general PTR match check, general local') or diag('failed to match a Net::Connection for a general PTR check when local matches');
# make sure the general PTR check works, testing foreign
$connection_args->{local_ptr}='test';
$connection_args->{foreign_ptr}='foo';
$conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'general PTR match check, foriegn') or diag('failed to match a Net::Connection for a general PTR check when one of the two matches');
# makes sure we can init with local good args
$worked=0;
eval{
$checker=Net::Connection::Match::RegexPTR->new( \%largs );
$worked=1;
};
ok( $worked eq '1', 'init check, local') or diag('Calling Net::Connection::Match::RegexPTR->new resulted in... '.$@);
# make sure the local PTR check works, testing foreign
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'local PTR match check, foreign') or diag('matched a Net::Connection object when looking for local PTRs when the foreign matches');
# make sure the local PTR check works, testing local
$returned=0;
$connection_args->{foreign_ptr}='test';
$connection_args->{local_ptr}='foo.bar';
$conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'local PTR match check, local') or diag('did not match a Net::Connection object when looking for local PTRs when the local matches');
# makes sure we can init with foreign good args
$worked=0;
eval{
$checker=Net::Connection::Match::RegexPTR->new( \%fargs );
$worked=1;
};
ok( $worked eq '1', 'init check, foreign') or diag('Calling Net::Connection::Match::RegexPTR->new resulted in... '.$@);
# make sure the foreign PTR check works, testing local
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'foreign PTR match check, local') or diag('matched a Net::Connection object when looking for foreign PTRs when the local matches');
# make sure the foreign PTR check works, testing foreign
$returned=0;
$connection_args->{local_ptr}='test';
$connection_args->{foreign_ptr}='foo.bar';
$conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'local PTR match check, foreign') or diag('did not match a Net::Connection object when looking for foreign PTRs when the foreign matches');
done_testing(13);

View File

@ -0,0 +1,89 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::UID' ) || print "Bail out!\n";
}
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'LISTEN',
uid=>0,
username=>'root',
};
my %args=(
uids=>[
'0',
'>1000',
],
);
my $checker;
# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::UID->new();
$worked=1;
};
ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
# makes sure we can init with good args
$worked=0;
eval{
$checker=Net::Connection::Match::UID->new( \%args );
$worked=1;
};
ok( $worked eq '1', 'init check') or diag('Calling Net::Connection::Match::UID->new resulted in... '.$@);
# make sure it will not accept null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'undef match check') or diag('match accepted undefined input');
# make sure it will not accept a improper ref type
$returned=1;
eval{
$returned=$checker->match($checker);
};
ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
# Create a connection with a matching UID and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'uid match check') or diag('Failed to match a matching good uid');
# Create a connection with a matching UID greater than 1000 protocol and make sure it does not match
$connection_args->{uid}='1001';
$connection_args->{username}='1001';
$conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'uid match check 2') or diag('Failed to match a good uid');
# Create a connection with a matching UID greater than 1000 protocol and make sure it does not match
$connection_args->{uid}='900';
$connection_args->{username}='900';
$conn=Net::Connection->new( $connection_args );
$returned=1;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'uid non-match check') or diag('Matched a UID that it should not of');
done_testing(8);

View File

@ -0,0 +1,78 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::Username' ) || print "Bail out!\n";
}
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'LISTEN',
uid=>0,
username=>'root'
};
my %args=(
usernames=>[
'root',
'foo',
],
);
my $checker;
# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::Username->new();
$worked=1;
};
ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
# makes sure we can init with good args
$worked=0;
eval{
$checker=Net::Connection::Match::Username->new( \%args );
$worked=1;
};
ok( $worked eq '1', 'init check') or diag('Calling Net::Connection::Match::Username->new resulted in... '.$@);
# make sure it will not accept null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'proto undef check') or diag('match accepted undefined input');
# make sure it will not accept a improper ref type
$returned=1;
eval{
$returned=$checker->match($checker);
};
ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
# Create a connection with a matching protocol and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'match check') or diag('Failed to match a matching good connection');
# Create a connection with a non-matching protocol and make sure it does not match
$connection_args->{username}='linda';
$conn=Net::Connection->new( $connection_args );
$returned=1;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'non-match check') or diag('Matched a connection that it should not of');
done_testing(7);