private void ParseAttributes(byte[] attributeByteArray) { int currentAttributeOffset = 0; while (currentAttributeOffset < attributeByteArray.Length) { //Get the RADIUS attribute type RadiusAttributeType type = (RadiusAttributeType)attributeByteArray[currentAttributeOffset]; //Get the RADIUS attribute length byte length = attributeByteArray[currentAttributeOffset + 1]; // Check minimum length and make sure the attribute doesn't run off the end of the packet if (length < 2 || currentAttributeOffset + length > _Length) { Valid = false; return; } //Get the RADIUS attribute data byte[] data = new byte[length - 2]; Array.Copy(attributeByteArray, currentAttributeOffset + 2, data, 0, length - 2); _Attributes.Add(type == RadiusAttributeType.VENDOR_SPECIFIC ? new VendorSpecificAttribute(attributeByteArray, currentAttributeOffset) : new RadiusAttribute(type, data)); currentAttributeOffset += length; } }
public static RadiusAttribute CreateAddress(RadiusAttributeType type, string ipString) { if (IPAddress.TryParse(ipString, out IPAddress address)) { return(new RadiusAttribute(type, address.GetAddressBytes())); } throw new ArgumentException($"IPString '{ipString} is not a valid IPAddres.'"); }
/// <summary> /// Constructs an attribute with a binary value. /// </summary> /// <param name="type">The RADIUS attribute type code.</param> /// <param name="value">The raw attribute data.</param> /// <exception cref="RadiusException">Thrown if the value size exceeds 253 bytes.</exception> public RadiusAttribute(RadiusAttributeType type, byte[] value) { this.Type = type; this.Value = value; if (value.Length > MaxValueLen) { throw new RadiusException("Attribute data size exceeds 253 bytes."); } }
/// <summary> /// Constructs an attribute with a IP Address as the value. /// </summary> /// <param name="type">The RADIUS attribute type code.</param> /// <param name="value">The IP address.</param> /// <exception cref="RadiusException">Thrown for IPv6 addresses.</exception> public RadiusAttribute(RadiusAttributeType type, IPAddress value) { this.Type = type; this.Value = value.GetAddressBytes(); if (this.Value.Length != 4) { throw new RadiusException("RADIUS does not support IPv6 addresses."); } }
/// <summary> /// Constructs an attribute with a string value encoded as UTF-8. /// </summary> /// <param name="type">The RADIUS attribute type code.</param> /// <param name="value">The attribute value.</param> /// <exception cref="RadiusException">Thrown if the value size exceeds 253 bytes.</exception> public RadiusAttribute(RadiusAttributeType type, string value) { this.Type = type; this.Value = Helper.ToUTF8(value); if (value.Length > MaxValueLen) { throw new RadiusException("Attribute data size exceeds 253 bytes."); } }
public RadiusAttribute Find(RadiusAttributeType type) { foreach (RadiusAttribute ra in this) { if (ra.AttributeType == type) { return(ra); } } return(null); }
public RadiusAttribute(RadiusAttributeType type, byte[] data) { Type = type; Data = data; Length = (byte)(Data.Length + ATTRIBUTE_HEADER_SIZE); RawData = new byte[Length]; RawData[0] = (byte)Type; RawData[1] = Length; Array.Copy(data, 0, RawData, ATTRIBUTE_HEADER_SIZE, data.Length); }
public RadiusAttribute(RadiusAttributeType type, byte[] data) { Type = type; Data = data; Length = (byte)(Data.Length + ATTRIBUTE_HEADER_SIZE); RawData = new byte[Length]; RawData[0] = (byte)Type; RawData[1] = Length; Array.Copy(data, 0, RawData, ATTRIBUTE_HEADER_SIZE, data.Length); }
public int CountType(RadiusAttributeType type) { int count = 0; foreach (RadiusAttribute ra in this) { if (ra.TypeField == (byte)type) { count++; } } return(count); }
private void ParseAttributes(byte[] rawattributes) { int x = 0; while (x < rawattributes.Length) { RadiusAttributeType type = (RadiusAttributeType)rawattributes[x]; int length = (int)rawattributes[x + 1]; byte[] data = new byte[length - 2]; Array.Copy(rawattributes, x + 2, data, 0, length - 2); this.attributes.Add(new RadiusAttribute(type, data)); x += length; } }
/// <summary> /// Searches the set of packet attributes for the first attribute with /// the specified type and returns its value as a TEXT string. /// </summary> /// <param name="type">The desired attribute type.</param> /// <param name="value">This will be filled in with the attribute value.</param> /// <returns><c>true</c> if the attribute was found.</returns> public bool GetAttributeAsBinary(RadiusAttributeType type, out byte[] value) { for (int i = 0; i < this.Attributes.Count; i++) { if (this.Attributes[i].Type == type) { value = Helper.Extract(this.Attributes[i].Value, 0); return(true); } } value = null; return(false); }
/// <summary> /// Searches the set of packet attributes for the first attribute with /// the specified type and returns its value as a TEXT string. /// </summary> /// <param name="type">The desired attribute type.</param> /// <param name="value">This will be filled in with the attribute value.</param> /// <returns><c>true</c> if the attribute was found.</returns> public bool GetAttributeAsText(RadiusAttributeType type, out string value) { for (int i = 0; i < this.Attributes.Count; i++) { if (this.Attributes[i].Type == type) { value = Helper.FromUTF8(this.Attributes[i].Value); return(true); } } value = null; return(false); }
/// <summary> /// Searches the set of packet attributes for the first attribute with /// the specified type and returns its value as a TEXT string. /// </summary> /// <param name="type">The desired attribute type.</param> /// <param name="value">This will be filled in with the attribute value.</param> /// <returns><c>true</c> if the attribute was found.</returns> public bool GetAttributeAsAddress(RadiusAttributeType type, out IPAddress value) { for (int i = 0; i < this.Attributes.Count; i++) { if (this.Attributes[i].Type == type) { value = new IPAddress(this.Attributes[i].Value); return(true); } } value = IPAddress.Any; return(false); }
/// <summary> /// Searches the set of packet attributes for the first attribute with /// the specified type and returns its value as a TEXT string. /// </summary> /// <param name="type">The desired attribute type.</param> /// <param name="value">This will be filled in with the attribute value.</param> /// <returns><c>true</c> if the attribute was found.</returns> public bool GetAttributeAsInteger(RadiusAttributeType type, out int value) { for (int i = 0; i < this.Attributes.Count; i++) { if (this.Attributes[i].Type == type) { int pos = 0; value = Helper.ReadInt32(this.Attributes[i].Value, ref pos); return(true); } } value = 0; return(false); }
/// <summary> /// Searches the set of packet attributes for the first attribute with /// the specified type and returns its value as a TEXT string. /// </summary> /// <param name="type">The desired attribute type.</param> /// <param name="value">This will be filled in with the attribute value.</param> /// <returns><c>true</c> if the attribute was found.</returns> public bool GetAttributeAsTime(RadiusAttributeType type, out DateTime value) { for (int i = 0; i < this.Attributes.Count; i++) { if (this.Attributes[i].Type == type) { int pos = 0; int time; time = Helper.ReadInt32(this.Attributes[i].Value, ref pos); value = UnixTime.FromSeconds(time); return(true); } } value = UnixTime.TimeZero; return(false); }
/// <summary> /// Initializes a new instance of the <see cref="RadiusAttribute"/> class. /// </summary> /// <param name="attribute">ID of the attribute according to RFC2865.</param> /// <param name="value">The value of the attribute.</param> public RadiusAttribute(RadiusAttributeType attribute, object value) : this((int)attribute, value) { }
public void SetAttributes(RadiusAttributeType type, byte[] data) { attributes.Add(new RadiusAttribute(type, data)); }
public RadiusAttribute(RadiusAttributeType type, byte[] data) { this.type = type; this.data = data; }
/// <summary> /// Creates a RADIUS attribute that has a string value type /// </summary> /// <param name="type"></param> /// <param name="data">UTF8 will be used for encoding</param> /// <returns></returns> public static RadiusAttribute CreateString(RadiusAttributeType type, string data) { return(new RadiusAttribute(type, Encoding.UTF8.GetBytes(data))); }
public RadiusAttribute(RadiusAttributeType type) { Type = type; }
public static RadiusAttribute CreateUInt64(RadiusAttributeType type, ulong data) { return(new RadiusAttribute(type, Utils.GetNetworkBytes(data))); }
/// <summary> /// Creates a RADIUS attribute that has a string value type /// </summary> /// <param name="type"></param> /// <param name="data">UTF8 will be used for encoding</param> /// <returns></returns> public static RadiusAttribute CreateString(RadiusAttributeType type, string data) { return new RadiusAttribute(type, Encoding.UTF8.GetBytes(data)); }
public static RadiusAttribute CreateInt64(RadiusAttributeType type, long data) { return new RadiusAttribute(type, Utils.GetNetworkBytes(data)); }
public RadiusAttribute(RadiusAttributeType type, Int32 value) { data = new byte[BUFFER_SIZE]; TypeField = (byte)type; IntegerValue = value; }
/// <summary> /// Initializes a new instance of the <see cref="RadiusAttribute"/> class. /// </summary> /// <param name="attribute">ID of the attribute according to RFC2865.</param> /// <param name="value">The value of the attribute.</param> public RadiusAttribute(RadiusAttributeType attribute, object value) : this((int)attribute, value) { }
public RadiusAttribute(RadiusAttributeType type) { Type = type; }
public RadiusAttributeTypeAttribute(RadiusAttributeType attributeType) { AttributeType = attributeType; }
public bool HasAttribute(RadiusAttributeType attrib) { return(this.Attributes.Any(attr => attr.AttributeType != null && attr.AttributeType.Value == attrib)); }
public RadiusAttribute(RadiusAttributeType type, IPAddress value) { data = new byte[BUFFER_SIZE]; TypeField = (byte)type; AddressValue = value; }
public static RadiusAttribute CreateUInt16(RadiusAttributeType type, ushort data) { return new RadiusAttribute(type, Utils.GetNetworkBytes(data)); }
public static RadiusAttribute CreateInt32(RadiusAttributeType type, int data) { return(new RadiusAttribute(type, Utils.GetNetworkBytes(data))); }
/// <summary> /// Constructs an attribute with a 32-bit integer value. /// </summary> /// <param name="type">The RADIUS attribute type code.</param> /// <param name="value">The integer attribute data.</param> public RadiusAttribute(RadiusAttributeType type, int value) { this.Type = type; this.Value = new byte[] { (byte)((value >> 24) & 0xFF), (byte)((value >> 16) & 0xFF), (byte)((value >> 8) & 0xFF), (byte)(value & 0xFF) }; }
public void SetAttributes(RadiusAttributeType type, byte[] data) { attributes.Add(new RadiusAttribute(type,data)); }
public RadiusAttribute(RadiusAttributeType type, byte[] data) { this.type = type; this.data = data; }
public RadiusAttribute(RadiusAttributeType type, byte[] value) { data = new byte[BUFFER_SIZE]; TypeField = (byte)type; StringValue = value; }