<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Net::FTP.pm
#
# Copyright (C) 1995-2004 Graham Barr.  All rights reserved.
# Copyright (C) 2013-2017, 2020 Steve Hay.  All rights reserved.
# This module is free software; you can redistribute it and/or modify it under
# the same terms as Perl itself, i.e. under the terms of either the GNU General
# Public License or the Artistic License, as specified in the F&lt;LICENCE&gt; file.
#
# Documentation (at end) improved 1996 by Nathan Torkington &lt;gnat@frii.com&gt;.

package Net::FTP;

use 5.008001;

use strict;
use warnings;

use Carp;
use Fcntl qw(O_WRONLY O_RDONLY O_APPEND O_CREAT O_TRUNC);
use IO::Socket;
use Net::Cmd;
use Net::Config;
use Socket;
use Time::Local;

our $VERSION = '3.13';

our $IOCLASS;
my $family_key;
BEGIN {
  # Code for detecting if we can use SSL
  my $ssl_class = eval {
    require IO::Socket::SSL;
    # first version with default CA on most platforms
    no warnings 'numeric';
    IO::Socket::SSL-&gt;VERSION(2.007);
  } &amp;&amp; 'IO::Socket::SSL';

  my $nossl_warn = !$ssl_class &amp;&amp;
    'To use SSL please install IO::Socket::SSL with version&gt;=2.007';

  # Code for detecting if we can use IPv6
  my $inet6_class = eval {
    require IO::Socket::IP;
    no warnings 'numeric';
    IO::Socket::IP-&gt;VERSION(0.25);
  } &amp;&amp; 'IO::Socket::IP' || eval {
    require IO::Socket::INET6;
    no warnings 'numeric';
    IO::Socket::INET6-&gt;VERSION(2.62);
  } &amp;&amp; 'IO::Socket::INET6';

  sub can_ssl   { $ssl_class };
  sub can_inet6 { $inet6_class };

  $IOCLASS = $ssl_class || $inet6_class || 'IO::Socket::INET';
  $family_key =
    ( $ssl_class ? $ssl_class-&gt;can_ipv6 : $inet6_class || '' )
      eq 'IO::Socket::IP'
      ? 'Family' : 'Domain';
}

our @ISA = ('Exporter','Net::Cmd',$IOCLASS);

use constant TELNET_IAC =&gt; 255;
use constant TELNET_IP  =&gt; 244;
use constant TELNET_DM  =&gt; 242;

use constant EBCDIC =&gt; $^O eq 'os390';

sub new {
  my $pkg = shift;
  my ($peer, %arg);
  if (@_ % 2) {
    $peer = shift;
    %arg  = @_;
  }
  else {
    %arg  = @_;
    $peer = delete $arg{Host};
  }

  my $host      = $peer;
  my $fire      = undef;
  my $fire_type = undef;

  if (exists($arg{Firewall}) || Net::Config-&gt;requires_firewall($peer)) {
         $fire = $arg{Firewall}
      || $ENV{FTP_FIREWALL}
      || $NetConfig{ftp_firewall}
      || undef;

    if (defined $fire) {
      $peer = $fire;
      delete $arg{Port};
           $fire_type = $arg{FirewallType}
        || $ENV{FTP_FIREWALL_TYPE}
        || $NetConfig{firewall_type}
        || undef;
    }
  }

  my %tlsargs;
  if (can_ssl()) {
    # for name verification strip port from domain:port, ipv4:port, [ipv6]:port
    (my $hostname = $host) =~s{(?&lt;!:):\d+$}{};
    %tlsargs = (
      SSL_verifycn_scheme =&gt; 'ftp',
      SSL_verifycn_name =&gt; $hostname,
      # use SNI if supported by IO::Socket::SSL
      $pkg-&gt;can_client_sni ? (SSL_hostname =&gt; $hostname):(),
      # reuse SSL session of control connection in data connections
      SSL_session_cache_size =&gt; 10,
      SSL_session_key =&gt; $hostname,
    );
    # user defined SSL arg
    $tlsargs{$_} = $arg{$_} for(grep { m{^SSL_} } keys %arg);
    $tlsargs{SSL_reuse_ctx} = IO::Socket::SSL::SSL_Context-&gt;new(%tlsargs)
      or return;

  } elsif ($arg{SSL}) {
    croak("IO::Socket::SSL &gt;= 2.007 needed for SSL support");
  }

  my $ftp = $pkg-&gt;SUPER::new(
    PeerAddr  =&gt; $peer,
    PeerPort  =&gt; $arg{Port} || ($arg{SSL} ? 'ftps(990)' : 'ftp(21)'),
    LocalAddr =&gt; $arg{'LocalAddr'},
    $family_key =&gt; $arg{Domain} || $arg{Family},
    Proto     =&gt; 'tcp',
    Timeout   =&gt; defined $arg{Timeout} ? $arg{Timeout} : 120,
    %tlsargs,
    $arg{SSL} ? ():( SSL_startHandshake =&gt; 0 ),
  ) or return;

  ${*$ftp}{'net_ftp_host'}    = $host;                             # Remote hostname
  ${*$ftp}{'net_ftp_type'}    = 'A';                               # ASCII/binary/etc mode
  ${*$ftp}{'net_ftp_blksize'} = abs($arg{'BlockSize'} || 10240);

  ${*$ftp}{'net_ftp_localaddr'} = $arg{'LocalAddr'};
  ${*$ftp}{'net_ftp_domain'} = $arg{Domain} || $arg{Family};

  ${*$ftp}{'net_ftp_firewall'} = $fire
    if (defined $fire);
  ${*$ftp}{'net_ftp_firewall_type'} = $fire_type
    if (defined $fire_type);

  ${*$ftp}{'net_ftp_passive'} =
      int exists $arg{Passive} ? $arg{Passive}
    : exists $ENV{FTP_PASSIVE} ? $ENV{FTP_PASSIVE}
    : defined $fire            ? $NetConfig{ftp_ext_passive}
    : $NetConfig{ftp_int_passive};    # Whew! :-)

  ${*$ftp}{net_ftp_tlsargs} = \%tlsargs if %tlsargs;
  if ($arg{SSL}) {
    ${*$ftp}{net_ftp_tlsprot} = 'P';
    ${*$ftp}{net_ftp_tlsdirect} = 1;
  }

  $ftp-&gt;hash(exists $arg{Hash} ? $arg{Hash} : 0, 1024);

  $ftp-&gt;autoflush(1);

  $ftp-&gt;debug(exists $arg{Debug} ? $arg{Debug} : undef);

  unless ($ftp-&gt;response() == CMD_OK) {
    $ftp-&gt;close();
    # keep @$ if no message. Happens, when response did not start with a code.
    $@ = $ftp-&gt;message || $@;
    undef $ftp;
  }

  $ftp;
}

##
## User interface methods
##


sub host {
  my $me = shift;
  ${*$me}{'net_ftp_host'};
}

sub passive {
  my $ftp = shift;
  return ${*$ftp}{'net_ftp_passive'} unless @_;
  ${*$ftp}{'net_ftp_passive'} = shift;
}


sub hash {
  my $ftp = shift;    # self

  my ($h, $b) = @_;
  unless ($h) {
    delete ${*$ftp}{'net_ftp_hash'};
    return [\*STDERR, 0];
  }
  ($h, $b) = (ref($h) ? $h : \*STDERR, $b || 1024);
  select((select($h), $| = 1)[0]);
  $b = 512 if $b &lt; 512;
  ${*$ftp}{'net_ftp_hash'} = [$h, $b];
}


sub quit {
  my $ftp = shift;

  $ftp-&gt;_QUIT;
  $ftp-&gt;close;
}


sub DESTROY { }


sub ascii  { shift-&gt;type('A', @_); }
sub binary { shift-&gt;type('I', @_); }


sub ebcdic {
  carp "TYPE E is unsupported, shall default to I";
  shift-&gt;type('E', @_);
}


sub byte {
  carp "TYPE L is unsupported, shall default to I";
  shift-&gt;type('L', @_);
}

# Allow the user to send a command directly, BE CAREFUL !!


sub quot {
  my $ftp = shift;
  my $cmd = shift;

  $ftp-&gt;command(uc $cmd, @_);
  $ftp-&gt;response();
}


sub site {
  my $ftp = shift;

  $ftp-&gt;command("SITE", @_);
  $ftp-&gt;response();
}


