2 // System.Net.NetworkInformation.IPv4InterfaceProperties
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
7 // Marek Habersack (mhabersack@novell.com)
9 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.InteropServices;
33 namespace System.Net.NetworkInformation {
34 abstract class UnixIPv4InterfaceProperties : IPv4InterfaceProperties
36 protected UnixNetworkInterface iface;
38 public UnixIPv4InterfaceProperties (UnixNetworkInterface iface)
43 public override int Index {
44 get { return iface.NameIndex; }
47 // TODO: how to discover that?
48 public override bool IsAutomaticPrivateAddressingActive {
52 // TODO: how to discover that?
53 public override bool IsAutomaticPrivateAddressingEnabled {
57 // TODO: how to discover that? The only way is distribution-specific...
58 public override bool IsDhcpEnabled {
62 public override bool UsesWins {
67 sealed class LinuxIPv4InterfaceProperties : UnixIPv4InterfaceProperties
69 public LinuxIPv4InterfaceProperties (LinuxNetworkInterface iface)
74 public override bool IsForwardingEnabled {
76 string iface_path = "/proc/sys/net/ipv4/conf/" + iface.Name + "/forwarding";
78 if (File.Exists (iface_path)) {
79 string val = LinuxNetworkInterface.ReadLine (iface_path);
88 public override int Mtu {
90 string iface_path = (iface as LinuxNetworkInterface).IfacePath + "mtu";
93 if (File.Exists (iface_path)) {
94 string val = LinuxNetworkInterface.ReadLine (iface_path);
97 ret = Int32.Parse (val);
108 sealed class MacOsIPv4InterfaceProperties : UnixIPv4InterfaceProperties
110 public MacOsIPv4InterfaceProperties (MacOsNetworkInterface iface)
116 public override bool IsForwardingEnabled {
117 get { return false; }
121 public override int Mtu {
127 sealed class Win32IPv4InterfaceProperties : IPv4InterfaceProperties
129 [DllImport ("iphlpapi.dll")]
130 static extern int GetPerAdapterInfo (int IfIndex, Win32_IP_PER_ADAPTER_INFO pPerAdapterInfo, ref int pOutBufLen);
132 Win32_IP_ADAPTER_INFO ainfo;
133 Win32_IP_PER_ADAPTER_INFO painfo;
136 public Win32IPv4InterfaceProperties (Win32_IP_ADAPTER_INFO ainfo, Win32_MIB_IFROW mib)
141 // get per-adapter info.
143 GetPerAdapterInfo (mib.Index, null, ref size);
144 painfo = new Win32_IP_PER_ADAPTER_INFO ();
145 int ret = GetPerAdapterInfo (mib.Index, painfo, ref size);
147 throw new NetworkInformationException (ret);
150 public override int Index {
151 get { return mib.Index; }
154 public override bool IsAutomaticPrivateAddressingActive {
155 get { return painfo.AutoconfigActive != 0; }
158 public override bool IsAutomaticPrivateAddressingEnabled {
159 get { return painfo.AutoconfigEnabled != 0; }
162 public override bool IsDhcpEnabled {
163 get { return ainfo.DhcpEnabled != 0; }
166 public override bool IsForwardingEnabled {
167 // Is it the right answer? In Vista there is MIB_IPINTERFACEROW.ForwardingEnabled, but not in former versions.
168 get { return Win32_FIXED_INFO.Instance.EnableRouting != 0; }
171 public override int Mtu {
172 get { return mib.Mtu; }
175 public override bool UsesWins {
176 get { return ainfo.HaveWins; }
180 [StructLayout (LayoutKind.Sequential)]
181 class Win32_IP_PER_ADAPTER_INFO
183 public uint AutoconfigEnabled;
184 public uint AutoconfigActive;
185 public IntPtr CurrentDnsServer; // to Win32_IP_ADDR_STRING
186 public Win32_IP_ADDR_STRING DnsServerList;