Beispiel #1
0
 /// <summary>
 /// Creates a <see cref="SecureTcpConnection"/> from the given <see cref="TcpClient"/>.
 /// </summary>
 /// <param name="tcpClient">The <see cref="TcpClient"/> to wrap.</param>
 /// <param name="rsaPair">The RSA key-pair to use.</param>
 /// <returns>The created <see cref="SecureTcpConnection"/>.</returns>
 /// <exception cref="ArgumentException">Thrown if the given <see cref="TcpClient"/>s socket is not connected.</exception>
 public static TcpConnection CreateSecureTcpConnection(TcpClient tcpClient, RSAPair rsaPair)
 {
     if (!tcpClient.Connected)
     {
         throw new ArgumentException("Socket is not connected.");
     }
     return(new SecureTcpConnection(rsaPair, tcpClient));
 }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SecureUdpConnection"/> class.
        /// </summary>
        /// <param name="udpClient">The <see cref="UdpClient"/> to use for sending and receiving data.</param>
        /// <param name="remoteEndPoint">The remote end point to connect to.</param>
        /// <param name="rsaPair">The local RSA key-pair.</param>
        /// <param name="writeLock">Whether the connection has a write lock.</param>
        internal SecureUdpConnection(UdpClient udpClient, IPEndPoint remoteEndPoint, RSAPair rsaPair, bool writeLock = false)
            : base(udpClient, remoteEndPoint, writeLock, skipInitializationProcess: true)
        {
            //Setup the RSAConnectionHelper object.
            RSAConnection        = new RSAConnection(this, rsaPair);
            PacketConverter      = base.PacketConverter;
            base.PacketConverter = RSAConnection;

            //Since we did skip the initialization,... DO IT!
            Init();
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SecureTcpConnection"/> class.
        /// </summary>
        /// <param name="rsaPair">The local RSA key-pair.</param>
        /// <param name="tcpClient">The <see cref="TcpClient"/> to use for sending and receiving data.</param>
        internal SecureTcpConnection(RSAPair rsaPair, TcpClient tcpClient)
            : base(tcpClient, skipInitializationProcess: true)
        {
            //Setup the RSAConnectionHelper object.
            RSAConnection        = new RSAConnection(this, rsaPair);
            PacketConverter      = base.PacketConverter;
            base.PacketConverter = RSAConnection;

            //Since we did skip the initialization,... DO IT!
            Init();
        }
Beispiel #4
0
 /// <summary>
 /// Creates a <see cref="SecureServerConnectionContainer"/> with the given IP address, listening on the given port.
 /// </summary>
 /// <param name="ipAddress">The IP address to run at.</param>
 /// <param name="port">The port to listen on.</param>
 /// <param name="rsaPair">The RSA key-pair to use.</param>
 /// <param name="start">Whether to start the server after instantiation.</param>
 /// <returns>The created <see cref="SecureServerConnectionContainer"/>.</returns>
 public static ServerConnectionContainer CreateSecureServerConnectionContainer(string ipAddress, int port, RSAPair rsaPair, bool start = true) =>
 new SecureServerConnectionContainer(ipAddress, port, rsaPair);
Beispiel #5
0
 /// <summary>
 /// Creates a <see cref="SecureServerConnectionContainer"/> listening on the given port.
 /// </summary>
 /// <param name="port">The port to listen on.</param>
 /// <param name="rsaPair">The RSA key-pair to use.</param>
 /// <param name="start">Whether to start the server after instantiation.</param>
 /// <returns>The created <see cref="SecureServerConnectionContainer"/>.</returns>
 public static ServerConnectionContainer CreateSecureServerConnectionContainer(int port, RSAPair rsaPair, bool start = true) => new SecureServerConnectionContainer(port, rsaPair, start);
Beispiel #6
0
        /// <summary>
        /// Creates a <see cref="SecureClientConnectionContainer"/> with the given <see cref="TcpConnection"/> and <see cref="UdpConnection"/>.
        /// </summary>
        /// <param name="tcpConnection">The <see cref="TcpConnection"/> to use.</param>
        /// <param name="udpConnection">The <see cref="UdpConnection"/> to use.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <returns>The created <see cref="SecureClientConnectionContainer"/>.</returns>
        /// <exception cref="ArgumentException">Thrown if the given <see cref="TcpConnection"/> is not connected.</exception>
        public static ClientConnectionContainer CreateSecureClientConnectionContainer(TcpConnection tcpConnection, UdpConnection udpConnection, RSAPair rsaPair)
        {
            if (tcpConnection == null || !tcpConnection.IsAlive)
            {
                throw new ArgumentException("TCP connection must be connected to an endpoint.");
            }

            var secureClientConnectionContainer = new SecureClientConnectionContainer(tcpConnection, udpConnection, rsaPair);

            secureClientConnectionContainer.Initialize();
            return(secureClientConnectionContainer);
        }
Beispiel #7
0
        /// <summary>
        /// Creates a <see cref="SecureClientConnectionContainer"/> and connects it to the given IP address and port.
        /// </summary>
        /// <param name="ipAddress">The IP address to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <returns>The created <see cref="SecureClientConnectionContainer"/>.</returns>
        public static ClientConnectionContainer CreateSecureClientConnectionContainer(string ipAddress, int port, RSAPair rsaPair)
        {
            var secureClientConnectionContainer = new SecureClientConnectionContainer(ipAddress, port, rsaPair);

            secureClientConnectionContainer.Initialize();
            return(secureClientConnectionContainer);
        }
Beispiel #8
0
        /// <summary>
        /// Asynchronously creates a <see cref="SecureUdpConnection"/> with the given parent <see cref="TcpConnection"/>.
        /// </summary>
        /// <param name="tcpConnection">The <see cref="TcpConnection"/> via which to connect the <see cref="SecureUdpConnection"/>.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation with the promise of a tuple holding the created
        /// <see cref="SecureUdpConnection"/> and <see cref="ConnectionResult"/> on completion.
        /// </returns>
        /// <exception cref="ArgumentException">The given <see cref="TcpConnection"/> isn't connected.</exception>
        public static async Task <Tuple <UdpConnection, ConnectionResult> > CreateSecureUdpConnectionAsync(TcpConnection tcpConnection, RSAPair rsaPair)
        {
            UdpConnection           udpConnection     = null;
            ConnectionResult        connectionResult  = ConnectionResult.Connected;
            CancellationTokenSource cancellationToken = new CancellationTokenSource();

            cancellationToken.CancelAfter(CONNECTION_TIMEOUT);
            if (tcpConnection == null || !tcpConnection.IsAlive)
            {
                return(new Tuple <UdpConnection, ConnectionResult>(udpConnection, ConnectionResult.TCPConnectionNotAlive));
            }
            tcpConnection.EstablishUdpConnection((localEndPoint, RemoteEndPoint) => udpConnection = new SecureUdpConnection(new UdpClient(localEndPoint), RemoteEndPoint, rsaPair));
            while (udpConnection == null && !cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(25);
            }
            if (udpConnection == null && cancellationToken.IsCancellationRequested)
            {
                connectionResult = ConnectionResult.Timeout;
            }
            return(new Tuple <UdpConnection, ConnectionResult>(udpConnection, connectionResult));
        }
Beispiel #9
0
        /// <summary>
        /// Creates a <see cref="SecureUdpConnection"/> with the given parent <see cref="TcpConnection"/>.
        /// </summary>
        /// <param name="tcpConnection">The <see cref="TcpConnection"/> via which to connect the <see cref="SecureUdpConnection"/>.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <param name="connectionResult">The connection result.</param>
        /// <returns>The created <see cref="SecureUdpConnection"/>.</returns>
        public static UdpConnection CreateSecureUdpConnection(TcpConnection tcpConnection, RSAPair rsaPair, out ConnectionResult connectionResult)
        {
            Tuple <UdpConnection, ConnectionResult> connectionRequest = CreateSecureUdpConnectionAsync(tcpConnection, rsaPair).Result;

            connectionResult = connectionRequest.Item2;
            return(connectionRequest.Item1);
        }
Beispiel #10
0
        /// <summary>
        /// Asynchronously creates a <see cref="SecureTcpConnection"/> and connects it to the given IP address and port.
        /// </summary>
        /// <param name="ipAddress">The IP address to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation with the promise of a tuple holding the created
        /// <see cref="SecureTcpConnection"/> and <see cref="ConnectionResult"/> on completion.
        /// </returns>
        public static async Task <Tuple <TcpConnection, ConnectionResult> > CreateSecureTcpConnectionAsync(string ipAddress, int port, RSAPair rsaPair)
        {
            TcpClient tcpClient   = new TcpClient();
            Task      timeoutTask = Task.Delay(CONNECTION_TIMEOUT);
            Task      connectTask = Task.Factory.StartNew(() => tcpClient.Connect(ipAddress, port));

            if (await Task.WhenAny(timeoutTask, connectTask).ConfigureAwait(false) != timeoutTask && tcpClient.Connected)
            {
                return(new Tuple <TcpConnection, ConnectionResult>(new SecureTcpConnection(rsaPair, tcpClient), ConnectionResult.Connected));
            }

            return(new Tuple <TcpConnection, ConnectionResult>(null, ConnectionResult.Timeout));
        }
Beispiel #11
0
        /// <summary>
        /// Creates a <see cref="SecureTcpConnection"/> and connects it to the given IP address and port.
        /// </summary>
        /// <param name="ipAddress">The IP address to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <param name="connectionResult">The connection result.</param>
        /// <returns>The created <see cref="SecureTcpConnection"/>.</returns>
        public static TcpConnection CreateSecureTcpConnection(string ipAddress, int port, RSAPair rsaPair, out ConnectionResult connectionResult)
        {
            Tuple <TcpConnection, ConnectionResult> tcpConnection = CreateSecureTcpConnectionAsync(ipAddress, port, rsaPair).Result;

            connectionResult = tcpConnection.Item2;
            return(tcpConnection.Item1);
        }
Beispiel #12
0
        /// <summary>
        /// Asynchronously creates a <see cref="SecureTcpConnection"/> and connects it to the given IP address and port.
        /// </summary>
        /// <param name="ipAddress">The IP address to connect to.</param>
        /// <param name="port">The port to connect to.</param>
        /// <param name="rsaPair">The RSA key-pair to use.</param>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation with the promise of a tuple holding the created
        /// <see cref="SecureTcpConnection"/> and <see cref="ConnectionResult"/> on completion.
        /// </returns>
        public static async Task <Tuple <TcpConnection, ConnectionResult> > CreateSecureTcpConnectionAsync(string ipAddress, int port, RSAPair rsaPair)
        {
            TcpClient tcpClient = new TcpClient(AddressFamily.InterNetworkV6);

            tcpClient.Client.DualMode = true;
            Task timeoutTask = Task.Delay(CONNECTION_TIMEOUT);
            Task connectTask = tcpClient.ConnectAsync(ipAddress, port);

            if (await Task.WhenAny(timeoutTask, connectTask).ConfigureAwait(false) != timeoutTask && tcpClient.Connected)
            {
                return(new Tuple <TcpConnection, ConnectionResult>(new SecureTcpConnection(rsaPair, tcpClient), ConnectionResult.Connected));
            }

            return(new Tuple <TcpConnection, ConnectionResult>(null, ConnectionResult.Timeout));
        }