| 1 | #!/usr/bin/perl -wT | ||||||||||||||||||||||||||||||||||||||||||
| 2 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ########################################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 4 | ## ## | ||||||||||||||||||||||||||||||||||||||||||
| 5 | ## Net::FileShare.pm 1-07-03 ## | ||||||||||||||||||||||||||||||||||||||||||
| 6 | ## Gene Gallistel <gravalo@uwm.edu> ## | ||||||||||||||||||||||||||||||||||||||||||
| 7 | ## Copyright (c) 1-07-03 All rights reserved. ## | ||||||||||||||||||||||||||||||||||||||||||
| 8 | ## ## | ||||||||||||||||||||||||||||||||||||||||||
| 9 | ## This program is free software. You can redistribute and/or modify this bundle ## | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ## under the same terms as Perl itself. ## | ||||||||||||||||||||||||||||||||||||||||||
| 11 | ## ## | ||||||||||||||||||||||||||||||||||||||||||
| 12 | ########################################################################################## | ||||||||||||||||||||||||||||||||||||||||||
| 13 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 16 | package Net::FileShare; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use POSIX ":sys_wait_h"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Fcntl qw( O_WRONLY O_RDONLY O_CREAT O_APPEND ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use vars qw( $VERSION @ISA @EXPORT ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | $VERSION = '0.18'; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | @EXPORT = qw(); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | ## Command codes for server and client | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use vars qw( $HELO $GET $LIST $QUIT $PERM_DENY $FILE_NOT_FOUND $INVAL_NAME $UNRECOG_CMD $PATH_EQ_DIR $ACK_CMD); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | ## Server Greeting | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | $HELO = '1'; | |||||||||||||||||||||||||||||||||||||||||
| 33 | ## Client Commands | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | $GET = '101'; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | $LIST = '102'; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | $QUIT = '103'; | |||||||||||||||||||||||||||||||||||||||||
| 37 | ## Server Responses | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | $PERM_DENY = '201'; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | $FILE_NOT_FOUND = '202'; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | $INVAL_NAME = '203'; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | $UNRECOG_CMD = '204'; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | $PATH_EQ_DIR = '205'; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $ACK_CMD = '301'; # acknowledge a command | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | ## all commands from the server should also contain a message. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | ## below are a series of stock messages, for the server to send. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | ## look at the handle_request sub to find their usage. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use vars qw( $helo_msg $perm_deny_msg $file_not_found_msg $inval_name_msg $unrecog_cmd_msg $path_eq_dir_msg); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | $helo_msg = "Welcome...this server uses Net::FileShare"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | $perm_deny_msg = "Permission denied"; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | $file_not_found_msg = "File requested was not found"; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $inval_name_msg = "Invalid file name, name supplied may contain invalid characters"; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $unrecog_cmd_msg = "Client commands are as follows: list, get, and quit"; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | $path_eq_dir_msg = "Path supplied is a directory, not a file"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 60 | bless { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | _send_only => $args{_send_only} || '0', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | _socket => $args{_socket} || '1', | ||||||||||||||||||||||||||||||||||||||||||
| 63 | _directory => $args{_directory} || '???', | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | _debug => $args{_debug} || '0', | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%4/8
$args{'_send_only'} || '0'
$args{'_socket'} || '1'
$args{'_directory'} || '???'
$args{'_debug'} || '0'
MC/DC0%0/1
$args{'_send_only'} || '0'
$args{'_send_only'}
MC/DC0%0/1
$args{'_socket'} || '1'
$args{'_socket'}
MC/DC0%0/1
$args{'_directory'} || '???'
$args{'_directory'}
MC/DC0%0/1
$args{'_debug'} || '0'
$args{'_debug'}
Truth table50%1/2
A
A= $args{'_send_only'}
Truth table50%1/2
A
A= $args{'_socket'}
Truth table50%1/2
A
A= $args{'_directory'}
Truth table50%1/2
A
A= $args{'_debug'}
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | ## self explanitory | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub version { $VERSION; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | sub DESTROY | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | my ($self) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | undef %$self; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | sleep 1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | sub REAPER { 1 until (waitpid(-1, WNOHANG) == -1) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
REAPER: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | ## will test if an object can become a server | ||||||||||||||||||||||||||||||||||||||||||
| 82 | sub server_run_once | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
server_run_once: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | my ($self, $port) = ($_[0], $_[1] || "3000"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[1] || '3000'
MC/DC0%0/1
$_[1] || '3000'
$_[1]
Truth table50%1/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | croak "Variable _send_only must be set to 1" if ($self->{_send_only} eq 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | ## Create a socket | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my ($socket) = new IO::Socket::INET( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | Listen => SOMAXCONN, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | LocalPort => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | Reuse => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 94 | Timeout => 120) | ||||||||||||||||||||||||||||||||||||||||||
| 95 | or croak "Unable to create server socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | sub server_connection | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
server_connection: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my ($self, $port) = ($_[0], $_[1] || "3000"); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_[1] || '3000'
MC/DC0%0/1
$_[1] || '3000'
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my ($hostinfo, $remote); | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | croak "Variable _send_only must be set to 1" if ($self->{_send_only} eq 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | ## Create a socket | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my ($socket) = new IO::Socket::INET( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | Listen => SOMAXCONN, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | LocalPort => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 109 | Reuse => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||
| 111 | Timeout => 120) | ||||||||||||||||||||||||||||||||||||||||||
| 112 | or croak "Unable to create server socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | while (1) | |||||||||||||||||||||||||||||||||||||||||
| 116 | { | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $SIG{CHLD} = \&REAPER; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | last if (!defined($remote = $socket->accept())); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | $hostinfo = gethostbyaddr($remote->peeraddr, AF_INET); | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | ## assign remote to _socket, so all protocol negotiations | ||||||||||||||||||||||||||||||||||||||||||
| 123 | ## can be done by handle_request | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $self->{_socket} = $remote; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | ## fork the child so the parent can go back to listening | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my ($pid) = fork(); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | ## signal trouble | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | unless (defined ($pid)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | ## add logging and debugging info here | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | $self->debug("Server process $$ fork failed!") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | sleep (5); | |||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | ## parent...should include a recording for child, then next to | ||||||||||||||||||||||||||||||||||||||||||
| 137 | ## listen for more inbound children | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ( $pid ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $self->debug("Server process $$ forked successfully") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | $self->handle_request(); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | sub client_automated | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
client_automated: not called CC=25 SCAR=64.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if(@_ < 4) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | croak "Usage: <server> <port> <cmd> <file if cmd eq \"get\">"; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my($self, $server, $port, $cmd, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my ($serv_cmd, $serv_msg, $clie_cmd, $clie_msg); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my ($buffer, $packet, $bytes_read) = (8,0,0); ## set buffer to 8 bytes | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my ($written, $read) = (0,0); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my ($loc); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my ($directory) = $self->{_directory}; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | local *FD; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | my $localfd = ref($file) || ref(\$file) eq "GLOB"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $file || ref \$file eq 'GLOB'
MC/DC0%0/2
ref $file || ref \$file eq 'GLOB'
ref $file
ref \$file eq 'GLOB'
Truth table0%0/3
A || B
A= ref $fileB= ref \$file eq 'GLOB'
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | ## check cmd...if invalid, why go on... | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if (($cmd =~ m/[Gg][Ee][Tt]/) || ($cmd =~ m/[Ll][Ii][Ss][Tt]|[Ll][Ss]|[Dd][Ii][Rr]/)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cmd =~ /[Gg][Ee][Tt]/ or $cmd =~ /[Ll][Ii][Ss][Tt]|[Ll][Ss]|[Dd][Ii][Rr]/
MC/DC0%0/2
$cmd =~ /[Gg][Ee][Tt]/ or $cmd =~ /[Ll][Ii][Ss][Tt]|[Ll][Ss]|[Dd][Ii][Rr]/
$cmd =~ /[Gg][Ee][Tt]/
$cmd =~ /[Ll][Ii][Ss][Tt]|[Ll][Ss]|[Dd][Ii][Rr]/
Truth table0%0/3
A or B
A= $cmd =~ /[Gg][Ee][Tt]/B= $cmd =~ /[Ll][Ii][Ss][Tt]|[Ll][Ss]|[Dd][Ii][Rr]/
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | my ($socket) = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | PeerAddr => $server, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | PeerPort => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | Proto => "tcp", | ||||||||||||||||||||||||||||||||||||||||||
| 172 | Type => SOCK_STREAM) | ||||||||||||||||||||||||||||||||||||||||||
| 173 | or croak "Cannot establish client socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | $self->debug("Client connected to host: $server port: $port") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | $self->{_socket} = $socket; | |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | ## wait for a helo | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ($serv_cmd, $serv_msg) = $self->recv_cmd(); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | if ($serv_cmd eq $HELO) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | $self->debug("\n$serv_cmd\t$serv_msg\n") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | ## if $cmd eq get, a file name should be supplied, else error on usage | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($cmd =~ m/[Gg][Ee][Tt]/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | ## quick check length of filename | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if (length($file) eq 0) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | { | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | croak "Invalid file name supplied: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | ## the situation where a filename does exist | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $self->send_cmd($GET,$file); | |||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ($serv_cmd, $serv_msg) = $self->recv_cmd; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | if ($serv_cmd eq $ACK_CMD) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | syswrite(STDOUT, "$serv_cmd\tFile Size: $serv_msg\n"); | |||||||||||||||||||||||||||||||||||||||||
| 198 | ## prereq: clients get request has been acknowledged by server | ||||||||||||||||||||||||||||||||||||||||||
| 199 | ## thus, the $serv_msg supplied contains the requested files size | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my $file_size = $serv_msg; | |||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | ## opening the file | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | if($localfd) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | $loc = $file; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | $loc = \*FD; | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | ▶ | unless(sysopen($loc, "$directory/$file", O_CREAT | O_WRONLY)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | { | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | carp "Cannot open $directory/$file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | ## file transfer section | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | last if ($bytes_read eq $file_size); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $read = sysread($socket, $packet, $buffer); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | unless (defined($read) && ($read eq length($packet))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $read and $read eq length $packet
MC/DC0%0/2
defined $read and $read eq length $packet
defined $read
$read eq length $packet
Truth table0%0/3
A and B
A= defined $readB= $read eq length $packet
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | croak "Error reading socket in client connection"; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | $written = syswrite($loc, $packet, length($packet)); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | unless (defined($written) && ($written eq length($packet))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $written and $written eq length $packet
MC/DC0%0/2
defined $written and $written eq length $packet
defined $written
$written eq length $packet
Truth table0%0/3
A and B
A= defined $writtenB= $written eq length $packet
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | croak "Unable to write to new file"; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | $bytes_read += $written; | |||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | syswrite(STDOUT, "*"); | |||||||||||||||||||||||||||||||||||||||||
| 229 | } while ($bytes_read != $file_size); | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | syswrite (STDOUT, "\nClosing FH\n"); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | ▶ | close $loc | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | or carp $! ? "Error closing file: $!" | ||||||||||||||||||||||||||||||||||||||||||
| 235 | : "Exit status $? from close"; | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | } elsif ($cmd =~ m/[Ll][Ii][Ss][Tt]|[Ll][Ss]|[Dd][Ii][Rr]/) { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $self->send_cmd($LIST); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ($serv_cmd, $serv_msg) = $self->recv_cmd; #should be ack_cmd + files | |||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | if($serv_cmd eq $ACK_CMD) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my @files = split(/\*/, $serv_msg); | |||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | my $files_msg = shift @files; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | syswrite(STDOUT, "$serv_cmd\t$files_msg\n"); | |||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | for (my $i=0; $i < $#files; ++$i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | syswrite(STDOUT, ">\t$files[$i]\n"); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | croak "Invalid Command: commands are get or list"; | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | croak "Server $server:$port not responding"; | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | croak "Invalid Command: commands are get or list"; | |||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | sub client_interactive | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
client_interactive: not called CC=23 SCAR=63.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my ($self,$server,$port) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | my ($directory) = $self->{_directory}; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my ($serv_cmd, $serv_msg, $clie_cmd, $clie_msg); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | my ($file_size, $loc, $local_file, $tmp); | |||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my ($buffer, $packet, $bytes_read) = (8,0,0); ## set buffer to 8 bytes | |||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | my ($written, $read) = (0,0); | |||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | local *FD; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my $localfd = ref($local_file) || ref(\$local_file) eq "GLOB"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $local_file || ref \$local_file eq 'GLOB'
MC/DC0%0/2
ref $local_file || ref \$local_file eq 'GLOB'
ref $local_file
ref \$local_file eq 'GLOB'
Truth table0%0/3
A || B
A= ref $local_fileB= ref \$local_file eq 'GLOB'
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | my ($socket) = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | PeerAddr => $server, | ||||||||||||||||||||||||||||||||||||||||||
| 275 | PeerPort => $port, | ||||||||||||||||||||||||||||||||||||||||||
| 276 | Proto => "tcp", | ||||||||||||||||||||||||||||||||||||||||||
| 277 | Type => SOCK_STREAM) | ||||||||||||||||||||||||||||||||||||||||||
| 278 | or croak "Cannot establish client socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | $self->debug("Client connected to host: $server port: $port") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | $self->{_socket} = $socket; | |||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ($serv_cmd, $serv_msg) = $self->recv_cmd(); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ($serv_cmd eq $HELO) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | ## prereq: client has connected to server, | ||||||||||||||||||||||||||||||||||||||||||
| 287 | ## server has forked client properly and passed | ||||||||||||||||||||||||||||||||||||||||||
| 288 | ## onto the handle_request sub. client has now | ||||||||||||||||||||||||||||||||||||||||||
| 289 | ## received a $HELO + $helo_msg from handle_request | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | ## display cmd and msg from server | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | syswrite(STDOUT, "\n$serv_cmd\t$serv_msg\n"); | |||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | ## client enters cmd loop. to exist, client will have to type 'quit' | ||||||||||||||||||||||||||||||||||||||||||
| 295 | ## minus the quotes of course | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | syswrite(STDOUT, "> Enter Command (list, get, or quit): "); | |||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | ## read and chomp clients cmd from STDIN. this can be tricky, if | ||||||||||||||||||||||||||||||||||||||||||
| 300 | ## client specifies 'get filename' instead of get, then filename | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | sysread(STDIN, $clie_cmd, 50); | |||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | chomp($clie_cmd); | |||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | ## handle list cmd, client can type list, ls, or dir, as i often | ||||||||||||||||||||||||||||||||||||||||||
| 305 | ## get confused when accessing various ftp servs, etc. so i thought | ||||||||||||||||||||||||||||||||||||||||||
| 306 | ## i would build in support for all | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | if ($clie_cmd =~ m/^[Ll][Ii][Ss][Tt]|^[Ll][Ss]|^[Dd][Ii][Rr]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | $self->send_cmd($LIST); | |||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ($serv_cmd, $serv_msg) = $self->recv_cmd; #should be ack_cmd + files | |||||||||||||||||||||||||||||||||||||||||
| 310 | ## if server acknowledges command | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | if($serv_cmd eq $ACK_CMD) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | ## $serv_msg is * delimited. The first element is a string | ||||||||||||||||||||||||||||||||||||||||||
| 313 | ## followed by a *, then every file after is followed by a | ||||||||||||||||||||||||||||||||||||||||||
| 314 | ## * save the last | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | my @files = split(/\*/, $serv_msg); | |||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | my $files_msg = shift @files; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | printf "%d\t%s\n", $serv_cmd, $files_msg; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | for (my $i=0; $i < $#files; ++$i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | print ">\t$files[$i]\n"; | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } elsif ($clie_cmd =~ m/^[Gg][Ee][Tt]/) { | ||||||||||||||||||||||||||||||||||||||||||
| 324 | ## initial check...see if client supplied filename in original line | ||||||||||||||||||||||||||||||||||||||||||
| 325 | ## its quite possible a user might add a space to the end of get, so | ||||||||||||||||||||||||||||||||||||||||||
| 326 | ## check to see if $clie_cmd is larger 4 chars and contains | ||||||||||||||||||||||||||||||||||||||||||
| 327 | ## space. if this is the case their should be a file name attached, else | ||||||||||||||||||||||||||||||||||||||||||
| 328 | ## supply one on the next line. | ||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | if (($clie_cmd =~ m/^[Gg][Ee][Tt]\s/) && (length($clie_cmd) > 4)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$clie_cmd =~ /^[Gg][Ee][Tt]\s/ and length $clie_cmd > 4
MC/DC0%0/2
$clie_cmd =~ /^[Gg][Ee][Tt]\s/ and length $clie_cmd > 4
$clie_cmd =~ /^[Gg][Ee][Tt]\s/
length $clie_cmd > 4
Truth table0%0/3
A and B
A= $clie_cmd =~ /^[Gg][Ee][Tt]\s/B= length $clie_cmd > 4
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | ## assign filename to $local_file and chomp | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | ($tmp, $local_file) = split(/ /, $clie_cmd); | |||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | chomp($local_file); | |||||||||||||||||||||||||||||||||||||||||
| 333 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | syswrite(STDOUT, "> Enter the name of the file you wish to download: "); | |||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | sysread(STDIN, $local_file, 25); | |||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | chomp($local_file); | |||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | $self->send_cmd($GET,$local_file); | |||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ($serv_cmd, $serv_msg) = $self->recv_cmd; | |||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | if ($serv_cmd eq $ACK_CMD) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | syswrite(STDOUT, "$serv_cmd\tFile Size: $serv_msg\n"); | |||||||||||||||||||||||||||||||||||||||||
| 344 | ## prereq: clients get request has been acknowledged by server | ||||||||||||||||||||||||||||||||||||||||||
| 345 | ## thus, the $serv_msg supplied contains the requested files size | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $file_size = $serv_msg; | |||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | ## opening the file | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if($localfd) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | $loc = $local_file; | |||||||||||||||||||||||||||||||||||||||||
| 351 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | $loc = \*FD; | |||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | unless(sysopen($loc, "$directory/$local_file", O_CREAT | O_WRONLY)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | { | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | carp "Cannot open $directory/$local_file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | ## file transfer section | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | last if ($bytes_read eq $file_size); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | $read = sysread($socket, $packet, $buffer); | |||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | unless (defined($read) && ($read eq length($packet))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $read and $read eq length $packet
MC/DC0%0/2
defined $read and $read eq length $packet
defined $read
$read eq length $packet
Truth table0%0/3
A and B
A= defined $readB= $read eq length $packet
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | croak "Error reading socket in client connection"; | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | $written = syswrite($loc, $packet, length($packet)); | |||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | unless (defined($written) && ($written eq length($packet))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $written and $written eq length $packet
MC/DC0%0/2
defined $written and $written eq length $packet
defined $written
$written eq length $packet
Truth table0%0/3
A and B
A= defined $writtenB= $written eq length $packet
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | croak "Unable to write to new file"; | |||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | $bytes_read += $written; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | syswrite(STDOUT, "*"); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } while ($bytes_read != $file_size); | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | ## reset used variables... | ||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ($read, $written, $bytes_read, $file_size) = (0,0,0,0); | |||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | syswrite (STDOUT, "\nClosing FH\n"); | |||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | close $loc | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | or carp $! ? "Error closing file: $!" | ||||||||||||||||||||||||||||||||||||||||||
| 382 | : "Exit status $? from close"; | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 384 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 385 | ## the case where a get request has not been successful | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | syswrite(STDOUT, "$serv_cmd\t$serv_msg\n"); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | } elsif ($clie_cmd =~ m/[Qq][Uu][Ii][Tt]/) { | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | $self->send_cmd($QUIT); | |||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | print "Goodbye!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 393 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | print "The only commands are: list, get, & quit\n"; | |||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | sub send_cmd | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_cmd: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | ## first off, @_ can either have two or three variables passed | ||||||||||||||||||||||||||||||||||||||||||
| 405 | ## here. if two, (ie. self and cmd) those are command is being sent | ||||||||||||||||||||||||||||||||||||||||||
| 406 | ## to the other side, if three, (ie self, cmd, msg) cmd and msg are | ||||||||||||||||||||||||||||||||||||||||||
| 407 | ## sent | ||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | if (@_ > 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | my ($self, $cmd, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | my ($buf) = "$cmd,$data"; | |||||||||||||||||||||||||||||||||||||||||
| 411 | 0 | $self->_send_packet($buf); | |||||||||||||||||||||||||||||||||||||||||
| 412 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | my ($self, $cmd) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | my ($buf) = "$cmd,"; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | $self->_send_packet($buf); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | sub recv_cmd | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
recv_cmd: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | my ($msg); | |||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | ## read the awaiting message | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | if (eval { $msg = $self->_recv_packet() }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | if (!defined($msg)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | my ($type, $buf) = split(/,/, $msg); | |||||||||||||||||||||||||||||||||||||||||
| 431 | ## add in check to confirm type is valid | ||||||||||||||||||||||||||||||||||||||||||
| 432 | ## this should not be a problem on server | ||||||||||||||||||||||||||||||||||||||||||
| 433 | ## server side, but kill client side... | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | ## return cmd type and buffer | ||||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | return($type, $buf); | |||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | sub debug | ||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | my ($self, $msg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | syswrite(STDOUT, "$msg\n"); | |||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | sub handle_request | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_request: not called CC=40 SCAR=74.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | ## purpose: interface with the client on the behalf | ||||||||||||||||||||||||||||||||||||||||||
| 450 | ## of the server. provide the client lists of available files | ||||||||||||||||||||||||||||||||||||||||||
| 451 | ## to download as well as providing the actual file transfer | ||||||||||||||||||||||||||||||||||||||||||
| 452 | ## piece | ||||||||||||||||||||||||||||||||||||||||||
| 453 | ## prereq: client has connected to the server, and server has | ||||||||||||||||||||||||||||||||||||||||||
| 454 | ## successfully forked the client. | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | my ($socket) = $self->{_socket}; | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | my ($directory) = $self->{_directory}; | |||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | my ($clie_cmd, $clie_msg, $file_size); | |||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | my ($buffer, $data) = (8, 0); | |||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | ## begin with sending a $HELO to client | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | $self->send_cmd($HELO,$helo_msg); | |||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 465 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 0 | my ($loc, $local_file); | |||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | ($clie_cmd, $clie_msg) = $self->recv_cmd; | |||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | ▶ | exit if ($clie_cmd eq $QUIT); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | if (($clie_cmd ne $GET) && ($clie_cmd ne $LIST)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$clie_cmd ne $Net::FileShare::GET and $clie_cmd ne $Net::FileShare::LIST
MC/DC0%0/2
$clie_cmd ne $Net::FileShare::GET and $clie_cmd ne $Net::FileShare::LIST
$clie_cmd ne $Net::FileShare::GET
$clie_cmd ne $Net::FileShare::LIST
Truth table0%0/3
A and B
A= $clie_cmd ne $Net::FileShare::GETB= $clie_cmd ne $Net::FileShare::LIST
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | $self->send_cmd($UNRECOG_CMD,$unrecog_cmd_msg); | |||||||||||||||||||||||||||||||||||||||||
| 474 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 475 | ## the instance where a client command is equal to either $GET or $LIST | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | ▶ | if ($clie_cmd eq $GET) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | $local_file = $clie_msg; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | ▶ | $self->debug("Client has requested $directory/$local_file") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | ## first check...file name contains more than 0 chars | ||||||||||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | $self->debug("Checking length of filename!") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | ▶ | if (length($local_file) > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | ▶ | $self->debug("Check Successful") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | ## second check...first character is a '/' or presence of a '..' | ||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | ▶ | $self->debug("Checking for control characters\n") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | if (($local_file =~ m/^\//)||($local_file =~ m/^\.\.?$/)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$local_file =~ m[^/] or $local_file =~ /^\.\.?$/
MC/DC0%0/2
$local_file =~ m[^/] or $local_file =~ /^\.\.?$/
$local_file =~ m[^/]
$local_file =~ /^\.\.?$/
Truth table0%0/3
A or B
A= $local_file =~ m[^/]B= $local_file =~ /^\.\.?$/
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | ▶ | $self->debug("Check Failed") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | $self->send_cmd($INVAL_NAME,$inval_name_msg); | |||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | $self->debug("Check Successful") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | ## third check...is a file | ||||||||||||||||||||||||||||||||||||||||||
| 496 | 0 | ▶ | $self->debug("Checking to see if $local_file is a file") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 0 | ▶ | if (-f "$directory/$local_file") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | ▶ | $self->debug("Check Successful") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | ## fourth check...exists and is readable | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 0 | ▶ | if ((-e "$directory/$local_file") && (-r "$directory/$local_file")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e "$directory/$local_file" and -r "$directory/$local_file"
MC/DC0%0/2
-e "$directory/$local_file" and -r "$directory/$local_file"
-e "$directory/$local_file"
-r "$directory/$local_file"
Truth table0%0/3
A and B
A= -e "$directory/$local_file"B= -r "$directory/$local_file"
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | ## determine size now | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | $file_size = (stat("$directory/$local_file"))[7]; | |||||||||||||||||||||||||||||||||||||||||
| 504 | ## if here, all checks are successful | ||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | $self->send_cmd($ACK_CMD, $file_size); | |||||||||||||||||||||||||||||||||||||||||
| 506 | ## begin transfer process here... | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | ▶ | $self->debug("Preparing to transfer file...") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | #my ($loc, $local_file); | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | local *FH; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | ▶ | my $localfd = ref($local_file) || ref(\$local_file) eq "GLOB"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $local_file || ref \$local_file eq 'GLOB'
MC/DC0%0/2
ref $local_file || ref \$local_file eq 'GLOB'
ref $local_file
ref \$local_file eq 'GLOB'
Truth table0%0/3
A || B
A= ref $local_fileB= ref \$local_file eq 'GLOB'
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | ▶ | if($localfd) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | $loc = $local_file; | |||||||||||||||||||||||||||||||||||||||||
| 518 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | $loc = \*FH; | |||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | unless(sysopen($loc,"$directory/$local_file", O_RDONLY)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | { | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | carp "Cannot open file"; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | my ($bytes_wrote, $read, $wrote) = (0,0,0); | |||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 530 | 0 | ▶ | last if ($bytes_wrote eq $file_size); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | $read = sysread($loc,$data,$buffer); | |||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | $wrote = syswrite($socket,$data,$buffer); | |||||||||||||||||||||||||||||||||||||||||
| 533 | 0 | ▶ | unless ((defined($read)) && (defined($wrote))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $read and defined $wrote
MC/DC0%0/2
defined $read and defined $wrote
defined $read
defined $wrote
Truth table0%0/3
A and B
A= defined $readB= defined $wrote
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | 0 | croak "Unable to read from file or write to client"; | |||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||
| 536 | 0 | $bytes_wrote += $wrote; | |||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | syswrite(STDOUT, "*"); | |||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | 0 | ▶ | $self->debug("\nWrote $bytes_wrote bytes of data to client\n") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | ▶ | close $loc | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 541 | or carp $! ? "Cannot close file: $!" | ||||||||||||||||||||||||||||||||||||||||||
| 542 | : "Exit status $? from close"; | ||||||||||||||||||||||||||||||||||||||||||
| 543 | #next; | ||||||||||||||||||||||||||||||||||||||||||
| 544 | ## reset file_size to be zero | ||||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | ($file_size) = (0); | |||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | ▶ | $self->debug("Check Failed") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 0 | $self->send_cmd($PERM_DENY,$perm_deny_msg); | |||||||||||||||||||||||||||||||||||||||||
| 550 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||
| 552 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | ▶ | $self->debug("Check Failed") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | 0 | $self->send_cmd($FILE_NOT_FOUND,$file_not_found_msg); | |||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||||||||||||||||
| 557 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 558 | 0 | ▶ | $self->debug("Check Failed") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | $self->send_cmd($INVAL_NAME,$inval_name_msg); | |||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||
| 562 | ## end of if $GET | ||||||||||||||||||||||||||||||||||||||||||
| 563 | } elsif ($clie_cmd eq $LIST) { | ||||||||||||||||||||||||||||||||||||||||||
| 564 | ## handle list command | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | ▶ | $self->debug("Client has requested a list: ") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | my ($directory) = $self->{_directory}; | |||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | my ($str,$file); | |||||||||||||||||||||||||||||||||||||||||
| 568 | 0 | $str = "The files available for download are as follows:"; | |||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | opendir(DIR, $directory) or croak "Cannot open directory $directory: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | ▶ | while (defined ($file = readdir(DIR))) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | ▶ | if(($file =~ m/^\./) or (-d "$directory/$file")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file =~ /^\./ or -d "$directory/$file"
MC/DC0%0/2
$file =~ /^\./ or -d "$directory/$file"
$file =~ /^\./
-d "$directory/$file"
Truth table0%0/3
A or B
A= $file =~ /^\./B= -d "$directory/$file"
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 573 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | $str .= "*$file"; | |||||||||||||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | $self->send_cmd($ACK_CMD,$str); | |||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | ▶ | $self->debug("Done") if ($self->{_debug} eq 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | } ## end of elsif $LIST | ||||||||||||||||||||||||||||||||||||||||||
| 580 | } ## end of inner else loop | ||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||
| 582 | } while ($clie_cmd ne $QUIT); | ||||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | sub _send_packet | ||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send_packet: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | my ($self, $packet) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | my ($socket) = $self->{_socket}; | |||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | my ($plen) = length($packet); | |||||||||||||||||||||||||||||||||||||||||
| 591 | ## current packet length | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | ▶ | croak "Error sending packet: packet > 255 bytes" if ($plen > 255); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | ## add terminating null to packet | ||||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | $packet .= "\0"; | |||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | $plen++; ## for addition of null. | |||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | ## add the packet length | ||||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | $packet = chr($plen).$packet; | |||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | $plen++; ## for addition of chr($plen) | |||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||
| 603 | 0 | my $wrote_length = syswrite($socket, $packet, $plen); | |||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||
| 605 | ## checking for errors w/ syswrite | ||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | ▶ | if (!defined($wrote_length)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 607 | 0 | croak "Error sending packet: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 608 | } elsif ($wrote_length != $plen) { | ||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | croak "Error sending packet: wrote $wrote_length of $plen: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 610 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 611 | 0 | return 'ok'; | |||||||||||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | sub _recv_packet | ||||||||||||||||||||||||||||||||||||||||||
| 617 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recv_packet: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | my ($socket) = $self->{_socket}; | |||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | my ($slen, $buffer, $ret); | |||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||
| 622 | ## this is a two step process | ||||||||||||||||||||||||||||||||||||||||||
| 623 | ## first, read in one byte of data | ||||||||||||||||||||||||||||||||||||||||||
| 624 | ## this will be the length of the packet data | ||||||||||||||||||||||||||||||||||||||||||
| 625 | ## then read only the amount of data as | ||||||||||||||||||||||||||||||||||||||||||
| 626 | ## specified by the first byte | ||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | $ret = sysread($socket,$slen,1,0); | |||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | ## troubleshooting sysread | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | ▶ | if(!defined($ret)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | croak "Error Receiving first byte: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 633 | } elsif (length($slen) != 1) { | ||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | croak "Error Receiving first byte not eq 1: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 635 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 636 | ## convert char to integer | ||||||||||||||||||||||||||||||||||||||||||
| 637 | 0 | $slen = ord($slen); | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 0 | ▶ | while ($slen) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 640 | 0 | my ($pbuf); | |||||||||||||||||||||||||||||||||||||||||
| 641 | 0 | $ret = sysread($socket,$pbuf,$slen,0); | |||||||||||||||||||||||||||||||||||||||||
| 642 | 0 | ▶ | if (!defined($ret)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 643 | 0 | croak "Error Receiving, return not define: $!" | |||||||||||||||||||||||||||||||||||||||||
| 644 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 645 | 0 | $slen -= length($pbuf); | |||||||||||||||||||||||||||||||||||||||||
| 646 | 0 | $buffer .= $pbuf; | |||||||||||||||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||||||||||||||||
| 648 | ## remove trailing null | ||||||||||||||||||||||||||||||||||||||||||
| 649 | 0 | chop($buffer); | |||||||||||||||||||||||||||||||||||||||||
| 650 | 0 | return($buffer); | |||||||||||||||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||||||||||||||||
| 652 | } # end of above else | ||||||||||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||||||||||||
| 656 | __END__ | ||||||||||||||||||||||||||||||||||||||||||