Beispiel #1
0
        /// <summary>
        /// Handle an InitialPacket to create a new QuicConnection related to this packet.
        /// </summary>
        /// <param name="packet">The packet received</param>
        /// <param name="client">The client that sent the packet</param>
        private void HandleInitialPacket(InitialPacket packet, IPEndPoint client)
        {
            InitialPacket incomingPacket = packet as InitialPacket;

            incomingPacket.DecodeFrames();

            // Create random connection ID and use it as SCID for server -> client communications
            // Make sure it's not already in use
            byte[] connID = new byte[8];
            do
            {
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetBytes(connID);
            } while (_connectionPool.Find(connID) != null);

            QuicConnection qc = new QuicConnection(_server, client, connID, incomingPacket.SCID);

            _connectionPool.AddConnection(qc, connID);

            InitialPacket responsePacket = new InitialPacket(incomingPacket.SCID, connID, 0);

            responsePacket.AddFrame(new PaddingFrame());
            byte[] b = responsePacket.Encode();
            _server.Send(b, b.Length, client);
            Logger.Write($"Connection established. This is server {BitConverter.ToString(connID)} connected to client {BitConverter.ToString(incomingPacket.SCID)}");
        }
Beispiel #2
0
        /// <summary>
        /// Connect to a remote server.
        /// </summary>
        /// <param name="ip">Ip of the remote server</param>
        /// <param name="port">Port of the remote server</param>
        public void Connect(string ip, int port)
        {
            // Create random DCID and SCID
            byte[] DCID = new byte[8];
            byte[] SCID = new byte[8];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            rng.GetBytes(DCID);
            rng.GetBytes(SCID);

            // Create and send an InitialPacket to open a connection with the remote server
            InitialPacket initialPacket = new InitialPacket(DCID, SCID, _packetNumber++);

            byte[] byteInitialPacket = initialPacket.Encode();
            _client.Send(byteInitialPacket, byteInitialPacket.Length, ip, port);

            IPEndPoint server = null;
            Packet     packet = Packet.Unpack(_client.Receive(ref server));

            // Start the connection with an InitialPacket
            if (packet.GetType() == typeof(InitialPacket))
            {
                packet.DecodeFrames();
                Logger.Write($"Data received from server {server.Address}:{server.Port}");

                InitialPacket initPack = packet as InitialPacket;
                Logger.Write($"Connection established. This is client {BitConverter.ToString(initPack.DCID)} connected to server {BitConverter.ToString(initPack.SCID)}");
                _connection = new QuicConnection(_client, server, initPack.DCID, initPack.SCID);
            }

            // Background task to receive packets from the remote server
            _receiveToken = new CancellationTokenSource();
            _receiveTask  = Task.Run(() => Receive(server), _receiveToken.Token);
        }