Esempio n. 1
0
        public static STUNv2Header ParseSTUNHeader(byte[] buffer)
        {
            if ((buffer[0] & STUN_INITIAL_BYTE_MASK) != 0)
            {
                throw new ApplicationException("The STUNv2 header did not begin with 0x00.");
            }

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

                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   = STUNv2MessageTypes.GetSTUNMessageTypeForId(stunTypeValue);
                stunHeader.MessageLength = stunMessageLength;
                Buffer.BlockCopy(buffer, 8, stunHeader.TransactionId, 0, TRANSACTION_ID_LENGTH);

                return(stunHeader);
            }

            return(null);
        }
        public static STUNv2Header ParseSTUNHeader(byte[] buffer)
        {
            if (buffer[0] != STUN_INITIAL_BYTE)
            {
                throw new ApplicationException("The STUNv2 header did not begin with 0x00.");
            }

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

                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 = STUNv2MessageTypes.GetSTUNMessageTypeForId(stunTypeValue);
                stunHeader.MessageLength = stunMessageLength;
                Buffer.BlockCopy(buffer, 4, stunHeader.TransactionId, 0, TRANSACTION_ID_LENGTH);

                return stunHeader;
            }

            return null;
        }
Esempio n. 3
0
        public static STUNv2Message ParseSTUNMessage(byte[] buffer, int bufferLength)
        {
            if (buffer != null && buffer.Length > 0 && buffer.Length >= bufferLength)
            {
                STUNv2Message stunMessage = new STUNv2Message();
                stunMessage.Header = STUNv2Header.ParseSTUNHeader(buffer);

                if (stunMessage.Header.MessageLength > 0)
                {
                    stunMessage.Attributes = STUNv2Attribute.ParseMessageAttributes(buffer, STUNv2Header.STUN_HEADER_LENGTH, bufferLength);
                }

                return(stunMessage);
            }

            return(null);
        }
Esempio n. 4
0
 public STUNv2Message(STUNv2MessageTypesEnum stunMessageType)
 {
     Header = new STUNv2Header(stunMessageType);
 }
Esempio n. 5
0
 public STUNv2Message(STUNv2MessageTypesEnum stunMessageType)
 {
     Header = new STUNv2Header(stunMessageType);
 }