Пример #1
0
        private bool GetIsForwardingEnabled()
        {
            string[] paths = new string[]
            {
                // /proc/sys/net/ipv4/conf/<name>/forwarding
                Path.Join(NetworkFiles.Ipv4ConfigFolder, _linuxNetworkInterface.Name, NetworkFiles.ForwardingFileName),
                // Fall back to global forwarding config /proc/sys/net/ipv4/ip_forward
                NetworkFiles.Ipv4GlobalForwardingFile
            };

            for (int i = 0; i < paths.Length; i++)
            {
                // Actual layout is specific to kernel version and it could change over time.
                // If the kernel version we're running on doesn't have this files we don't want to fail, but instead continue. We've hit this exceptions in Windows Subsystem for Linux in the past.
                // Also the /proc directory may not be mounted or accessible for other reasons. Therefore we catch these potential exceptions and return false instead.
                try
                {
                    return(StringParsingHelpers.ParseRawIntFile(paths[i]) == 1);
                }
                catch (IOException) { }
                catch (UnauthorizedAccessException) { }
            }

            return(false);
        }
Пример #2
0
            internal LinuxNetworkInterfaceSystemProperties()
            {
                if (File.Exists(NetworkFiles.Ipv4RouteFile))
                {
                    try
                    {
                        IPv4Routes = File.ReadAllLines(NetworkFiles.Ipv4RouteFile);
                    }
                    catch (UnauthorizedAccessException)
                    {
                    }
                }

                if (File.Exists(NetworkFiles.Ipv6RouteFile))
                {
                    try
                    {
                        IPv6Routes = File.ReadAllLines(NetworkFiles.Ipv6RouteFile);
                    }
                    catch (UnauthorizedAccessException)
                    {
                    }
                }

                try
                {
                    string resolverConfig = File.ReadAllText(NetworkFiles.EtcResolvConfFile);
                    DnsSuffix    = StringParsingHelpers.ParseDnsSuffixFromResolvConfFile(resolverConfig);
                    DnsAddresses = new InternalIPAddressCollection(StringParsingHelpers.ParseDnsAddressesFromResolvConfFile(resolverConfig));
                }
                catch (Exception e) when(e is FileNotFoundException || e is UnauthorizedAccessException)
                {
                }
            }
Пример #3
0
        private bool GetIsForwardingEnabled()
        {
            // /proc/sys/net/ipv4/conf/<name>/forwarding
            string path = Path.Combine(NetworkFiles.Ipv4ConfigFolder, _linuxNetworkInterface.Name, NetworkFiles.ForwardingFileName);

            return(StringParsingHelpers.ParseRawIntFile(path) == 1);
        }
        private IPAddressCollection GetWinsServerAddresses()
        {
            List <IPAddress> internalCollection
                = StringParsingHelpers.ParseWinsServerAddressesFromSmbConfFile(NetworkFiles.SmbConfFile);

            return(new InternalIPAddressCollection(internalCollection));
        }
Пример #5
0
        // /proc/net/route contains some information about gateway addresses,
        // and seperates the information about by each interface.
        public GatewayIPAddressInformationCollection GetGatewayAddresses()
        {
            List <GatewayIPAddressInformation> innerCollection
                = StringParsingHelpers.ParseGatewayAddressesFromRouteFile(NetworkFiles.Ipv4RouteFile, _linuxNetworkInterface.Name);

            return(new GatewayIPAddressInformationCollection(innerCollection));
        }
        private IPAddressCollection GetDhcpServerAddresses()
        {
            List <IPAddress> internalCollection
                = StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(NetworkFiles.DHClientLeasesFile, _linuxNetworkInterface.Name);

            return(new InternalIPAddressCollection(internalCollection));
        }
