2008-07-14 Marek Habersack <mhabersack@novell.com>
[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         sealed class LinuxIPv4InterfaceProperties : IPv4InterfaceProperties
50         {
51                 LinuxNetworkInterface iface;
52                 
53                 public LinuxIPv4InterfaceProperties (LinuxNetworkInterface iface)
54                 {
55                         this.iface = iface;
56                 }
57                 
58                 public override int Index {
59                         get { return LinuxNetworkInterface.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 IsForwardingEnabled {
78                         get {
79                                 string iface_path = "/proc/sys/net/ipv4/conf/" + iface.Name + "/forwarding";
80
81                                 if (File.Exists (iface_path)) {
82                                         string val = NetworkInterface.ReadLine (iface_path);
83
84                                         return val != "0";
85                                 }
86
87                                 return false;
88                         }
89                 }
90                 public override int Mtu {
91                         get {
92                                 string iface_path = iface.IfacePath + "mtu";
93                                 int ret = 0;
94
95                                 if (File.Exists (iface_path)) {
96                                         string val = NetworkInterface.ReadLine (iface_path);
97                                         
98                                         try {
99                                                 ret = Int32.Parse (val);
100                                         } catch {
101                                         }
102                                 }
103
104                                 return ret;
105                                                 
106                         }
107                 }
108         
109                 public override bool UsesWins {
110                         get { return false; }
111                 }
112         }
113         
114         sealed class Win32IPv4InterfaceProperties : IPv4InterfaceProperties
115         {
116                 [DllImport ("iphlpapi.dll")]
117                 static extern int GetPerAdapterInfo (int IfIndex, Win32_IP_PER_ADAPTER_INFO pPerAdapterInfo, ref int pOutBufLen);
118
119                 Win32_IP_ADAPTER_INFO ainfo;
120                 Win32_IP_PER_ADAPTER_INFO painfo;
121                 Win32_MIB_IFROW mib;
122
123                 public Win32IPv4InterfaceProperties (Win32_IP_ADAPTER_INFO ainfo, Win32_MIB_IFROW mib)
124                 {
125                         this.ainfo = ainfo;
126                         this.mib = mib;
127
128                         // get per-adapter info.
129                         int size = 0;
130                         GetPerAdapterInfo (mib.Index, null, ref size);
131                         painfo = new Win32_IP_PER_ADAPTER_INFO ();
132                         int ret = GetPerAdapterInfo (mib.Index, painfo, ref size);
133                         if (ret != 0)
134                                 throw new NetworkInformationException (ret);
135                 }
136
137                 public override int Index {
138                         get { return mib.Index; }
139                 }
140
141                 public override bool IsAutomaticPrivateAddressingActive {
142                         get { return painfo.AutoconfigActive != 0; }
143                 }
144
145                 public override bool IsAutomaticPrivateAddressingEnabled {
146                         get { return painfo.AutoconfigEnabled != 0; }
147                 }
148
149                 public override bool IsDhcpEnabled {
150                         get { return ainfo.DhcpEnabled != 0; }
151                 }
152
153                 public override bool IsForwardingEnabled {
154                         // Is it the right answer? In Vista there is MIB_IPINTERFACEROW.ForwardingEnabled, but not in former versions.
155                         get { return Win32_FIXED_INFO.Instance.EnableRouting != 0; }
156                 }
157
158                 public override int Mtu {
159                         get { return mib.Mtu; }
160                 }
161
162                 public override bool UsesWins {
163                         get { return ainfo.HaveWins; }
164                 }
165         }
166
167         [StructLayout (LayoutKind.Sequential)]
168         class Win32_IP_PER_ADAPTER_INFO
169         {
170                 public uint AutoconfigEnabled;
171                 public uint AutoconfigActive;
172                 public IntPtr CurrentDnsServer; // to Win32_IP_ADDR_STRING
173                 public Win32_IP_ADDR_STRING DnsServerList;
174         }
175 }
176 #endif
177