コード例 #1
0
ファイル: STUNMessage.cs プロジェクト: xyanlong/AKStream
        public void AddXORAddressAttribute(STUNAttributeTypesEnum addressType, IPAddress remoteAddress, int remotePort)
        {
            STUNXORAddressAttribute xorAddressAttribute =
                new STUNXORAddressAttribute(addressType, remotePort, remoteAddress);

            Attributes.Add(xorAddressAttribute);
        }
コード例 #2
0
        public STUNAddressAttribute(STUNAttributeTypesEnum attributeType, int port, IPAddress address)
            : base(attributeType, null)
        {
            Port    = port;
            Address = address;

            base.AttributeType = attributeType;
            //base.Length = ADDRESS_ATTRIBUTE_LENGTH;
        }
コード例 #3
0
ファイル: STUNAttribute.cs プロジェクト: xyanlong/AKStream
        public STUNAttribute(STUNAttributeTypesEnum attributeType, int value)
        {
            AttributeType = attributeType;

            if (BitConverter.IsLittleEndian)
            {
                Value = BitConverter.GetBytes(NetConvert.DoReverseEndian(Convert.ToUInt32(value)));
            }
            else
            {
                Value = BitConverter.GetBytes(value);
            }
        }
コード例 #4
0
 public STUNXORAddressAttribute(STUNAttributeTypesEnum attributeType, byte[] attributeValue)
     : base(attributeType, attributeValue)
 {
     if (BitConverter.IsLittleEndian)
     {
         Port = NetConvert.DoReverseEndian(BitConverter.ToUInt16(attributeValue, 2)) ^ (UInt16)(STUNHeader.MAGIC_COOKIE >> 16);
         UInt32 address = NetConvert.DoReverseEndian(BitConverter.ToUInt32(attributeValue, 4)) ^ STUNHeader.MAGIC_COOKIE;
         Address = new IPAddress(NetConvert.DoReverseEndian(address));
     }
     else
     {
         Port = BitConverter.ToUInt16(attributeValue, 2) ^ (UInt16)(STUNHeader.MAGIC_COOKIE >> 16);
         UInt32 address = BitConverter.ToUInt32(attributeValue, 4) ^ STUNHeader.MAGIC_COOKIE;
         Address = new IPAddress(address);
     }
 }
コード例 #5
0
ファイル: STUNAttribute.cs プロジェクト: xyanlong/AKStream
        public static List <STUNAttribute> ParseMessageAttributes(byte[] buffer, int startIndex, int endIndex)
        {
            if (buffer != null && buffer.Length > startIndex && buffer.Length >= endIndex)
            {
                List <STUNAttribute> attributes = new List <STUNAttribute>();
                int startAttIndex = startIndex;

                while (startAttIndex < endIndex)
                {
                    UInt16 stunAttributeType   = BitConverter.ToUInt16(buffer, startAttIndex);
                    UInt16 stunAttributeLength = BitConverter.ToUInt16(buffer, startAttIndex + 2);
                    byte[] stunAttributeValue  = null;

                    if (BitConverter.IsLittleEndian)
                    {
                        stunAttributeType   = NetConvert.DoReverseEndian(stunAttributeType);
                        stunAttributeLength = NetConvert.DoReverseEndian(stunAttributeLength);
                    }

                    if (stunAttributeLength > 0)
                    {
                        if (stunAttributeLength + startIndex + 4 > endIndex)
                        {
                            logger.LogWarning(
                                "The attribute length on a STUN parameter was greater than the available number of bytes.");
                        }
                        else
                        {
                            stunAttributeValue = new byte[stunAttributeLength];
                            Buffer.BlockCopy(buffer, startAttIndex + 4, stunAttributeValue, 0, stunAttributeLength);
                        }
                    }

                    STUNAttributeTypesEnum attributeType =
                        STUNAttributeTypes.GetSTUNAttributeTypeForId(stunAttributeType);

                    STUNAttribute attribute = null;
                    if (attributeType == STUNAttributeTypesEnum.ChangeRequest)
                    {
                        attribute = new STUNChangeRequestAttribute(stunAttributeValue);
                    }
                    else if (attributeType == STUNAttributeTypesEnum.MappedAddress)
                    {
                        attribute = new STUNAddressAttribute(stunAttributeValue);
                    }
                    else if (attributeType == STUNAttributeTypesEnum.ErrorCode)
                    {
                        attribute = new STUNErrorCodeAttribute(stunAttributeValue);
                    }
                    else if (attributeType == STUNAttributeTypesEnum.XORMappedAddress ||
                             attributeType == STUNAttributeTypesEnum.XORPeerAddress ||
                             attributeType == STUNAttributeTypesEnum.XORRelayedAddress)
                    {
                        attribute = new STUNXORAddressAttribute(attributeType, stunAttributeValue);
                    }
                    else
                    {
                        attribute = new STUNAttribute(attributeType, stunAttributeValue);
                    }

                    attributes.Add(attribute);

                    // Attributes start on 32 bit word boundaries so where an attribute length is not a multiple of 4 it gets padded.
                    int padding = (stunAttributeLength % 4 != 0) ? 4 - (stunAttributeLength % 4) : 0;

                    startAttIndex = startAttIndex + 4 + stunAttributeLength + padding;
                }

                return(attributes);
            }
            else
            {
                return(null);
            }
        }
