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