public STUNAttribute FindAttribute(StunAttributeType type) { foreach (STUNAttributeContainer cont in Attributes) { if (cont.ParsedAttribute.Type == type) { return(cont.ParsedAttribute); } } return(null); }
// 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; } }
/// <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; }
/// <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)); }
/// <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) { }
/// <summary> /// Constructs an empty StunAttribute /// </summary> public StunAttribute(StunAttributeType type) { }
/// <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)) { }
/// <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) { }
public STUNAttribute FindAttribute(StunAttributeType type) { foreach (STUNAttributeContainer cont in Attributes) { if (cont.ParsedAttribute.Type == type) { return cont.ParsedAttribute; } } return null; }
public bool HasAttribute(StunAttributeType attribute) { return(availableAttributes.Contains(attribute)); }
/// <summary> /// Constructs an empty StunAttribute /// </summary> public StunAttribute(StunAttributeType type) { }
/// <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 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; }
void SetAttribute(STUNAttribute attr) { StunAttributeType = attr.Type; m_objParsedAttribute = attr; }
/// <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; }
public STUNAttribute(StunAttributeType nType) { Type = nType; }
/// <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); }
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)) { }
/// <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) { }
/// <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); }