Merge pull request #323 from crazyjncsu/master
[mono.git] / mcs / class / System / System.Net.NetworkInformation / IPv4InterfaceProperties.cs
1 //
2 // System.Net.NetworkInformation.IPv4InterfaceProperties
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 //      Marek Habersack (mhabersack@novell.com)
8 //
9 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30 using System.IO;
31 using System.Runtime.InteropServices;
32
33 namespace System.Net.NetworkInformation {
34         public abstract class IPv4InterfaceProperties {
35                 protected IPv4InterfaceProperties ()
36                 {
37                 }
38
39                 public abstract int Index { get; }
40                 public abstract bool IsAutomaticPrivateAddressingActive { get; }
41                 public abstract bool IsAutomaticPrivateAddressingEnabled { get; }
42                 public abstract bool IsDhcpEnabled { get; }
43                 public abstract bool IsForwardingEnabled { get; }
44                 public abstract int Mtu { get; }
45                 public abstract bool UsesWins { get; }
46         }
47
48         abstract class UnixIPv4InterfaceProperties : IPv4InterfaceProperties
49         {
50                 protected UnixNetworkInterface iface;
51                 
52                 public UnixIPv4InterfaceProperties (UnixNetworkInterface iface)
53                 {
54                         this.iface = iface;
55                 }
56                 
57                 public override int Index {
58                         get { return UnixNetworkInterface.IfNameToIndex (iface.Name); }
59                 }
60
61                 // TODO: how to discover that?
62                 public override bool IsAutomaticPrivateAddressingActive {
63                         get { return false; }
64                 }
65
66                 // TODO: how to discover that?
67                 public override bool IsAutomaticPrivateAddressingEnabled {
68                         get { return false; }
69                 }
70
71                 // TODO: how to discover that? The only way is distribution-specific...
72                 public override bool IsDhcpEnabled {
73                         get { return false; }
74                 }
75         
76                 public override bool UsesWins {
77                         get { return false; }
78                 }
79         }
80         
81         sealed class LinuxIPv4InterfaceProperties : UnixIPv4InterfaceProperties
82         {
83                 public LinuxIPv4InterfaceProperties (LinuxNetworkInterface iface)
84                         : base (iface)
85                 {
86                 }
87                 
88                 public override bool IsForwardingEnabled {
89                         get {
90                                 string iface_path = "/proc/sys/net/ipv4/conf/" + iface.Name + "/forwarding";
91
92                                 if (File.Exists (iface_path)) {
93                                         string val = NetworkInterface.ReadLine (iface_path);
94
95                                         return val != "0";
96                                 }
97
98                                 return false;
99                         }
100                 }
101
102                 public override int Mtu {
103                         get {
104                                 string iface_path = (iface as LinuxNetworkInterface).IfacePath + "mtu";
105                                 int ret = 0;
106
107                                 if (File.Exists (iface_path)) {
108                                         string val = NetworkInterface.ReadLine (iface_path);
109                                         
110                                         try {
111                                                 ret = Int32.Parse (val);
112                                         } catch {
113                                         }
114                                 }
115
116                                 return ret;
117                                                 
118                         }
119                 }
120         }
121
122         sealed class MacOsIPv4InterfaceProperties : UnixIPv4InterfaceProperties
123         {
124                 public MacOsIPv4InterfaceProperties (MacOsNetworkInterface iface)
125                         : base (iface)
126                 {
127                 }
128
129                 // dummy
130                 public override bool IsForwardingEnabled {
131                         get { return false; }
132                 }
133
134                 // dummy
135                 public override int Mtu {
136                         get { return 0; }
137                 }
138         }
139         
140         sealed class Win32IPv4InterfaceProperties : IPv4InterfaceProperties
141         {
142                 [DllImport ("iphlpapi.dll")]
143                 static extern int GetPerAdapterInfo (int IfIndex, Win32_IP_PER_ADAPTER_INFO pPerAdapterInfo, ref int pOutBufLen);
144
145                 Win32_IP_ADAPTER_INFO ainfo;
146                 Win32_IP_PER_ADAPTER_INFO painfo;
147                 Win32_MIB_IFROW mib;
148
149                 public Win32IPv4InterfaceProperties (Win32_IP_ADAPTER_INFO ainfo, Win32_MIB_IFROW mib)
150                 {
151                         this.ainfo = ainfo;
152                         this.mib = mib;
153
154                         // get per-adapter info.
155                         int size = 0;
156                         GetPerAdapterInfo (mib.Index, null, ref size);
157                         painfo = new Win32_IP_PER_ADAPTER_INFO ();
158                         int ret = GetPerAdapterInfo (mib.Index, painfo, ref size);
159                         if (ret != 0)
160                                 throw new NetworkInformationException (ret);
161                 }
162
163                 public override int Index {
164                         get { return mib.Index; }
165                 }
166
167                 public override bool IsAutomaticPrivateAddressingActive {
168                         get { return painfo.AutoconfigActive != 0; }
169                 }
170
171                 public override bool IsAutomaticPrivateAddressingEnabled {
172                         get { return painfo.AutoconfigEnabled != 0; }
173                 }
174
175                 public override bool IsDhcpEnabled {
176                         get { return ainfo.DhcpEnabled != 0; }
177                 }
178
179                 public override bool IsForwardingEnabled {
180                         // Is it the right answer? In Vista there is MIB_IPINTERFACEROW.ForwardingEnabled, but not in former versions.
181                         get { return Win32_FIXED_INFO.Instance.EnableRouting != 0; }
182                 }
183
184                 public override int Mtu {
185                         get { return mib.Mtu; }
186                 }
187
188                 public override bool UsesWins {
189                         get { return ainfo.HaveWins; }
190                 }
191         }
192
193         [StructLayout (LayoutKind.Sequential)]
194         class Win32_IP_PER_ADAPTER_INFO
195         {
196                 public uint AutoconfigEnabled;
197                 public uint AutoconfigActive;
198                 public IntPtr CurrentDnsServer; // to Win32_IP_ADDR_STRING
199                 public Win32_IP_ADDR_STRING DnsServerList;
200         }
201 }
202