Example #1
0
        /// <summary>
        /// Initialize the server and listen for incoming connections
        /// </summary>
        /// 
        /// <param name="Address">The servers IP Address</param>
        /// <param name="Port">The servers Port number</param>
        /// <param name="Async">Listen on a non-blocking TCP connection</param>
        /// 
        /// <exception cref="CryptoNetworkingException">Thrown if a socket error is returned</exception>
        public void Listen(IPAddress Address, int Port, bool Async = true)
        {
            _clientSocket = new TcpSocket();
            _clientSocket.Connected += new TcpSocket.ConnectedDelegate(OnServerConnected);
            _clientSocket.DataReceived += new TcpSocket.DataReceivedDelegate(OnDataReceived);

            try
            {
                if (Async)
                    _clientSocket.ListenAsync(Address, Port);
                else
                    _clientSocket.Listen(Address, Port);
            }
            catch (Exception ex)
            {
                if (SessionError != null)
                    SessionError(this, new DtmErrorEventArgs(new CryptoSocketException("DtmKex:Listen", "The server received a socket error!", ex), DtmErrorSeverity.Connection));
            }
        }
Example #2
0
        /// <summary>
        /// Disconnect from the remote host and teardown the connection
        /// </summary>
        public void Disconnect()
        {
            _isEstablished = false;
            _isDisconnecting = true;
            // stop sending keepalives
            StopPulse();

            try
            {
                if (_clientSocket.IsConnected)
                {
                    Transmit(DtmPacketTypes.Service, (short)DtmServiceFlags.Terminate, 0, null, true);
                    _clientSocket.TcpStream.Flush();
                    _clientSocket.Close();
                }
            }
            catch { }

            try
            {
                if (_clientSocket != null)
                {
                    _clientSocket.Dispose();
                    _clientSocket = null;
                }
                if (_evtSendWait != null)
                {
                    _evtSendWait.Dispose();
                    _evtSendWait = null;
                }
                if (_rcvBuffer != null)
                {
                    _rcvBuffer.Dispose();
                    _rcvBuffer = null;
                }
                if (_sndBuffer != null)
                {
                    _sndBuffer.Dispose();
                    _sndBuffer = null;
                }
            }
            catch { }

            try
            {
                TearDown();
            }
            catch (Exception ex)
            {
                if (SessionError != null)
                    SessionError(this, new DtmErrorEventArgs(new CryptoProcessingException("DtmKex:Disconnect", "The tear down operation experienced an error!", ex), DtmErrorSeverity.Warning));
            }
            finally
            {
                if (Disconnected != null)
                    Disconnected(this);
            }
        }
Example #3
0
        public void StartSend(IPAddress Address, int Port, string FilePath)
        {
            // store the path
            _filePath = FilePath;
            // start listening on the port
            _clientSocket = new TcpSocket();
            // use the DataReceived callback
            _clientSocket.DataReceived += new TcpSocket.DataReceivedDelegate(OnDataReceived);
            _clientSocket.Connected += new TcpSocket.ConnectedDelegate(OnConnected);
            // non blocking listen
            _clientSocket.ListenAsync(Address, Port);

            if (!_clientSocket.IsConnected)
            {
                // connect attempt failed
                throw new CryptoSocketException("DtmFileTransfer:BeginSendFile", "Could not connect to the remote host!", new SocketException((int)SocketError.ConnectionAborted));
            }
            else
            {
                // connection established
                _clientSocket.ReceiveBufferSize = _bufferSize;
                _clientSocket.SendBufferSize = _bufferSize;
                _isConnected = true;
            }
        }
Example #4
0
        /// <summary>
        /// Connect to a server and begin the key exchange
        /// </summary>
        /// 
        /// <param name="Address">The servers IP Address</param>
        /// <param name="Port">The servers Port number</param>
        /// <param name="Async">Connect on a non-blocking TCP channel</param>
        /// 
        /// <exception cref="CryptoNetworkingException">Thrown if a socket error is returned</exception>
        public void Connect(IPAddress Address, int Port, bool Async = true)
        {
            // create the connection
            _clientSocket = new TcpSocket();
            _clientSocket.Connected += new TcpSocket.ConnectedDelegate(OnClientConnected);
            _clientSocket.DataReceived += new TcpSocket.DataReceivedDelegate(OnDataReceived);

            try
            {
                if (Async)
                    _clientSocket.ConnectAsync(Address, Port);
                else
                    _clientSocket.Connect(Address, Port);
            }
            catch (Exception ex)
            {
                if (SessionError != null)
                    SessionError(this, new DtmErrorEventArgs(new CryptoSocketException("DtmKex:Connect", "The connection attempt has failed", ex), DtmErrorSeverity.Connection));
            }
        }
Example #5
0
        public void StartReceive(IPAddress Address, int Port, string FilePath)
        {
            // store destination path
            _filePath = FilePath;
            // initialize the file socket
            _clientSocket = new TcpSocket();
            // use the FileDataReceived callback
            _clientSocket.DataReceived += new TcpSocket.DataReceivedDelegate(OnDataReceived);
            _clientSocket.Connected += new TcpSocket.ConnectedDelegate(OnConnected);
            _clientSocket.DisConnected += new TcpSocket.DisConnectedDelegate(OnDisConnected);
            _clientSocket.Connect(Address, Port);

            if (!_clientSocket.IsConnected)
            {
                // connect attempt failed
                throw new CryptoSocketException("DtmFileTransfer:StartReceive", "Could not connect to the remote host!", new SocketException((int)SocketError.ConnectionAborted));
            }
            else
            {
                // create the temp file  note: is WriteThrough more secure here?
                _tempPath = Path.Combine(Path.GetDirectoryName(_filePath), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()) + ".tmp");
                using (new FileStream(_tempPath, FileMode.Create, FileAccess.Write, FileShare.Read)) { }
                // set to hidden to avoid cross process errors
                File.SetAttributes(_tempPath, File.GetAttributes(_tempPath) | FileAttributes.Hidden);
                _clientSocket.ReceiveBufferSize = _bufferSize;
                _clientSocket.SendBufferSize = _bufferSize;
                // start receiving
                _clientSocket.ReceiveAsync();
                _clientSocket.ReceiveTimeout = -1;
                // connection established
                _isConnected = true;
            }
        }