0e18a9b87842a5c7f1ce8a151e5aeccff18a0203
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Runtime.Versioning;
7
8 namespace System.Net.NetworkInformation
9 {
10     internal sealed class LinuxIPInterfaceProperties : UnixIPInterfaceProperties
11     {
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;
18
19         public LinuxIPInterfaceProperties(LinuxNetworkInterface lni, LinuxNetworkInterface.LinuxNetworkInterfaceSystemProperties systemProperties)
20             : base(lni, globalConfig: true)
21         {
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);
30         }
31
32         [UnsupportedOSPlatform("linux")]
33         public override bool IsDynamicDnsEnabled { get { throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform); } }
34
35         [UnsupportedOSPlatform("linux")]
36         public override IPAddressInformationCollection AnycastAddresses { get { throw new PlatformNotSupportedException(SR.net_InformationUnavailableOnPlatform); } }
37
38         public override GatewayIPAddressInformationCollection GatewayAddresses { get { return _gatewayAddresses; } }
39
40         public override IPAddressCollection DhcpServerAddresses { get { return _dhcpServerAddresses; } }
41
42         public override IPAddressCollection WinsServersAddresses { get { return _winsServerAddresses; } }
43
44         public override IPv4InterfaceProperties GetIPv4Properties()
45         {
46             return _ipv4Properties;
47         }
48
49         public override IPv6InterfaceProperties GetIPv6Properties()
50         {
51             return _ipv6Properties;
52         }
53
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)
57         {
58             List<GatewayIPAddressInformation> collection = new List<GatewayIPAddressInformation>();
59
60             if (systemProperties.IPv4Routes != null)
61             {
62                 StringParsingHelpers.ParseIPv4GatewayAddressesFromRouteFile(collection, systemProperties.IPv4Routes, _linuxNetworkInterface.Name);
63             }
64
65             if (systemProperties.IPv6Routes != null)
66             {
67                 StringParsingHelpers.ParseIPv6GatewayAddressesFromRouteFile(collection, systemProperties.IPv6Routes, _linuxNetworkInterface.Name, _linuxNetworkInterface.Index);
68             }
69
70             return new GatewayIPAddressInformationCollection(collection);
71         }
72
73         private IPAddressCollection GetDhcpServerAddresses()
74         {
75             List<IPAddress> internalCollection = new List<IPAddress>();
76
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);
80
81             return new InternalIPAddressCollection(internalCollection);
82         }
83
84         private static IPAddressCollection GetWinsServerAddresses()
85         {
86             List<IPAddress> internalCollection
87                 = StringParsingHelpers.ParseWinsServerAddressesFromSmbConfFile(NetworkFiles.SmbConfFile);
88             return new InternalIPAddressCollection(internalCollection);
89         }
90     }
91 }