IPv4InterfaceProperties.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // System.Net.NetworkInformation.IPv4InterfaceProperties
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Runtime.InteropServices;
  31. namespace System.Net.NetworkInformation {
  32. public abstract class IPv4InterfaceProperties {
  33. protected IPv4InterfaceProperties ()
  34. {
  35. }
  36. public abstract int Index { get; }
  37. public abstract bool IsAutomaticPrivateAddressingActive { get; }
  38. public abstract bool IsAutomaticPrivateAddressingEnabled { get; }
  39. public abstract bool IsDhcpEnabled { get; }
  40. public abstract bool IsForwardingEnabled { get; }
  41. public abstract int Mtu { get; }
  42. public abstract bool UsesWins { get; }
  43. }
  44. class Win32IPv4InterfaceProperties : IPv4InterfaceProperties
  45. {
  46. [DllImport ("iphlpapi.dll")]
  47. static extern int GetPerAdapterInfo (int IfIndex, Win32_IP_PER_ADAPTER_INFO pPerAdapterInfo, ref int pOutBufLen);
  48. Win32_IP_ADAPTER_INFO ainfo;
  49. Win32_IP_PER_ADAPTER_INFO painfo;
  50. Win32_MIB_IFROW mib;
  51. public Win32IPv4InterfaceProperties (Win32_IP_ADAPTER_INFO ainfo, Win32_MIB_IFROW mib)
  52. {
  53. this.ainfo = ainfo;
  54. this.mib = mib;
  55. // get per-adapter info.
  56. int size = 0;
  57. GetPerAdapterInfo (mib.Index, null, ref size);
  58. painfo = new Win32_IP_PER_ADAPTER_INFO ();
  59. int ret = GetPerAdapterInfo (mib.Index, painfo, ref size);
  60. if (ret != 0)
  61. throw new NetworkInformationException (ret);
  62. }
  63. public override int Index {
  64. get { return mib.Index; }
  65. }
  66. public override bool IsAutomaticPrivateAddressingActive {
  67. get { return painfo.AutoconfigActive != 0; }
  68. }
  69. public override bool IsAutomaticPrivateAddressingEnabled {
  70. get { return painfo.AutoconfigEnabled != 0; }
  71. }
  72. public override bool IsDhcpEnabled {
  73. get { return ainfo.DhcpEnabled != 0; }
  74. }
  75. public override bool IsForwardingEnabled {
  76. // Is it the right answer? In Vista there is MIB_IPINTERFACEROW.ForwardingEnabled, but not in former versions.
  77. get { return Win32NetworkInterface2.FixedInfo.EnableRouting != 0; }
  78. }
  79. public override int Mtu {
  80. get { return mib.Mtu; }
  81. }
  82. public override bool UsesWins {
  83. get { return ainfo.HaveWins; }
  84. }
  85. }
  86. [StructLayout (LayoutKind.Sequential)]
  87. class Win32_IP_PER_ADAPTER_INFO
  88. {
  89. public uint AutoconfigEnabled;
  90. public uint AutoconfigActive;
  91. public IntPtr CurrentDnsServer; // to Win32_IP_ADDR_STRING
  92. public Win32_IP_ADDR_STRING DnsServerList;
  93. }
  94. }
  95. #endif