コード例 #1
0
        /// <summary>
        /// Parses data received from radius server and creates Attribute.
        /// </summary>
        public static Attribute Parse(byte[] source)
        {
            if (source.Length < 3)
                return null;

            //var attributeTypes = new List<byte>();
            //attributeTypes.AddRange(Enum.GetValues(typeof (AttributeType)));

            Attribute result = null;
            var attributeType = AttributeType.Unknown;

            foreach (int value in Enum.GetValues(typeof(AttributeType)))
                if (value == source[0])
                    attributeType = (AttributeType) value;

            var sourceValue = new byte[source.Length - 2];
            Array.Copy(source, 2, sourceValue, 0, sourceValue.Length);

            //TODO: Add other attributes
            switch (attributeType)
            {
                case AttributeType.NasIpAddress:
                    result = new IpAddressAttribute(attributeType, new IPAddress(sourceValue));
                    break;
                case AttributeType.ServiceType:
                    result = new ServiceTypeAttribute((ServiceType)BitConverter.ToInt32(sourceValue, 0));
                    break;
                case AttributeType.SessionTimeout:
                    result = new IntegerAttribute(attributeType, BitConverter.ToUInt32(sourceValue, 0));
                    break;
                case AttributeType.AcctAuthentic:
                    result =
                        new AuthenticationTypeAttribute(
                            (AuthenticationType) BitConverter.ToUInt32(sourceValue, 0));
                    break;
                case AttributeType.AcctStatusType:
                    result = new StatusTypeAttribute((StatusType)BitConverter.ToUInt32(sourceValue, 0));
                    break;
                case AttributeType.ReplyMessage:
                    result = new ReplyMessage(Encoding.UTF8.GetString(sourceValue));
                    break;
                default:
                    result = new StringAttribute(attributeType, Encoding.UTF8.GetString(sourceValue));
                    break;
            }

            return result;
        }