Example #1
0
        public static void MalformedLine(string data)
        {
            RowConfigReader rcr = new RowConfigReader(data);
            string          unused;

            Assert.False(rcr.TryGetNextValue("key", out unused));
        }
Example #2
0
        public static UdpStatistics CreateUdpIPv6Statistics()
        {
            LinuxUdpStatistics stats = new LinuxUdpStatistics();

            string fileContents = File.ReadAllText(NetworkFiles.SnmpV6StatsFile);

            RowConfigReader reader = new RowConfigReader(fileContents);

            stats._inDatagrams = reader.GetNextValueAsInt32("Udp6InDatagrams");
            stats._noPorts = reader.GetNextValueAsInt32("Udp6NoPorts");
            stats._inErrors = reader.GetNextValueAsInt32("Udp6InErrors");
            stats._outDatagrams = reader.GetNextValueAsInt32("Udp6OutDatagrams");
            stats._rcvbufErrors = reader.GetNextValueAsInt32("Udp6RcvbufErrors");
            stats._sndbufErrors = reader.GetNextValueAsInt32("Udp6SndbufErrors");
            stats._inCsumErrors = reader.GetNextValueAsInt32("Udp6InCsumErrors");

            // Parse the number of active connections out of /proc/net/sockstat6
            string sockstatFile = File.ReadAllText(NetworkFiles.Sockstat6File);
            int indexOfUdp = sockstatFile.IndexOf("UDP6:");
            int endOfUdpLine = sockstatFile.IndexOf(Environment.NewLine, indexOfUdp + 1);
            string udpLineData = sockstatFile.Substring(indexOfUdp, endOfUdpLine - indexOfUdp);
            StringParser sockstatParser = new StringParser(udpLineData, ' ');
            sockstatParser.MoveNextOrFail(); // Skip "UDP6:"
            sockstatParser.MoveNextOrFail(); // Skip: "inuse"
            stats._udpListeners = sockstatParser.ParseNextInt32();

            return stats;
        }
Example #3
0
        public static void KeyContainedInValue()
        {
            string          data = $"first keyFirstValue{Environment.NewLine}key value";
            RowConfigReader rcr  = new RowConfigReader(data);

            Assert.Equal("value", rcr.GetNextValue("key"));
        }
Example #4
0
 public static void BasicTestCaseInsensitive(string data)
 {
     // Use a case-insensitive comparer and use differently-cased keys.
     RowConfigReader rcr = new RowConfigReader(data, StringComparison.OrdinalIgnoreCase);
     Assert.Equal("stringVal", rcr.GetNextValue("stringkey"));
     Assert.Equal(12, rcr.GetNextValueAsInt32("intkey"));
 }
Example #5
0
        public static void BasicTest(string data)
        {
            RowConfigReader rcr = new RowConfigReader(data);

            Assert.Equal("stringVal", rcr.GetNextValue("stringKey"));
            Assert.Equal(12, rcr.GetNextValueAsInt32("intKey"));
        }
Example #6
0
        public static void BasicTestCaseInsensitive(string data)
        {
            // Use a case-insensitive comparer and use differently-cased keys.
            RowConfigReader rcr = new RowConfigReader(data, StringComparison.OrdinalIgnoreCase);

            Assert.Equal("stringVal", rcr.GetNextValue("stringkey"));
            Assert.Equal(12, rcr.GetNextValueAsInt32("intkey"));
        }
Example #7
0
 public static void BasicTestWrongCase(string data)
 {
     // Default is Ordinal comparison. Keys with different case should not be found.
     RowConfigReader rcr = new RowConfigReader(data);
     string unused;
     Assert.False(rcr.TryGetNextValue("stringkey", out unused));
     Assert.False(rcr.TryGetNextValue("intkey", out unused));
 }
        internal static string ParseDnsSuffixFromResolvConfFile(string filePath)
        {
            string data = File.ReadAllText(filePath);
            RowConfigReader rcr = new RowConfigReader(data);
            string dnsSuffix;

            return rcr.TryGetNextValue("search", out dnsSuffix) ? dnsSuffix : string.Empty;
        }
