Exemplo n.º 1
0
        private void OpenPassiveConnection()
        {
            FtpCommandResult rs = null;

            String[] pasv;

            rs = this.ExecutePasv();
            if (rs.StatusCode != FtpCommandResultCode.EnteringPassiveMode)
            {
                throw new FtpClientException(rs);
            }

            Int32 i1 = rs.Text.IndexOf('(') + 1;
            Int32 i2 = rs.Text.IndexOf(')') - i1;

            pasv = rs.Text.Substring(i1, i2).Split(',');
            if (pasv.Length < 6)
            {
                throw new FtpClientException(rs);
            }
            String server = String.Format("{0}.{1}.{2}.{3}", pasv[0], pasv[1], pasv[2], pasv[3]);
            Int32  port   = (Int32.Parse(pasv[4]) << 8) + Int32.Parse(pasv[5]);

            this._DataSocket = new SocketClient(server, port);
            this._DataSocket.Connect();
        }
Exemplo n.º 2
0
        private void OpenActiveConnection()
        {
            FtpCommandResult rs = null;

            this._ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            String ad = this.Socket.LocalEndPoint.ToString();
            Int32  ix = ad.IndexOf(':');

            if (ix < 0)
            {
                throw new FtpClientException("Failed to parse the local address: " + ad);
            }
            String sIPAddr = ad.Substring(0, ix);
            var    localEP = new IPEndPoint(IPAddress.Parse(sIPAddr), 0);

            this._ListenSocket.Bind(localEP);
            ad = this._ListenSocket.LocalEndPoint.ToString();
            ix = ad.IndexOf(':');
            if (ix < 0)
            {
                throw new FtpClientException("Failed to parse the local address: " + ad);
            }
            Int32 nPort = Int32.Parse(ad.Substring(ix + 1));

            this._ListenSocket.Listen(1);
            String port = String.Format("{0},{1},{2}", sIPAddr.Replace('.', ','), nPort / 256, nPort % 256);

            rs = this.ExecutePort(port);
            if (rs.StatusCode != FtpCommandResultCode.CommandOkey)
            {
                throw new FtpClientException(rs);
            }
        }
Exemplo n.º 3
0
        public FtpIPCommandResult(FtpCommandResult result)
        {
            this.StatusCode = result.StatusCode;
            this.Text = result.Text;

            var match = _pathRegex.Match(result.Text);
            this.IPInfo = match.Groups["Ip"].Value;
        }
        public FtpDirectoryCommandResult(FtpCommandResult result)
        {
            this.StatusCode = result.StatusCode;
            this.Text = result.Text;

            var match = _pathRegex.Match(result.Text);
            this.DirectoryPath = match.Groups["Path"].Value;
        }
Exemplo n.º 5
0
        public Int64 UploadFile(String localFilePath, String remoteFileName)
        {
            FtpCommandResult rs       = null;
            Int64            fileSize = 0;
            FileStream       stm      = null;

            if (this.EnsureOpen() == FtpConnectionState.Disconnected)
            {
                throw new FtpClientException("Connection must be opened");
            }
            if (this.State == FtpConnectionState.Connected)
            {
                if (this.Authenticate() == false)
                {
                    throw new FtpClientException("Authenticate failed");
                }
            }
            this.OpenDataSocket();

            rs = this.ExecuteType(this.DataType);
            this.OpenDataSocket();

            using (stm = new FileStream(localFilePath, FileMode.Open))
            {
                fileSize = stm.Length;

                if (this.Resume == true)
                {
                    Int64 size = this.GetFileSize(remoteFileName);
                    rs = this.ExecuteRest(size);

                    if (rs.StatusCode == FtpCommandResultCode.RequestedFileActionPendingFurtherInformation)
                    {
                        stm.Seek(size, SeekOrigin.Begin);
                    }
                }
                rs = this.ExecuteStor(remoteFileName);

                switch (rs.StatusCode)
                {
                case FtpCommandResultCode.DataConnectionAlreadyOpenTransferStarting:
                case FtpCommandResultCode.FileStatusOkayAboutToOpenDataConnection:
                    break;

                default:
                {
                    stm.Close();
                    throw new FtpClientException(rs);
                }
                }
                ConnectDataSocket();
                this._DataSocket.Send(stm);

                this.Disconnect();
            }
            return(fileSize);
        }
Exemplo n.º 6
0
        public FtpDirectoryCommandResult(FtpCommandResult result)
        {
            this.StatusCode = result.StatusCode;
            this.Text       = result.Text;

            var match = _pathRegex.Match(result.Text);

            this.DirectoryPath = match.Groups["Path"].Value;
        }
Exemplo n.º 7
0
        public FtpIPCommandResult(FtpCommandResult result)
        {
            this.StatusCode = result.StatusCode;
            this.Text       = result.Text;

            var match = _pathRegex.Match(result.Text);

            this.IPInfo = match.Groups["Ip"].Value;
        }
Exemplo n.º 8
0
        public FtpFileCommandResult(FtpCommandResult result)
        {
            this.StatusCode = result.StatusCode;
            this.Text = result.Text;

            var match = _pathRegex.Match(result.Text);
            if (!String.IsNullOrEmpty(match.Value))
            {
                this.NewFileName = match.Value + ".tmp";
            }
        }