Пример #7
0
        public LinuxIPGlobalStatistics(bool ipv4)
        {
            if (ipv4)
            {
                _table         = StringParsingHelpers.ParseIPv4GlobalStatisticsFromSnmpFile(NetworkFiles.SnmpV4StatsFile);
                _numRoutes     = StringParsingHelpers.ParseNumRoutesFromRouteFile(NetworkFiles.Ipv4RouteFile);
                _numInterfaces = StringParsingHelpers.ParseNumIPInterfaces(NetworkFiles.Ipv4ConfigFolder);
            }
            else
            {
                _table         = StringParsingHelpers.ParseIPv6GlobalStatisticsFromSnmp6File(NetworkFiles.SnmpV6StatsFile);
                _numRoutes     = StringParsingHelpers.ParseNumRoutesFromRouteFile(NetworkFiles.Ipv6RouteFile);
                _numInterfaces = StringParsingHelpers.ParseNumIPInterfaces(NetworkFiles.Ipv6ConfigFolder);

                // /proc/sys/net/ipv6/conf/default/forwarding
                string forwardingConfigFile = Path.Combine(NetworkFiles.Ipv6ConfigFolder,
                                                           NetworkFiles.DefaultNetworkInterfaceFileName,
                                                           NetworkFiles.ForwardingFileName);
                _table.Forwarding = StringParsingHelpers.ParseRawIntFile(forwardingConfigFile) == 1;

                // snmp6 does not include Default TTL info. Read it from snmp.
                _table.DefaultTtl = StringParsingHelpers.ParseDefaultTtlFromFile(NetworkFiles.SnmpV4StatsFile);
            }

            _numIPAddresses = GetNumIPAddresses();
        }
        public LinuxIPInterfaceStatistics(string name)
        {
            _table = StringParsingHelpers.ParseInterfaceStatisticsTableFromFile(NetworkFiles.InterfaceListingFile, name);

            // sys/class/net/<interfacename>/tx_queue_len
            string transmitQueueLengthFilePath = Path.Combine(NetworkFiles.SysClassNetFolder, name, NetworkFiles.TransmitQueueLengthFileName);

            _transmitQueueLength = StringParsingHelpers.ParseRawIntFile(transmitQueueLengthFilePath);
        }
Пример #9
0
        private static bool GetSupportsMulticast(string name)
        {
            // /sys/class/net/<interface_name>/flags
            string path = Path.Combine(NetworkFiles.SysClassNetFolder, name, NetworkFiles.FlagsFileName);

            Interop.LinuxNetDeviceFlags flags = (Interop.LinuxNetDeviceFlags)StringParsingHelpers.ParseRawHexFileAsInt(path);

            return((flags & Interop.LinuxNetDeviceFlags.IFF_MULTICAST) == Interop.LinuxNetDeviceFlags.IFF_MULTICAST);
        }
Пример #10
0
        public LinuxTcpStatistics(bool ipv4)
        {
            _table = StringParsingHelpers.ParseTcpGlobalStatisticsFromSnmpFile(NetworkFiles.SnmpV4StatsFile);

            string sockstatFile = ipv4 ? NetworkFiles.SockstatFile : NetworkFiles.Sockstat6File;
            string protoName    = ipv4 ? "TCP" : "TCP6";

            _currentConnections = StringParsingHelpers.ParseNumSocketConnections(sockstatFile, protoName);
        }
Пример #11
0
        private IPAddressCollection GetDhcpServerAddresses()
        {
            List <IPAddress> internalCollection = new List <IPAddress>();

            StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, NetworkFiles.DHClientLeasesFile, _linuxNetworkInterface.Name);
            StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, string.Format(NetworkFiles.DHClientInterfaceLeasesFile, _linuxNetworkInterface.Name), _linuxNetworkInterface.Name);
            StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, string.Format(NetworkFiles.DHClientSecondaryInterfaceLeasesFile, _linuxNetworkInterface.Name), _linuxNetworkInterface.Name);

            return(new InternalIPAddressCollection(internalCollection));
        }
