2 // System.Net.NetworkInformation.IPAddressCollection
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // Copyright (c) 2006-2007 Novell, Inc. (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Collections.Generic;
33 using System.Runtime.InteropServices;
35 namespace System.Net.NetworkInformation {
38 class Win32IPAddressCollection : IPAddressCollection
40 public static readonly Win32IPAddressCollection Empty = new Win32IPAddressCollection (IntPtr.Zero);
45 Win32IPAddressCollection ()
49 public Win32IPAddressCollection (params IntPtr [] heads)
51 foreach (IntPtr head in heads)
52 AddSubsequentlyString (head);
56 public Win32IPAddressCollection (params Win32_IP_ADDR_STRING [] al)
58 foreach (Win32_IP_ADDR_STRING a in al) {
59 if (String.IsNullOrEmpty (a.IpAddress))
61 Add (IPAddress.Parse (a.IpAddress));
62 AddSubsequentlyString (a.Next);
67 public static Win32IPAddressCollection FromAnycast (IntPtr ptr)
69 Win32IPAddressCollection c = new Win32IPAddressCollection ();
70 Win32_IP_ADAPTER_ANYCAST_ADDRESS a;
71 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
72 a = (Win32_IP_ADAPTER_ANYCAST_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_ANYCAST_ADDRESS));
73 c.Add (a.Address.GetIPAddress ());
79 public static Win32IPAddressCollection FromDnsServer (IntPtr ptr)
81 Win32IPAddressCollection c = new Win32IPAddressCollection ();
82 Win32_IP_ADAPTER_DNS_SERVER_ADDRESS a;
83 for (IntPtr p = ptr; p != IntPtr.Zero; p = a.Next) {
84 a = (Win32_IP_ADAPTER_DNS_SERVER_ADDRESS) Marshal.PtrToStructure (p, typeof (Win32_IP_ADAPTER_DNS_SERVER_ADDRESS));
85 // FIXME: It somehow fails here. Looks like there is something wrong.
86 //if (a.Address.Sockaddr == IntPtr.Zero) throw new Exception ("pointer " + p + " a.length " + a.Address.SockaddrLength);
87 c.Add (a.Address.GetIPAddress ());
93 void AddSubsequentlyString (IntPtr head)
95 Win32_IP_ADDR_STRING a;
96 for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) {
97 a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING));
98 Add (IPAddress.Parse (a.IpAddress));
102 public override bool IsReadOnly {
103 get { return is_readonly; }