Exemplo n.º 9
0
        public FtpFileCommandResult(FtpCommandResult result)
        {
            this.StatusCode = result.StatusCode;
            this.Text       = result.Text;

            var match = _pathRegex.Match(result.Text);

            if (!String.IsNullOrEmpty(match.Value))
            {
                this.NewFileName = match.Value + ".tmp";
            }
        }
Exemplo n.º 10
0
        private Int32?GetFileSize(FtpCommandResult result)
        {
            var rx = RegexList.FileSize;
            var m  = rx.Match(result.Text);

            if (m.Success == false)
            {
                return(null);
            }
            Int32 size = 0;

            if (Int32.TryParse(m.Groups["Size"].Value, out size) == true)
            {
                return(size);
            }
            return(null);
        }
Exemplo n.º 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List <FtpDirectory> ExecuteNlst(FtpEntryType type)
        {
            FtpCommandResult rs = null;

            this.EnsureOpen();
            this.OpenDataSocket();

            Byte[] bytes          = new Byte[512];
            List <FtpDirectory> l = new List <FtpDirectory>();

            rs = this.Execute("Nlst");

            switch (rs.StatusCode)
            {
            case FtpCommandResultCode.DataConnectionAlreadyOpenTransferStarting: break;

            case FtpCommandResultCode.FileStatusOkayAboutToOpenDataConnection: break;

            default:
            {
                CloseDataSocket();
                throw new FtpClientException(rs);
            }
            }
            this.ConnectDataSocket();
            String text = this._DataSocket.GetResponseText();

            text = this.GetResponseText();
            this.CloseDataSocket();

            rs = this.GetResponse();

            if (rs.StatusCode != FtpCommandResultCode.ClosingDataConnection)
            {
                throw new FtpClientException(rs);
            }
            StringReader sr = new StringReader(text);

            while (sr.Peek() > -1)
            {
                l.Add(new FtpDirectory(sr.ReadLine()));
            }
            return(l);
        }
Exemplo n.º 12
0
        /// <summary>
        /// 認証を行います。
        /// </summary>
        /// <returns></returns>
        public Boolean Authenticate()
        {
            FtpCommandResult rs = null;

            if (this.EnsureOpen() == FtpConnectionState.Connected)
            {
                rs = this.Execute("user " + this.UserName);
                if (rs.StatusCode == FtpCommandResultCode.UserNameOkey)
                {
                    rs = this.Execute("pass " + this.Password);
                    if (rs.StatusCode == FtpCommandResultCode.UserLoggedIn)
                    {
                        this.State = FtpConnectionState.Authenticated;
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 13
0
        public Int64 DownloadFile(String remoteFileName, String localFilePath)
        {
            FtpCommandResult rs   = null;
            FileStream       stm  = null;
            Int64            size = 0;

            if (this.EnsureOpen() == FtpConnectionState.Disconnected)
            {
                throw new FtpClientException("Connection must be opened");
            }
            if (this.State == FtpConnectionState.Connected)
            {
                if (this.Authenticate() == false)
                {
                    throw new FtpClientException("Authenticate failed");
                }
            }
            this.OpenDataSocket();

            rs = this.ExecuteType(this.DataType);

            OpenDataSocket();
            rs = this.ExecuteRetr(remoteFileName);
            switch (rs.StatusCode)
            {
            case FtpCommandResultCode.DataConnectionAlreadyOpenTransferStarting: break;

            case FtpCommandResultCode.FileStatusOkayAboutToOpenDataConnection: break;

            default: throw new FtpClientException(rs);
            }
            var fileSize = this.GetFileSize(rs);

            ConnectDataSocket();
            try
            {
                if (this.Resume == true && File.Exists(localFilePath) == true)
                {
                    stm = new FileStream(localFilePath, FileMode.Open);

                    rs = this.ExecuteRest(stm.Length);
                    if (rs.StatusCode != FtpCommandResultCode.RequestedFileActionPendingFurtherInformation)
                    {
                        throw new FtpClientException(rs);
                    }
                    stm.Seek(stm.Length, SeekOrigin.Begin);
                }
                else
                {
                    stm = new FileStream(localFilePath, FileMode.Create);
                }
                if (fileSize.HasValue)
                {
                    this.GetResponseStream(new FtpDataDownloadContext(stm, this.ResponseEncoding, fileSize.Value));
                }
                else
                {
                    this._DataSocket.GetResponseStream(stm);
                }
                size = stm.Length;
                stm.Flush();
            }
            finally
            {
                if (stm != null)
                {
                    stm.Dispose();
                }
            }
            this.Disconnect();

            return(size);
        }
Exemplo n.º 14
0
 /// <summary>
 ///
 /// </summary>
 public FtpClientException(FtpCommandResult result)
 {
     this.Result = result;
 }
Exemplo n.º 15
0
 /// <summary>
 /// 
 /// </summary>
 public FtpClientException(FtpCommandResult result)
 {
     this.Result = result;
 }