3 // Author: Mads Pultz (mpultz@diku.dk)
4 // Author: Lawrence Pit (loz@cable.a2000.nl)
6 // (C) Mads Pultz, 2001
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Net.Sockets;
32 using System.Runtime.CompilerServices;
34 namespace System.Net {
36 internal static class Dns {
38 [MethodImplAttribute(MethodImplOptions.InternalCall)]
39 private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);
41 internal static IPAddress [] GetHostAddresses (string hostNameOrAddress)
43 if (hostNameOrAddress == null)
44 throw new ArgumentNullException ("hostNameOrAddress");
46 if (hostNameOrAddress == "0.0.0.0" || hostNameOrAddress == "::0")
47 throw new ArgumentException ("Addresses 0.0.0.0 (IPv4) " +
48 "and ::0 (IPv6) are unspecified addresses. You " +
49 "cannot use them as target address.",
53 if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
54 return new IPAddress[1] { addr };
57 string[] h_aliases, h_addrlist;
59 bool ret = GetHostByName_internal (hostNameOrAddress, out h_name, out h_aliases, out h_addrlist);
61 throw new SocketException(11001);
63 IPHostEntry entry = hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist);
64 return entry.AddressList;
67 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist)
69 IPHostEntry he = new IPHostEntry();
70 ArrayList addrlist = new ArrayList();
73 he.Aliases = h_aliases;
74 for(int i=0; i<h_addrlist.Length; i++) {
76 IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
78 if( (Socket.SupportsIPv6 && newAddress.AddressFamily == AddressFamily.InterNetworkV6) ||
79 (Socket.SupportsIPv4 && newAddress.AddressFamily == AddressFamily.InterNetwork) )
80 addrlist.Add(newAddress);
81 } catch (ArgumentNullException) {
82 /* Ignore this, as the
83 * internal call might have
84 * left some blank entries at
85 * the end of the array
90 if(addrlist.Count == 0)
91 throw new SocketException(11001);
93 he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];