public override unsafe bool Decrypt(NetIncomingMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            int unEncLenBits = (int)message.ReadUInt32();
            int byteLen      = NetBitWriter.BytesForBits(unEncLenBits);
            var result       = message.StoragePool.Rent(byteLen);

            // TODO: create stream that takes Memory<byte> overload
            fixed(byte *msgPtr = message.GetBuffer())
            {
                using var ms        = new UnmanagedMemoryStream(msgPtr + 4, message.ByteLength - 4);
                using var decryptor = Algorithm.CreateDecryptor();
                using var cs        = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
                cs.Read(result, 0, byteLen);
            }

            message.SetBuffer(result, true);
            message.BitLength   = unEncLenBits;
            message.BitPosition = 0;

            return(true);
        }
Exemple #2
0
        internal void Encode(Span <byte> destination, ref int offset, int sequenceNumber)
        {
            //  8 bits - NetMessageType
            //  1 bit  - Fragment?
            // 15 bits - Sequence number
            // 16 bits - Payload length in bits

            destination[offset++] = (byte)_messageType;
            destination[offset++] = (byte)((_fragmentGroup == 0 ? 0 : 1) | (sequenceNumber << 1));
            destination[offset++] = (byte)(sequenceNumber >> 7);

            if (_fragmentGroup == 0)
            {
                destination[offset++] = (byte)BitLength;
                destination[offset++] = (byte)(BitLength >> 8);

                int byteLen = NetBitWriter.BytesForBits(BitLength);
                GetBuffer().AsSpan(0, byteLen).CopyTo(destination[offset..]);
        private void WriteLocalHail(NetOutgoingMessage om)
        {
            if (LocalHailMessage == null)
            {
                return;
            }

            int bitsToAppend = LocalHailMessage.BitLength - LocalHailMessage.BitPosition;

            if (bitsToAppend > 0)
            {
                if (om.ByteLength + NetBitWriter.BytesForBits(bitsToAppend) > _peerConfiguration._maximumTransmissionUnit - 10)
                {
                    Peer.ThrowOrLog(
                        "Hail message too large; can maximally be " +
                        (_peerConfiguration._maximumTransmissionUnit - 10 - om.ByteLength));
                }

                om.Write(LocalHailMessage);
            }
        }
Exemple #4
0
        private void WriteLocalHail(NetOutgoingMessage om)
        {
            if (LocalHailMessage == null)
            {
                return;
            }

            int bitsToAppend = LocalHailMessage.BitLength - LocalHailMessage.BitPosition;

            if (bitsToAppend > 0)
            {
                int resultSize = om.ByteLength + NetBitWriter.BytesForBits(bitsToAppend);
                int maxMtuSize = _peerConfiguration._maximumTransmissionUnit - 10;
                if (resultSize > maxMtuSize)
                {
                    int maxHailSize = maxMtuSize - om.ByteLength;
                    Peer.LogError(NetLogMessage.FromValues(NetLogCode.HailMessageTooLarge, value: resultSize, maxValue: maxHailSize));
                }

                om.Write(LocalHailMessage);
            }
        }