1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
4 using System.Collections.Generic;
6 using System.Runtime.Versioning;
8 namespace System.Net.NetworkInformation
10 internal sealed class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
12 private readonly LinuxNetworkInterface _linuxNetworkInterface;
13 private readonly GatewayIPAddressInformationCollection _gatewayAddresses;
14 private readonly IPAddressCollection _dhcpServerAddresses;
15 private readonly IPAddressCollection _winsServerAddresses;
16 private readonly LinuxIPv4InterfaceProperties _ipv4Properties;
17 private readonly LinuxIPv6InterfaceProperties _ipv6Properties;
19 public LinuxIPInterfaceProperties(LinuxNetworkInterface lni, LinuxNetworkInterface.LinuxNetworkInterfaceSystemProperties systemProperties)
20 : base(lni, globalConfig: true)
22 _linuxNetworkInterface = lni;
23 _gatewayAddresses = GetGatewayAddresses(systemProperties);
24 _dhcpServerAddresses = GetDhcpServerAddresses();
25 _winsServerAddresses = GetWinsServerAddresses();
26 _dnsSuffix = systemProperties.DnsSuffix;
27 _dnsAddresses = systemProperties.DnsAddresses;
28 _ipv4Properties = new LinuxIPv4InterfaceProperties(lni);
29 _ipv6Properties = new LinuxIPv6InterfaceProperties(lni);
32 [UnsupportedOSPlatform("linux")]
33 public override bool IsDynamicDnsEnabled { get { throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform); } }
35 [UnsupportedOSPlatform("linux")]
36 public override IPAddressInformationCollection AnycastAddresses { get { throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform); } }
38 public override GatewayIPAddressInformationCollection GatewayAddresses { get { return _gatewayAddresses; } }
40 public override IPAddressCollection DhcpServerAddresses { get { return _dhcpServerAddresses; } }
42 public override IPAddressCollection WinsServersAddresses { get { return _winsServerAddresses; } }
44 public override IPv4InterfaceProperties GetIPv4Properties()
46 return _ipv4Properties;
49 public override IPv6InterfaceProperties GetIPv6Properties()
51 return _ipv6Properties;
54 // /proc/net/route contains some information about gateway addresses,
55 // and separates the information about by each interface.
56 public GatewayIPAddressInformationCollection GetGatewayAddresses(LinuxNetworkInterface.LinuxNetworkInterfaceSystemProperties systemProperties)
58 List<GatewayIPAddressInformation> collection = new List<GatewayIPAddressInformation>();
60 if (systemProperties.IPv4Routes != null)
62 StringParsingHelpers.ParseIPv4GatewayAddressesFromRouteFile(collection, systemProperties.IPv4Routes, _linuxNetworkInterface.Name);
65 if (systemProperties.IPv6Routes != null)
67 StringParsingHelpers.ParseIPv6GatewayAddressesFromRouteFile(collection, systemProperties.IPv6Routes, _linuxNetworkInterface.Name, _linuxNetworkInterface.Index);
70 return new GatewayIPAddressInformationCollection(collection);
73 private IPAddressCollection GetDhcpServerAddresses()
75 List<IPAddress> internalCollection = new List<IPAddress>();
77 StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, NetworkFiles.DHClientLeasesFile, _linuxNetworkInterface.Name);
78 StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, string.Format(NetworkFiles.DHClientInterfaceLeasesFile, _linuxNetworkInterface.Name), _linuxNetworkInterface.Name);
79 StringParsingHelpers.ParseDhcpServerAddressesFromLeasesFile(internalCollection, string.Format(NetworkFiles.DHClientSecondaryInterfaceLeasesFile, _linuxNetworkInterface.Name), _linuxNetworkInterface.Name);
81 return new InternalIPAddressCollection(internalCollection);
84 private static IPAddressCollection GetWinsServerAddresses()
86 List<IPAddress> internalCollection
87 = StringParsingHelpers.ParseWinsServerAddressesFromSmbConfFile(NetworkFiles.SmbConfFile);
88 return new InternalIPAddressCollection(internalCollection);