/// <summary>
        /// Returns the value of the Radius attribute of the given type or null if
        /// there is no such attribute.
        /// @param type attribute type name
        /// @return value of the attribute as a string or null if there is no such
        /// attribute
        /// @throws ArgumentException if the type name is unknown
        /// @throws NotImplementedException attribute occurs multiple times
        /// </summary>
        public String GetSubAttributeValue(String type)
        {
            RadiusAttribute attr = GetSubAttribute(type);

            if (attr == null)
            {
                return(null);
            }
            else
            {
                return(attr.Value);
            }
        }
Example #2
0
        /// <summary>
        ///  Creates a RadiusAttribute object of the appropriate type.
        ///  @param dictionary Hashtable to use
        ///  @param vendorId vendor ID or -1
        ///  @param attributeType attribute type
        ///  @return RadiusAttribute object
        /// </summary>
        public static RadiusAttribute CreateRadiusAttribute(IWritableDictionary dictionary, int vendorId,
                                                            int attributeType)
        {
            var attribute = new RadiusAttribute();

            AttributeType at = dictionary.GetAttributeTypeByCode(vendorId, attributeType);

            if (at != null && at.Class != null)
            {
                try
                {
                    attribute = (RadiusAttribute)Activator.CreateInstance(at.Class);
                }
                catch (Exception e)
                {
                    // error instantiating class - should not occur
                }
            }

            attribute.Type       = attributeType;
            attribute.Dictionary = dictionary;
            attribute.VendorId   = vendorId;
            return(attribute);
        }
        /// <summary>
        ///  Removes the specified attribute from this packet.
        ///  @param attribute RadiusAttribute to remove
        /// </summary>
        public void RemoveAttribute(RadiusAttribute attribute)
        {
            if (attribute.VendorId == -1)
            {
                if (!_attributes.Remove(attribute))
                    throw new ArgumentException("no such attribute");
            }
            else
            {
                // remove Vendor-Specific sub-attribute
                IList<RadiusAttribute> vsas = GetVendorAttributes(attribute.VendorId);

                foreach (VendorSpecificAttribute vsa in vsas)
                {
                    List<RadiusAttribute> sas = vsa.SubAttributes;
                    if (sas.Contains(attribute))
                    {
                        vsa.RemoveSubAttribute(attribute);
                        if (sas.Count == 1)
                            // removed the last sub-attribute
                            // --> remove the whole Vendor-Specific attribute
                            RemoveAttribute(vsa);
                    }
                }
            }
        }
 /// <summary>
 ///  Adds a Radius attribute to this packet. Can also be used
 ///  to add Vendor-Specific sub-attributes. If a attribute with
 ///  a vendor code != -1 is passed in, a VendorSpecificAttribute
 ///  is created for the sub-attribute.
 ///  @param attribute RadiusAttribute object
 /// </summary>
 public void AddAttribute(RadiusAttribute attribute)
 {
     if (attribute == null)
         throw new ArgumentNullException("attribute", "attribute is null");
     attribute.Dictionary = Dictionary;
     if (attribute.VendorId == -1)
         _attributes.Add(attribute);
     else
     {
         var vsa = new VendorSpecificAttribute(attribute.VendorId);
         vsa.AddSubAttribute(attribute);
         _attributes.Add(vsa);
     }
 }
        /// <summary>
        /// Reads a Vendor-Specific attribute and decodes the internal sub-attribute
        /// structure.
        /// @see TinyRadius.attribute.RadiusAttribute#readAttribute(byte[], int,
        /// int)
        /// </summary>
        public override void ReadAttribute(byte[] data, int offset, int length)
        {
            // check Length
            if (length < 6)
            {
                throw new RadiusException("Vendor-Specific attribute too short: "
                                          + length);
            }

            int vsaCode = data[offset];
            int vsaLen  = (data[offset + 1] & 0x000000ff) - 6;

            if (vsaCode != VENDOR_SPECIFIC)
            {
                throw new RadiusException("not a Vendor-Specific attribute");
            }

            // read vendor ID and vendor data

            /*
             * int vendorId = (data[offset + 2] << 24 | data[offset + 3] << 16 |
             * data[offset + 4] << 8 | ((int)data[offset + 5] & 0x000000ff));
             */
            int vendorId = (UnsignedByteToInt(data[offset + 2]) << 24
                            | UnsignedByteToInt(data[offset + 3]) << 16
                            | UnsignedByteToInt(data[offset + 4]) << 8 | UnsignedByteToInt(data[offset + 5]));

            ChildVendorId = vendorId;

            // validate sub-attribute structure
            int pos   = 0;
            int count = 0;

            while (pos < vsaLen)
            {
                if (pos + 1 >= vsaLen)
                {
                    throw new RadiusException("Vendor-Specific attribute malformed");
                }
                // int vsaSubType = data[(offset + 6) + pos] & 0x0ff;
                int vsaSubLen = data[(offset + 6) + pos + 1] & 0x0ff;
                pos += vsaSubLen;
                count++;
            }
            if (pos != vsaLen)
            {
                throw new RadiusException("Vendor-Specific attribute malformed");
            }

            subAttributes = new List <RadiusAttribute>(count);
            pos           = 0;
            while (pos < vsaLen)
            {
                int             subtype   = data[(offset + 6) + pos] & 0x0ff;
                int             sublength = data[(offset + 6) + pos + 1] & 0x0ff;
                RadiusAttribute a         = CreateRadiusAttribute(Dictionary,
                                                                  vendorId, subtype);
                a.ReadAttribute(data, (offset + 6) + pos, sublength);
                subAttributes.Add(a);
                pos += sublength;
            }
        }
        /// <summary>
        ///  Creates a RadiusAttribute object of the appropriate type.
        ///  @param dictionary Hashtable to use
        ///  @param vendorId vendor ID or -1
        ///  @param attributeType attribute type
        ///  @return RadiusAttribute object
        /// </summary>
        public static RadiusAttribute CreateRadiusAttribute(IWritableDictionary dictionary, int vendorId,
                                                            int attributeType)
        {
            var attribute = new RadiusAttribute();

            AttributeType at = dictionary.GetAttributeTypeByCode(vendorId, attributeType);
            if (at != null && at.Class != null)
            {
                try
                {
                    attribute = (RadiusAttribute) Activator.CreateInstance(at.Class);
                }
                catch (Exception e)
                {
                    // error instantiating class - should not occur
                }
            }

            attribute.Type = attributeType;
            attribute.Dictionary = dictionary;
            attribute.VendorId = vendorId;
            return attribute;
        }