sub mdtm {
  my $ftp  = shift;
  my $file = shift;

  # Server Y2K bug workaround
  #
  # sigh; some idiotic FTP servers use ("19%d",tm.tm_year) instead of
  # ("%d",tm.tm_year+1900).  This results in an extra digit in the
  # string returned. To account for this we allow an optional extra
  # digit in the year. Then if the first two digits are 19 we use the
  # remainder, otherwise we subtract 1900 from the whole year.

  $ftp-&gt;_MDTM($file)
    &amp;&amp; $ftp-&gt;message =~ /((\d\d)(\d\d\d?))(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/
    ? timegm($8, $7, $6, $5, $4 - 1, $2 eq '19' ? ($3 + 1900) : $1)
    : undef;
}


sub size {
  my $ftp  = shift;
  my $file = shift;
  my $io;
  if ($ftp-&gt;supported("SIZE")) {
    return $ftp-&gt;_SIZE($file)
      ? ($ftp-&gt;message =~ /(\d+)\s*(bytes?\s*)?$/)[0]
      : undef;
  }
  elsif ($ftp-&gt;supported("STAT")) {
    my @msg;
    return
      unless $ftp-&gt;_STAT($file) &amp;&amp; (@msg = $ftp-&gt;message) == 3;
    foreach my $line (@msg) {
      return (split(/\s+/, $line))[4]
        if $line =~ /^[-rwxSsTt]{10}/;
    }
  }
  else {
    my @files = $ftp-&gt;dir($file);
    if (@files) {
      return (split(/\s+/, $1))[4]
        if $files[0] =~ /^([-rwxSsTt]{10}.*)$/;
    }
  }
  undef;
}


sub starttls {
  my $ftp = shift;
  can_ssl() or croak("IO::Socket::SSL &gt;= 2.007 needed for SSL support");
  $ftp-&gt;is_SSL and croak("called starttls within SSL session");
  $ftp-&gt;_AUTH('TLS') == CMD_OK or return;

  $ftp-&gt;connect_SSL or return;
  $ftp-&gt;prot('P');
  return 1;
}

sub prot {
  my ($ftp,$prot) = @_;
  $prot eq 'C' or $prot eq 'P' or croak("prot must by C or P");
  $ftp-&gt;_PBSZ(0) or return;
  $ftp-&gt;_PROT($prot) or return;
  ${*$ftp}{net_ftp_tlsprot} = $prot;
  return 1;
}

sub stoptls {
  my $ftp = shift;
  $ftp-&gt;is_SSL or croak("called stoptls outside SSL session");
  ${*$ftp}{net_ftp_tlsdirect} and croak("cannot stoptls direct SSL session");
  $ftp-&gt;_CCC() or return;
  $ftp-&gt;stop_SSL();
  return 1;
}

sub login {
  my ($ftp, $user, $pass, $acct) = @_;
  my ($ok, $ruser, $fwtype);

  unless (defined $user) {
    require Net::Netrc;

    my $rc = Net::Netrc-&gt;lookup(${*$ftp}{'net_ftp_host'});

    ($user, $pass, $acct) = $rc-&gt;lpa()
      if ($rc);
  }

  $user ||= "anonymous";
  $ruser = $user;

  $fwtype = ${*$ftp}{'net_ftp_firewall_type'}
    || $NetConfig{'ftp_firewall_type'}
    || 0;

  if ($fwtype &amp;&amp; defined ${*$ftp}{'net_ftp_firewall'}) {
    if ($fwtype == 1 || $fwtype == 7) {
      $user .= '@' . ${*$ftp}{'net_ftp_host'};
    }
    else {
      require Net::Netrc;

      my $rc = Net::Netrc-&gt;lookup(${*$ftp}{'net_ftp_firewall'});

      my ($fwuser, $fwpass, $fwacct) = $rc ? $rc-&gt;lpa() : ();

      if ($fwtype == 5) {
        $user = join('@', $user, $fwuser, ${*$ftp}{'net_ftp_host'});
        $pass = $pass . '@' . $fwpass;
      }
      else {
        if ($fwtype == 2) {
          $user .= '@' . ${*$ftp}{'net_ftp_host'};
        }
        elsif ($fwtype == 6) {
          $fwuser .= '@' . ${*$ftp}{'net_ftp_host'};
        }

        $ok = $ftp-&gt;_USER($fwuser);

        return 0 unless $ok == CMD_OK || $ok == CMD_MORE;

        $ok = $ftp-&gt;_PASS($fwpass || "");

        return 0 unless $ok == CMD_OK || $ok == CMD_MORE;

        $ok = $ftp-&gt;_ACCT($fwacct)
          if defined($fwacct);

        if ($fwtype == 3) {
          $ok = $ftp-&gt;command("SITE", ${*$ftp}{'net_ftp_host'})-&gt;response;
        }
        elsif ($fwtype == 4) {
          $ok = $ftp-&gt;command("OPEN", ${*$ftp}{'net_ftp_host'})-&gt;response;
        }

        return 0 unless $ok == CMD_OK || $ok == CMD_MORE;
      }
    }
  }

  $ok = $ftp-&gt;_USER($user);

  # Some dumb firewalls don't prefix the connection messages
  $ok = $ftp-&gt;response()
    if ($ok == CMD_OK &amp;&amp; $ftp-&gt;code == 220 &amp;&amp; $user =~ /\@/);

  if ($ok == CMD_MORE) {
    unless (defined $pass) {
      require Net::Netrc;

      my $rc = Net::Netrc-&gt;lookup(${*$ftp}{'net_ftp_host'}, $ruser);

      ($ruser, $pass, $acct) = $rc-&gt;lpa()
        if ($rc);

      $pass = '-anonymous@'
        if (!defined $pass &amp;&amp; (!defined($ruser) || $ruser =~ /^anonymous/o));
    }

    $ok = $ftp-&gt;_PASS($pass || "");
  }

  $ok = $ftp-&gt;_ACCT($acct)
    if (defined($acct) &amp;&amp; ($ok == CMD_MORE || $ok == CMD_OK));

  if ($fwtype == 7 &amp;&amp; $ok == CMD_OK &amp;&amp; defined ${*$ftp}{'net_ftp_firewall'}) {
    my ($f, $auth, $resp) = _auth_id($ftp);
    $ftp-&gt;authorize($auth, $resp) if defined($resp);
  }

  $ok == CMD_OK;
}


sub account {
  @_ == 2 or croak 'usage: $ftp-&gt;account($acct)';
  my $ftp  = shift;
  my $acct = shift;
  $ftp-&gt;_ACCT($acct) == CMD_OK;
}


sub _auth_id {
  my ($ftp, $auth, $resp) = @_;

  unless (defined $resp) {
    require Net::Netrc;

    $auth ||= eval { (getpwuid($&gt;))[0] } || $ENV{NAME};

    my $rc = Net::Netrc-&gt;lookup(${*$ftp}{'net_ftp_firewall'}, $auth)
      || Net::Netrc-&gt;lookup(${*$ftp}{'net_ftp_firewall'});

    ($auth, $resp) = $rc-&gt;lpa()
      if ($rc);
  }
  ($ftp, $auth, $resp);
}


sub authorize {
  @_ &gt;= 1 || @_ &lt;= 3 or croak 'usage: $ftp-&gt;authorize([$auth[, $resp]])';

  my ($ftp, $auth, $resp) = &amp;_auth_id;

  my $ok = $ftp-&gt;_AUTH($auth || "");

  return $ftp-&gt;_RESP($resp || "")
    if ($ok == CMD_MORE);

  $ok == CMD_OK;
}


sub rename {
  @_ == 3 or croak 'usage: $ftp-&gt;rename($oldname, $newname)';

  my ($ftp, $oldname, $newname) = @_;

  $ftp-&gt;_RNFR($oldname)
    &amp;&amp; $ftp-&gt;_RNTO($newname);
}


sub type {
  my $ftp    = shift;
  my $type   = shift;
  my $oldval = ${*$ftp}{'net_ftp_type'};

  return $oldval
    unless (defined $type);

  return
    unless ($ftp-&gt;_TYPE($type, @_));

  ${*$ftp}{'net_ftp_type'} = join(" ", $type, @_);

  $oldval;
}


sub alloc {
  my $ftp    = shift;
  my $size   = shift;
  my $oldval = ${*$ftp}{'net_ftp_allo'};

  return $oldval
    unless (defined $size);

  return
    unless ($ftp-&gt;supported("ALLO") and $ftp-&gt;_ALLO($size, @_));

  ${*$ftp}{'net_ftp_allo'} = join(" ", $size, @_);

  $oldval;
}


sub abort {
  my $ftp = shift;

  send($ftp, pack("CCC", TELNET_IAC, TELNET_IP, TELNET_IAC), MSG_OOB);

  $ftp-&gt;command(pack("C", TELNET_DM) . "ABOR");

  ${*$ftp}{'net_ftp_dataconn'}-&gt;close()
    if defined ${*$ftp}{'net_ftp_dataconn'};

  $ftp-&gt;response();

  $ftp-&gt;status == CMD_OK;
}


sub get {
  my ($ftp, $remote, $local, $where) = @_;

  my ($loc, $len, $buf, $resp, $data);
  local *FD;

  my $localfd = ref($local) || ref(\$local) eq "GLOB";

  ($local = $remote) =~ s#^.*/##
    unless (defined $local);

  croak("Bad remote filename '$remote'\n")
    if $remote =~ /[\r\n]/s;

  ${*$ftp}{'net_ftp_rest'} = $where if defined $where;
  my $rest = ${*$ftp}{'net_ftp_rest'};

  delete ${*$ftp}{'net_ftp_port'};
  delete ${*$ftp}{'net_ftp_pasv'};

  $data = $ftp-&gt;retr($remote)
    or return;

  if ($localfd) {
    $loc = $local;
  }
  else {
    $loc = \*FD;

    unless (sysopen($loc, $local, O_CREAT | O_WRONLY | ($rest ? O_APPEND: O_TRUNC))) {
      carp "Cannot open Local file $local: $!\n";
      $data-&gt;abort;
      return;
    }
  }

  if ($ftp-&gt;type eq 'I' &amp;&amp; !binmode($loc)) {
    carp "Cannot binmode Local file $local: $!\n";
    $data-&gt;abort;
    close($loc) unless $localfd;
    return;
  }

  $buf = '';
  my ($count, $hashh, $hashb, $ref) = (0);

  ($hashh, $hashb) = @$ref
    if ($ref = ${*$ftp}{'net_ftp_hash'});

  my $blksize = ${*$ftp}{'net_ftp_blksize'};
  local $\;    # Just in case

  while (1) {
    last unless $len = $data-&gt;read($buf, $blksize);

    if (EBCDIC &amp;&amp; $ftp-&gt;type ne 'I') {
      $buf = $ftp-&gt;toebcdic($buf);
      $len = length($buf);
    }

    if ($hashh) {
      $count += $len;
      print $hashh "#" x (int($count / $hashb));
      $count %= $hashb;
    }
    unless (print $loc $buf) {
      carp "Cannot write to Local file $local: $!\n";
      $data-&gt;abort;
      close($loc)
        unless $localfd;
      return;
    }
  }

  print $hashh "\n" if $hashh;

  unless ($localfd) {
    unless (close($loc)) {
      carp "Cannot close file $local (perhaps disk space) $!\n";
      return;
    }
  }

  unless ($data-&gt;close())    # implied $ftp-&gt;response
  {
    carp "Unable to close datastream";
    return;
  }

  return $local;
}


sub cwd {
  @_ == 1 || @_ == 2 or croak 'usage: $ftp-&gt;cwd([$dir])';

  my ($ftp, $dir) = @_;

  $dir = "/" unless defined($dir) &amp;&amp; $dir =~ /\S/;

  $dir eq ".."
    ? $ftp-&gt;_CDUP()
    : $ftp-&gt;_CWD($dir);
}


sub cdup {
  @_ == 1 or croak 'usage: $ftp-&gt;cdup()';
  $_[0]-&gt;_CDUP;
}


sub pwd {
  @_ == 1 || croak 'usage: $ftp-&gt;pwd()';
  my $ftp = shift;

  $ftp-&gt;_PWD();
  $ftp-&gt;_extract_path;
}

# rmdir( $ftp, $dir, [ $recurse ] )
#
# Removes $dir on remote host via FTP.
# $ftp is handle for remote host
#
# If $recurse is TRUE, the directory and deleted recursively.
# This means all of its contents and subdirectories.
#
# Initial version contributed by Dinkum Software
#
sub rmdir {
  @_ == 2 || @_ == 3 or croak('usage: $ftp-&gt;rmdir($dir[, $recurse])');

  # Pick off the args
  my ($ftp, $dir, $recurse) = @_;
  my $ok;

  return $ok
    if $ok = $ftp-&gt;_RMD($dir)
    or !$recurse;

  # Try to delete the contents
  # Get a list of all the files in the directory, excluding the current and parent directories
  my @filelist = map { /^(?:\S+;)+ (.+)$/ ? ($1) : () } grep { !/^(?:\S+;)*type=[cp]dir;/i } $ftp-&gt;_list_cmd("MLSD", $dir);

  # Fallback to using the less well-defined NLST command if MLSD fails
  @filelist = grep { !/^\.{1,2}$/ } $ftp-&gt;ls($dir)
    unless @filelist;

  return
    unless @filelist;    # failed, it is probably not a directory

  return $ftp-&gt;delete($dir)
    if @filelist == 1 and $dir eq $filelist[0];

  # Go thru and delete each file or the directory
  foreach my $file (map { m,/, ? $_ : "$dir/$_" } @filelist) {
    next                 # successfully deleted the file
      if $ftp-&gt;delete($file);

    # Failed to delete it, assume its a directory
    # Recurse and ignore errors, the final rmdir() will
    # fail on any errors here
    return $ok
      unless $ok = $ftp-&gt;rmdir($file, 1);
  }

  # Directory should be empty
  # Try to remove the directory again
  # Pass results directly to caller
  # If any of the prior deletes failed, this
  # rmdir() will fail because directory is not empty
  return $ftp-&gt;_RMD($dir);
}


sub restart {
  @_ == 2 || croak 'usage: $ftp-&gt;restart($where)';

  my ($ftp, $where) = @_;

  ${*$ftp}{'net_ftp_rest'} = $where;

  return;
}


sub mkdir {
  @_ == 2 || @_ == 3 or croak 'usage: $ftp-&gt;mkdir($dir[, $recurse])';

  my ($ftp, $dir, $recurse) = @_;

  $ftp-&gt;_MKD($dir) || $recurse
    or return;

  my $path = $dir;

  unless ($ftp-&gt;ok) {
    my @path = split(m#(?=/+)#, $dir);

    $path = "";

    while (@path) {
      $path .= shift @path;

      $ftp-&gt;_MKD($path);

      $path = $ftp-&gt;_extract_path($path);
    }

    # If the creation of the last element was not successful, see if we
    # can cd to it, if so then return path

    unless ($ftp-&gt;ok) {
      my ($status, $message) = ($ftp-&gt;status, $ftp-&gt;message);
      my $pwd = $ftp-&gt;pwd;

      if ($pwd &amp;&amp; $ftp-&gt;cwd($dir)) {
        $path = $dir;
        $ftp-&gt;cwd($pwd);
      }
      else {
        undef $path;
      }
      $ftp-&gt;set_status($status, $message);
    }
  }

  $path;
}


sub delete {
  @_ == 2 || croak 'usage: $ftp-&gt;delete($filename)';

  $_[0]-&gt;_DELE($_[1]);
}


sub put        { shift-&gt;_store_cmd("stor", @_) }
sub put_unique { shift-&gt;_store_cmd("stou", @_) }
sub append     { shift-&gt;_store_cmd("appe", @_) }


sub nlst { shift-&gt;_data_cmd("NLST", @_) }
sub list { shift-&gt;_data_cmd("LIST", @_) }
sub retr { shift-&gt;_data_cmd("RETR", @_) }
sub stor { shift-&gt;_data_cmd("STOR", @_) }
sub stou { shift-&gt;_data_cmd("STOU", @_) }
sub appe { shift-&gt;_data_cmd("APPE", @_) }


sub _store_cmd {
  my ($ftp, $cmd, $local, $remote) = @_;
  my ($loc, $sock, $len, $buf);
  local *FD;

  my $localfd = ref($local) || ref(\$local) eq "GLOB";

  if (!defined($remote) and 'STOU' ne uc($cmd)) {
    croak 'Must specify remote filename with stream input'
      if $localfd;

    require File::Basename;
    $remote = File::Basename::basename($local);
  }
  if (defined ${*$ftp}{'net_ftp_allo'}) {
    delete ${*$ftp}{'net_ftp_allo'};
  }
  else {

    # if the user hasn't already invoked the alloc method since the last
    # _store_cmd call, figure out if the local file is a regular file(not
    # a pipe, or device) and if so get the file size from stat, and send
    # an ALLO command before sending the STOR, STOU, or APPE command.
    my $size = do { local $^W; -f $local &amp;&amp; -s _ };    # no ALLO if sending data from a pipe
    ${*$ftp}{'net_ftp_allo'} = $size if $size;
  }
  croak("Bad remote filename '$remote'\n")
    if defined($remote) and $remote =~ /[\r\n]/s;

  if ($localfd) {
    $loc = $local;
  }
  else {
    $loc = \*FD;

    unless (sysopen($loc, $local, O_RDONLY)) {
      carp "Cannot open Local file $local: $!\n";
      return;
    }
  }

  if ($ftp-&gt;type eq 'I' &amp;&amp; !binmode($loc)) {
    carp "Cannot binmode Local file $local: $!\n";
    return;
  }

  delete ${*$ftp}{'net_ftp_port'};
  delete ${*$ftp}{'net_ftp_pasv'};

  $sock = $ftp-&gt;_data_cmd($cmd, grep { defined } $remote)
    or return;

  $remote = ($ftp-&gt;message =~ /\w+\s*:\s*(.*)/)[0]
    if 'STOU' eq uc $cmd;

  my $blksize = ${*$ftp}{'net_ftp_blksize'};

  my ($count, $hashh, $hashb, $ref) = (0);

  ($hashh, $hashb) = @$ref
    if ($ref = ${*$ftp}{'net_ftp_hash'});

  while (1) {
    last unless $len = read($loc, $buf = "", $blksize);

    if (EBCDIC &amp;&amp; $ftp-&gt;type ne 'I') {
      $buf = $ftp-&gt;toascii($buf);
      $len = length($buf);
    }

    if ($hashh) {
      $count += $len;
      print $hashh "#" x (int($count / $hashb));
      $count %= $hashb;
    }

    my $wlen;
    unless (defined($wlen = $sock-&gt;write($buf, $len)) &amp;&amp; $wlen == $len) {
      $sock-&gt;abort;
      close($loc)
        unless $localfd;
      print $hashh "\n" if $hashh;
      return;
    }
  }

  print $hashh "\n" if $hashh;

  close($loc)
    unless $localfd;

  $sock-&gt;close()
    or return;

  if ('STOU' eq uc $cmd and $ftp-&gt;message =~ m/unique\s+file\s*name\s*:\s*(.*)\)|"(.*)"/) {
    require File::Basename;
    $remote = File::Basename::basename($+);
  }

  return $remote;
}


sub port {
    @_ == 1 || @_ == 2 or croak 'usage: $self-&gt;port([$port])';
    return _eprt('PORT',@_);
}

sub eprt {
  @_ == 1 || @_ == 2 or croak 'usage: $self-&gt;eprt([$port])';
  return _eprt('EPRT',@_);
}

sub _eprt {
  my ($cmd,$ftp,$port) = @_;
  delete ${*$ftp}{net_ftp_intern_port};
  unless ($port) {
    my $listen = ${*$ftp}{net_ftp_listen} ||= $IOCLASS-&gt;new(
      Listen    =&gt; 1,
      Timeout   =&gt; $ftp-&gt;timeout,
      LocalAddr =&gt; $ftp-&gt;sockhost,
      $family_key  =&gt; $ftp-&gt;sockdomain,
      can_ssl() ? (
        %{ ${*$ftp}{net_ftp_tlsargs} },
        SSL_startHandshake =&gt; 0,
      ):(),
    );
    ${*$ftp}{net_ftp_intern_port} = 1;
    my $fam = ($listen-&gt;sockdomain == AF_INET) ? 1:2;
    if ( $cmd eq 'EPRT' || $fam == 2 ) {
      $port = "|$fam|".$listen-&gt;sockhost."|".$listen-&gt;sockport."|";
      $cmd = 'EPRT';
    } else {
      my $p = $listen-&gt;sockport;
      $port = join(',',split(m{\.},$listen-&gt;sockhost),$p &gt;&gt; 8,$p &amp; 0xff);
    }
  } elsif (ref($port) eq 'ARRAY') {
    $port = join(',',split(m{\.},@$port[0]),@$port[1] &gt;&gt; 8,@$port[1] &amp; 0xff);
  }
  my $ok = $cmd eq 'EPRT' ? $ftp-&gt;_EPRT($port) : $ftp-&gt;_PORT($port);
  ${*$ftp}{net_ftp_port} = $port if $ok;
  return $ok;
}


sub ls  { shift-&gt;_list_cmd("NLST", @_); }
sub dir { shift-&gt;_list_cmd("LIST", @_); }


sub pasv {
  my $ftp = shift;
  @_ and croak 'usage: $ftp-&gt;port()';
  return $ftp-&gt;epsv if $ftp-&gt;sockdomain != AF_INET;
  delete ${*$ftp}{net_ftp_intern_port};

  if ( $ftp-&gt;_PASV &amp;&amp;
    $ftp-&gt;message =~ m{(\d+,\d+,\d+,\d+),(\d+),(\d+)} ) {
    my $port = 256 * $2 + $3;
    ( my $ip = $1 ) =~s{,}{.}g;
    return ${*$ftp}{net_ftp_pasv} = [ $ip,$port ];
  }
  return;
}

sub epsv {
  my $ftp = shift;
  @_ and croak 'usage: $ftp-&gt;epsv()';
  delete ${*$ftp}{net_ftp_intern_port};

  $ftp-&gt;_EPSV &amp;&amp; $ftp-&gt;message =~ m{\(([\x33-\x7e])\1\1(\d+)\1\)}
    ? ${*$ftp}{net_ftp_pasv} = [ $ftp-&gt;peerhost, $2 ]
    : undef;
}


sub unique_name {
  my $ftp = shift;
  ${*$ftp}{'net_ftp_unique'} || undef;
}


sub supported {
  @_ == 2 or croak 'usage: $ftp-&gt;supported($cmd)';
  my $ftp  = shift;
  my $cmd  = uc shift;
  my $hash = ${*$ftp}{'net_ftp_supported'} ||= {};

  return $hash-&gt;{$cmd}
    if exists $hash-&gt;{$cmd};

  return $hash-&gt;{$cmd} = 1
    if $ftp-&gt;feature($cmd);

  return $hash-&gt;{$cmd} = 0
    unless $ftp-&gt;_HELP($cmd);

  my $text = $ftp-&gt;message;
  if ($text =~ /following.+commands/i) {
    $text =~ s/^.*\n//;
    while ($text =~ /(\*?)(\w+)(\*?)/sg) {
      $hash-&gt;{"\U$2"} = !length("$1$3");
    }
  }
  else {
    $hash-&gt;{$cmd} = $text !~ /unimplemented/i;
  }

  $hash-&gt;{$cmd} ||= 0;
}

##
## Deprecated methods
##


sub lsl {
  carp "Use of Net::FTP::lsl deprecated, use 'dir'"
    if $^W;
  goto &amp;dir;
}


sub authorise {
  carp "Use of Net::FTP::authorise deprecated, use 'authorize'"
    if $^W;
  goto &amp;authorize;
}


##
## Private methods
##


sub _extract_path {
  my ($ftp, $path) = @_;

  # This tries to work both with and without the quote doubling
  # convention (RFC 959 requires it, but the first 3 servers I checked
  # didn't implement it).  It will fail on a server which uses a quote in
  # the message which isn't a part of or surrounding the path.
  $ftp-&gt;ok
    &amp;&amp; $ftp-&gt;message =~ /(?:^|\s)\"(.*)\"(?:$|\s)/
    &amp;&amp; ($path = $1) =~ s/\"\"/\"/g;

  $path;
}

##
## Communication methods
##


sub _dataconn {
  my $ftp = shift;
  my $pkg = "Net::FTP::" . $ftp-&gt;type;
  eval "require " . $pkg ## no critic (BuiltinFunctions::ProhibitStringyEval)
    or croak("cannot load $pkg required for type ".$ftp-&gt;type);
  $pkg =~ s/ /_/g;
  delete ${*$ftp}{net_ftp_dataconn};

  my $conn;
  my $pasv = ${*$ftp}{net_ftp_pasv};
  if ($pasv) {
    $conn = $pkg-&gt;new(
      PeerAddr  =&gt; $pasv-&gt;[0],
      PeerPort  =&gt; $pasv-&gt;[1],
      LocalAddr =&gt; ${*$ftp}{net_ftp_localaddr},
      $family_key =&gt; ${*$ftp}{net_ftp_domain},
      Timeout   =&gt; $ftp-&gt;timeout,
      can_ssl() ? (
        SSL_startHandshake =&gt; 0,
        $ftp-&gt;is_SSL ? (
          SSL_reuse_ctx =&gt; $ftp,
          SSL_verifycn_name =&gt; ${*$ftp}{net_ftp_tlsargs}{SSL_verifycn_name},
          # This will cause the use of SNI if supported by IO::Socket::SSL.
          $ftp-&gt;can_client_sni ? (
            SSL_hostname  =&gt; ${*$ftp}{net_ftp_tlsargs}{SSL_hostname}
          ):(),
        ) :( %{${*$ftp}{net_ftp_tlsargs}} ),
      ):(),
    ) or return;
  } elsif (my $listen =  delete ${*$ftp}{net_ftp_listen}) {
    $conn = $listen-&gt;accept($pkg) or return;
    $conn-&gt;timeout($ftp-&gt;timeout);
    close($listen);
  } else {
    croak("no listener in active mode");
  }

  if (( ${*$ftp}{net_ftp_tlsprot} || '') eq 'P') {
    if ($conn-&gt;connect_SSL) {
      # SSL handshake ok
    } else {
      carp("failed to ssl upgrade dataconn: $IO::Socket::SSL::SSL_ERROR");
      return;
    }
  }

  ${*$ftp}{net_ftp_dataconn} = $conn;
  ${*$conn} = "";
  ${*$conn}{net_ftp_cmd} = $ftp;
  ${*$conn}{net_ftp_blksize} = ${*$ftp}{net_ftp_blksize};
  return $conn;
}


sub _list_cmd {
  my $ftp = shift;
  my $cmd = uc shift;

  delete ${*$ftp}{'net_ftp_port'};
  delete ${*$ftp}{'net_ftp_pasv'};

  my $data = $ftp-&gt;_data_cmd($cmd, @_);

  return
    unless (defined $data);

  require Net::FTP::A;
  bless $data, "Net::FTP::A";    # Force ASCII mode

  my $databuf = '';
  my $buf     = '';
  my $blksize = ${*$ftp}{'net_ftp_blksize'};

  while ($data-&gt;read($databuf, $blksize)) {
    $buf .= $databuf;
  }

  my $list = [split(/\n/, $buf)];

  $data-&gt;close();

  if (EBCDIC) {
    for (@$list) { $_ = $ftp-&gt;toebcdic($_) }
  }

  wantarray
    ? @{$list}
    : $list;
}


sub _data_cmd {
  my $ftp   = shift;
  my $cmd   = uc shift;
  my $ok    = 1;
  my $where = delete ${*$ftp}{'net_ftp_rest'} || 0;
  my $arg;

  for my $arg (@_) {
    croak("Bad argument '$arg'\n")
      if $arg =~ /[\r\n]/s;
  }

  if ( ${*$ftp}{'net_ftp_passive'}
    &amp;&amp; !defined ${*$ftp}{'net_ftp_pasv'}
    &amp;&amp; !defined ${*$ftp}{'net_ftp_port'})
  {
    return unless defined $ftp-&gt;pasv;

    if ($where and !$ftp-&gt;_REST($where)) {
      my ($status, $message) = ($ftp-&gt;status, $ftp-&gt;message);
      $ftp-&gt;abort;
      $ftp-&gt;set_status($status, $message);
      return;
    }

    # first send command, then open data connection
    # otherwise the peer might not do a full accept (with SSL
    # handshake if PROT P)
    $ftp-&gt;command($cmd, @_);
    my $data = $ftp-&gt;_dataconn();
    if (CMD_INFO == $ftp-&gt;response()) {
      $data-&gt;reading
        if $data &amp;&amp; $cmd =~ /RETR|LIST|NLST|MLSD/;
      return $data;
    }
    $data-&gt;_close if $data;

    return;
  }

  $ok = $ftp-&gt;port
    unless (defined ${*$ftp}{'net_ftp_port'}
    || defined ${*$ftp}{'net_ftp_pasv'});

  $ok = $ftp-&gt;_REST($where)
    if $ok &amp;&amp; $where;

  return
    unless $ok;

  if ($cmd =~ /(STOR|APPE|STOU)/ and exists ${*$ftp}{net_ftp_allo} and
      $ftp-&gt;supported("ALLO"))
  {
    $ftp-&gt;_ALLO(delete ${*$ftp}{net_ftp_allo})
      or return;
  }

  $ftp-&gt;command($cmd, @_);

  return 1
    if (defined ${*$ftp}{'net_ftp_pasv'});

  $ok = CMD_INFO == $ftp-&gt;response();

  return $ok
    unless exists ${*$ftp}{'net_ftp_intern_port'};

  if ($ok) {
    my $data = $ftp-&gt;_dataconn();

    $data-&gt;reading
      if $data &amp;&amp; $cmd =~ /RETR|LIST|NLST|MLSD/;

    return $data;
  }


  close(delete ${*$ftp}{'net_ftp_listen'});

  return;
}

##
## Over-ride methods (Net::Cmd)
##


sub debug_text { $_[2] =~ /^(pass|resp|acct)/i ? "$1 ....\n" : $_[2]; }


sub command {
  my $ftp = shift;

  delete ${*$ftp}{'net_ftp_port'};
  $ftp-&gt;SUPER::command(@_);
}


sub response {
  my $ftp  = shift;
  my $code = $ftp-&gt;SUPER::response() || 5;    # assume 500 if undef

  delete ${*$ftp}{'net_ftp_pasv'}
    if ($code != CMD_MORE &amp;&amp; $code != CMD_INFO);

  $code;
}


sub parse_response {
  return ($1, $2 eq "-")
    if $_[1] =~ s/^(\d\d\d)([- ]?)//o;

  my $ftp = shift;

  # Darn MS FTP server is a load of CRAP !!!!
  # Expect to see undef here.
  return ()
    unless 0 + (${*$ftp}{'net_cmd_code'} || 0);

  (${*$ftp}{'net_cmd_code'}, 1);
}

##
## Allow 2 servers to talk directly
##


sub pasv_xfer_unique {
  my ($sftp, $sfile, $dftp, $dfile) = @_;
  $sftp-&gt;pasv_xfer($sfile, $dftp, $dfile, 1);
}


sub pasv_xfer {
  my ($sftp, $sfile, $dftp, $dfile, $unique) = @_;

  ($dfile = $sfile) =~ s#.*/##
    unless (defined $dfile);

  my $port = $sftp-&gt;pasv
    or return;

  $dftp-&gt;port($port)
    or return;

  return
    unless ($unique ? $dftp-&gt;stou($dfile) : $dftp-&gt;stor($dfile));

  unless ($sftp-&gt;retr($sfile) &amp;&amp; $sftp-&gt;response == CMD_INFO) {
    $sftp-&gt;retr($sfile);
    $dftp-&gt;abort;
    $dftp-&gt;response();
    return;
  }

  $dftp-&gt;pasv_wait($sftp);
}


sub pasv_wait {
  @_ == 2 or croak 'usage: $ftp-&gt;pasv_wait($non_pasv_server)';

  my ($ftp, $non_pasv_server) = @_;
  my ($file, $rin, $rout);

  vec($rin = '', fileno($ftp), 1) = 1;
  select($rout = $rin, undef, undef, undef);

  my $dres = $ftp-&gt;response();
  my $sres = $non_pasv_server-&gt;response();

  return
    unless $dres == CMD_OK &amp;&amp; $sres == CMD_OK;

  return
    unless $ftp-&gt;ok() &amp;&amp; $non_pasv_server-&gt;ok();

  return $1
    if $ftp-&gt;message =~ /unique file name:\s*(\S*)\s*\)/;

  return $1
    if $non_pasv_server-&gt;message =~ /unique file name:\s*(\S*)\s*\)/;

  return 1;
}


