Ejemplo n.º 1
0
 internal void Unmask()
 {
     if (_mask != 0)
     {
         _mask = Mask.Unmask;
         _payloadData.Mask(_maskingKey);
         _maskingKey = new byte[0];
     }
 }
Ejemplo n.º 2
0
        private void maskPayloadData()
        {
            var key  = new byte[4];
            var rand = new Random();

            rand.NextBytes(key);

            MaskingKey = key;
            PayloadData.Mask(key);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        internal void Unmask()
        {
            if (_mask == Mask.Off)
            {
                return;
            }

            _mask = Mask.Off;
            _payloadData.Mask(_maskingKey);
            _maskingKey = WebSocket.EmptyBytes;
        }
Ejemplo n.º 4
0
        internal void Unmask()
        {
            if (_mask == Mask.Unmask)
            {
                return;
            }

            _mask = Mask.Unmask;
            _payloadData.Mask(_maskingKey);
            _maskingKey = new byte[0];
        }
Ejemplo n.º 5
0
        internal void Unmask()
        {
            if (_mask == Mask.Unmask)
            {
                return;
            }

            _mask = Mask.Unmask;
            _payloadData.Mask(_maskingKey);
            _maskingKey = Ext.EmptyByteArray;
        }
Ejemplo n.º 6
0
        internal WebSocketFrame(
            Fin fin,
            Opcode opcode,
            PayloadData payloadData,
            bool compressed,
            bool mask
            )
        {
            _fin    = fin;
            _opcode = opcode;

            _rsv1 = opcode.IsData() && compressed ? Rsv.On : Rsv.Off;
            _rsv2 = Rsv.Off;
            _rsv3 = Rsv.Off;

            var len = payloadData.Length;

            if (len < 126)
            {
                _payloadLength    = (byte)len;
                _extPayloadLength = WebSocket.EmptyBytes;
            }
            else if (len < 0x010000)
            {
                _payloadLength    = (byte)126;
                _extPayloadLength = ((ushort)len).InternalToByteArray(ByteOrder.Big);
            }
            else
            {
                _payloadLength    = (byte)127;
                _extPayloadLength = len.InternalToByteArray(ByteOrder.Big);
            }

            if (mask)
            {
                _mask       = Mask.On;
                _maskingKey = createMaskingKey();

                payloadData.Mask(_maskingKey);
            }
            else
            {
                _mask       = Mask.Off;
                _maskingKey = WebSocket.EmptyBytes;
            }

            _payloadData = payloadData;
        }
Ejemplo n.º 7
0
        public WsFrame(
            Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
        {
            Fin    = fin;
            Rsv1   = isData(opcode) && compressed ? Rsv.On : Rsv.Off;
            Rsv2   = Rsv.Off;
            Rsv3   = Rsv.Off;
            Opcode = opcode;
            Mask   = mask;

            /* PayloadLen */

            var dataLen    = payload.Length;
            var payloadLen = dataLen < 126
                     ? (byte)dataLen
                     : dataLen < 0x010000
                       ? (byte)126
                       : (byte)127;

            PayloadLen = payloadLen;

            /* ExtPayloadLen */

            ExtPayloadLen = payloadLen < 126
                    ? new byte [] {}
                    : payloadLen == 126
                      ? ((ushort)dataLen).ToByteArrayInternally(ByteOrder.Big)
                      : dataLen.ToByteArrayInternally(ByteOrder.Big);

            /* MaskingKey */

            var masking    = mask == Mask.Mask;
            var maskingKey = masking
                     ? createMaskingKey()
                     : new byte [] {};

            MaskingKey = maskingKey;

            /* PayloadData */

            if (masking)
            {
                payload.Mask(maskingKey);
            }

            PayloadData = payload;
        }
Ejemplo n.º 8
0
        internal WebSocketFrame(
            Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
        {
            _fin    = fin;
            _rsv1   = isData(opcode) && compressed ? Rsv.On : Rsv.Off;
            _rsv2   = Rsv.Off;
            _rsv3   = Rsv.Off;
            _opcode = opcode;
            _mask   = mask;

            var len = payload.Length;

            if (len < 126)
            {
                _payloadLength    = (byte)len;
                _extPayloadLength = new byte[0];
            }
            else if (len < 0x010000)
            {
                _payloadLength    = (byte)126;
                _extPayloadLength = ((ushort)len).InternalToByteArray(ByteOrder.Big);
            }
            else
            {
                _payloadLength    = (byte)127;
                _extPayloadLength = len.InternalToByteArray(ByteOrder.Big);
            }

            if (mask == Mask.Mask)
            {
                _maskingKey = createMaskingKey();
                payload.Mask(_maskingKey);
            }
            else
            {
                _maskingKey = new byte[0];
            }

            _payloadData = payload;
        }
Ejemplo n.º 9
0
        public WsFrame(Fin fin, Opcode opcode, Mask mask, PayloadData payload, bool compressed)
        {
            this.Fin    = fin;
            this.Rsv1   = ((!WsFrame.isData(opcode) || !compressed) ? Rsv.Off : Rsv.On);
            this.Rsv2   = Rsv.Off;
            this.Rsv3   = Rsv.Off;
            this.Opcode = opcode;
            this.Mask   = mask;
            ulong length = payload.Length;
            byte  b      = (length >= 126UL) ? ((length >= 65536UL) ? 127 : 126) : ((byte)length);

            this.PayloadLen    = b;
            this.ExtPayloadLen = ((b >= 126) ? ((b != 126) ? length.ToByteArrayInternally(ByteOrder.Big) : ((ushort)length).ToByteArrayInternally(ByteOrder.Big)) : new byte[0]);
            bool flag = mask == Mask.Mask;

            byte[] maskingKey = (!flag) ? new byte[0] : WsFrame.createMaskingKey();
            this.MaskingKey = maskingKey;
            if (flag)
            {
                payload.Mask(maskingKey);
            }
            this.PayloadData = payload;
        }
Ejemplo n.º 10
0
        internal WebSocketFrame(Fin fin, Opcode opcode, PayloadData payloadData, bool compressed, bool mask)
        {
            _fin    = fin;
            _rsv1   = ((isData(opcode) && compressed) ? Rsv.On : Rsv.Off);
            _rsv2   = Rsv.Off;
            _rsv3   = Rsv.Off;
            _opcode = opcode;
            ulong length = payloadData.Length;

            if (length < 126)
            {
                _payloadLength    = (byte)length;
                _extPayloadLength = new byte[0];
            }
            else if (length < 65536)
            {
                _payloadLength    = 126;
                _extPayloadLength = ((ushort)length).InternalToByteArray(ByteOrder.Big);
            }
            else
            {
                _payloadLength    = 127;
                _extPayloadLength = length.InternalToByteArray(ByteOrder.Big);
            }
            if (mask)
            {
                _mask       = Mask.Mask;
                _maskingKey = createMaskingKey();
                payloadData.Mask(_maskingKey);
            }
            else
            {
                _mask       = Mask.Unmask;
                _maskingKey = new byte[0];
            }
            _payloadData = payloadData;
        }
Ejemplo n.º 11
0
        private static WsFrame parse(byte[] header, Stream stream, bool unmask)
        {
            Fin    fin    = ((header[0] & 128) != 128) ? Fin.More : Fin.Final;
            Rsv    rsv    = ((header[0] & 64) != 64) ? Rsv.Off : Rsv.On;
            Rsv    rsv2   = ((header[0] & 32) != 32) ? Rsv.Off : Rsv.On;
            Rsv    rsv3   = ((header[0] & 16) != 16) ? Rsv.Off : Rsv.On;
            Opcode opcode = (Opcode)(header[0] & 15);
            Mask   mask   = ((header[1] & 128) != 128) ? Mask.Unmask : Mask.Mask;
            byte   b      = header[1] & 127;
            string text   = (!WsFrame.isControl(opcode) || fin != Fin.More) ? ((WsFrame.isData(opcode) || rsv != Rsv.On) ? null : "A non data frame is compressed.") : "A control frame is fragmented.";

            if (text != null)
            {
                throw new WebSocketException(CloseStatusCode.IncorrectData, text);
            }
            if (WsFrame.isControl(opcode) && b > 125)
            {
                throw new WebSocketException(CloseStatusCode.InconsistentData, "The payload data length of a control frame is greater than 125 bytes.");
            }
            WsFrame wsFrame = new WsFrame
            {
                Fin        = fin,
                Rsv1       = rsv,
                Rsv2       = rsv2,
                Rsv3       = rsv3,
                Opcode     = opcode,
                Mask       = mask,
                PayloadLen = b
            };
            int num = (b >= 126) ? ((b != 126) ? 8 : 2) : 0;

            byte[] array = (num <= 0) ? new byte[0] : stream.ReadBytes(num);
            if (num > 0 && array.Length != num)
            {
                throw new WebSocketException("The 'Extended Payload Length' of a frame cannot be read from the data source.");
            }
            wsFrame.ExtPayloadLen = array;
            bool flag = mask == Mask.Mask;

            byte[] array2 = (!flag) ? new byte[0] : stream.ReadBytes(4);
            if (flag && array2.Length != 4)
            {
                throw new WebSocketException("The 'Masking Key' of a frame cannot be read from the data source.");
            }
            wsFrame.MaskingKey = array2;
            ulong num2 = (b >= 126) ? ((b != 126) ? array.ToUInt64(ByteOrder.Big) : ((ulong)array.ToUInt16(ByteOrder.Big))) : ((ulong)b);

            byte[] array3;
            if (num2 > 0UL)
            {
                if (b > 126 && num2 > 9223372036854775807UL)
                {
                    throw new WebSocketException(CloseStatusCode.TooBig, "The 'Payload Data' length is greater than the allowable length.");
                }
                array3 = ((b <= 126) ? stream.ReadBytes((int)num2) : stream.ReadBytes((long)num2, 1024));
                if (array3.LongLength != (long)num2)
                {
                    throw new WebSocketException("The 'Payload Data' of a frame cannot be read from the data source.");
                }
            }
            else
            {
                array3 = new byte[0];
            }
            PayloadData payloadData = new PayloadData(array3, flag);

            if (flag && unmask)
            {
                payloadData.Mask(array2);
                wsFrame.Mask       = Mask.Unmask;
                wsFrame.MaskingKey = new byte[0];
            }
            wsFrame.PayloadData = payloadData;
            return(wsFrame);
        }
Ejemplo n.º 12
0
        private static WsFrame parse(byte [] header, Stream stream, bool unmask)
        {
            /* Header */

            // FIN
            var fin = (header [0] & 0x80) == 0x80 ? Fin.Final : Fin.More;
            // RSV1
            var rsv1 = (header [0] & 0x40) == 0x40 ? Rsv.On : Rsv.Off;
            // RSV2
            var rsv2 = (header [0] & 0x20) == 0x20 ? Rsv.On : Rsv.Off;
            // RSV3
            var rsv3 = (header [0] & 0x10) == 0x10 ? Rsv.On : Rsv.Off;
            // Opcode
            var opcode = (Opcode)(header [0] & 0x0f);
            // MASK
            var mask = (header [1] & 0x80) == 0x80 ? Mask.Mask : Mask.Unmask;
            // Payload len
            var payloadLen = (byte)(header [1] & 0x7f);

            // Check if correct frame.
            var incorrect = isControl(opcode) && fin == Fin.More
                    ? "A control frame is fragmented."
                    : !isData(opcode) && rsv1 == Rsv.On
                      ? "A non data frame is compressed."
                      : null;

            if (incorrect != null)
            {
                throw new WebSocketException(CloseStatusCode.IncorrectData, incorrect);
            }

            // Check if consistent frame.
            if (isControl(opcode) && payloadLen > 125)
            {
                throw new WebSocketException(
                          CloseStatusCode.InconsistentData,
                          "The payload data length of a control frame is greater than 125 bytes.");
            }

            var frame = new WsFrame {
                Fin        = fin,
                Rsv1       = rsv1,
                Rsv2       = rsv2,
                Rsv3       = rsv3,
                Opcode     = opcode,
                Mask       = mask,
                PayloadLen = payloadLen
            };

            /* Extended Payload Length */

            var extLen = payloadLen < 126
                 ? 0
                 : payloadLen == 126
                   ? 2
                   : 8;

            var extPayloadLen = extLen > 0
                        ? stream.ReadBytes(extLen)
                        : new byte [] {};

            if (extLen > 0 && extPayloadLen.Length != extLen)
            {
                throw new WebSocketException(
                          "The 'Extended Payload Length' of a frame cannot be read from the data source.");
            }

            frame.ExtPayloadLen = extPayloadLen;

            /* Masking Key */

            var masked     = mask == Mask.Mask;
            var maskingKey = masked
                     ? stream.ReadBytes(4)
                     : new byte [] {};

            if (masked && maskingKey.Length != 4)
            {
                throw new WebSocketException(
                          "The 'Masking Key' of a frame cannot be read from the data source.");
            }

            frame.MaskingKey = maskingKey;

            /* Payload Data */

            ulong dataLen = payloadLen < 126
                    ? payloadLen
                    : payloadLen == 126
                      ? extPayloadLen.ToUInt16(ByteOrder.Big)
                      : extPayloadLen.ToUInt64(ByteOrder.Big);

            byte [] data = null;
            if (dataLen > 0)
            {
                // Check if allowable payload data length.
                if (payloadLen > 126 && dataLen > PayloadData.MaxLength)
                {
                    throw new WebSocketException(
                              CloseStatusCode.TooBig, "The 'Payload Data' length is greater than the allowable length.");
                }

                data = payloadLen > 126
             ? stream.ReadBytes((long)dataLen, 1024)
             : stream.ReadBytes((int)dataLen);

                if (data.LongLength != (long)dataLen)
                {
                    throw new WebSocketException(
                              "The 'Payload Data' of a frame cannot be read from the data source.");
                }
            }
            else
            {
                data = new byte [] {};
            }

            var payload = new PayloadData(data, masked);

            if (masked && unmask)
            {
                payload.Mask(maskingKey);
                frame.Mask       = Mask.Unmask;
                frame.MaskingKey = new byte [] {};
            }

            frame.PayloadData = payload;
            return(frame);
        }
Ejemplo n.º 13
0
        private static WsFrame parse(byte[] header, Stream stream, bool unmask)
        {
            /* Header */

            // FIN
            var fin = (header[0] & 0x80) == 0x80 ? Fin.FINAL : Fin.MORE;
            // RSV1
            var rsv1 = (header[0] & 0x40) == 0x40 ? Rsv.ON : Rsv.OFF;
            // RSV2
            var rsv2 = (header[0] & 0x20) == 0x20 ? Rsv.ON : Rsv.OFF;
            // RSV3
            var rsv3 = (header[0] & 0x10) == 0x10 ? Rsv.ON : Rsv.OFF;
            // Opcode
            var opcode = (Opcode)(header[0] & 0x0f);
            // MASK
            var mask = (header[1] & 0x80) == 0x80 ? Mask.MASK : Mask.UNMASK;
            // Payload len
            var payloadLen = (byte)(header[1] & 0x7f);

            if (isControl(opcode) && payloadLen > 125)
            {
                return(createCloseFrame(CloseStatusCode.INCONSISTENT_DATA,
                                        "The payload length of a control frame must be 125 bytes or less."));
            }

            var frame = new WsFrame {
                Fin        = fin,
                Rsv1       = rsv1,
                Rsv2       = rsv2,
                Rsv3       = rsv3,
                Opcode     = opcode,
                Mask       = mask,
                PayloadLen = payloadLen
            };

            /* Extended Payload Length */

            var extLen = payloadLen < 126
                 ? 0
                 : payloadLen == 126
                   ? 2
                   : 8;

            var extPayloadLen = extLen > 0
                        ? stream.ReadBytesInternal(extLen)
                        : new byte[] {};

            if (extLen > 0 && extPayloadLen.Length != extLen)
            {
                return(createCloseFrame(CloseStatusCode.ABNORMAL,
                                        "'Extended Payload Length' of a frame cannot be read from the data stream."));
            }

            frame.ExtPayloadLen = extPayloadLen;

            /* Masking Key */

            var masked     = mask == Mask.MASK ? true : false;
            var maskingKey = masked
                     ? stream.ReadBytesInternal(4)
                     : new byte[] {};

            if (masked && maskingKey.Length != 4)
            {
                return(createCloseFrame(CloseStatusCode.ABNORMAL,
                                        "'Masking Key' of a frame cannot be read from the data stream."));
            }

            frame.MaskingKey = maskingKey;

            /* Payload Data */

            ulong dataLen = payloadLen < 126
                    ? payloadLen
                    : payloadLen == 126
                      ? extPayloadLen.To <ushort>(ByteOrder.BIG)
                      : extPayloadLen.To <ulong>(ByteOrder.BIG);

            byte[] data = null;
            if (dataLen > 0)
            {
                if (payloadLen > 126 && dataLen > PayloadData.MaxLength)
                {
                    var code = CloseStatusCode.TOO_BIG;
                    return(createCloseFrame(code, code.GetMessage()));
                }

                data = dataLen > 1024
             ? stream.ReadBytesInternal((long)dataLen, 1024)
             : stream.ReadBytesInternal((int)dataLen);

                if (data.LongLength != (long)dataLen)
                {
                    return(createCloseFrame(CloseStatusCode.ABNORMAL,
                                            "'Payload Data' of a frame cannot be read from the data stream."));
                }
            }
            else
            {
                data = new byte[] {};
            }

            var payloadData = new PayloadData(data, masked);

            if (masked && unmask)
            {
                payloadData.Mask(maskingKey);
                frame.Mask       = Mask.UNMASK;
                frame.MaskingKey = new byte[] {};
            }

            frame.PayloadData = payloadData;
            return(frame);
        }