Пример #1
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);
        }
Пример #2
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);
        }
Пример #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
 private RetryPacket(PacketVersion version,
                     PacketConnectionId destinationConnectionId,
                     PacketConnectionId sourceConnectionId,
                     PacketToken retryToken,
                     PacketRetryIntegrityTag retryIntegrityTag)
 {
     Version = version;
     DestinationConnectionId = destinationConnectionId;
     SourceConnectionId      = sourceConnectionId;
     RetryToken        = retryToken;
     RetryIntegrityTag = retryIntegrityTag;
 }
Пример #5
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;
 }
Пример #6
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));
        }