1*0Sstevel@tonic-gate## 2*0Sstevel@tonic-gate## Generic data connection package 3*0Sstevel@tonic-gate## 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gatepackage Net::FTP::dataconn; 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateuse Carp; 8*0Sstevel@tonic-gateuse vars qw(@ISA $timeout $VERSION); 9*0Sstevel@tonic-gateuse Net::Cmd; 10*0Sstevel@tonic-gateuse Errno; 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate$VERSION = '0.11'; 13*0Sstevel@tonic-gate@ISA = qw(IO::Socket::INET); 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gatesub reading 16*0Sstevel@tonic-gate{ 17*0Sstevel@tonic-gate my $data = shift; 18*0Sstevel@tonic-gate ${*$data}{'net_ftp_bytesread'} = 0; 19*0Sstevel@tonic-gate} 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gatesub abort 22*0Sstevel@tonic-gate{ 23*0Sstevel@tonic-gate my $data = shift; 24*0Sstevel@tonic-gate my $ftp = ${*$data}{'net_ftp_cmd'}; 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate # no need to abort if we have finished the xfer 27*0Sstevel@tonic-gate return $data->close 28*0Sstevel@tonic-gate if ${*$data}{'net_ftp_eof'}; 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate # for some reason if we continously open RETR connections and not 31*0Sstevel@tonic-gate # read a single byte, then abort them after a while the server will 32*0Sstevel@tonic-gate # close our connection, this prevents the unexpected EOF on the 33*0Sstevel@tonic-gate # command channel -- GMB 34*0Sstevel@tonic-gate if(exists ${*$data}{'net_ftp_bytesread'} 35*0Sstevel@tonic-gate && (${*$data}{'net_ftp_bytesread'} == 0)) { 36*0Sstevel@tonic-gate my $buf=""; 37*0Sstevel@tonic-gate my $timeout = $data->timeout; 38*0Sstevel@tonic-gate $data->can_read($timeout) && sysread($data,$buf,1); 39*0Sstevel@tonic-gate } 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate ${*$data}{'net_ftp_eof'} = 1; # fake 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate $ftp->abort; # this will close me 44*0Sstevel@tonic-gate} 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gatesub _close 47*0Sstevel@tonic-gate{ 48*0Sstevel@tonic-gate my $data = shift; 49*0Sstevel@tonic-gate my $ftp = ${*$data}{'net_ftp_cmd'}; 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate $data->SUPER::close(); 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate delete ${*$ftp}{'net_ftp_dataconn'} 54*0Sstevel@tonic-gate if exists ${*$ftp}{'net_ftp_dataconn'} && 55*0Sstevel@tonic-gate $data == ${*$ftp}{'net_ftp_dataconn'}; 56*0Sstevel@tonic-gate} 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gatesub close 59*0Sstevel@tonic-gate{ 60*0Sstevel@tonic-gate my $data = shift; 61*0Sstevel@tonic-gate my $ftp = ${*$data}{'net_ftp_cmd'}; 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate if(exists ${*$data}{'net_ftp_bytesread'} && !${*$data}{'net_ftp_eof'}) { 64*0Sstevel@tonic-gate my $junk; 65*0Sstevel@tonic-gate $data->read($junk,1,0); 66*0Sstevel@tonic-gate return $data->abort unless ${*$data}{'net_ftp_eof'}; 67*0Sstevel@tonic-gate } 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate $data->_close; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate $ftp->response() == CMD_OK && 72*0Sstevel@tonic-gate $ftp->message =~ /unique file name:\s*(\S*)\s*\)/ && 73*0Sstevel@tonic-gate (${*$ftp}{'net_ftp_unique'} = $1); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate $ftp->status == CMD_OK; 76*0Sstevel@tonic-gate} 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gatesub _select { 79*0Sstevel@tonic-gate my ($data, $timeout, $do_read) = @_; 80*0Sstevel@tonic-gate my ($rin,$rout,$win,$wout,$tout,$nfound); 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate vec($rin='',fileno($data),1) = 1; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate ($win, $rin) = ($rin, $win) unless $do_read; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate while (1) { 87*0Sstevel@tonic-gate $nfound = select($rout=$rin, $wout=$win, undef, $tout=$timeout); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate last if $nfound >= 0; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate croak "select: $!" 92*0Sstevel@tonic-gate unless $!{EINTR}; 93*0Sstevel@tonic-gate } 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate $nfound; 96*0Sstevel@tonic-gate} 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gatesub can_read 99*0Sstevel@tonic-gate{ 100*0Sstevel@tonic-gate _select(@_[0,1],1); 101*0Sstevel@tonic-gate} 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gatesub can_write 104*0Sstevel@tonic-gate{ 105*0Sstevel@tonic-gate _select(@_[0,1],0); 106*0Sstevel@tonic-gate} 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gatesub cmd 109*0Sstevel@tonic-gate{ 110*0Sstevel@tonic-gate my $ftp = shift; 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate ${*$ftp}{'net_ftp_cmd'}; 113*0Sstevel@tonic-gate} 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gatesub bytes_read { 116*0Sstevel@tonic-gate my $ftp = shift; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate ${*$ftp}{'net_ftp_bytesread'} || 0; 119*0Sstevel@tonic-gate} 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate1; 122