/[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 138 - (show annotations)
Tue Aug 4 15:25:09 2009 UTC (14 years, 8 months ago) by dpavlin
File size: 2156 byte(s)
quite huge refactoring

- config is now place to define client configuration
- upstream provide mirroring
- pxelinux creates per-client specific configurations
- various tweaks and cleanups

1 package tftpd;
2
3 use warnings;
4 use strict;
5
6 use Net::TFTPd 0.03 qw(%OPCODES);
7 use Data::Dump qw/dump/;
8
9 use server;
10
11 our $debug = server::debug;
12
13 our $dir = "$server::base_dir/tftp";
14
15 sub path {
16 my $glob = shift;
17 my $path = (glob("$dir/$glob"))[0];
18 die "can't find anything for $dir/$glob" unless $path;
19 warn 'path ', $path if $debug;
20 $path =~ s{^$dir}{};
21 return $path;
22 }
23
24 use progress_bar;
25
26 sub transfer_status {
27 my $request = shift;
28 my $r = $request->{'_REQUEST_'} || die "no _REQUEST_ in ",dump( $request );
29
30 if( $r->{'OPCODE'} eq $OPCODES{'RRQ'} ) {
31 progress_bar::tick( $r->{FileName}, $r->{BlkSize} * $r->{LASTACK}, $r->{BlkSize} * $r->{LASTBLK} );
32 } elsif ( $r->{'OPCODE'} eq $OPCODES{'WRQ'} ) {
33 die "WRQ disabled";
34 } else {
35 warn "IGNORED: ", dump( $request );
36 }
37 }
38
39 use config;
40
41 sub tftp_request {
42 my $request = shift;
43
44 server->refresh;
45
46 warn 'request: ', dump( $request ) if $debug;
47
48 config::for_ip( $request->{_REQUEST_}->{PeerAddr} );
49
50 if ( $request->{RootDir} ne $dir ) {
51 $request->{RootDir} = $dir;
52 warn "new root: $dir";
53 }
54
55 my $file = $request->{'_REQUEST_'}{'FileName'};
56 # received request
57 print $OPCODES{$request->{'_REQUEST_'}{'OPCODE'}}, " $file\n";
58
59 progress_bar::start;
60
61 # process the request
62 if( $request->processRQ() ) {
63 print "\nOK completed $file ", -s "$dir/$file", "\n";
64 } else {
65 print "ERROR ", Net::TFTPd->error, "\n";
66 $request->processRQ();
67 }
68
69 }
70
71 use server;
72
73 sub start {
74
75 warn 'start';
76
77 my $listener = Net::TFTPd->new(
78 RootDir => $dir,
79 Writable => 0,
80 Timeout => 3600,
81 CallBack => \&transfer_status,
82 # LocalAddr => $server::ip,
83 LocalAddr => '0.0.0.0',
84 # BlkSize => 8192,
85 # BlkSize => 512,
86 BlkSize => 1456, # IBM GE seems to be picky
87 Debug => 99,
88 ) || die Net::TFTPd->error;
89
90 warn 'listener: ',dump( $listener ) if $debug;
91
92 printf "TFTP listen %s:%d timeout: %d dir: $dir\n",
93 $listener->{LocalAddr},
94 $listener->{LocalPort},
95 $listener->{Timeout};
96
97 while(1) {
98
99 # wait for any request (RRQ or WRQ)
100 if(my $request = $listener->waitRQ()) {
101 tftp_request $request;
102 } elsif ( my $error = Net::TFTPd->error ) {
103 warn $error;
104 }
105
106 }
107
108 }
109
110 warn "loaded";
111
112 1;

  ViewVC Help
Powered by ViewVC 1.1.26