예제 #1
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);
        }
예제 #2
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);
        }