Example #9
0
        public static void BasicTestWrongCase(string data)
        {
            // Default is Ordinal comparison. Keys with different case should not be found.
            RowConfigReader rcr = new RowConfigReader(data);
            string          unused;

            Assert.False(rcr.TryGetNextValue("stringkey", out unused));
            Assert.False(rcr.TryGetNextValue("intkey", out unused));
        }
Example #10
0
        public static void KeyDoesNotStartLine()
        {
            string data = $"key value{Environment.NewLine} key2 value2";
            RowConfigReader rcr = new RowConfigReader(data);

            string unused;
            Assert.False(rcr.TryGetNextValue("key2", out unused));

            // Can retrieve value if key includes the preceding space.
            Assert.Equal("value2", rcr.GetNextValue(" key2"));
        }
Example #11
0
        public static void KeyDoesNotStartLine()
        {
            string          data = $"key value{Environment.NewLine} key2 value2";
            RowConfigReader rcr  = new RowConfigReader(data);

            string unused;

            Assert.False(rcr.TryGetNextValue("key2", out unused));

            // Can retrieve value if key includes the preceding space.
            Assert.Equal("value2", rcr.GetNextValue(" key2"));
        }
Example #12
0
        public static void NewlineTests(string data)
        {
            // Test strings which have newlines mixed in between data pairs.

            RowConfigReader rcr = new RowConfigReader(data);

            Assert.Equal("00", rcr.GetNextValue("value0"));
            Assert.Equal(0, rcr.GetNextValueAsInt32("value0"));
            Assert.Equal(1, rcr.GetNextValueAsInt32("value1"));
            Assert.Equal("2", rcr.GetNextValue("value2"));
            Assert.Equal("3", rcr.GetNextValue("value3"));

            string unused;

            Assert.False(rcr.TryGetNextValue("Any", out unused));
        }
