Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.2 KiB

  1. ##
  2. ## Generic data connection package
  3. ##
  4. package Net::FTP::dataconn;
  5. use Carp;
  6. use vars qw(@ISA $timeout);
  7. use Net::Cmd;
  8. @ISA = qw(IO::Socket::INET);
  9. sub reading
  10. {
  11. my $data = shift;
  12. ${*$data}{'net_ftp_bytesread'} = 0;
  13. }
  14. sub abort
  15. {
  16. my $data = shift;
  17. my $ftp = ${*$data}{'net_ftp_cmd'};
  18. # no need to abort if we have finished the xfer
  19. return $data->close
  20. if ${*$data}{'net_ftp_eof'};
  21. # for some reason if we continously open RETR connections and not
  22. # read a single byte, then abort them after a while the server will
  23. # close our connection, this prevents the unexpected EOF on the
  24. # command channel -- GMB
  25. if(exists ${*$data}{'net_ftp_bytesread'}
  26. && (${*$data}{'net_ftp_bytesread'} == 0)) {
  27. my $buf="";
  28. my $timeout = $data->timeout;
  29. $data->can_read($timeout) && sysread($data,$buf,1);
  30. }
  31. ${*$data}{'net_ftp_eof'} = 1; # fake
  32. $ftp->abort; # this will close me
  33. }
  34. sub _close
  35. {
  36. my $data = shift;
  37. my $ftp = ${*$data}{'net_ftp_cmd'};
  38. $data->SUPER::close();
  39. delete ${*$ftp}{'net_ftp_dataconn'}
  40. if exists ${*$ftp}{'net_ftp_dataconn'} &&
  41. $data == ${*$ftp}{'net_ftp_dataconn'};
  42. }
  43. sub close
  44. {
  45. my $data = shift;
  46. my $ftp = ${*$data}{'net_ftp_cmd'};
  47. if(exists ${*$data}{'net_ftp_bytesread'} && !${*$data}{'net_ftp_eof'}) {
  48. my $junk;
  49. $data->read($junk,1,0);
  50. return $data->abort unless ${*$data}{'net_ftp_eof'};
  51. }
  52. $data->_close;
  53. $ftp->response() == CMD_OK &&
  54. $ftp->message =~ /unique file name:\s*(\S*)\s*\)/ &&
  55. (${*$ftp}{'net_ftp_unique'} = $1);
  56. $ftp->status == CMD_OK;
  57. }
  58. sub _select
  59. {
  60. my $data = shift;
  61. local *timeout = \$_[0]; shift;
  62. my $rw = shift;
  63. my($rin,$win);
  64. return 1 unless $timeout;
  65. $rin = '';
  66. vec($rin,fileno($data),1) = 1;
  67. $win = $rw ? undef : $rin;
  68. $rin = undef unless $rw;
  69. my $nfound = select($rin, $win, undef, $timeout);
  70. croak "select: $!"
  71. if $nfound < 0;
  72. return $nfound;
  73. }
  74. sub can_read
  75. {
  76. my $data = shift;
  77. local *timeout = \$_[0];
  78. $data->_select($timeout,1);
  79. }
  80. sub can_write
  81. {
  82. my $data = shift;
  83. local *timeout = \$_[0];
  84. $data->_select($timeout,0);
  85. }
  86. sub cmd
  87. {
  88. my $ftp = shift;
  89. ${*$ftp}{'net_ftp_cmd'};
  90. }
  91. 1;