/[pxelator]/lib/PXElator/tftpd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/PXElator/tftpd.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 482 - (show annotations)
Sat Jan 23 18:31:14 2010 UTC (14 years, 2 months ago) by dpavlin
File size: 2842 byte(s)
use store::audit proxy all over the place

1 package tftpd;
2
3 use warnings;
4 use strict;
5
6 use Net::TFTPd 0.03 qw(%OPCODES);
7 use IO::Socket::INET;
8 use Data::Dump qw/dump/;
9 use store;
10
11 use server;
12
13 our $debug = server::debug;
14
15 our $dir = "$server::base_dir/tftp";
16
17 sub path {
18 my $glob = shift;
19 my $path = (glob("$dir/$glob"))[0];
20 die "can't find anything for $dir/$glob" unless $path;
21 warn 'path ', $path if $debug;
22 $path =~ s{^$dir}{};
23 return $path;
24 }
25
26 use progress_bar;
27
28 sub transfer_status {
29 my $request = shift;
30 my $r = $request->{'_REQUEST_'} || die "no _REQUEST_ in ",dump( $request );
31
32 if( $r->{'OPCODE'} eq $OPCODES{'RRQ'} ) {
33 progress_bar::tick( $r->{FileName}, $r->{BlkSize} * $r->{LASTACK}, $r->{BlkSize} * $r->{LASTBLK} );
34 } elsif ( $r->{'OPCODE'} eq $OPCODES{'WRQ'} ) {
35 die "WRQ disabled";
36 } else {
37 warn "IGNORED: ", dump( $request );
38 }
39 }
40
41 use config;
42
43 sub tftp_request {
44 my $request = shift;
45
46 warn 'request: ', dump( $request ) if $debug;
47
48 my $ip = $request->{_REQUEST_}->{PeerAddr};
49 config::for_ip( $ip );
50
51 if ( $request->{RootDir} ne $dir ) {
52 $request->{RootDir} = $dir;
53 warn "new root: $dir";
54 }
55
56 my $file = $request->{'_REQUEST_'}{'FileName'};
57 my $opcode = $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}};
58
59 my $audit = {
60 ip => $ip,
61 opcode => $opcode,
62 path => $file,
63 state => 'start',
64 };
65 store::audit( $opcode, $audit );
66
67 progress_bar::start;
68
69 # process the request
70 if( $request->processRQ() ) {
71 my $size = -s "$dir/$file";
72 $audit->{state} = 'finish';
73 $audit->{size} = $size;
74 store::audit( $opcode, $audit );
75 } else {
76 $audit->{state} = 'error';
77 $audit->{error} = Net::TFTPd->error;
78 store::audit( $opcode, $audit );
79 }
80
81 }
82
83 use server;
84
85 sub start {
86
87 warn 'start';
88
89 # XXX we need to setup listener ourselfs because we need Reuse
90 my %params = (
91 Proto => 'udp',
92 # LocalAddr => $server::ip,
93 # LocalAddr => '0.0.0.0',
94 LocalPort => 69,
95 Reuse => 1,
96 );
97
98 my $udpserver = IO::Socket::INET->new(%params);
99 die "can't start server ",dump( \%params ), " $!" unless $udpserver;
100
101 my $listener = bless {
102 RootDir => $dir,
103
104 ACKtimeout => 4,
105 ACKretries => 4,
106 Readable => 1,
107 Writable => 0,
108 Timeout => 3600,
109
110 CallBack => \&transfer_status,
111 # BlkSize => 8192,
112 BlkSize => 512, # Dell's RAC doesn't like bigger packets
113 # BlkSize => 1456, # IBM GE seems to be picky
114 Debug => 99,
115 %params, # merge user parameters
116 _UDPSERVER_ => $udpserver,
117 }, 'Net::TFTPd';
118
119 warn 'listener: ',dump( $listener ) if $debug;
120
121 store::audit( 'start', {
122 addr => $listener->{LocalAddr},
123 port => $listener->{LocalPort},
124 timeout => $listener->{Timeout},
125 params => { %params },
126 });
127
128 while(1) {
129
130 # wait for any request (RRQ or WRQ)
131 if(my $request = $listener->waitRQ()) {
132 server->refresh;
133 tftp_request $request;
134 } elsif ( my $error = Net::TFTPd->error ) {
135 warn $error;
136 }
137
138 }
139
140 }
141
142 warn "loaded";
143
144 1;

  ViewVC Help
Powered by ViewVC 1.1.26