Exemple #1
0
        public static STUNHeader ParseSTUNHeader(byte[] buffer)
        {
            if (buffer != null && buffer.Length > 0 && buffer.Length >= STUN_HEADER_LENGTH)
            {
                STUNHeader stunHeader = new STUNHeader();

                UInt16 stunTypeValue     = BitConverter.ToUInt16(buffer, 0);
                UInt16 stunMessageLength = BitConverter.ToUInt16(buffer, 2);

                if (BitConverter.IsLittleEndian)
                {
                    stunTypeValue     = Utility.ReverseEndian(stunTypeValue);
                    stunMessageLength = Utility.ReverseEndian(stunMessageLength);
                }

                stunHeader.MessageType   = STUNMessageTypes.GetSTUNMessageTypeForId(stunTypeValue);
                stunHeader.MessageLength = stunMessageLength;
                //stunHeader.TransactionId = BitConverter.ToString(buffer, 4, TRANSACTION_ID_LENGTH);
                Buffer.BlockCopy(buffer, 4, stunHeader.TransactionId, 0, TRANSACTION_ID_LENGTH);

                return(stunHeader);
            }

            return(null);
        }
Exemple #2
0
        public static STUNHeader ParseSTUNHeader(byte[] buffer)
        {
            if ((buffer[0] & STUN_INITIAL_BYTE_MASK) != 0)
            {
                throw new ApplicationException("The STUN header did not begin with 0x00.");
            }

            if (buffer != null && buffer.Length > 0 && buffer.Length >= STUN_HEADER_LENGTH)
            {
                STUNHeader stunHeader = new STUNHeader();

                UInt16 stunTypeValue     = BitConverter.ToUInt16(buffer, 0);
                UInt16 stunMessageLength = BitConverter.ToUInt16(buffer, 2);

                if (BitConverter.IsLittleEndian)
                {
                    stunTypeValue     = NetConvert.DoReverseEndian(stunTypeValue);
                    stunMessageLength = NetConvert.DoReverseEndian(stunMessageLength);
                }

                stunHeader.MessageType   = STUNMessageTypes.GetSTUNMessageTypeForId(stunTypeValue);
                stunHeader.MessageLength = stunMessageLength;
                Buffer.BlockCopy(buffer, 8, stunHeader.TransactionId, 0, TRANSACTION_ID_LENGTH);

                return(stunHeader);
            }

            return(null);
        }