Пример #1
0
        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;
            }
        }
Пример #2
0
        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.'");
        }
Пример #3
0
        /// <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.");
            }
        }
Пример #4
0
        /// <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.");
            }
        }
Пример #5
0
        /// <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);
 }
Пример #7
0
		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);
		}
Пример #8
0
        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);
        }
Пример #10
0
        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;
            }
        }
Пример #11
0
        /// <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);
        }
Пример #12
0
        /// <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);
        }
Пример #13
0
        /// <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);
        }
Пример #14
0
        /// <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);
        }
Пример #15
0
        /// <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);
        }
Пример #16
0
 /// <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)
 {
 }
Пример #17
0
 public void SetAttributes(RadiusAttributeType type, byte[] data)
 {
     attributes.Add(new RadiusAttribute(type, data));
 }
Пример #18
0
 public RadiusAttribute(RadiusAttributeType type, byte[] data)
 {
     this.type = type;
     this.data = data;
 }
Пример #19
0
 /// <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)));
 }
Пример #20
0
 public RadiusAttribute(RadiusAttributeType type)
 {
     Type = type;
 }
Пример #21
0
 public static RadiusAttribute CreateUInt64(RadiusAttributeType type, ulong data)
 {
     return(new RadiusAttribute(type, Utils.GetNetworkBytes(data)));
 }
Пример #22
0
		/// <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));
		}
Пример #23
0
		public static RadiusAttribute CreateInt64(RadiusAttributeType type, long data)
		{
			return new RadiusAttribute(type, Utils.GetNetworkBytes(data));
		}
Пример #24
0
 public RadiusAttribute(RadiusAttributeType type, Int32 value)
 {
     data         = new byte[BUFFER_SIZE];
     TypeField    = (byte)type;
     IntegerValue = value;
 }
Пример #25
0
 /// <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)
 {
 }
Пример #26
0
		public RadiusAttribute(RadiusAttributeType type)
		{
			Type = type;
		}
 public RadiusAttributeTypeAttribute(RadiusAttributeType attributeType)
 {
     AttributeType = attributeType;
 }
Пример #28
0
 public bool HasAttribute(RadiusAttributeType attrib)
 {
     return(this.Attributes.Any(attr => attr.AttributeType != null && attr.AttributeType.Value == attrib));
 }
Пример #29
0
 public RadiusAttribute(RadiusAttributeType type, IPAddress value)
 {
     data         = new byte[BUFFER_SIZE];
     TypeField    = (byte)type;
     AddressValue = value;
 }
Пример #30
0
		public static RadiusAttribute CreateUInt16(RadiusAttributeType type, ushort data)
		{
			return new RadiusAttribute(type, Utils.GetNetworkBytes(data));
		}
Пример #31
0
 public static RadiusAttribute CreateInt32(RadiusAttributeType type, int data)
 {
     return(new RadiusAttribute(type, Utils.GetNetworkBytes(data)));
 }
Пример #32
0
 /// <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) };
 }
Пример #33
0
 public void SetAttributes(RadiusAttributeType type, byte[] data)
 {
     attributes.Add(new RadiusAttribute(type,data));
 }
Пример #34
0
 public RadiusAttribute(RadiusAttributeType type, byte[] data)
 {
     this.type = type;
     this.data = data;
 }
Пример #35
0
 public RadiusAttribute(RadiusAttributeType type, byte[] value)
 {
     data        = new byte[BUFFER_SIZE];
     TypeField   = (byte)type;
     StringValue = value;
 }