sub feature {
  @_ == 2 or croak 'usage: $ftp-&gt;feature($name)';
  my ($ftp, $name) = @_;

  my $feature = ${*$ftp}{net_ftp_feature} ||= do {
    my @feat;

    # Example response
    # 211-Features:
    #  MDTM
    #  REST STREAM
    #  SIZE
    # 211 End

    @feat = map { /^\s+(.*\S)/ } $ftp-&gt;message
      if $ftp-&gt;_FEAT;

    \@feat;
  };

  return grep { /^\Q$name\E\b/i } @$feature;
}


sub cmd { shift-&gt;command(@_)-&gt;response() }

########################################
#
# RFC959 + RFC2428 + RFC4217 commands
#


sub _ABOR { shift-&gt;command("ABOR")-&gt;response() == CMD_OK }
sub _ALLO { shift-&gt;command("ALLO", @_)-&gt;response() == CMD_OK }
sub _CDUP { shift-&gt;command("CDUP")-&gt;response() == CMD_OK }
sub _NOOP { shift-&gt;command("NOOP")-&gt;response() == CMD_OK }
sub _PASV { shift-&gt;command("PASV")-&gt;response() == CMD_OK }
sub _QUIT { shift-&gt;command("QUIT")-&gt;response() == CMD_OK }
sub _DELE { shift-&gt;command("DELE", @_)-&gt;response() == CMD_OK }
sub _CWD  { shift-&gt;command("CWD", @_)-&gt;response() == CMD_OK }
sub _PORT { shift-&gt;command("PORT", @_)-&gt;response() == CMD_OK }
sub _RMD  { shift-&gt;command("RMD", @_)-&gt;response() == CMD_OK }
sub _MKD  { shift-&gt;command("MKD", @_)-&gt;response() == CMD_OK }
sub _PWD  { shift-&gt;command("PWD", @_)-&gt;response() == CMD_OK }
sub _TYPE { shift-&gt;command("TYPE", @_)-&gt;response() == CMD_OK }
sub _RNTO { shift-&gt;command("RNTO", @_)-&gt;response() == CMD_OK }
sub _RESP { shift-&gt;command("RESP", @_)-&gt;response() == CMD_OK }
sub _MDTM { shift-&gt;command("MDTM", @_)-&gt;response() == CMD_OK }
sub _SIZE { shift-&gt;command("SIZE", @_)-&gt;response() == CMD_OK }
sub _HELP { shift-&gt;command("HELP", @_)-&gt;response() == CMD_OK }
sub _STAT { shift-&gt;command("STAT", @_)-&gt;response() == CMD_OK }
sub _FEAT { shift-&gt;command("FEAT", @_)-&gt;response() == CMD_OK }
sub _PBSZ { shift-&gt;command("PBSZ", @_)-&gt;response() == CMD_OK }
sub _PROT { shift-&gt;command("PROT", @_)-&gt;response() == CMD_OK }
sub _CCC  { shift-&gt;command("CCC", @_)-&gt;response() == CMD_OK }
sub _EPRT { shift-&gt;command("EPRT", @_)-&gt;response() == CMD_OK }
sub _EPSV { shift-&gt;command("EPSV", @_)-&gt;response() == CMD_OK }
sub _APPE { shift-&gt;command("APPE", @_)-&gt;response() == CMD_INFO }
sub _LIST { shift-&gt;command("LIST", @_)-&gt;response() == CMD_INFO }
sub _NLST { shift-&gt;command("NLST", @_)-&gt;response() == CMD_INFO }
sub _RETR { shift-&gt;command("RETR", @_)-&gt;response() == CMD_INFO }
sub _STOR { shift-&gt;command("STOR", @_)-&gt;response() == CMD_INFO }
sub _STOU { shift-&gt;command("STOU", @_)-&gt;response() == CMD_INFO }
sub _RNFR { shift-&gt;command("RNFR", @_)-&gt;response() == CMD_MORE }
sub _REST { shift-&gt;command("REST", @_)-&gt;response() == CMD_MORE }
sub _PASS { shift-&gt;command("PASS", @_)-&gt;response() }
sub _ACCT { shift-&gt;command("ACCT", @_)-&gt;response() }
sub _AUTH { shift-&gt;command("AUTH", @_)-&gt;response() }


