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