Пример #12
0
 private static string?GetDnsSuffix()
 {
     try
     {
         return(StringParsingHelpers.ParseDnsSuffixFromResolvConfFile(File.ReadAllText(NetworkFiles.EtcResolvConfFile)));
     }
     catch (FileNotFoundException)
     {
         return(null);
     }
 }
Пример #13
0
 private static IPAddressCollection?GetDnsAddresses()
 {
     try
     {
         List <IPAddress> internalAddresses = StringParsingHelpers.ParseDnsAddressesFromResolvConfFile(File.ReadAllText(NetworkFiles.EtcResolvConfFile));
         return(new InternalIPAddressCollection(internalAddresses));
     }
     catch (FileNotFoundException)
     {
         return(null);
     }
 }
 private static long?GetSpeed(string name)
 {
     try
     {
         string path = Path.Combine(NetworkFiles.SysClassNetFolder, name, NetworkFiles.SpeedFileName);
         return(StringParsingHelpers.ParseRawLongFile(path));
     }
     catch (Exception) // Ignore any problems accessing or parsing the file.
     {
         return(null);
     }
 }
Пример #15
0
 private static long?GetSpeed(string name)
 {
     try
     {
         string path = Path.Combine(NetworkFiles.SysClassNetFolder, name, NetworkFiles.SpeedFileName);
         return(StringParsingHelpers.ParseRawLongFile(path));
     }
     catch (IOException) // Some interfaces may give an "Invalid argument" error when opening this file.
     {
         return(null);
     }
 }
Пример #16
0
 public LinuxUdpStatistics(bool ipv4)
 {
     if (ipv4)
     {
         _table        = StringParsingHelpers.ParseUdpv4GlobalStatisticsFromSnmpFile(NetworkFiles.SnmpV4StatsFile);
         _udpListeners = StringParsingHelpers.ParseNumSocketConnections(NetworkFiles.SockstatFile, "UDP");
     }
     else
     {
         _table        = StringParsingHelpers.ParseUdpv6GlobalStatisticsFromSnmp6File(NetworkFiles.SnmpV6StatsFile);
         _udpListeners = StringParsingHelpers.ParseNumSocketConnections(NetworkFiles.Sockstat6File, "UDP6");
     }
 }
Пример #17
0
 private static long?GetSpeed(string name)
 {
     try
     {
         string path = Path.Combine(NetworkFiles.SysClassNetFolder, name, NetworkFiles.SpeedFileName);
         long   megabitsPerSecond = StringParsingHelpers.ParseRawLongFile(path);
         return(megabitsPerSecond == -1
             ? megabitsPerSecond
             : megabitsPerSecond * 1_000_000); // Value must be returned in bits per second, not megabits.
     }
     catch (Exception)                         // Ignore any problems accessing or parsing the file.
     {
         return(null);
     }
 }
        // /proc/net/route contains some information about gateway addresses,
        // and separates the information about by each interface.
        public GatewayIPAddressInformationCollection GetGatewayAddresses(LinuxNetworkInterface.LinuxNetworkInterfaceSystemProperties systemProperties)
        {
            List <GatewayIPAddressInformation> collection = new List <GatewayIPAddressInformation>();

            if (systemProperties.IPv4Routes != null)
            {
                StringParsingHelpers.ParseIPv4GatewayAddressesFromRouteFile(collection, systemProperties.IPv4Routes, _linuxNetworkInterface.Name);
            }

            if (systemProperties.IPv6Routes != null)
            {
                StringParsingHelpers.ParseIPv6GatewayAddressesFromRouteFile(collection, systemProperties.IPv6Routes, _linuxNetworkInterface.Name, _linuxNetworkInterface.Index);
            }

            return(new GatewayIPAddressInformationCollection(collection));
        }
        // /proc/net/route contains some information about gateway addresses,
        // and separates the information about by each interface.
        public GatewayIPAddressInformationCollection GetGatewayAddresses()
        {
            List <GatewayIPAddressInformation> collection = new List <GatewayIPAddressInformation>();

            if (File.Exists(NetworkFiles.Ipv4RouteFile))
            {
                StringParsingHelpers.ParseIPv4GatewayAddressesFromRouteFile(collection, NetworkFiles.Ipv4RouteFile, _linuxNetworkInterface.Name);
            }

            if (File.Exists(NetworkFiles.Ipv6RouteFile))
            {
                StringParsingHelpers.ParseIPv6GatewayAddressesFromRouteFile(collection, NetworkFiles.Ipv6RouteFile, _linuxNetworkInterface.Name, _linuxNetworkInterface.Index);
            }

            return(new GatewayIPAddressInformationCollection(collection));
        }