sub _USER {
  my $ftp = shift;
  my $ok  = $ftp-&gt;command("USER", @_)-&gt;response();

  # A certain brain dead firewall :-)
  $ok = $ftp-&gt;command("user", @_)-&gt;response()
    unless $ok == CMD_MORE or $ok == CMD_OK;

  $ok;
}


sub _SMNT { shift-&gt;unsupported(@_) }
sub _MODE { shift-&gt;unsupported(@_) }
sub _SYST { shift-&gt;unsupported(@_) }
sub _STRU { shift-&gt;unsupported(@_) }
sub _REIN { shift-&gt;unsupported(@_) }


1;

__END__

=head1 NAME

Net::FTP - FTP Client class

=head1 SYNOPSIS

    use Net::FTP;

    $ftp = Net::FTP-&gt;new("some.host.name", Debug =&gt; 0)
      or die "Cannot connect to some.host.name: $@";

    $ftp-&gt;login("anonymous",'-anonymous@')
      or die "Cannot login ", $ftp-&gt;message;

    $ftp-&gt;cwd("/pub")
      or die "Cannot change working directory ", $ftp-&gt;message;

    $ftp-&gt;get("that.file")
      or die "get failed ", $ftp-&gt;message;

    $ftp-&gt;quit;

=head1 DESCRIPTION

