blib/lib/App/pnc.pm

filter: Statementstmt 28.4% 25 / 88 Branchbran 2.2% 1 / 44 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 4 totaltotal 18.0% 32 / 177 CC 28 SCAR 55.7
CC
28
Cov
17%
netcat_socket
462.0
netcat4
42.0
_shutdown
12.0
CRAP 263.2
↑ Summary
1 package App::pnc;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Errno qw(ENOTSOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $max_buffer_size = 64 * 1024;
14  
15 1 require Exporter;
16 1 our @ISA = qw(Exporter);
17 1 our @EXPORT_OK = qw(netcat4 netcat_socket);
18  
19 0 ▶sub netcat4 {
Subroutine netcat4: not called CC=6 SCAR=37.4
Pod undocumented
20 0 my ($server, $port) = @_;
21 0 ▶ if ($port =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port =~ /\D/) 0 0 0
22 0 ▶ $port = getservbyname($port, 'tcp')
Branch: 1 branch
BranchTrueFalseTotal
$port = getservbyname($port, "tcp") or croak("unable to convert service name to port number: $!") 0 0 0
23 or croak "unable to convert service name to port number: $!";
24 }
25 0 ▶ my $iaddr = inet_aton($server) or croak "unable to resolve host name: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $iaddr = inet_aton($server) or croak("unable to resolve host name: $!") 0 0 0
26 0 my $paddr = sockaddr_in($port, $iaddr);
27 0 ▶ socket (my $socket, AF_INET, SOCK_STREAM, 0) or croak "unable to create socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket my $socket, 2, 1, 0 or croak("unable to create socket: $!") 0 0 0
28 0 ▶ connect ($socket, $paddr) or croak "unable to connect to host: $!";
Branch: 1 branch
BranchTrueFalseTotal
connect $socket, $paddr or croak("unable to connect to host: $!") 0 0 0
29  
30 0 netcat_socket($socket);
31 }
32  
33 0 ▶sub netcat6 {
Subroutine netcat6: not called CC=1 SCAR=6.9
Pod undocumented
34 #my ($server, $port) = @_;
35 #netcat_socket($server, AF_INET6, $port);
36 0 croak "not implemented yet!";
37 }
38  
39 0 ▶sub _shutdown {
Subroutine _shutdown: not called CC=3 SCAR=24.8
40 0 my ($socket, $dir) = @_;
41 0 ▶ unless (shutdown($socket, $dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (shutdown $socket, $dir) 0 0 0
42 0 ▶ if ($! == ENOTSOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 88) 0 0 0
43 0 return close ($socket);
44 }
45 }
46 0 undef;
47 }
48  
49 0 ▶sub netcat_socket {
Subroutine netcat_socket: not called CC=21 SCAR=61.4
Pod undocumented
50 0 my $socket = shift;
51  
52 0 for my $fh ($socket, *STDIN, *STDOUT) {
53 0 my $flags = fcntl($fh, F_GETFL, 0);
54 0 fcntl($fh, F_SETFL, fcntl($fh, F_GETFL, 0) | O_NONBLOCK);
55 0 binmode $fh;
56 }
57  
58 0 my @in = (*STDIN, $socket);
59 0 my @out = ($socket, *STDOUT);
60 0 my @buffer = ('', '');
61  
62 0 my @in_open = (1, 1);
63 0 my @out_open = (1, 1);
64  
65 0 local $SIG{PIPE} = 'IGNORE';
66  
67 0 ▶ while (grep $_, @in_open, @out_open) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep $_, @in_open, @out_open) 0 0 0
68 0 my $iv = '';
69 0 my $ov = '';
70 0 for my $ix (0, 1) {
71 0 ▶ if ($in_open[$ix] and length $buffer[$ix] < $max_buffer_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_open[$ix] and length $buffer[$ix] < $App::pnc::max_buffer_size) 0 0 0
Condition0%0/3
$in_open[$ix] and length $buffer[$ix] < $App::pnc::max_buffer_size
!ll&&!rl&&r
000
MC/DC0%0/2
$in_open[$ix] and length $buffer[$ix] < $App::pnc::max_buffer_size
$in_open[$ix] length $buffer[$ix] < $App::pnc::max_buffer_size
Truth table0%0/3
A and B
A= $in_open[$ix]B= length $buffer[$ix] < $App::pnc::max_buffer_size
ABresult
0X0
100
111
72 0 vec($iv, fileno($in[$ix]), 1) = 1;
73 }
74 0 ▶ if ($out_open[$ix] and length $buffer[$ix] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out_open[$ix] and length $buffer[$ix] > 0) 0 0 0
Condition0%0/3
$out_open[$ix] and length $buffer[$ix] > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$out_open[$ix] and length $buffer[$ix] > 0
$out_open[$ix] length $buffer[$ix] > 0
Truth table0%0/3
A and B
A= $out_open[$ix]B= length $buffer[$ix] > 0
ABresult
0X0
100
111
75 0 vec($ov, fileno($out[$ix]), 1) = 1;
76 }
77 }
78 0 ▶ if (select($iv, $ov, undef, 5) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (select($iv, $ov, undef, 5) > 0) 0 0 0
79 0 for my $ix (0, 1) {
80 0 ▶ if ($in_open[$ix] and vec($iv, fileno($in[$ix]), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_open[$ix] and vec $iv, fileno $in[$ix], 1) 0 0 0
Condition0%0/3
$in_open[$ix] and vec $iv, fileno $in[$ix], 1
!ll&&!rl&&r
000
MC/DC0%0/2
$in_open[$ix] and vec $iv, fileno $in[$ix], 1
$in_open[$ix] vec $iv, fileno $in[$ix], 1
Truth table0%0/3
A and B
A= $in_open[$ix]B= vec $iv, fileno $in[$ix], 1
ABresult
0X0
100
111
81 0 my $bytes = sysread($in[$ix], $buffer[$ix], 16 * 1024, length $buffer[$ix]);
82 0 ▶ unless ($bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 0 0 0
83 0 $in_open[$ix] = 0;
84 0 _shutdown($in[$ix], 0);
85 0 ▶ unless (length $buffer[$ix]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $buffer[$ix]) 0 0 0
86 0 $out_open[$ix] = 0;
87 0 _shutdown($out[$ix], 1);
88 }
89 }
90 }
91 0 ▶ if ($out_open[$ix] and vec($ov, fileno($out[$ix]), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out_open[$ix] and vec $ov, fileno $out[$ix], 1) 0 0 0
Condition0%0/3
$out_open[$ix] and vec $ov, fileno $out[$ix], 1
!ll&&!rl&&r
000
MC/DC0%0/2
$out_open[$ix] and vec $ov, fileno $out[$ix], 1
$out_open[$ix] vec $ov, fileno $out[$ix], 1
Truth table0%0/3
A and B
A= $out_open[$ix]B= vec $ov, fileno $out[$ix], 1
ABresult
0X0
100
111
92 0 my $bytes = syswrite($out[$ix], $buffer[$ix], 16 * 1024);
93 0 ▶ if ($bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes) { } 0 0 0
94 0 substr($buffer[$ix], 0, $bytes, '');
95 0 ▶ unless ($in_open[$ix] or length $buffer[$ix]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_open[$ix] or length $buffer[$ix]) 0 0 0
Condition0%0/3
$in_open[$ix] or length $buffer[$ix]
l!l&&r!l&&!r
000
MC/DC0%0/2
$in_open[$ix] or length $buffer[$ix]
$in_open[$ix] length $buffer[$ix]
Truth table0%0/3
A or B
A= $in_open[$ix]B= length $buffer[$ix]
ABresult
1X1
011
000
96 0 $out_open[$ix] = 0;
97 0 _shutdown($out[$ix], 1);
98 }
99 }
100 else {
101 0 $out_open[$ix] = 0;
102 0 _shutdown($out[$ix], 1);
103 0 $buffer[$ix] = '';
104 0 ▶ if ($in_open[$ix]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_open[$ix]) 0 0 0
105 0 $in_open[$ix] = 0;
106 0 _shutdown($in[$ix], 0);
107 }
108 }
109 }
110 }
111 }
112 }
113 0 for my $fd ($socket, *STDIN, *STDOUT) {
114 0 close $fd;
115 }
116 }
117  
118 0 ▶sub version { "pnc $VERSION - a netcat alike program written in Perl\n\n" }
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
119  
120 1 ▶unless (defined caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined caller) 0 1 1
121 0 ▶ if (@ARGV == 1 and $ARGV[0] eq '-V') {
Branch: 2 branches
BranchTrueFalseTotal
if (@ARGV == 1 and $ARGV[0] eq '-V') { } 0 0 0
elsif (@ARGV == 2) { } 0 0 0
Condition0%0/3
@ARGV == 1 and $ARGV[0] eq "-V"
!ll&&!rl&&r
000
MC/DC0%0/2
@ARGV == 1 and $ARGV[0] eq "-V"
@ARGV == 1 $ARGV[0] eq "-V"
Truth table0%0/3
A and B
A= @ARGV == 1B= $ARGV[0] eq "-V"
ABresult
0X0
100
111
122 0 print version();
123 }
124 elsif (@ARGV == 2) {
125 0 netcat4(@ARGV);
126 }
127 else {
128 0 ▶ @ARGV == 2 or die "Usage:\n pnc host port\n\n";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV == 2 or die "Usage:\n pnc host port\n\n" 0 0 0
129 }
130 }
131  
132  
133 1 1;
134  
135 __END__
↑ Summary