Example #13
0
        public static IcmpV6Statistics CreateIcmpV6Statistics()
        {
            LinuxIcmpV6Statistics stats = new LinuxIcmpV6Statistics();

            string fileContents = File.ReadAllText(NetworkFiles.SnmpV6StatsFile);

            RowConfigReader reader = new RowConfigReader(fileContents);

            stats._inMsgs = reader.GetNextValueAsInt32("Icmp6InMsgs");
            stats._inErrors = reader.GetNextValueAsInt32("Icmp6InErrors");
            stats._outMsgs = reader.GetNextValueAsInt32("Icmp6OutMsgs");
            stats._outErrors = reader.GetNextValueAsInt32("Icmp6OutErrors");
            stats._inDestUnreachs = reader.GetNextValueAsInt32("Icmp6InDestUnreachs");
            stats._inPktTooBigs = reader.GetNextValueAsInt32("Icmp6InPktTooBigs");
            stats._inTimeExcds = reader.GetNextValueAsInt32("Icmp6InTimeExcds");
            stats._inParmProblems = reader.GetNextValueAsInt32("Icmp6InParmProblems");
            stats._inEchos = reader.GetNextValueAsInt32("Icmp6InEchos");
            stats._inEchoReplies = reader.GetNextValueAsInt32("Icmp6InEchoReplies");
            stats._inGroupMembQueries = reader.GetNextValueAsInt32("Icmp6InGroupMembQueries");
            stats._inGroupMembResponses = reader.GetNextValueAsInt32("Icmp6InGroupMembResponses");
            stats._inGroupMembReductions = reader.GetNextValueAsInt32("Icmp6InGroupMembReductions");
            stats._inRouterSolicits = reader.GetNextValueAsInt32("Icmp6InRouterSolicits");
            stats._inRouterAdvertisements = reader.GetNextValueAsInt32("Icmp6InRouterAdvertisements");
            stats._inNeighborSolicits = reader.GetNextValueAsInt32("Icmp6InNeighborSolicits");
            stats._inNeighborAdvertisements = reader.GetNextValueAsInt32("Icmp6InNeighborAdvertisements");
            stats._inRedirects = reader.GetNextValueAsInt32("Icmp6InRedirects");
            stats._outDestUnreachs = reader.GetNextValueAsInt32("Icmp6OutDestUnreachs");
            stats._outPktTooBigs = reader.GetNextValueAsInt32("Icmp6OutPktTooBigs");
            stats._outTimeExcds = reader.GetNextValueAsInt32("Icmp6OutTimeExcds");
            stats._outParmProblems = reader.GetNextValueAsInt32("Icmp6OutParmProblems");
            stats._outEchos = reader.GetNextValueAsInt32("Icmp6OutEchos");
            stats._outEchoReplies = reader.GetNextValueAsInt32("Icmp6OutEchoReplies");
            stats._outInGroupMembQueries = reader.GetNextValueAsInt32("Icmp6OutGroupMembQueries");
            stats._outGroupMembResponses = reader.GetNextValueAsInt32("Icmp6OutGroupMembResponses");
            stats._outGroupMembReductions = reader.GetNextValueAsInt32("Icmp6OutGroupMembReductions");
            stats._outRouterSolicits = reader.GetNextValueAsInt32("Icmp6OutRouterSolicits");
            stats._outRouterAdvertisements = reader.GetNextValueAsInt32("Icmp6OutRouterAdvertisements");
            stats._outNeighborSolicits = reader.GetNextValueAsInt32("Icmp6OutNeighborSolicits");
            stats._outNeighborAdvertisements = reader.GetNextValueAsInt32("Icmp6OutNeighborAdvertisements");
            stats._outRedirects = reader.GetNextValueAsInt32("Icmp6OutRedirects");

            return stats;
        }
        internal static List<IPAddress> ParseDnsAddressesFromResolvConfFile(string filePath)
        {
            // Parse /etc/resolv.conf for all of the "nameserver" entries.
            // These are the DNS servers the machine is configured to use.
            // On OSX, this file is not directly used by most processes for DNS
            // queries/routing, but it is automatically generated instead, with
            // the machine's DNS servers listed in it.
            string data = File.ReadAllText(filePath);
            RowConfigReader rcr = new RowConfigReader(data);
            List<IPAddress> addresses = new List<IPAddress>();

            string addressString = null;
            while (rcr.TryGetNextValue("nameserver", out addressString))
            {
                IPAddress parsedAddress;
                if (IPAddress.TryParse(addressString, out parsedAddress))
                {
                    addresses.Add(parsedAddress);
                }
            }

            return addresses;
        }
Example #15
0
 public static void BasicTest(string data)
 {
     RowConfigReader rcr = new RowConfigReader(data);
     Assert.Equal("stringVal", rcr.GetNextValue("stringKey"));
     Assert.Equal(12, rcr.GetNextValueAsInt32("intKey"));
 }
        internal static UdpGlobalStatisticsTable ParseUdpv6GlobalStatisticsFromSnmp6File(string filePath)
        {
            string fileContents = File.ReadAllText(filePath);
            RowConfigReader reader = new RowConfigReader(fileContents);

            return new UdpGlobalStatisticsTable()
            {
                InDatagrams = reader.GetNextValueAsInt32("Udp6InDatagrams"),
                NoPorts = reader.GetNextValueAsInt32("Udp6NoPorts"),
                InErrors = reader.GetNextValueAsInt32("Udp6InErrors"),
                OutDatagrams = reader.GetNextValueAsInt32("Udp6OutDatagrams"),
                RcvbufErrors = reader.GetNextValueAsInt32("Udp6RcvbufErrors"),
                SndbufErrors = reader.GetNextValueAsInt32("Udp6SndbufErrors"),
                InCsumErrors = reader.GetNextValueAsInt32("Udp6InCsumErrors"),
            };
        }
Example #17
0
 public static void KeyContainedInValue()
 {
     string data = $"first keyFirstValue{Environment.NewLine}key value";
     RowConfigReader rcr = new RowConfigReader(data);
     Assert.Equal("value", rcr.GetNextValue("key"));
 }