C&lt;Net::FTP&gt; is a class implementing a simple FTP client in Perl as
described in RFC959.  It provides wrappers for the commonly used subset of the
RFC959 commands.
If L&lt;IO::Socket::IP&gt; or L&lt;IO::Socket::INET6&gt; is installed it also provides
support for IPv6 as defined in RFC2428.
And with L&lt;IO::Socket::SSL&gt; installed it provides support for implicit FTPS
and explicit FTPS as defined in RFC4217.

The Net::FTP class is a subclass of Net::Cmd and (depending on avaibility) of
IO::Socket::IP, IO::Socket::INET6 or IO::Socket::INET.

=head2 Overview

FTP stands for File Transfer Protocol.  It is a way of transferring
files between networked machines.  The protocol defines a client
(whose commands are provided by this module) and a server (not
implemented in this module).  Communication is always initiated by the
client, and the server responds with a message and a status code (and
sometimes with data).

The FTP protocol allows files to be sent to or fetched from the
server.  Each transfer involves a B&lt;local file&gt; (on the client) and a
B&lt;remote file&gt; (on the server).  In this module, the same file name
will be used for both local and remote if only one is specified.  This
means that transferring remote file C&lt;/path/to/file&gt; will try to put
that file in C&lt;/path/to/file&gt; locally, unless you specify a local file
name.