Пример #20
0
        private static bool?GetSupportsMulticast(string name)
        {
            // /sys/class/net/<interface_name>/flags
            string path = Path.Combine(NetworkFiles.SysClassNetFolder, name, NetworkFiles.FlagsFileName);

            if (File.Exists(path))
            {
                try
                {
                    Interop.LinuxNetDeviceFlags flags = (Interop.LinuxNetDeviceFlags)StringParsingHelpers.ParseRawHexFileAsInt(path);
                    return((flags & Interop.LinuxNetDeviceFlags.IFF_MULTICAST) == Interop.LinuxNetDeviceFlags.IFF_MULTICAST);
                }
                catch (Exception) // Ignore any problems accessing or parsing the file.
                {
                }
            }

            return(null);
        }
Пример #21
0
 public override IPEndPoint[] GetActiveUdpListeners()
 {
     return(StringParsingHelpers.ParseActiveUdpListenersFromFiles(Socket.OSSupportsIPv4 ? NetworkFiles.Udp4ConnectionsFile : null,
                                                                  Socket.OSSupportsIPv6 ? NetworkFiles.Udp6ConnectionsFile : null));
 }
Пример #22
0
 private static string GetDnsSuffix()
 {
     return(StringParsingHelpers.ParseDnsSuffixFromResolvConfFile(NetworkFiles.EtcResolvConfFile));
 }
Пример #23
0
 public override TcpConnectionInformation[] GetActiveTcpConnections()
 {
     return(StringParsingHelpers.ParseActiveTcpConnectionsFromFiles(Socket.OSSupportsIPv4 ? NetworkFiles.Tcp4ConnectionsFile : null,
                                                                    Socket.OSSupportsIPv6 ? NetworkFiles.Tcp6ConnectionsFile : null));
 }
Пример #24
0
        private static IPAddressCollection GetDnsAddresses()
        {
            List <IPAddress> internalAddresses = StringParsingHelpers.ParseDnsAddressesFromResolvConfFile(NetworkFiles.EtcResolvConfFile);

            return(new InternalIPAddressCollection(internalAddresses));
        }
 public override TcpConnectionInformation[] GetActiveTcpConnections()
 {
     return(StringParsingHelpers.ParseActiveTcpConnectionsFromFiles(NetworkFiles.Tcp4ConnectionsFile, NetworkFiles.Tcp6ConnectionsFile));
 }
Пример #26
0
        private int GetMtu()
        {
            string path = path = Path.Combine(NetworkFiles.SysClassNetFolder, _linuxNetworkInterface.Name, NetworkFiles.MtuFileName);

            return(StringParsingHelpers.ParseRawIntFile(path));
        }
 public override IPEndPoint[] GetActiveUdpListeners()
 {
     return(StringParsingHelpers.ParseActiveUdpListenersFromFiles(NetworkFiles.Udp4ConnectionsFile, NetworkFiles.Udp6ConnectionsFile));
 }
Пример #28
0
 // The table is a fairly large struct (108 bytes), pass it by reference
 public LinuxIcmpV4Statistics()
 {
     _table = StringParsingHelpers.ParseIcmpv4FromSnmpFile(NetworkFiles.SnmpV4StatsFile);
 }