Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System / System.Net.NetworkInformation / IPAddressCollection.cs
1 //
2 // System.Net.NetworkInformation.IPAddressCollection
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
6 //      Atsushi Enomoto (atsushi@ximian.com)
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.Net;
33 using System.Runtime.InteropServices;
34
35 namespace System.Net.NetworkInformation {
36         public class IPAddressCollection : ICollection<IPAddress>, IEnumerable<IPAddress>, IEnumerable {
37                 IList <IPAddress> list = new List<IPAddress> ();
38
39                 protected internal IPAddressCollection ()
40                 {
41                 }
42
43                 internal void SetReadOnly ()
44                 {
45                         if (!IsReadOnly)
46                                 list = ((List<IPAddress>) list).AsReadOnly ();
47                 }
48
49                 public virtual void Add (IPAddress address)
50                 {
51                         if (IsReadOnly)
52                                 throw new NotSupportedException ("The collection is read-only.");
53                         list.Add (address);
54                 }
55
56                 public virtual void Clear ()
57                 {
58                         if (IsReadOnly)
59                                 throw new NotSupportedException ("The collection is read-only.");
60                         list.Clear ();
61                 }
62
63                 public virtual bool Contains (IPAddress address)
64                 {
65                         return list.Contains (address);
66                 }
67
68                 public virtual void CopyTo (IPAddress [] array, int offset)
69                 {
70                         list.CopyTo (array, offset);
71                 }
72
73                 public virtual IEnumerator<IPAddress> GetEnumerator ()
74                 {
75                         return ((IEnumerable<IPAddress>)list).GetEnumerator ();
76                 }
77
78                 public virtual bool Remove (IPAddress address)
79                 {
80                         if (IsReadOnly)
81                                 throw new NotSupportedException ("The collection is read-only.");
82                         return list.Remove (address);
83                 }
84
85                 IEnumerator IEnumerable.GetEnumerator ()
86                 {
87                         return list.GetEnumerator ();
88                 }
89
90                 public virtual int Count {
91                         get { return list.Count; }
92                 }
93
94                 public virtual bool IsReadOnly {
95                         get { return list.IsReadOnly; }
96                 }
97
98                 public virtual IPAddress this [int index] {
99                         get { return list [index]; }
100                 }
101         }
102
103         class Win32IPAddressCollection : IPAddressCollection
104         {
105                 public static readonly Win32IPAddressCollection Empty = new Win32IPAddressCollection (IntPtr.Zero);
106
107                 bool is_readonly;
108
109                 // for static methods
110                 Win32IPAddressCollection ()
111                 {
112                 }
113
114                 public Win32IPAddressCollection (params IntPtr [] heads)
115                 {
116                         foreach (IntPtr head in heads)
117                                 AddSubsequentlyString (head);
118                         is_readonly = true;
119                 }
120
121                 public Win32IPAddressCollection (params Win32_IP_ADDR_STRING [] al)
122                 {
123                         foreach (Win32_IP_ADDR_STRING a in al) {
124                                 if (String.IsNullOrEmpty (a.IpAddress))
125                                         continue;
126                                 Add (IPAddress.Parse (a.IpAddress));
127                                 AddSubsequentlyString (a.Next);
128                         }
129                         is_readonly = true;
130                 }
131
132                 public static Win32IPAddressCollection FromAnycast (IntPtr ptr)
133                 {
134                         Win32IPAddressCollection c = new Win32IPAddressCollection ();
135                         Win32_IP_ADAPTER_ANYCAST_ADDRESS a;
136                         for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
137                                 a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS));
138                                 c.Add (a.Address.GetIPAddress ());
139                         }
140                         c.is_readonly = true;
141                         return c;
142                 }
143
144                 public static Win32IPAddressCollection FromDnsServer (IntPtr ptr)
145                 {
146                         Win32IPAddressCollection c = new Win32IPAddressCollection ();
147                         Win32_IP_ADAPTER_DNS_SERVER_ADDRESS a;
148                         for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
149                                 a = (Win32_IP_ADAPTER_DNS_SERVER_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_DNS_SERVER_ADDRESS));
150 // FIXME: It somehow fails here. Looks like there is something wrong.
151 //if (a.Address.Sockaddr == IntPtr.Zero) throw new Exception ("pointer " + p + " a.length " + a.Address.SockaddrLength);
152                                 c.Add (a.Address.GetIPAddress ());
153                         }
154                         c.is_readonly = true;
155                         return c;
156                 }
157
158                 void AddSubsequentlyString (IntPtr head)
159                 {
160                         Win32_IP_ADDR_STRING a;
161                         for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) {
162                                 a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING));
163                                 Add (IPAddress.Parse (a.IpAddress));
164                         }
165                 }
166
167                 public override bool IsReadOnly {
168                         get { return is_readonly; }
169                 }
170         }
171 }
172
173