The protocol also defines several standard B&lt;translations&gt; which the
file can undergo during transfer.  These are ASCII, EBCDIC, binary,
and byte.  ASCII is the default type, and indicates that the sender of
files will translate the ends of lines to a standard representation
which the receiver will then translate back into their local
representation.  EBCDIC indicates the file being transferred is in
EBCDIC format.  Binary (also known as image) format sends the data as
a contiguous bit stream.  Byte format transfers the data as bytes, the
values of which remain the same regardless of differences in byte size
between the two machines (in theory - in practice you should only use
this if you really know what you're doing).  This class does not support
the EBCDIC or byte formats, and will default to binary instead if they
are attempted.

=head2 Class Methods

=over 4

=item C&lt;new([$host][, %options])&gt;

This is the constructor for a new Net::FTP object. C&lt;$host&gt; is the
name of the remote host to which an FTP connection is required.

C&lt;$host&gt; is optional. If C&lt;$host&gt; is not given then it may instead be
passed as the C&lt;Host&gt; option described below. 

C&lt;%options&gt; are passed in a hash like fashion, using key and value pairs.
Possible options are:

B&lt;Host&gt; - FTP host to connect to. It may be a single scalar, as defined for
the C&lt;PeerAddr&gt; option in L&lt;IO::Socket::INET&gt;, or a reference to
an array with hosts to try in turn. The L&lt;/host&gt; method will return the value
which was used to connect to the host.

B&lt;Firewall&gt; - The name of a machine which acts as an FTP firewall. This can be
overridden by an environment variable C&lt;FTP_FIREWALL&gt;. If specified, and the
given host cannot be directly connected to, then the
connection is made to the firewall machine and the string C&lt;@hostname&gt; is
appended to the login identifier. This kind of setup is also referred to
as an ftp proxy.

B&lt;FirewallType&gt; - The type of firewall running on the machine indicated by
B&lt;Firewall&gt;. This can be overridden by an environment variable
C&lt;FTP_FIREWALL_TYPE&gt;. For a list of permissible types, see the description of
ftp_firewall_type in L&lt;Net::Config&gt;.

B&lt;BlockSize&gt; - This is the block size that Net::FTP will use when doing
transfers. (defaults to 10240)

B&lt;Port&gt; - The port number to connect to on the remote machine for the
FTP connection

B&lt;SSL&gt; - If the connection should be done from start with SSL, contrary to later
upgrade with C&lt;starttls&gt;.

B&lt;SSL_*&gt; - SSL arguments which will be applied when upgrading the control or
data connection to SSL. You can use SSL arguments as documented in
L&lt;IO::Socket::SSL&gt;, but it will usually use the right arguments already.

B&lt;Timeout&gt; - Set a timeout value in seconds (defaults to 120)

B&lt;Debug&gt; - debug level (see the debug method in L&lt;Net::Cmd&gt;)

B&lt;Passive&gt; - If set to a non-zero value then all data transfers will
be done using passive mode. If set to zero then data transfers will be
done using active mode.  If the machine is connected to the Internet
directly, both passive and active mode should work equally well.
Behind most firewall and NAT configurations passive mode has a better
chance of working.  However, in some rare firewall configurations,
active mode actually works when passive mode doesn't.  Some really old
FTP servers might not implement passive transfers.  If not specified,
then the transfer mode is set by the environment variable
C&lt;FTP_PASSIVE&gt; or if that one is not set by the settings done by the
F&lt;libnetcfg&gt; utility.  If none of these apply then passive mode is
used.

B&lt;Hash&gt; - If given a reference to a file handle (e.g., C&lt;\*STDERR&gt;),
print hash marks (#) on that filehandle every 1024 bytes.  This
simply invokes the C&lt;hash()&gt; method for you, so that hash marks
are displayed for all transfers.  You can, of course, call C&lt;hash()&gt;
explicitly whenever you'd like.

B&lt;LocalAddr&gt; - Local address to use for all socket connections. This
argument will be passed to the super class, i.e. L&lt;IO::Socket::INET&gt;
or L&lt;IO::Socket::IP&gt;.

B&lt;Domain&gt; - Domain to use, i.e. AF_INET or AF_INET6. This
argument will be passed to the IO::Socket super class.
This can be used to enforce IPv4 even with L&lt;IO::Socket::IP&gt;
which would default to IPv6.
B&lt;Family&gt; is accepted as alternative name for B&lt;Domain&gt;.

If the constructor fails undef will be returned and an error message will
be in $@

=back

=head2 Object Methods

Unless otherwise stated all methods return either a I&lt;true&gt; or I&lt;false&gt;
value, with I&lt;true&gt; meaning that the operation was a success. When a method
states that it returns a value, failure will be returned as I&lt;undef&gt; or an
empty list.

C&lt;Net::FTP&gt; inherits from C&lt;Net::Cmd&gt; so methods defined in C&lt;Net::Cmd&gt; may
be used to send commands to the remote FTP server in addition to the methods
documented here.

=over 4

=item C&lt;login([$login[, $password[, $account]]])&gt;

Log into the remote FTP server with the given login information. If
no arguments are given then the C&lt;Net::FTP&gt; uses the C&lt;Net::Netrc&gt;
package to lookup the login information for the connected host.
If no information is found then a login of I&lt;anonymous&gt; is used.
If no password is given and the login is I&lt;anonymous&gt; then I&lt;anonymous@&gt;
will be used for password.

If the connection is via a firewall then the C&lt;authorize&gt; method will
be called with no arguments.

=item C&lt;starttls()&gt;

Upgrade existing plain connection to SSL.
The SSL arguments have to be given in C&lt;new&gt; already because they are needed for
data connections too.

=item C&lt;stoptls()&gt;

Downgrade existing SSL connection back to plain.
This is needed to work with some FTP helpers at firewalls, which need to see the
PORT and PASV commands and responses to dynamically open the necessary ports.
In this case C&lt;starttls&gt; is usually only done to protect the authorization.

=item C&lt;prot($level)&gt;

Set what type of data channel protection the client and server will be using.
Only C&lt;$level&gt;s "C" (clear) and "P" (private) are supported.

=item C&lt;host()&gt;

Returns the value used by the constructor, and passed to the IO::Socket super
class to connect to the host.

=item C&lt;account($acct)&gt;

Set a string identifying the user's account.

=item C&lt;authorize([$auth[, $resp]])&gt;

This is a protocol used by some firewall ftp proxies. It is used
to authorise the user to send data out.  If both arguments are not specified
then C&lt;authorize&gt; uses C&lt;Net::Netrc&gt; to do a lookup.

=item C&lt;site($args)&gt;

Send a SITE command to the remote server and wait for a response.

Returns most significant digit of the response code.

=item C&lt;ascii()&gt;

Transfer file in ASCII. CRLF translation will be done if required

=item C&lt;binary()&gt;

Transfer file in binary mode. No transformation will be done.

B&lt;Hint&gt;: If both server and client machines use the same line ending for
text files, then it will be faster to transfer all files in binary mode.

=item C&lt;type([$type])&gt;

Set or get if files will be transferred in ASCII or binary mode.

=item C&lt;rename($oldname, $newname)&gt;

Rename a file on the remote FTP server from C&lt;$oldname&gt; to C&lt;$newname&gt;. This
is done by sending the RNFR and RNTO commands.

=item C&lt;delete($filename)&gt;

Send a request to the server to delete C&lt;$filename&gt;.

=item C&lt;cwd([$dir])&gt;

Attempt to change directory to the directory given in C&lt;$dir&gt;.  If
C&lt;$dir&gt; is C&lt;".."&gt;, the FTP C&lt;CDUP&gt; command is used to attempt to
move up one directory. If no directory is given then an attempt is made
to change the directory to the root directory.

=item C&lt;cdup()&gt;

Change directory to the parent of the current directory.

=item C&lt;passive([$passive])&gt;

Set or get if data connections will be initiated in passive mode.

=item C&lt;pwd()&gt;

Returns the full pathname of the current directory.

=item C&lt;restart($where)&gt;

Set the byte offset at which to begin the next data transfer. Net::FTP simply
records this value and uses it when during the next data transfer. For this
reason this method will not return an error, but setting it may cause
a subsequent data transfer to fail.

=item C&lt;rmdir($dir[, $recurse])&gt;

Remove the directory with the name C&lt;$dir&gt;. If C&lt;$recurse&gt; is I&lt;true&gt; then
C&lt;rmdir&gt; will attempt to delete everything inside the directory.

=item C&lt;mkdir($dir[, $recurse])&gt;

Create a new directory with the name C&lt;$dir&gt;. If C&lt;$recurse&gt; is I&lt;true&gt; then
C&lt;mkdir&gt; will attempt to create all the directories in the given path.

Returns the full pathname to the new directory.

=item C&lt;alloc($size[, $record_size])&gt;

The alloc command allows you to give the ftp server a hint about the size
of the file about to be transferred using the ALLO ftp command. Some storage
systems use this to make intelligent decisions about how to store the file.
The C&lt;$size&gt; argument represents the size of the file in bytes. The
C&lt;$record_size&gt; argument indicates a maximum record or page size for files
sent with a record or page structure.

The size of the file will be determined, and sent to the server
automatically for normal files so that this method need only be called if
you are transferring data from a socket, named pipe, or other stream not
associated with a normal file.

=item C&lt;ls([$dir])&gt;

Get a directory listing of C&lt;$dir&gt;, or the current directory.

In an array context, returns a list of lines returned from the server. In
a scalar context, returns a reference to a list.

=item C&lt;dir([$dir])&gt;

Get a directory listing of C&lt;$dir&gt;, or the current directory in long format.

In an array context, returns a list of lines returned from the server. In
a scalar context, returns a reference to a list.

=item C&lt;get($remote_file[, $local_file[, $where]])&gt;

Get C&lt;$remote_file&gt; from the server and store locally. C&lt;$local_file&gt; may be
a filename or a filehandle. If not specified, the file will be stored in
the current directory with the same leafname as the remote file.

If C&lt;$where&gt; is given then the first C&lt;$where&gt; bytes of the file will
not be transferred, and the remaining bytes will be appended to
the local file if it already exists.

Returns C&lt;$local_file&gt;, or the generated local file name if C&lt;$local_file&gt;
is not given. If an error was encountered undef is returned.

=item C&lt;put($local_file[, $remote_file])&gt;

Put a file on the remote server. C&lt;$local_file&gt; may be a name or a filehandle.
If C&lt;$local_file&gt; is a filehandle then C&lt;$remote_file&gt; must be specified. If
C&lt;$remote_file&gt; is not specified then the file will be stored in the current
directory with the same leafname as C&lt;$local_file&gt;.

Returns C&lt;$remote_file&gt;, or the generated remote filename if C&lt;$remote_file&gt;
is not given.

B&lt;NOTE&gt;: If for some reason the transfer does not complete and an error is
returned then the contents that had been transferred will not be remove
automatically.

=item C&lt;put_unique($local_file[, $remote_file])&gt;

Same as put but uses the C&lt;STOU&gt; command.

Returns the name of the file on the server.

=item C&lt;append($local_file[, $remote_file])&gt;

Same as put but appends to the file on the remote server.

Returns C&lt;$remote_file&gt;, or the generated remote filename if C&lt;$remote_file&gt;
is not given.

=item C&lt;unique_name()&gt;

Returns the name of the last file stored on the server using the
C&lt;STOU&gt; command.

=item C&lt;mdtm($file)&gt;

Returns the I&lt;modification time&gt; of the given file

=item C&lt;size($file)&gt;

Returns the size in bytes for the given file as stored on the remote server.

B&lt;NOTE&gt;: The size reported is the size of the stored file on the remote server.
If the file is subsequently transferred from the server in ASCII mode
and the remote server and local machine have different ideas about
"End Of Line" then the size of file on the local machine after transfer
may be different.

=item C&lt;supported($cmd)&gt;

Returns TRUE if the remote server supports the given command.

=item C&lt;hash([$filehandle_glob_ref[, $bytes_per_hash_mark]])&gt;

Called without parameters, or with the first argument false, hash marks
are suppressed.  If the first argument is true but not a reference to a 
file handle glob, then \*STDERR is used.  The second argument is the number
of bytes per hash mark printed, and defaults to 1024.  In all cases the
return value is a reference to an array of two:  the filehandle glob reference
and the bytes per hash mark.

=item C&lt;feature($name)&gt;

Determine if the server supports the specified feature. The return
value is a list of lines the server responded with to describe the
options that it supports for the given feature. If the feature is
unsupported then the empty list is returned.

  if ($ftp-&gt;feature( 'MDTM' )) {
    # Do something
  }

  if (grep { /\bTLS\b/ } $ftp-&gt;feature('AUTH')) {
    # Server supports TLS
  }

=back

The following methods can return different results depending on
how they are called. If the user explicitly calls either
of the C&lt;pasv&gt; or C&lt;port&gt; methods then these methods will
return a I&lt;true&gt; or I&lt;false&gt; value. If the user does not
call either of these methods then the result will be a
reference to a C&lt;Net::FTP::dataconn&gt; based object.

=over 4

=item C&lt;nlst([$dir])&gt;

Send an C&lt;NLST&gt; command to the server, with an optional parameter.

=item C&lt;list([$dir])&gt;

Same as C&lt;nlst&gt; but using the C&lt;LIST&gt; command

=item C&lt;retr($file)&gt;

Begin the retrieval of a file called C&lt;$file&gt; from the remote server.

=item C&lt;stor($file)&gt;

Tell the server that you wish to store a file. C&lt;$file&gt; is the
name of the new file that should be created.

=item C&lt;stou($file)&gt;

Same as C&lt;stor&gt; but using the C&lt;STOU&gt; command. The name of the unique
file which was created on the server will be available via the C&lt;unique_name&gt;
method after the data connection has been closed.

=item C&lt;appe($file)&gt;

Tell the server that we want to append some data to the end of a file
called C&lt;$file&gt;. If this file does not exist then create it.

=back

If for some reason you want to have complete control over the data connection,
this includes generating it and calling the C&lt;response&gt; method when required,
then the user can use these methods to do so.

However calling these methods only affects the use of the methods above that
can return a data connection. They have no effect on methods C&lt;get&gt;, C&lt;put&gt;,
C&lt;put_unique&gt; and those that do not require data connections.

=over 4

=item C&lt;port([$port])&gt;

=item C&lt;eprt([$port])&gt;

Send a C&lt;PORT&gt; (IPv4) or C&lt;EPRT&gt; (IPv6) command to the server. If C&lt;$port&gt; is
specified then it is sent to the server. If not, then a listen socket is created
and the correct information sent to the server.

=item C&lt;pasv()&gt;

=item C&lt;epsv()&gt;

Tell the server to go into passive mode (C&lt;pasv&gt; for IPv4, C&lt;epsv&gt; for IPv6).
Returns the text that represents the port on which the server is listening, this
text is in a suitable form to send to another ftp server using the C&lt;port&gt; or
C&lt;eprt&gt; method.

=back

The following methods can be used to transfer files between two remote
servers, providing that these two servers can connect directly to each other.

=over 4

=item C&lt;pasv_xfer($src_file, $dest_server[, $dest_file ])&gt;

This method will do a file transfer between two remote ftp servers. If
C&lt;$dest_file&gt; is omitted then the leaf name of C&lt;$src_file&gt; will be used.

=item C&lt;pasv_xfer_unique($src_file, $dest_server[, $dest_file ])&gt;

Like C&lt;pasv_xfer&gt; but the file is stored on the remote server using
the STOU command.

=item C&lt;pasv_wait($non_pasv_server)&gt;

This method can be used to wait for a transfer to complete between a passive
server and a non-passive server. The method should be called on the passive
server with the C&lt;Net::FTP&gt; object for the non-passive server passed as an
argument.

=item C&lt;abort()&gt;

Abort the current data transfer.

=item C&lt;quit()&gt;

Send the QUIT command to the remote FTP server and close the socket connection.

=back

=head2 Methods for the Adventurous

=over 4

=item C&lt;quot($cmd[, $args])&gt;

Send a command, that Net::FTP does not directly support, to the remote
server and wait for a response.

Returns most significant digit of the response code.

B&lt;WARNING&gt; This call should only be used on commands that do not require
data connections. Misuse of this method can hang the connection.

=item C&lt;can_inet6()&gt;

Returns whether we can use IPv6.

=item C&lt;can_ssl()&gt;

Returns whether we can use SSL.

=back

=head2 The dataconn Class

Some of the methods defined in C&lt;Net::FTP&gt; return an object which will
be derived from the C&lt;Net::FTP::dataconn&gt; class. See L&lt;Net::FTP::dataconn&gt; for
more details.

=head2 Unimplemented

The following RFC959 commands have not been implemented:

=over 4

=item C&lt;SMNT&gt;

Mount a different file system structure without changing login or
accounting information.

=item C&lt;HELP&gt;

Ask the server for "helpful information" (that's what the RFC says) on
the commands it accepts.

=item C&lt;MODE&gt;

Specifies transfer mode (stream, block or compressed) for file to be
transferred.

=item C&lt;SYST&gt;

Request remote server system identification.

=item C&lt;STAT&gt;

Request remote server status.

=item C&lt;STRU&gt;

Specifies file structure for file to be transferred.

=item C&lt;REIN&gt;

Reinitialize the connection, flushing all I/O and account information.

=back

=head1 EXAMPLES

For an example of the use of Net::FTP see

=over 4

=item L&lt;https://www.csh.rit.edu/~adam/Progs/&gt;

C&lt;autoftp&gt; is a program that can retrieve, send, or list files via
the FTP protocol in a non-interactive manner.

=back

=head1 EXPORTS

I&lt;None&gt;.

=head1 KNOWN BUGS

See L&lt;https://rt.cpan.org/Dist/Display.html?Status=Active&amp;Queue=libnet&gt;.

=head2 Reporting Bugs

When reporting bugs/problems please include as much information as possible.
It may be difficult for me to reproduce the problem as almost every setup
is different.

A small script which yields the problem will probably be of help. It would
also be useful if this script was run with the extra options C&lt;&lt; Debug =&gt; 1 &gt;&gt;
passed to the constructor, and the output sent with the bug report. If you
cannot include a small script then please include a Debug trace from a
run of your program which does yield the problem.

=head1 SEE ALSO

L&lt;Net::Netrc&gt;,
L&lt;Net::Cmd&gt;,
L&lt;IO::Socket::SSL&gt;;

L&lt;ftp(1)&gt;,
L&lt;ftpd(8)&gt;;

L&lt;https://www.ietf.org/rfc/rfc959.txt&gt;,
L&lt;https://www.ietf.org/rfc/rfc2428.txt&gt;,
L&lt;https://www.ietf.org/rfc/rfc4217.txt&gt;.

=head1 ACKNOWLEDGEMENTS

Henry Gabryjelski E&lt;lt&gt;L&lt;henryg@WPI.EDU|mailto:henryg@WPI.EDU&gt;E&lt;gt&gt; - for the
suggestion of creating directories recursively.

Nathan Torkington E&lt;lt&gt;L&lt;gnat@frii.com|mailto:gnat@frii.com&gt;E&lt;gt&gt; - for some
input on the documentation.

Roderick Schertler E&lt;lt&gt;L&lt;roderick@gate.net|mailto:roderick@gate.net&gt;E&lt;gt&gt; - for
various inputs

=head1 AUTHOR

Graham Barr E&lt;lt&gt;L&lt;gbarr@pobox.com|mailto:gbarr@pobox.com&gt;E&lt;gt&gt;.

Steve Hay E&lt;lt&gt;L&lt;shay@cpan.org|mailto:shay@cpan.org&gt;E&lt;gt&gt; is now maintaining
libnet as of version 1.22_02.

=head1 COPYRIGHT

Copyright (C) 1995-2004 Graham Barr.  All rights reserved.

Copyright (C) 2013-2017, 2020 Steve Hay.  All rights reserved.

=head1 LICENCE

This module is free software; you can redistribute it and/or modify it under the
same terms as Perl itself, i.e. under the terms of either the GNU General Public
License or the Artistic License, as specified in the F&lt;LICENCE&gt; file.

=head1 VERSION

Version 3.13

=head1 DATE

23 Dec 2020

=head1 HISTORY

See the F&lt;Changes&gt; file.

=cut
</pre></body></html>