UnicastIPAddressInformationCollection.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Net.NetworkInformation.UnicastIPAddressInformationCollection
  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. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Runtime.InteropServices;
  33. namespace System.Net.NetworkInformation {
  34. public class UnicastIPAddressInformationCollection : ICollection<UnicastIPAddressInformation>,
  35. IEnumerable<UnicastIPAddressInformation> {
  36. List<UnicastIPAddressInformation> list = new List<UnicastIPAddressInformation> ();
  37. protected internal UnicastIPAddressInformationCollection ()
  38. {
  39. }
  40. public virtual void Add (UnicastIPAddressInformation address)
  41. {
  42. if (IsReadOnly)
  43. throw new NotSupportedException ("The collection is read-only.");
  44. list.Add (address);
  45. }
  46. public virtual void Clear ()
  47. {
  48. if (IsReadOnly)
  49. throw new NotSupportedException ("The collection is read-only.");
  50. list.Clear ();
  51. }
  52. public virtual bool Contains (UnicastIPAddressInformation address)
  53. {
  54. return list.Contains (address);
  55. }
  56. public virtual void CopyTo (UnicastIPAddressInformation [] array, int offset)
  57. {
  58. list.CopyTo (array, offset);
  59. }
  60. public virtual IEnumerator<UnicastIPAddressInformation> GetEnumerator ()
  61. {
  62. return ((IEnumerable<UnicastIPAddressInformation>)list).GetEnumerator ();
  63. }
  64. public virtual bool Remove (UnicastIPAddressInformation address)
  65. {
  66. if (IsReadOnly)
  67. throw new NotSupportedException ("The collection is read-only.");
  68. return list.Remove (address);
  69. }
  70. IEnumerator IEnumerable.GetEnumerator ()
  71. {
  72. return list.GetEnumerator ();
  73. }
  74. public virtual int Count {
  75. get { return list.Count; }
  76. }
  77. public virtual bool IsReadOnly {
  78. get { return true; }
  79. }
  80. public virtual UnicastIPAddressInformation this [int index] {
  81. get { return list [index]; }
  82. }
  83. }
  84. class UnicastIPAddressInformationImplCollection : UnicastIPAddressInformationCollection
  85. {
  86. public static readonly UnicastIPAddressInformationImplCollection Empty = new UnicastIPAddressInformationImplCollection (true);
  87. bool is_readonly;
  88. // for static methods
  89. UnicastIPAddressInformationImplCollection (bool isReadOnly)
  90. {
  91. is_readonly = isReadOnly;
  92. }
  93. public override bool IsReadOnly {
  94. get { return is_readonly; }
  95. }
  96. public static UnicastIPAddressInformationCollection Win32FromUnicast (int ifIndex, IntPtr ptr)
  97. {
  98. UnicastIPAddressInformationImplCollection c = new UnicastIPAddressInformationImplCollection (false);
  99. Win32_IP_ADAPTER_UNICAST_ADDRESS a;
  100. for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
  101. a = (Win32_IP_ADAPTER_UNICAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_UNICAST_ADDRESS));
  102. c.Add (new Win32UnicastIPAddressInformation (ifIndex, a));
  103. }
  104. c.is_readonly = true;
  105. return c;
  106. }
  107. public static UnicastIPAddressInformationCollection LinuxFromList (List<IPAddress> addresses)
  108. {
  109. UnicastIPAddressInformationImplCollection c = new UnicastIPAddressInformationImplCollection (false);
  110. foreach (IPAddress address in addresses) {
  111. c.Add (new LinuxUnicastIPAddressInformation (address));
  112. }
  113. c.is_readonly = true;
  114. return c;
  115. }
  116. }
  117. }