Ejemplo n.º 1
0
        public static PacketPayload.CursorWritingContext StartWriting(MemoryCursor cursor,
                                                                      PacketVersion version,
                                                                      PacketConnectionId destinationConnectionId,
                                                                      PacketConnectionId sourceConnectionId,
                                                                      PacketNumber packetNumber,
                                                                      PacketToken token)
        {
            var startOffset = cursor.AsOffset();
            var firstByte   = new PacketFirstByte()
                              .SetInitial()
                              .SetMaxPacketNumberLength();

            firstByte.Write(cursor);
            version.WriteBytes(cursor);
            destinationConnectionId.WriteBytes(cursor);
            sourceConnectionId.WriteBytes(cursor);
            token.WriteBytes(cursor);

            var context           = PacketPayload.StartPacketWriting(cursor, startOffset);
            var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor);

            packetNumber.Fill(packetNumberBytes.Span);

            return(context);
        }
Ejemplo n.º 2
0
        public static bool TryParseProtected(IAead aead, ICipher cipher, MemoryCursor cursor, out InitialPacket result)
        {
            result = new InitialPacket();

            var startOffset = cursor.AsOffset();
            var firstByte   = PacketFirstByte.Parse(cursor.Peek(1).Span[0]);

            if (!firstByte.IsInitialType())
            {
                return(false);
            }

            cursor.Move(1);

            var version = PacketVersion.Parse(cursor);
            var destinationConnectionId = PacketConnectionId.Parse(cursor);
            var sourceConnectionId      = PacketConnectionId.Parse(cursor);
            var token   = PacketToken.Parse(cursor);
            var payload = PacketPayload.SliceLongProtectedPacketBytes(cursor, aead, cipher, startOffset, firstByte, null, out var packetNumber);

            result = new InitialPacket(version,
                                       destinationConnectionId,
                                       sourceConnectionId,
                                       token,
                                       packetNumber,
                                       payload);

            return(true);
        }
Ejemplo n.º 3
0
        public static bool TryParseProtectedByConnectionId(IAead aead,
                                                           ICipher cipher,
                                                           MemoryCursor cursor,
                                                           PacketConnectionId connectionId,
                                                           PacketNumber largestAcknowledgedPacketNumber,
                                                           out ShortPacket result)
        {
            result = new ShortPacket();

            var startOffset = cursor.AsOffset();
            var firstByte   = PacketFirstByte.Parse(cursor.Peek(1).Span[0]);

            if (!firstByte.IsShortHeader())
            {
                return(false);
            }

            cursor.Move(1);

            if (!connectionId.TrySliceValue(cursor))
            {
                return(false);
            }

            var payload = PacketPayload.SliceShortProtectedPacketBytes(cursor, aead, cipher, startOffset, firstByte, largestAcknowledgedPacketNumber, out var packetNumber);

            result = new ShortPacket(connectionId, packetNumber, payload);

            return(true);
        }
Ejemplo n.º 4
0
        public static PacketPayload.CursorWritingContext StartWriting(MemoryCursor cursor,
                                                                      PacketVersion version,
                                                                      PacketConnectionId destinationConnectionId,
                                                                      PacketConnectionId sourceConnectionId,
                                                                      PacketNumber largestAcknowledgedPacketNumber,
                                                                      PacketNumber packetNumber)
        {
            var startOffset = cursor.AsOffset();
            var firstByte   = new PacketFirstByte()
                              .SetHandshake()
                              .SetMaxPacketNumberLength();

            firstByte.Write(cursor);
            version.WriteBytes(cursor);
            destinationConnectionId.WriteBytes(cursor);
            sourceConnectionId.WriteBytes(cursor);

            var context           = PacketPayload.StartPacketWriting(cursor, startOffset);
            var packetNumberBytes = firstByte.SlicePacketNumberBytes(cursor);

            packetNumber
            .EncodeByLargestAcknowledged(largestAcknowledgedPacketNumber)
            .Fill(packetNumberBytes.Span);

            return(context);
        }
Ejemplo n.º 5
0
        public static bool TryParse(MemoryCursor cursor, PacketNumber largestAcknowledgedPacketNumber, out HandshakePacket result)
        {
            result = new HandshakePacket();

            var startOffset = cursor.AsOffset();
            var firstByte   = PacketFirstByte.Parse(cursor.Peek(1).Span[0]);

            if (!firstByte.IsHandshakeType())
            {
                return(false);
            }

            cursor.Move(1);

            var version = PacketVersion.Parse(cursor);
            var destinationConnectionId = PacketConnectionId.Parse(cursor);
            var sourceConnectionId      = PacketConnectionId.Parse(cursor);
            var payload = PacketPayload.SlicePacketBytes(cursor, firstByte, startOffset, largestAcknowledgedPacketNumber, out var packetNumber);

            result = new HandshakePacket(version,
                                         destinationConnectionId,
                                         sourceConnectionId,
                                         packetNumber,
                                         payload);

            return(true);
        }
Ejemplo n.º 6
0
        public static PacketPayload.ShortProtectedWritingContext StartProtectedWriting(IAead aead,
                                                                                       ICipher cipher,
                                                                                       MemoryCursor cursor,
                                                                                       PacketConnectionId destinationConnectionId,
                                                                                       PacketNumber packetNumber,
                                                                                       PacketNumber largestAcknowledgedPacketNumber)
        {
            var startOffset = cursor.AsOffset();
            var firstByte   = new PacketFirstByte().SetShort();

            firstByte.Write(cursor);
            destinationConnectionId.WriteValueBytes(cursor);

            return(PacketPayload.StartShortProtectedPacketWriting(cursor, aead, cipher, startOffset, firstByte, packetNumber, largestAcknowledgedPacketNumber));
        }
Ejemplo n.º 7
0
        public static PacketPayload.LongProtectedWritingContext StartProtectedWriting(IAead aead,
                                                                                      ICipher cipher,
                                                                                      MemoryCursor cursor,
                                                                                      PacketVersion version,
                                                                                      PacketConnectionId destinationConnectionId,
                                                                                      PacketConnectionId sourceConnectionId,
                                                                                      PacketNumber packetNumber,
                                                                                      PacketNumber largestAcknowledgedPacketNumber)
        {
            var startPacketOffset = cursor.AsOffset();
            var firstByte         = new PacketFirstByte()
                                    .SetHandshake()
                                    .SetMaxPacketNumberLength();

            firstByte.Write(cursor);
            version.WriteBytes(cursor);
            destinationConnectionId.WriteBytes(cursor);
            sourceConnectionId.WriteBytes(cursor);

            return(PacketPayload.StartLongProtectedPacketWriting(cursor, aead, cipher, startPacketOffset, firstByte, packetNumber, largestAcknowledgedPacketNumber));
        }