Example #1
0
        /// <summary>
        /// Connect to a VNC Host and determine which type of Authentication it uses. If the host uses Password Authentication, a call to Authenticate() will be required.
        /// </summary>
        /// <param name="host">The IP Address or Host Name of the VNC Host.</param>
        /// <param name="display">The Display number (used on Unix hosts).</param>
        /// <param name="port">The Port number used by the Host, usually 5900.</param>
        /// <param name="viewOnly">True if mouse/keyboard events are to be ignored.</param>
        /// <returns>Returns True if the VNC Host requires a Password to be sent after Connect() is called, otherwise False.</returns>
        public bool Connect(string host, int display, int port, bool viewOnly)
        {
            if (host == null) throw new ArgumentNullException("host");

            // If a diplay number is specified (used to connect to Unix servers)
            // it must be 0 or greater.  This gets added to the default port number
            // in order to determine where the server will be listening for connections.
            if (display < 0) throw new ArgumentOutOfRangeException("display", display, "Display number must be non-negative.");
            port += display;

            rfb = new RfbProtocol();

            if (viewOnly) {
                inputPolicy = new VncViewInputPolicy(rfb);
            } else {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try {
                rfb.Connect(host, port);
                rfb.ReadProtocolVersion();
                rfb.WriteProtocolVersion();

                // Figure out which type of authentication the server uses
                byte[] types = rfb.ReadSecurityTypes();

                // Based on what the server sends back in the way of supported Security Types, one of
                // two things will need to be done: either the server will reject the connection (i.e., type = 0),
                // or a list of supported types will be sent, of which we need to choose and use one.
                if (types.Length > 0) {
                    if (types[0] == 0) {
                        // The server is not able (or willing) to accept the connection.
                        // A message follows indicating why the connection was dropped.
                        throw new VncProtocolException("Connection Failed. The server rejected the connection for the following reason: " + rfb.ReadSecurityFailureReason());
                    } else {
                        securityType = GetSupportedSecurityType(types);
                        Debug.Assert(securityType > 0, "Unknown Security Type(s)", "The server sent one or more unknown Security Types.");

                        rfb.WriteSecurityType(securityType);

                        // Protocol 3.8 states that a SecurityResult is still sent when using NONE (see 6.2.1)
                        if (rfb.ServerVersion == 3.8f && securityType == 1) {
                            if (rfb.ReadSecurityResult() > 0) {
                                // For some reason, the server is not accepting the connection.  Get the
                                // reason and throw an exception
                                throw new VncProtocolException("Unable to Connecto to the Server. The Server rejected the connection for the following reason: " + rfb.ReadSecurityFailureReason());
                            }
                        }

                        return (securityType > 1) ? true : false;
                    }
                } else {
                    // Something is wrong, since we should have gotten at least 1 Security Type
                    throw new VncProtocolException("Protocol Error Connecting to Server. The Server didn't send any Security Types during the initial handshake.");
                }
            } catch (Exception e) {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }
Example #2
0
        /// <summary>
        ///     Connect to a VNC Host and determine which type of Authentication it uses. If the host uses Password Authentication,
        ///     a call to Authenticate() will be required.
        /// </summary>
        /// <param name="host">The IP Address or Host Name of the VNC Host.</param>
        /// <param name="display">The Display number (used on Unix hosts).</param>
        /// <param name="port">The Port number used by the Host, usually 5900.</param>
        /// <param name="viewOnly">True if mouse/keyboard events are to be ignored.</param>
        /// <returns>Returns True if the VNC Host requires a Password to be sent after Connect() is called, otherwise False.</returns>
        public bool Connect(string host, int display, int port, bool viewOnly)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            // If a diplay number is specified (used to connect to Unix servers)
            // it must be 0 or greater.  This gets added to the default port number
            // in order to determine where the server will be listening for connections.
            if (display < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(display), display, "Display number must be non-negative.");
            }
            port += display;

            rfb = new RfbProtocol();

            viewOnlyMode = viewOnly;
            if (viewOnly)
            {
                inputPolicy = new VncViewInputPolicy(rfb);
            }
            else
            {
                inputPolicy = new VncDefaultInputPolicy(rfb);
            }

            // Connect and determine version of server, and set client protocol version to match
            try
            {
                rfb.Connect(host, port);
                rfb.ReadProtocolVersion();

                // Handle possible repeater connection
                if (rfb.ServerVersion == 0.0)
                {
                    rfb.WriteProxyAddress();
                    // Now we are connected to the real server; read the protocol version of the
                    // server
                    rfb.ReadProtocolVersion();
                    // Resume normal handshake and protocol
                }

                rfb.WriteProtocolVersion();

                // Figure out which type of authentication the server uses
                var types = rfb.ReadSecurityTypes();

                // Based on what the server sends back in the way of supported Security Types, one of
                // two things will need to be done: either the server will reject the connection (i.e., type = 0),
                // or a list of supported types will be sent, of which we need to choose and use one.
                if (types.Length <= 0)
                {
                    // Something is wrong, since we should have gotten at least 1 Security Type
                    throw new VncProtocolException(
                              "Protocol Error Connecting to Server. The Server didn't send any Security Types during the initial handshake.");
                }
                if (types[0] == 0)
                {
                    // The server is not able (or willing) to accept the connection.
                    // A message follows indicating why the connection was dropped.
                    throw new VncProtocolException(
                              "Connection Failed. The server rejected the connection for the following reason: " +
                              rfb.ReadSecurityFailureReason());
                }

                securityType = GetSupportedSecurityType(types);
                Debug.Assert(securityType > 0, "Unknown Security Type(s)",
                             "The server sent one or more unknown Security Types.");

                rfb.WriteSecurityType(securityType);

                // Protocol 3.8 states that a SecurityResult is still sent when using NONE (see 6.2.1)
                if (rfb.ServerVersion != 3.8f || securityType != 1)
                {
                    return(securityType > 1);
                }
                if (rfb.ReadSecurityResult() > 0)
                {
                    // For some reason, the server is not accepting the connection.  Get the
                    // reason and throw an exception
                    throw new VncProtocolException(
                              "Unable to Connecto to the Server. The Server rejected the connection for the following reason: " +
                              rfb.ReadSecurityFailureReason());
                }

                return(securityType > 1);
            }
            catch (Exception e)
            {
                throw new VncProtocolException("Unable to connect to the server. Error was: " + e.Message, e);
            }
        }