コード例 #1
0
        private void ParseFile(string dictionaryFilePath)
        {
            using (var reader = new StreamReader(dictionaryFilePath))
            {
                var  vendorName = string.Empty;
                uint vendorId   = 0;

                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line.StartsWith("$INCLUDE"))
                    {
                        var lineParts = SplitLine(line);
                        var fileName  = lineParts[1];
                        var filePath  = Path.Combine(Path.GetDirectoryName(dictionaryFilePath), fileName);
                        ParseFile(filePath);
                    }
                    else if (line.StartsWith("VENDOR"))
                    {
                        var lineParts = SplitLine(line);
                        vendorName = lineParts[1];
                        vendorId   = Convert.ToUInt32(lineParts[2]);
                    }
                    else if (line.StartsWith("END-VENDOR"))
                    {
                        vendorName = string.Empty;
                        vendorId   = 0;
                    }
                    else if (line.StartsWith("ATTRIBUTE"))
                    {
                        var lineParts = SplitLine(line);
                        if (vendorId == 0)
                        {
                            var name = lineParts[1];
                            if (!byte.TryParse(lineParts[2], out var code))
                            {
                                continue;
                            }

                            var type      = lineParts[3];
                            var attribute = new DictionaryAttribute(name, code, type);
                            attributes[code]     = attribute;
                            attributeNames[name] = attribute;
                        }
                        else
                        {
                            var name = lineParts[1];
                            if (!uint.TryParse(lineParts[2], out var code))
                            {
                                continue;
                            }

                            var type      = lineParts[3];
                            var attribute = new DictionaryVendorAttribute(vendorId, name, code, type);
                            vendorSpecificAttributes.Add(attribute);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Load the dictionary from a dictionary file
        /// </summary>
        public RadiusDictionary(string dictionaryFilePath, ILogger <RadiusDictionary> logger)
        {
            _logger = logger;

            using (var sr = new StreamReader(dictionaryFilePath))
            {
                while (sr.Peek() >= 0)
                {
                    var line = sr.ReadLine();
                    if (line.StartsWith("Attribute"))
                    {
                        var lineparts = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        var key       = Convert.ToByte(lineparts[1]);

                        // If duplicates are encountered, the last one will prevail
                        if (Attributes.ContainsKey(key))
                        {
                            Attributes.Remove(key);
                        }
                        if (AttributeNames.ContainsKey(lineparts[2]))
                        {
                            AttributeNames.Remove(lineparts[2]);
                        }
                        var attributeDefinition = new DictionaryAttribute(lineparts[2], key, lineparts[3]);
                        Attributes.Add(key, attributeDefinition);
                        AttributeNames.Add(attributeDefinition.Name, attributeDefinition);
                    }

                    if (line.StartsWith("VendorSpecificAttribute"))
                    {
                        var lineparts = line.Split(new char[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        var vsa       = new DictionaryVendorAttribute(
                            Convert.ToUInt32(lineparts[1]),
                            lineparts[3],
                            Convert.ToUInt32(lineparts[2]),
                            lineparts[4]);

                        VendorSpecificAttributes.Add(vsa);

                        if (AttributeNames.ContainsKey(vsa.Name))
                        {
                            AttributeNames.Remove(vsa.Name);
                        }
                        AttributeNames.Add(vsa.Name, vsa);
                    }
                }

                _logger.LogInformation($"Parsed {Attributes.Count} attributes and {VendorSpecificAttributes.Count} vendor attributes from file");
            }
        }