Пример #1
0
            /// <summary>
            /// Initializes a new instance of the PerDirection class.
            /// </summary>
            /// <param name="forwarder">
            /// The forwarder object to which this instance belongs.
            /// </param>
            /// <param name="from">The connection to read from.</param>
            /// <param name="to">The connection to write to.</param>
            public PerDirection(Forwarder forwarder, Stream from, Stream to, DiagnosticsHelper wd)
            {
                this.wd = wd;
                #region ENTER
                using (AutoEnterExitTrace aeet = new AutoEnterExitTrace(wd, wd.WebTrace, "Forwarder_PerDirection_ctor()"))
                {
                    #endregion ENTER

                    aeet.WriteDiagnosticInfo(System.Diagnostics.TraceEventType.Information, TraceEventID.traceFlow, "PerDirection Forwarder object: {0} was created for streams:  from:{1} to:{2}", this.GetHashCode(), from.GetHashCode(), to.GetHashCode());

                    this.forwarder      = forwarder;
                    this.inbound        = from;
                    this.outbound       = to;
                    this.buffer         = new byte[1500];
                    this.streamBufState = new StreamBufferState();
                    this.streamBufState.SetBuffer(this.buffer, 0, this.buffer.Length);

                    // -
                    // Start things going by issuing a receive on the inbound side.
                    // -
                    this.StartReceive();
                    #region LEAVE
                }
                #endregion LEAVE
            }
            /// <summary>
            /// Initializes a new instance of the PerDirection class.
            /// </summary>
            /// <param name="forwarder">
            /// The forwarder object to which this instance belongs.
            /// </param>
            /// <param name="from">The connection to read from.</param>
            /// <param name="to">The connection to write to.</param>
            public PerDirection(Forwarder forwarder, Stream from, Stream to, DiagnosticsHelper wd)
            {
                this.wd = wd;
#region ENTER
using (AutoEnterExitTrace aeet = new AutoEnterExitTrace(wd, wd.WebTrace, "Forwarder_PerDirection_ctor()"))
{
#endregion ENTER

                aeet.WriteDiagnosticInfo(System.Diagnostics.TraceEventType.Information, TraceEventID.traceFlow, "PerDirection Forwarder object: {0} was created for streams:  from:{1} to:{2}", this.GetHashCode(), from.GetHashCode(), to.GetHashCode());
                
                this.forwarder = forwarder;
                this.inbound = from;
                this.outbound = to;
                this.buffer = new byte[1500];
                this.streamBufState = new StreamBufferState();
                this.streamBufState.SetBuffer(this.buffer, 0, this.buffer.Length);

                // -
                // Start things going by issuing a receive on the inbound side.
                // -
                this.StartReceive();
#region LEAVE
}
#endregion LEAVE
            }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the ServiceConnection class.
        /// </summary>
        /// <param name="connected">
        /// The socket for the new connection.
        /// </param>
        /// <param name="token">
        /// The token for this client of the gatekeeper service,
        /// or zero if this is the registration connection.
        /// </param>
        /// <param name="handler">
        /// The handler routine to call upon forwarding a connection.
        /// </param>
        public ServiceConnection(
            string serverHost,
            Socket connected,
            uint token,
            ForwardingHandler handler,
            VLogger logger)
        {
            this.useSecureStream = true;

            // -
            // Set up the connection state.
            // -
            this.socket        = connected;
            this.netstream     = new NetworkStream(this.socket, true /*ownSocket*/);
            this.sslServerHost = serverHost;
            if (this.useSecureStream)
            {
                this.sslStream = new SslStream(
                    this.netstream,
                    false /* leaveInnerStreamOpen */,
                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                    null
                    );
                // The server name must match the name on the server certificate.
                try
                {
                    sslStream.AuthenticateAsClient(this.sslServerHost);
                }
                catch (Exception e)
                {
                    logger.Log("Exception: {0}", e.Message);
                    if (e.InnerException != null)
                    {
                        logger.Log("Inner exception: {0}", e.InnerException.Message);
                    }
                    logger.Log("Authentication failed - closing the connection.");
                    this.ShutdownAndClose();
                    return;
                }
            }
            this.clientToken          = token;
            this.handler              = handler;
            this.forwarding           = false;
            this.identifier           = HomeOS.Shared.Gatekeeper.Settings.HomeId;
            this.simpleAuthentication = HomeOS.Shared.Gatekeeper.Settings.HomePassword;

            this.logger = logger;
//#if false
            //-
            //We use keep-alives on the home <-> cloud service
            //connection in an attempt to prevent NAT/firewall
            //state from timing out and dropping our connection.
            //-
            StaticUtilities.SetKeepAlive(this.socket, 120000, 1000);
//#endif

            // -
            // Prepare our buffer space and asynchronous state holder.
            // This is currently just a simplistic single buffer system.
            // Note that this code assumes that the buffer is larger than
            // the largest possible single message (currently 257 bytes).
            // -
            this.buffer       = new byte[1500];
            this.bufferOffset = 0;

            this.streamBufState = new StreamBufferState();
            this.streamBufState.SetBuffer(this.buffer, 0, this.buffer.Length);

            // -
            // Start the dialog with our peer.
            // -
            this.AppendMessage(
                MessageType.Version,
                ServiceConnection.ProtocolVersion);
            this.SendMessage();
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the ServiceConnection class.
        /// </summary>
        /// <param name="connected">
        /// The socket for the new connection.
        /// </param>
        /// <param name="token">
        /// The token for this client of the gatekeeper service,
        /// or zero if this is the registration connection.
        /// </param>
        /// <param name="handler">
        /// The handler routine to call upon forwarding a connection.
        /// </param>
        public ServiceConnection(
            string serverHost,
            Socket connected,
            uint token,
            ForwardingHandler handler, 
            VLogger logger)
        {
            this.useSecureStream = true;

            // -
            // Set up the connection state.
            // -
            this.socket = connected;
            this.netstream = new NetworkStream(this.socket, true /*ownSocket*/);
            this.sslServerHost = serverHost;
            if (this.useSecureStream)
            {
                this.sslStream = new SslStream(
                                    this.netstream,
                                    false /* leaveInnerStreamOpen */,
                                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                                    null
                                    );
                // The server name must match the name on the server certificate.
                try
                {
                    sslStream.AuthenticateAsClient(this.sslServerHost);
                }
                catch (Exception e)
                {
                    logger.Log("Exception: {0}", e.Message);
                    if (e.InnerException != null)
                    {
                        logger.Log("Inner exception: {0}", e.InnerException.Message);
                    }
                    logger.Log("Authentication failed - closing the connection.");
                    this.ShutdownAndClose();
                    return;
                }
            }
            this.clientToken = token;
            this.handler = handler;
            this.forwarding = false;
            this.identifier = HomeOS.Shared.Gatekeeper.Settings.HomeId;
            this.simpleAuthentication = HomeOS.Shared.Gatekeeper.Settings.HomePassword;

            this.logger = logger;
            //#if false
             //-
             //We use keep-alives on the home <-> cloud service
             //connection in an attempt to prevent NAT/firewall
             //state from timing out and dropping our connection.
             //-
            StaticUtilities.SetKeepAlive(this.socket, 120000, 1000);
            //#endif

            // -
            // Prepare our buffer space and asynchronous state holder.
            // This is currently just a simplistic single buffer system.
            // Note that this code assumes that the buffer is larger than
            // the largest possible single message (currently 257 bytes).
            // -
            this.buffer = new byte[1500];
            this.bufferOffset = 0;

            this.streamBufState = new StreamBufferState();
            this.streamBufState.SetBuffer(this.buffer, 0, this.buffer.Length);

            // -
            // Start the dialog with our peer.
            // -
            this.AppendMessage(
                MessageType.Version,
                ServiceConnection.ProtocolVersion);
            this.SendMessage();
        }