Example #18
0
 public static void MalformedLine(string data)
 {
     RowConfigReader rcr = new RowConfigReader(data);
     string unused;
     Assert.False(rcr.TryGetNextValue("key", out unused));
 }
        /// <summary>
        /// Constructs an IPGlobalStatistics object from the snmp6 stats file.
        /// </summary>
        public static LinuxIPGlobalStatistics CreateIPv6Statistics()
        {
            LinuxIPGlobalStatistics stats = new LinuxIPGlobalStatistics();

            // /proc/sys/net/ipv6/conf/default/forwarding
            string path = Path.Combine(NetworkFiles.Ipv4ConfigFolder,
                                        NetworkFiles.DefaultNetworkInterfaceFileName,
                                        NetworkFiles.ForwardingFileName);
            stats._forwarding = int.Parse(File.ReadAllText(path)) == 1;

            // snmp6 does not include Default TTL info. Read it from snmp.
            string snmp4FileContents = File.ReadAllText(NetworkFiles.SnmpV4StatsFile);
            int firstIpHeader = snmp4FileContents.IndexOf("Ip:");
            int secondIpHeader = snmp4FileContents.IndexOf("Ip:", firstIpHeader + 1);
            int endOfSecondLine = snmp4FileContents.IndexOf(Environment.NewLine, secondIpHeader);
            string ipData = snmp4FileContents.Substring(secondIpHeader, endOfSecondLine - secondIpHeader);
            StringParser parser = new StringParser(ipData, ' ');
            parser.MoveNextOrFail(); // Skip Ip:
            // According to RFC 1213, "1" indicates "acting as a gateway". "2" indicates "NOT acting as a gateway".
            parser.MoveNextOrFail(); // Skip forwarding
            stats._defaultTtl = parser.ParseNextInt32();

            // Read the remainder of statistics from snmp6.
            string fileContents = File.ReadAllText(NetworkFiles.SnmpV6StatsFile);
            RowConfigReader reader = new RowConfigReader(fileContents);

            stats._inReceives = reader.GetNextValueAsInt32("Ip6InReceives");
            stats._inHeaderErrors = reader.GetNextValueAsInt32("Ip6InHdrErrors");
            stats._inAddressErrors = reader.GetNextValueAsInt32("Ip6InAddrErrors");
            stats._inUnknownProtocols = reader.GetNextValueAsInt32("Ip6InUnknownProtos");
            stats._inDiscards = reader.GetNextValueAsInt32("Ip6InDiscards");
            stats._inDelivers = reader.GetNextValueAsInt32("Ip6InDelivers");
            stats._forwardedDatagrams = reader.GetNextValueAsInt32("Ip6OutForwDatagrams");
            stats._outRequests = reader.GetNextValueAsInt32("Ip6OutRequests");
            stats._outDiscards = reader.GetNextValueAsInt32("Ip6OutDiscards");
            stats._outNoRoutes = reader.GetNextValueAsInt32("Ip6OutNoRoutes");
            stats._reassemblyTimeout = reader.GetNextValueAsInt32("Ip6ReasmTimeout");
            stats._reassemblyRequireds = reader.GetNextValueAsInt32("Ip6ReasmReqds");
            stats._reassemblyOKs = reader.GetNextValueAsInt32("Ip6ReasmOKs");
            stats._reassemblyFails = reader.GetNextValueAsInt32("Ip6ReasmFails");
            stats._fragmentOKs = reader.GetNextValueAsInt32("Ip6FragOKs");
            stats._fragmentFails = reader.GetNextValueAsInt32("Ip6FragFails");
            stats._fragmentCreates = reader.GetNextValueAsInt32("Ip6FragCreates");

            string routeFile = ReadProcConfigFile(NetworkFiles.Ipv6RouteFile);
            stats._numRoutes = CountOccurences(Environment.NewLine, routeFile);

            int interfaceCount = 0;
            var files = new DirectoryInfo(NetworkFiles.Ipv6ConfigFolder).GetFiles();
            foreach (var file in files)
            {
                if (file.Name != NetworkFiles.AllNetworkInterfaceFileName
                    && file.Name != NetworkFiles.DefaultNetworkInterfaceFileName)
                {
                    interfaceCount++;
                }
            }
            stats._numInterfaces = interfaceCount;
            stats._numIPAddresses = GetNumIPAddresses();
            return stats;
        }
