示例#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);
        }
示例#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);
        }
示例#3
0
        public static bool TryParse(ReadOnlyMemory <byte> bytes, out RetryPacket result)
        {
            result = new RetryPacket();

            if (bytes.IsEmpty)
            {
                return(false);
            }

            var firstByte = PacketFirstByte.Parse(bytes.Span[0]);

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

            var afterFirstByteBytes     = bytes.Slice(1);
            var version                 = PacketVersion.Parse(afterFirstByteBytes, out var afterVersionBytes);
            var destinationConnectionId = PacketConnectionId.Parse(afterVersionBytes, out var afterDestinationConnectionIdBytes);
            var sourceConnectionId      = PacketConnectionId.Parse(afterDestinationConnectionIdBytes, out var afterSourceConnectionIdBytes);
            var tag   = PacketRetryIntegrityTag.Parse(afterSourceConnectionIdBytes, out var beforeTagBytes);
            var token = new PacketToken(beforeTagBytes);

            result = new RetryPacket(version,
                                     destinationConnectionId,
                                     sourceConnectionId,
                                     token,
                                     tag);

            return(true);
        }
示例#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);
        }
示例#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);
        }
示例#6
0
 private RetryPacket(PacketVersion version,
                     PacketConnectionId destinationConnectionId,
                     PacketConnectionId sourceConnectionId,
                     PacketToken retryToken,
                     PacketRetryIntegrityTag retryIntegrityTag)
 {
     Version = version;
     DestinationConnectionId = destinationConnectionId;
     SourceConnectionId      = sourceConnectionId;
     RetryToken        = retryToken;
     RetryIntegrityTag = retryIntegrityTag;
 }
示例#7
0
 private HandshakePacket(PacketVersion version,
                         PacketConnectionId destinationConnectionId,
                         PacketConnectionId sourceConnectionId,
                         PacketNumber number,
                         MemoryBuffer payload)
 {
     Version = version;
     DestinationConnectionId = destinationConnectionId;
     SourceConnectionId      = sourceConnectionId;
     Number  = number;
     Payload = payload;
 }
示例#8
0
 private InitialPacket(PacketVersion version,
                       PacketConnectionId destinationConnectionId,
                       PacketConnectionId sourceConnectionId,
                       PacketToken token,
                       PacketNumber number,
                       MemoryBuffer payload)
 {
     Version = version;
     DestinationConnectionId = destinationConnectionId;
     SourceConnectionId      = sourceConnectionId;
     Token   = token;
     Number  = number;
     Payload = payload;
 }
示例#9
0
        public static PacketPayload.LongProtectedWritingContext StartProtectedWriting(IAead aead,
                                                                                      ICipher cipher,
                                                                                      MemoryCursor cursor,
                                                                                      PacketVersion version,
                                                                                      PacketConnectionId destinationConnectionId,
                                                                                      PacketConnectionId sourceConnectionId,
                                                                                      PacketNumber packetNumber,
                                                                                      PacketToken token)
        {
            var startPacketOffset = cursor.AsOffset();
            var firstByte         = new PacketFirstByte()
                                    .SetInitial()
                                    .SetMaxPacketNumberLength();

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

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