Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public static STUNMessage ParseSTUNMessage(byte[] buffer, int bufferLength)
        {
            if (buffer != null && buffer.Length > 0 && buffer.Length >= bufferLength)
            {
                STUNMessage stunMessage = new STUNMessage();
                stunMessage.Header = STUNHeader.ParseSTUNHeader(buffer);

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

                return(stunMessage);
            }

            return(null);
        }
Exemplo n.º 4
0
        public static STUNMessage ParseSTUNMessage(byte[] buffer, int bufferLength)
        {
            if (buffer != null && buffer.Length > 0 && buffer.Length >= bufferLength)
            {
                STUNMessage stunMessage = new STUNMessage();
                stunMessage._receivedBuffer = buffer.Take(bufferLength).ToArray();
                stunMessage.Header          = STUNHeader.ParseSTUNHeader(buffer);

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

                if (stunMessage.Attributes.Count > 0 &&
                    stunMessage.Attributes.Last().AttributeType == STUNAttributeTypesEnum.FingerPrint)
                {
                    // Check fingerprint.
                    var fingerprintAttribute = stunMessage.Attributes.Last();

                    var input = buffer.Take(buffer.Length - STUNAttribute.STUNATTRIBUTE_HEADER_LENGTH -
                                            FINGERPRINT_ATTRIBUTE_CRC32_LENGTH).ToArray();

                    uint   crc         = Crc32.Compute(input) ^ FINGERPRINT_XOR;
                    byte[] fingerPrint = (BitConverter.IsLittleEndian)
                        ? BitConverter.GetBytes(NetConvert.DoReverseEndian(crc))
                        : BitConverter.GetBytes(crc);

                    //logger.LogDebug($"STUNMessage supplied fingerprint attribute: {fingerprintAttribute.Value.HexStr()}.");
                    //logger.LogDebug($"STUNMessage calculated fingerprint attribute: {fingerPrint.HexStr()}.");

                    if (fingerprintAttribute.Value.HexStr() == fingerPrint.HexStr())
                    {
                        stunMessage.isFingerprintValid = true;
                    }
                }

                return(stunMessage);
            }

            return(null);
        }
Exemplo n.º 5
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;
        }
Exemplo n.º 6
0
 public STUNMessage(STUNMessageTypesEnum stunMessageType)
 {
     Header = new STUNHeader(stunMessageType);
 }
Exemplo n.º 7
0
 public STUNMessage(STUNMessageTypesEnum stunMessageType)
 {
     Header = new STUNHeader(stunMessageType);
 }