public FtpResponse RunRetrCommand(
            FtpCommandId CommandId,
            Nullable <bool> OutNotFound, string CmdText,
            CommLog CommLog, FileStream InPcFileStream)
        {
            ReplyLines replyLines = null;
            object     rcvdData   = null;
            Socket     cSocket    = null;

            // return flag. file or dir is not found.
            if (OutNotFound != null)
            {
                OutNotFound = false;
            }

            try
            {
                // create the socket to receive on.
                cSocket = CreatePasvDataConnect(CommLog);

                // Send the FTP command,
                replyLines = SendCommand(CommandId, CmdText);

                if (replyLines.IncludesAnyReplyCode(new int[] { 125, 150 }) == false)
                {
                    throw new FtpException(CmdText, CommandId, replyLines);
                }

                // receive the file bytes on the PASV connection.
                while (true)
                {
                    Array.Clear(m_aBuffer, 0, m_aBuffer.Length);
                    m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0);
                    InPcFileStream.Write(m_aBuffer, 0, m_iBytes);

                    if (m_iBytes <= 0)
                    {
                        break;
                    }
                }
            }

            finally
            {
                if (cSocket != null)
                {
                    cSocket.Close();
                }
            }

            // receive back the response on the command connection after all file
            // bytes received on the PASV connection.
            {
                ReplyLines postReplyLines = ReadReply();
                if (postReplyLines.IncludesReplyCode(550) == true)
                {
                    if (OutNotFound == null)
                    {
                        throw new FtpException(CmdText, CommandId, postReplyLines);
                    }
                    else
                    {
                        OutNotFound = true;
                    }
                }
                else
                {
                    if (postReplyLines.IncludesAnyReplyCode(new int[] { 226, 250 }) == false)
                    {
                        string moreInfo =
                            "Unexpected FTP server reply following " +
                            "data channel disconnect. " +
                            postReplyLines.Lines.ToArray();
                        throw new FtpException(CmdText, CommandId, replyLines, moreInfo);
                    }
                }
            }

            return(new FtpResponse(CommandId, CommLog, CmdText, replyLines, rcvdData));
        }
Esempio n. 2
0
        public FtpResponse RunStorCommand(
            FtpCommandId CommandId,
            Nullable <bool> OutNotFound, string CmdText,
            CommLog CommLog, FileStream InPcFileStream)
        {
            ReplyLines replyLines = null;
            object     rcvdData   = null;
            Socket     cSocket    = null;

            // return flag. file or dir is not found.
            if (OutNotFound != null)
            {
                OutNotFound = false;
            }

            try
            {
                // create the socket to send on.
                cSocket = CreatePasvDataConnect(CommLog);

                // Send the FTP command,
                replyLines = SendCommand(CommandId, CmdText);

                // send the file bytes on the PASV channel.
                if (CommandId == FtpCommandId.STOR)
                {
                    byte[] buf = new byte[9999];
                    while (true)
                    {
                        int byteCx = InPcFileStream.Read(buf, 0, buf.Length);
                        if (byteCx == 0)
                        {
                            break;
                        }
                        cSocket.Send(buf, byteCx, 0);
                    }
                }
            }

            finally
            {
                if (cSocket != null)
                {
                    cSocket.Close();
                }
            }

            // receive back the response on the command connection after file bytes
            // sent on the data connection.
            if (CommandId == FtpCommandId.STOR)
            {
                ReplyLines postReplyLines = ReadReply();
                if (postReplyLines.IncludesReplyCode(550) == true)
                {
                    if (OutNotFound == null)
                    {
                        throw new FtpException(CmdText, CommandId, postReplyLines);
                    }
                    else
                    {
                        OutNotFound = true;
                    }
                }
                else
                {
                    if (postReplyLines.IncludesAnyReplyCode(new int[] { 226, 250 }) == false)
                    {
                        string moreInfo =
                            "Unexpected FTP server reply following " +
                            "data channel disconnect. " +
                            postReplyLines.Lines.ToArray();
                        throw new FtpException(CmdText, CommandId, replyLines, moreInfo);
                    }
                }
            }

            return(new FtpResponse(CommandId, CommLog, CmdText, replyLines, rcvdData));
        }
        public FtpResponse RunListCommand(FtpCommandId CommandId, string CmdText)
        {
            ReplyLines         replyLines = null;
            CommLog            commLog    = new CommLog();
            Socket             cSocket    = null;
            List <FtpDirEntry> listDire   = null;

            try
            {
                cSocket = CreatePasvDataConnect(commLog);

                // Send the FTP command,
                replyLines = SendCommand(CommandId, CmdText);

                // 550 = directory not found
                if (replyLines.IncludesReplyCode(550))
                {
                    throw new FtpException(
                              "Directory to list is not found.", CommandId, replyLines);
                }

                // the reply from the list command on the command connection should say
                // to look on the passive connection for the directory listing.
                var replyLine = replyLines.FindAnyLine(new int[] { 125, 150, 250 });
                if (replyLine == null)
                {
                    throw new FtpException(
                              "not expected response to LIST command", CommandId, replyLines);
                }

                // receive the directory listing on the passive connection.
                string[] rcvdLines = null;
                {
                    Int32  bytes;
                    char[] seperator = new char[] { '\n' };

                    StringBuilder sb = new StringBuilder();
                    while (true)
                    {
                        Array.Clear(m_aBuffer, 0, m_aBuffer.Length);
                        bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0);
                        sb.Append(ASCII.GetString(m_aBuffer, 0, bytes));
                        if (bytes == 0)
                        {
                            break;
                        }
                    }

                    rcvdLines = sb.ToString().Split(seperator);
                }

                // parse the directory listing
                if ((CommandId == FtpCommandId.LIST) || (CommandId == FtpCommandId.NLST))
                {
                    listDire = new List <FtpDirEntry>();
                    foreach (string line in rcvdLines)
                    {
                        if (line.Length > 0)
                        {
                            var dire = new FtpDirEntry(line);
                            listDire.Add(dire);
                        }
                    }
                }
            }

            finally
            {
                if (cSocket != null)
                {
                    cSocket.Close();
                }
            }

            // receive back the response on the command connection after dir listing
            // sent on the data connection.
            {
                ReplyLines postReplyLines = ReadReply( );
                if (postReplyLines.IncludesReplyCode(550) == true)
                {
                    throw new FtpException(
                              "Directory to list is not found", CommandId, postReplyLines);
                }
                else
                {
                    if (postReplyLines.IncludesAnyReplyCode(new int[] { 226, 250 }) == false)
                    {
                        string moreInfo =
                            "Unexpected FTP server reply following " +
                            "data channel disconnect. " +
                            postReplyLines.Lines.ToArray();
                        throw new FtpException(moreInfo, CommandId, replyLines);
                    }
                }
            }

            return(new FtpResponse_DirList(
                       CommandId,
                       commLog, CommandId.ToString(), replyLines, listDire));
        }