Beispiel #1
0
 public STUNAttribute FindAttribute(StunAttributeType type)
 {
     foreach (STUNAttributeContainer cont in Attributes)
     {
         if (cont.ParsedAttribute.Type == type)
         {
             return(cont.ParsedAttribute);
         }
     }
     return(null);
 }
Beispiel #2
0
        // Construct a message from a received byte array
        public StunMessage(byte[] bytes)
        {
            // take a copy of the bytes as we can't assume that ownership is transferred
            buffer = new byte[bytes.Length];
            Array.Copy(bytes, buffer, bytes.Length);

            // extract some often used fields
            type          = ExtractShort(buffer, 0);
            length        = ExtractShort(buffer, 2);
            transactionID = new byte[12];
            Array.Copy(bytes, 8, transactionID, 0, 12);

            // scan through the rest of the buffer to build a list of available attributes
            availableAttributes = new List <StunAttributeType>();
            int pos = 20;

            while (pos < buffer.Length)
            {
                StunAttributeType type = (StunAttributeType)ExtractShort(buffer, pos);
                availableAttributes.Add(type);
                pos += 2;

                ushort length = ExtractShort(buffer, pos);
                pos += 2;

                // TODO: it might just be easier to get all the values too, especially the nonce which needs some special processing
                // when getting the nonce, check for the cookie, base64 decode the next 4 bytes and deconstruct the bit field
                if (type == StunAttributeType.NONCE)
                {
                    byte[] value = new byte[length];
                    Array.Copy(buffer, pos, value, 0, length);
                    nonce = System.Text.Encoding.UTF8.GetString(value);

                    if (NonceMatchesCookie())
                    {
                        securityFeatureSet = Convert.FromBase64String(nonce.Substring(9, 4));
                    }
                }
                else if (type == StunAttributeType.ERROR_CODE)
                {
                    ErrorCode   = buffer[pos + 2] * 100 + buffer[pos + 3];
                    ErrorReason = System.Text.Encoding.UTF8.GetString(buffer, 4, length - 4);
                }

                pos += length;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Constructs a new StunAttribute
        /// </summary>
        /// <param name="type">The type of this StunAttribute</param>
        /// <param name="typeBytes">The value of the type of this StunAttribute in bytes</param>
        /// <param name="value">The value of this StunAttribute</param>
        public StunAttribute(StunAttributeType type, byte[] typeBytes, byte[] value)
        {
            if (typeBytes == null)
            {
                throw new ArgumentNullException("value", "Cannot be null");
            }

            if (value == null)
            {
                throw new ArgumentNullException("value", "Cannot be null");
            }

            switch (type)
            {
            case StunAttributeType.Software:
            case StunAttributeType.Realm:
            case StunAttributeType.Nonce:
            case StunAttributeType.ErrorCode:
                if (value.Length > 763)
                {
                    throw new ArgumentOutOfRangeException("value", "Cannot be greater than 763 bytes for the given type as described in RFC 5389");
                }
                break;

            case StunAttributeType.Username:
                if (value.Length > 513)
                {
                    throw new ArgumentOutOfRangeException("value", "Cannot be greater than 513 bytes for the given type as described in RFC 5389");
                }
                break;
            }

            switch (type)
            {
            case StunAttributeType.Realm:
            case StunAttributeType.Username:
                String saslPrepValue = new SASLprep().Prepare(StunMessage.Encoder.GetString(value));

                value = StunMessage.Encoder.GetBytes(saslPrepValue);
                break;
            }

            this.TypeBytes = typeBytes;
            this.Type      = type;
            this.Value     = value;
        }
Beispiel #4
0
        /// <summary>
        /// Convert a StunAttributeType to a network-byte ordered array of bytes
        /// </summary>
        /// <param name="type">The StunAttributeType to convert</param>
        /// <returns>
        /// The array of 2 bytes (16bits) matching the StunAttributeType
        /// Returns max UInt16 value if the type parameter is StunAttributeType.Unmanaged
        /// </returns>
        public static byte[] AttributeTypeToBytes(StunAttributeType type)
        {
            foreach (FieldInfo field in typeof(StunAttributeType).GetFields())
            {
                if (field.Name == type.ToString())
                {
                    Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                    if (fieldAttributes.Length == 1)
                    {
                        StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                        byte[] typeBytes = BitConverter.GetBytes(stunValueAttribute.Value);
                        Array.Reverse(typeBytes);

                        return(typeBytes);
                    }
                }
            }
            return(BitConverter.GetBytes(0xFFFF));
        }
Beispiel #5
0
 /// <summary>
 /// Constructs a new StunAttribute
 /// </summary>
 /// <param name="type">The type of this StunAttribute</param>
 /// <param name="value">The value of this StunAttribute</param>
 public StunAttribute(StunAttributeType type, byte[] value)
     : this(type, StunAttribute.AttributeTypeToBytes(type), value)
 {
 }
 /// <summary>
 /// Constructs a XorMappedAddress based on an existing StunAttribute
 /// </summary>
 /// <param name="type">The StunAttributeType associated with the attribute parameter</param>
 /// <param name="attribute">The StunAttribute base for this XorMappedAddress</param>
 /// <param name="transactionID">
 /// The transaction ID of the message from where the attribute parameter originates
 /// It may be used by this XorMappedAddress to decode an IPV6 address
 /// </param>
 public XorMappedAddress(StunAttributeType type, StunAttribute attribute, byte[] transactionID)
     : base(type, attribute)
 {
     this.TransactionID = transactionID;
 }
 /// <summary>
 /// Constructs an empty MappedAddress
 /// </summary>
 /// <param name="type">The StunAttributeType of this MappedAddress</param>
 protected MappedAddress(StunAttributeType type)
     : base(type)
 { }
 /// <summary>
 /// Constructs a MappedAddress based on an existing StunAttribute
 /// </summary>
 /// <param name="type">The StunAttributeType associated with the attribute parameter</param>
 /// <param name="attribute">The StunAttribute base for this MappedAddress</param>
 public MappedAddress(StunAttributeType type, StunAttribute attribute)
     : base(type, attribute.Value)
 { }
 /// <summary>
 /// Constructs an HexAttribute based on an existing StunAttribute
 /// </summary>
 /// <param name="attribute">The StunAttribute base for this HexAttribute</param>
 public HexAttribute(StunAttributeType type, StunAttribute attribute)
     : base(type, attribute.Value)
 { }
Beispiel #10
0
 /// <summary>
 /// Constructs an empty StunAttribute
 /// </summary>
 public StunAttribute(StunAttributeType type)
 { }
Beispiel #11
0
 /// <summary>
 /// Constructs a new UTF8Attribute
 /// </summary>
 /// <param name="type">The type of this UTF8Attribute</param>
 /// <param name="value">The value of this UTF8Attribute</param>
 public UTF8Attribute(StunAttributeType type, String value)
     : base(type, StunMessage.Encoder.GetBytes(value))
 {
 }
Beispiel #12
0
 /// <summary>
 /// Constructs a new StunAttribute
 /// </summary>
 /// <param name="type">The type of this StunAttribute</param>
 /// <param name="value">The value of this StunAttribute</param>
 public StunAttribute(StunAttributeType type, byte[] value)
     : this(type, StunAttribute.AttributeTypeToBytes(type), value)
 { }
Beispiel #13
0
 public STUNAttribute FindAttribute(StunAttributeType type)
 {
     foreach (STUNAttributeContainer cont in Attributes)
     {
         if (cont.ParsedAttribute.Type == type)
         {
             return cont.ParsedAttribute;
         }
     }
     return null;
 }
Beispiel #14
0
 public bool HasAttribute(StunAttributeType attribute)
 {
     return(availableAttributes.Contains(attribute));
 }
Beispiel #15
0
 /// <summary>
 /// Constructs an empty StunAttribute
 /// </summary>
 public StunAttribute(StunAttributeType type)
 {
 }
Beispiel #16
0
 /// <summary>
 /// Constructs a XorMappedAddress based on an existing StunAttribute
 /// </summary>
 /// <param name="type">The StunAttributeType associated with the attribute parameter</param>
 /// <param name="attribute">The StunAttribute base for this XorMappedAddress</param>
 /// <param name="transactionID">
 /// The transaction ID of the message from where the attribute parameter originates
 /// It may be used by this XorMappedAddress to decode an IPV6 address
 /// </param>
 public XorMappedAddress(StunAttributeType type, StunAttribute attribute, byte[] transactionID)
     : base(type, attribute)
 {
     this.TransactionID = transactionID;
 }
Beispiel #17
0
 /// <summary>
 /// Constructs an empty MappedAddress
 /// </summary>
 /// <param name="type">The StunAttributeType of this MappedAddress</param>
 protected MappedAddress(StunAttributeType type)
     : base(type)
 {
 }
Beispiel #18
0
 /// <summary>
 /// Constructs a MappedAddress based on an existing StunAttribute
 /// </summary>
 /// <param name="type">The StunAttributeType associated with the attribute parameter</param>
 /// <param name="attribute">The StunAttribute base for this MappedAddress</param>
 public MappedAddress(StunAttributeType type, StunAttribute attribute)
     : base(type, attribute.Value)
 {
 }
Beispiel #19
0
        /// <summary>
        /// Constructs a new StunAttribute
        /// </summary>
        /// <param name="type">The type of this StunAttribute</param>
        /// <param name="typeBytes">The value of the type of this StunAttribute in bytes</param>
        /// <param name="value">The value of this StunAttribute</param>
        public StunAttribute(StunAttributeType type, byte[] typeBytes, byte[] value)
        {
            if (typeBytes == null)
                throw new ArgumentNullException("value", "Cannot be null");

            if (value == null)
                throw new ArgumentNullException("value", "Cannot be null");

            switch (type)
            {
                case StunAttributeType.Software:
                case StunAttributeType.Realm:
                case StunAttributeType.Nonce:
                case StunAttributeType.ErrorCode:
                    if (value.Length > 763)
                        throw new ArgumentOutOfRangeException("value", "Cannot be greater than 763 bytes for the given type as described in RFC 5389");
                    break;

                case StunAttributeType.Username:
                    if (value.Length > 513)
                        throw new ArgumentOutOfRangeException("value", "Cannot be greater than 513 bytes for the given type as described in RFC 5389");
                    break;
            }

            switch (type)
            {
                case StunAttributeType.Realm:
                case StunAttributeType.Username:
                    String saslPrepValue = new SASLprep().Prepare(StunMessage.Encoder.GetString(value));

                    value = StunMessage.Encoder.GetBytes(saslPrepValue);
                    break;
            }

            this.TypeBytes = typeBytes;
            this.Type = type;
            this.Value = value;
        }
Beispiel #20
0
 void SetAttribute(STUNAttribute attr)
 {
     StunAttributeType = attr.Type;
     m_objParsedAttribute = attr;
 }
Beispiel #21
0
 /// <summary>
 /// Retrieve the attribute of a given type in this message
 /// </summary>
 /// <param name="type">The type of the attribute to retrieve</param>
 /// <returns>The known attribute matching the given type or null if it doesn't exists</returns>
 public StunAttribute GetAttribute(StunAttributeType type)
 {
     return this.attributesList.ContainsKey(type) ? this.attributesList[type] : null;
 }
Beispiel #22
0
 public STUNAttribute(StunAttributeType nType)
 {
     Type = nType;
 }
Beispiel #23
0
 /// <summary>
 /// Retrieve the attribute of a given type in this message
 /// </summary>
 /// <param name="type">The type of the attribute to retrieve</param>
 /// <returns>The known attribute matching the given type or null if it doesn't exists</returns>
 public StunAttribute GetAttribute(StunAttributeType type)
 {
     return(this.attributesList.ContainsKey(type) ? this.attributesList[type] : null);
 }
Beispiel #24
0
 public STUNAttribute(StunAttributeType nType)
 {
     Type = nType;
 }
 /// <summary>
 /// Constructs a new UTF8Attribute
 /// </summary>
 /// <param name="type">The type of this UTF8Attribute</param>
 /// <param name="value">The value of this UTF8Attribute</param>
 public UTF8Attribute(StunAttributeType type, String value)
     : base(type, StunMessage.Encoder.GetBytes(value))
 { }
Beispiel #26
0
 /// <summary>
 /// Constructs an HexAttribute based on an existing StunAttribute
 /// </summary>
 /// <param name="attribute">The StunAttribute base for this HexAttribute</param>
 public HexAttribute(StunAttributeType type, StunAttribute attribute)
     : base(type, attribute.Value)
 {
 }
Beispiel #27
0
        /// <summary>
        /// Convert a StunAttributeType to a network-byte ordered array of bytes
        /// </summary>
        /// <param name="type">The StunAttributeType to convert</param>
        /// <returns>
        /// The array of 2 bytes (16bits) matching the StunAttributeType
        /// Returns max UInt16 value if the type parameter is StunAttributeType.Unmanaged
        /// </returns>
        public static byte[] AttributeTypeToBytes(StunAttributeType type)
        {
            foreach (FieldInfo field in typeof(StunAttributeType).GetFields())
            {
                if (field.Name == type.ToString())
                {
                    Object[] fieldAttributes = field.GetCustomAttributes(typeof(StunValueAttribute), false);

                    if (fieldAttributes.Length == 1)
                    {
                        StunValueAttribute stunValueAttribute = fieldAttributes.GetValue(0) as StunValueAttribute;

                        byte[] typeBytes = BitConverter.GetBytes(stunValueAttribute.Value);
                        Array.Reverse(typeBytes);

                        return typeBytes;
                    }
                }
            }
            return BitConverter.GetBytes(0xFFFF);
        }