コード例 #6
0
ファイル: STUNAttribute.cs プロジェクト: xyanlong/AKStream
 public STUNAttribute(STUNAttributeTypesEnum attributeType, byte[] value)
 {
     AttributeType = attributeType;
     Value         = value;
 }
コード例 #7
0
 public STUNAttribute(STUNAttributeTypesEnum attributeType, UInt16 length, byte[] value)
 {
     AttributeType = attributeType;
     Length = length;
     Value = value;
 }
コード例 #8
0
 public STUNAttribute(STUNAttributeTypesEnum attributeType, byte[] value)
 {
     AttributeType = attributeType;
     Value = value;
     Length = Convert.ToUInt16(Value.Length);
 }
コード例 #9
0
        public STUNAddressAttribute(STUNAttributeTypesEnum attributeType, int port, IPAddress address)
            : base(attributeType, ADDRESS_ATTRIBUTE_LENGTH, null)
        {
            Port = port;
            Address = address;

            base.AttributeType = attributeType;
            base.Length = ADDRESS_ATTRIBUTE_LENGTH;
        }
コード例 #10
0
        public static List <STUNAttribute> ParseMessageAttributes(byte[] buffer, int startIndex, int endIndex)
        {
            if (buffer != null && buffer.Length > startIndex && buffer.Length >= endIndex)
            {
                List <STUNAttribute> attributes = new List <STUNAttribute>();
                int startAttIndex = startIndex;

                while (startAttIndex < endIndex)
                {
                    UInt16 stunAttributeType   = BitConverter.ToUInt16(buffer, startAttIndex);
                    UInt16 stunAttributeLength = BitConverter.ToUInt16(buffer, startAttIndex + 2);
                    byte[] stunAttributeValue  = null;

                    if (BitConverter.IsLittleEndian)
                    {
                        stunAttributeType   = Utility.ReverseEndian(stunAttributeType);
                        stunAttributeLength = Utility.ReverseEndian(stunAttributeLength);
                    }

                    if (stunAttributeLength > 0)
                    {
                        if (stunAttributeType == (int)STUNAttributeTypesEnum.Username && stunAttributeLength > buffer.Length - startIndex - 4)
                        {
                            // Received some STUN messages where the username is shorter than the claimed length.
                            int realLength = buffer.Length - startIndex - 4;
                            stunAttributeValue = new byte[realLength];
                            Buffer.BlockCopy(buffer, startIndex + 4, stunAttributeValue, 0, realLength);
                        }
                        else
                        {
                            stunAttributeValue = new byte[stunAttributeLength];
                            Buffer.BlockCopy(buffer, startIndex + 4, stunAttributeValue, 0, stunAttributeLength);
                        }
                    }

                    STUNAttributeTypesEnum attributeType = STUNAttributeTypes.GetSTUNAttributeTypeForId(stunAttributeType);

                    STUNAttribute attribute = null;
                    if (attributeType == STUNAttributeTypesEnum.ChangeRequest)
                    {
                        attribute = new STUNChangeRequestAttribute(stunAttributeValue);
                    }
                    else if (attributeType == STUNAttributeTypesEnum.MappedAddress)
                    {
                        attribute = new STUNAddressAttribute(stunAttributeValue);
                    }
                    else
                    {
                        attribute = new STUNAttribute(attributeType, stunAttributeLength, stunAttributeValue);
                    }

                    attributes.Add(attribute);

                    startAttIndex = startAttIndex + 4 + stunAttributeLength;
                }

                return(attributes);
            }
            else
            {
                return(null);
            }
        }
コード例 #11
0
 public STUNAttribute(STUNAttributeTypesEnum attributeType, UInt16 length, byte[] value)
 {
     AttributeType = attributeType;
     Length        = length;
     Value         = value;
 }
コード例 #12
0
 public STUNAttribute(STUNAttributeTypesEnum attributeType, byte[] value)
 {
     AttributeType = attributeType;
     Value         = value;
     Length        = Convert.ToUInt16(Value.Length);
 }
コード例 #13
0
 public STUNXORAddressAttribute(STUNAttributeTypesEnum attributeType, int port, IPAddress address)
     : base(attributeType, null)
 {
     Port    = port;
     Address = address;
 }
コード例 #14
0
 public STUNAttribute(STUNAttributeTypesEnum attributeType, ulong value)
 {
     AttributeType = attributeType;
     Value         = NetConvert.GetBytes(value);
 }