예제 #1
0
        private Socket InitDataConnection()
#endif
        {
            FtpStatus status;

            if (usePassive)
            {
                status = SendCommand(PassiveCommand);
                if (status.StatusCode != FtpStatusCode.EnteringPassive)
                {
                    throw CreateExceptionFromResponse(status);
                }

                return(SetupPassiveConnection(status.StatusDescription));
            }

            // Open a socket to listen the server's connection
#if SSHARP
            CrestronListenerSocket sock = new CrestronListenerSocket(IPAddress.Any, 0);
#else
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
#endif
            try
            {
                sock.Bind(new IPEndPoint(localEndPoint.Address, 0));
                sock.Listen(1);                  // We only expect a connection from server
            }
            catch (SocketException e)
            {
                sock.Close();

                throw new WebException("Couldn't open listening socket on client", e);
            }

            IPEndPoint ep       = (IPEndPoint)sock.LocalEndPoint;
            string     ipString = ep.Address.ToString().Replace('.', ',');
            int        h1       = ep.Port >> 8; // ep.Port / 256
            int        h2       = ep.Port % 256;

            string portParam = ipString + "," + h1 + "," + h2;
            status = SendCommand(PortCommand, portParam);

            if (status.StatusCode != FtpStatusCode.CommandOK)
            {
                sock.Close();
                throw (CreateExceptionFromResponse(status));
            }

            return(sock);
        }
예제 #2
0
        private void OpenControlConnection()
        {
            Exception exception = null;

#if SSHARP
            CrestronClientSocket sock = null;
#else
            Socket sock = null;
#endif
            foreach (IPAddress address in hostEntry.AddressList)
            {
#if SSHARP
#if !IPV6
                if (address.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    continue;
                }
#endif

                sock = new CrestronClientSocket();
#else
                sock = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
#endif

                IPEndPoint remote = new IPEndPoint(address, requestUri.Port);

                if (!ServicePoint.CallEndPointDelegate(sock, remote))
                {
                    sock.Close();
                    sock = null;
                }
                else
                {
                    try
                    {
                        sock.Connect(remote);
                        localEndPoint = (IPEndPoint)sock.LocalEndPoint;
                        break;
                    }
                    catch (SocketException exc)
                    {
                        exception = exc;
                        sock.Close();
                        sock = null;
                    }
                }
            }

            // Couldn't connect to any address
            if (sock == null)
            {
                throw new WebException("Unable to connect to remote server", exception, WebExceptionStatus.UnknownError, ftpResponse);
            }

            controlStream = new NetworkStream(sock);
            controlReader = new StreamReader(controlStream, Encoding.ASCII);

            State = RequestState.Authenticating;

            Authenticate();
            FtpStatus status = SendCommand("OPTS", "utf8", "on");
            // ignore status for OPTS
            status       = SendCommand(WebRequestMethods.Ftp.PrintWorkingDirectory);
            initial_path = GetInitialPath(status);
        }
예제 #3
0
        private void OpenDataConnection()
        {
            FtpStatus status;

#if SSHARP
            object s = InitDataConnection();
#else
            Socket s = InitDataConnection();
#endif

            // Handle content offset
            if (offset > 0)
            {
                status = SendCommand(RestCommand, offset.ToString());
                if (status.StatusCode != FtpStatusCode.FileCommandPending)
                {
                    throw CreateExceptionFromResponse(status);
                }
            }

            if (method != WebRequestMethods.Ftp.ListDirectory && method != WebRequestMethods.Ftp.ListDirectoryDetails &&
                method != WebRequestMethods.Ftp.UploadFileWithUniqueName)
            {
                status = SendCommand(method, file_name);
            }
            else
            {
                status = SendCommand(method);
            }

            if (status.StatusCode != FtpStatusCode.OpeningData && status.StatusCode != FtpStatusCode.DataAlreadyOpen)
            {
                throw CreateExceptionFromResponse(status);
            }

            if (usePassive)
            {
#if SSHARP
                origDataStream = new NetworkStream((CrestronClientSocket)s, true);
#else
                origDataStream = new NetworkStream(s, true);
#endif
                dataStream = origDataStream;
                if (EnableSsl)
                {
                    ChangeToSSLSocket(ref dataStream);
                }
            }
            else
            {
                // Active connection (use Socket.Blocking to true)
#if SSHARP
                CrestronServerSocket incoming = null;
                try
                {
                    incoming = ((CrestronListenerSocket)s).Accept();
                }
#else
                Socket incoming = null;
                try
                {
                    incoming = s.Accept();
                }
#endif
                catch (SocketException)
                {
#if SSHARP
                    ((CrestronListenerSocket)s).Close();
#else
                    s.Close();
#endif
                    if (incoming != null)
                    {
                        incoming.Close();
                    }

                    throw new ProtocolViolationException("Server commited a protocol violation.");
                }

#if SSHARP
                ((CrestronListenerSocket)s).Close();
#else
                s.Close();
#endif
                origDataStream = new NetworkStream(incoming, true);
                dataStream     = origDataStream;
                if (EnableSsl)
                {
                    ChangeToSSLSocket(ref dataStream);
                }
            }

            ftpResponse.UpdateStatus(status);
        }
예제 #4
0
        // Probably we could do better having here a regex
        private Socket SetupPassiveConnection(string statusDescription)
        {
            // Current response string
            string response = statusDescription;

            if (response.Length < 4)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Look for first digit after code
            int i;

            for (i = 3; i < response.Length && !Char.IsDigit(response[i]); i++)
            {
                ;
            }
            if (i >= response.Length)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Get six elements
            string[] digits = response.Substring(i).Split(new char[] { ',' }, 6);
            if (digits.Length != 6)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Clean non-digits at the end of last element
            int j;

            for (j = digits[5].Length - 1; j >= 0 && !Char.IsDigit(digits[5][j]); j--)
            {
                ;
            }
            if (j < 0)
            {
                throw new WebException("Cannot open passive data connection");
            }

            digits[5] = digits[5].Substring(0, j + 1);

            IPAddress ip;

            try
            {
                ip = IPAddress.Parse(String.Join(".", digits, 0, 4));
            }
            catch (FormatException)
            {
                throw new WebException("Cannot open passive data connection");
            }

            // Get the port
            int p1, p2, port;

#if SSHARP
            if (!TryParsers.Int32TryParse(digits[4], out p1) || !TryParsers.Int32TryParse(digits[5], out p2))
#else
            if (!Int32.TryParse(digits[4], out p1) || !Int32.TryParse(digits[5], out p2))
#endif
            { throw new WebException("Cannot open passive data connection"); }

            port = (p1 << 8) + p2;             // p1 * 256 + p2
            //port = p1 * 256 + p2;
            if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
            {
                throw new WebException("Cannot open passive data connection");
            }

            IPEndPoint ep = new IPEndPoint(ip, port);
#if SSHARP
            CrestronClientSocket sock = new CrestronClientSocket();
#else
            Socket sock = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
#endif
            try
            {
                sock.Connect(ep);
            }
            catch (SocketException)
            {
                sock.Close();
                throw new WebException("Cannot open passive data connection");
            }

            return(sock);
        }