script/tftpd-simple.pl untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 124 CC 6 SCAR 31.7
CC
6
Cov
0%
yyyymmddhhmmss
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 23.8
1 #!/usr/local/bin/perl
2  
3 use strict;
4 use warnings;
5 use Getopt::Long qw(:config no_ignore_case); #bundling
6 use Pod::Usage;
7  
8 use Net::TFTPd qw( :all );
9  
10 my %opt;
11 my ($opt_help, $opt_man);
12  
13 GetOptions(
14 '4!' => \$opt{4},
15 '6!' => \$opt{6},
16 'directory=s' => \$opt{dir},
17 'interface:i' => \$opt{interface},
18 'time!' => \$opt{time},
19 'help!' => \$opt_help,
20 'man!' => \$opt_man
21 ) or pod2usage(-verbose => 0);
22  
23 pod2usage(-verbose => 1) if defined $opt_help;
24 pod2usage(-verbose => 2) if defined $opt_man;
25  
26 # Default to IPv4
27 my $family = 4;
28 if ($opt{6}) {
29 $family = 6
30 }
31  
32 $opt{time} = $opt{time} || 0;
33  
34 # -d is a directory, if it exists, assign it
35 if (defined $opt{dir}) {
36  
37 # replace \ with / for compatibility with UNIX/Windows
38 $opt{dir} =~ s/\\/\//g;
39  
40 # remove trailing / so we're sure it does NOT exist and we CAN put it in later
41 $opt{dir} =~ s/\/$//;
42  
43 if (!(-e $opt{dir})) {
44 print "$0: directory does not exist - $opt{dir}";
45 exit 1
46 }
47 $opt{write} = 1 if (!$opt{write})
48 } else {
49 $opt{dir} = '.'
50 }
51  
52 if (defined $opt{interface}) {
53 if (!(($opt{interface} > 0) && ($opt{interface} < 65536))) {
54 print "$0: port not valid - $opt{interface}"
55 }
56 } else {
57 $opt{interface} = '69'
58 }
59  
60 my $tftpd = Net::TFTPd->new(
61 RootDir => $opt{dir},
62 Writable => 1,
63 LocalPort => $opt{interface},
64 Family => $family
65 );
66  
67 if (!$tftpd) {
68 printf "$0: Error creating TFTPd listener: %s", Net::TFTPd->error;
69 exit 1
70 }
71  
72 printf "Listening on %s:%i\n" .
73 "TFTP Root Dir = %s\n\n",
74 $tftpd->{_UDPSERVER_}->sockhost,
75 $opt{interface},
76 $opt{dir};
77  
78 my $tftpdRQ;
79 while (1) {
80 if (!($tftpdRQ = $tftpd->waitRQ())) { next }
81  
82 my $p = sprintf "%s\t%s\t%i\t%s\t%s\t%s", ($opt{time} ? yyyymmddhhmmss() : time), $tftpdRQ->getPeerAddr, $tftpdRQ->getPeerPort, $OPCODES{$tftpdRQ->{_REQUEST_}->{OPCODE}}, $tftpdRQ->getMode, $tftpdRQ->getFileName;
83 print "$p\tSTARTED\n";
84  
85 my $pid = fork();
86  
87 if (!defined $pid) {
88 print "fork() Error!\n";
89 exit
90 } elsif ($pid == 0) {
91 printf $p;
92 if (defined $tftpdRQ->processRQ()) {
93 printf "\tSUCCESS [%i bytes]\n", $tftpdRQ->getTotalBytes
94 } else {
95 print "\t" . Net::TFTPd->error . "\n"
96 }
97 exit
98 } else {
99 # parent
100 }
101 }
102  
103 sub yyyymmddhhmmss {
104 my @time = localtime();
105 return (($time[5] + 1900) . ((($time[4] + 1) < 10)?("0" . ($time[4] + 1)):($time[4] + 1)) . (($time[3] < 10)?("0" . $time[3]):$time[3]) . (($time[2] < 10)?("0" . $time[2]):$time[2]) . (($time[1] < 10)?("0" . $time[1]):$time[1]) . (($time[0] < 10)?("0" . $time[0]):$time[0]))
106 }
107  
108 __END__