| 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
| |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $port = getservbyname($port, 'tcp') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | connect ($socket, $paddr) or croak "unable to connect to host: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($! == ENOTSOCK) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$in_open[$ix] and length $buffer[$ix] < $App::pnc::max_buffer_size
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
| |||||||||||||||||||||||||||||||||
| 72 | 0 | vec($iv, fileno($in[$ix]), 1) = 1; | |||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($out_open[$ix] and length $buffer[$ix] > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$out_open[$ix] and length $buffer[$ix] > 0
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
| |||||||||||||||||||||||||||||||||
| 75 | 0 | vec($ov, fileno($out[$ix]), 1) = 1; | |||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (select($iv, $ov, undef, 5) > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | for my $ix (0, 1) { | |||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($in_open[$ix] and vec($iv, fileno($in[$ix]), 1)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$in_open[$ix] and vec $iv, fileno $in[$ix], 1
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
| |||||||||||||||||||||||||||||||||
| 81 | 0 | my $bytes = sysread($in[$ix], $buffer[$ix], 16 * 1024, length $buffer[$ix]); | |||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | unless ($bytes) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 0 | $in_open[$ix] = 0; | |||||||||||||||||||||||||||||||
| 84 | 0 | _shutdown($in[$ix], 0); | |||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | unless (length $buffer[$ix]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$out_open[$ix] and vec $ov, fileno $out[$ix], 1
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
| |||||||||||||||||||||||||||||||||
| 92 | 0 | my $bytes = syswrite($out[$ix], $buffer[$ix], 16 * 1024); | |||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($bytes) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 0 | substr($buffer[$ix], 0, $bytes, ''); | |||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | unless ($in_open[$ix] or length $buffer[$ix]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$in_open[$ix] or length $buffer[$ix]
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]
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (@ARGV == 1 and $ARGV[0] eq '-V') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
@ARGV == 1 and $ARGV[0] eq "-V"
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"
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 1 | 1; | |||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||||||