Example #20
0
        public static void NewlineTests(string data)
        {
            // Test strings which have newlines mixed in between data pairs.

            RowConfigReader rcr = new RowConfigReader(data);
            Assert.Equal("00", rcr.GetNextValue("value0"));
            Assert.Equal(0, rcr.GetNextValueAsInt32("value0"));
            Assert.Equal(1, rcr.GetNextValueAsInt32("value1"));
            Assert.Equal("2", rcr.GetNextValue("value2"));
            Assert.Equal("3", rcr.GetNextValue("value3"));

            string unused;
            Assert.False(rcr.TryGetNextValue("Any", out unused));
        }
Example #21
0
 public static void StaticHelper(string data)
 {
     Assert.Equal("stringVal", RowConfigReader.ReadFirstValueFromString(data, "stringKey"));
     Assert.Equal("12", RowConfigReader.ReadFirstValueFromString(data, "intKey"));
 }
        public static Icmpv6StatisticsTable ParseIcmpv6FromSnmp6File(string filePath)
        {
            string fileContents = File.ReadAllText(filePath);
            RowConfigReader reader = new RowConfigReader(fileContents);

            return new Icmpv6StatisticsTable()
            {
                InMsgs = reader.GetNextValueAsInt32("Icmp6InMsgs"),
                InErrors = reader.GetNextValueAsInt32("Icmp6InErrors"),
                OutMsgs = reader.GetNextValueAsInt32("Icmp6OutMsgs"),
                OutErrors = reader.GetNextValueAsInt32("Icmp6OutErrors"),
                InDestUnreachs = reader.GetNextValueAsInt32("Icmp6InDestUnreachs"),
                InPktTooBigs = reader.GetNextValueAsInt32("Icmp6InPktTooBigs"),
                InTimeExcds = reader.GetNextValueAsInt32("Icmp6InTimeExcds"),
                InParmProblems = reader.GetNextValueAsInt32("Icmp6InParmProblems"),
                InEchos = reader.GetNextValueAsInt32("Icmp6InEchos"),
                InEchoReplies = reader.GetNextValueAsInt32("Icmp6InEchoReplies"),
                InGroupMembQueries = reader.GetNextValueAsInt32("Icmp6InGroupMembQueries"),
                InGroupMembResponses = reader.GetNextValueAsInt32("Icmp6InGroupMembResponses"),
                InGroupMembReductions = reader.GetNextValueAsInt32("Icmp6InGroupMembReductions"),
                InRouterSolicits = reader.GetNextValueAsInt32("Icmp6InRouterSolicits"),
                InRouterAdvertisements = reader.GetNextValueAsInt32("Icmp6InRouterAdvertisements"),
                InNeighborSolicits = reader.GetNextValueAsInt32("Icmp6InNeighborSolicits"),
                InNeighborAdvertisements = reader.GetNextValueAsInt32("Icmp6InNeighborAdvertisements"),
                InRedirects = reader.GetNextValueAsInt32("Icmp6InRedirects"),
                OutDestUnreachs = reader.GetNextValueAsInt32("Icmp6OutDestUnreachs"),
                OutPktTooBigs = reader.GetNextValueAsInt32("Icmp6OutPktTooBigs"),
                OutTimeExcds = reader.GetNextValueAsInt32("Icmp6OutTimeExcds"),
                OutParmProblems = reader.GetNextValueAsInt32("Icmp6OutParmProblems"),
                OutEchos = reader.GetNextValueAsInt32("Icmp6OutEchos"),
                OutEchoReplies = reader.GetNextValueAsInt32("Icmp6OutEchoReplies"),
                OutInGroupMembQueries = reader.GetNextValueAsInt32("Icmp6OutGroupMembQueries"),
                OutGroupMembResponses = reader.GetNextValueAsInt32("Icmp6OutGroupMembResponses"),
                OutGroupMembReductions = reader.GetNextValueAsInt32("Icmp6OutGroupMembReductions"),
                OutRouterSolicits = reader.GetNextValueAsInt32("Icmp6OutRouterSolicits"),
                OutRouterAdvertisements = reader.GetNextValueAsInt32("Icmp6OutRouterAdvertisements"),
                OutNeighborSolicits = reader.GetNextValueAsInt32("Icmp6OutNeighborSolicits"),
                OutNeighborAdvertisements = reader.GetNextValueAsInt32("Icmp6OutNeighborAdvertisements"),
                OutRedirects = reader.GetNextValueAsInt32("Icmp6OutRedirects")
            };
        }
        internal static IPGlobalStatisticsTable ParseIPv6GlobalStatisticsFromSnmp6File(string filePath)
        {
            // Read the remainder of statistics from snmp6.
            string fileContents = File.ReadAllText(filePath);
            RowConfigReader reader = new RowConfigReader(fileContents);

            return new IPGlobalStatisticsTable()
            {
                InReceives = reader.GetNextValueAsInt32("Ip6InReceives"),
                InHeaderErrors = reader.GetNextValueAsInt32("Ip6InHdrErrors"),
                InAddressErrors = reader.GetNextValueAsInt32("Ip6InAddrErrors"),
                InUnknownProtocols = reader.GetNextValueAsInt32("Ip6InUnknownProtos"),
                InDiscards = reader.GetNextValueAsInt32("Ip6InDiscards"),
                InDelivers = reader.GetNextValueAsInt32("Ip6InDelivers"),
                ForwardedDatagrams = reader.GetNextValueAsInt32("Ip6OutForwDatagrams"),
                OutRequests = reader.GetNextValueAsInt32("Ip6OutRequests"),
                OutDiscards = reader.GetNextValueAsInt32("Ip6OutDiscards"),
                OutNoRoutes = reader.GetNextValueAsInt32("Ip6OutNoRoutes"),
                ReassemblyTimeout = reader.GetNextValueAsInt32("Ip6ReasmTimeout"),
                ReassemblyRequireds = reader.GetNextValueAsInt32("Ip6ReasmReqds"),
                ReassemblyOKs = reader.GetNextValueAsInt32("Ip6ReasmOKs"),
                ReassemblyFails = reader.GetNextValueAsInt32("Ip6ReasmFails"),
                FragmentOKs = reader.GetNextValueAsInt32("Ip6FragOKs"),
                FragmentFails = reader.GetNextValueAsInt32("Ip6FragFails"),
                FragmentCreates = reader.GetNextValueAsInt32("Ip6FragCreates"),
            };
        }
        private static string GetDnsSuffix()
        {
            string data = File.ReadAllText(NetworkFiles.EtcResolvConfFile);
            RowConfigReader rcr = new RowConfigReader(data);
            string dnsSuffix;

            return rcr.TryGetNextValue("search", out dnsSuffix) ? dnsSuffix : string.Empty;
        }
        private static IPAddressCollection GetDnsAddresses()
        {
            // Parse /etc/resolv.conf for all of the "nameserver" entries.
            // These are the DNS servers the machine is configured to use.
            // On OSX, this file is not directly used by most processes for DNS
            // queries/routing, but it is automatically generated instead, with
            // the machine's DNS servers listed in it.
            string data = File.ReadAllText(NetworkFiles.EtcResolvConfFile);
            RowConfigReader rcr = new RowConfigReader(data);
            InternalIPAddressCollection addresses = new InternalIPAddressCollection();

            string addressString = null;
            while (rcr.TryGetNextValue("nameserver", out addressString))
            {
                IPAddress parsedAddress;
                if (IPAddress.TryParse(addressString, out parsedAddress))
                {
                    addresses.InternalAdd(parsedAddress);
                }
            }

            return addresses;
        }