6a02ed522bf3bf33dafa5e4ef6ef7c6e37e06d4a
[mono.git] / mcs / class / System.Net / System.Net / Dns_2_1.cs
1 // System.Net.Dns.cs
2 //
3 // Author: Mads Pultz (mpultz@diku.dk)
4 // Author: Lawrence Pit (loz@cable.a2000.nl)
5 //
6 // (C) Mads Pultz, 2001
7
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Net.Sockets;
32 using System.Runtime.CompilerServices;
33
34 namespace System.Net {
35
36         internal static class Dns {
37
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);
40
41                 internal static IPAddress [] GetHostAddresses (string hostNameOrAddress)
42                 {
43                         if (hostNameOrAddress == null)
44                                 throw new ArgumentNullException ("hostNameOrAddress");
45
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.",
50                                         "hostNameOrAddress");
51
52                         IPAddress addr;
53                         if (hostNameOrAddress.Length > 0 && IPAddress.TryParse (hostNameOrAddress, out addr))
54                                 return new IPAddress[1] { addr };
55
56                         string h_name;
57                         string[] h_aliases, h_addrlist;
58
59                         bool ret = GetHostByName_internal (hostNameOrAddress, out h_name, out h_aliases, out h_addrlist);
60                         if (ret == false)
61                                 throw new SocketException(11001);
62
63                         IPHostEntry entry = hostent_to_IPHostEntry (h_name, h_aliases, h_addrlist);
64                         return entry.AddressList;
65                 }
66
67                 private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) 
68                 {
69                         IPHostEntry he = new IPHostEntry();
70                         ArrayList addrlist = new ArrayList();
71
72                         he.HostName = h_name;
73                         he.Aliases = h_aliases;
74                         for(int i=0; i<h_addrlist.Length; i++) {
75                                 try {
76                                         IPAddress newAddress = IPAddress.Parse(h_addrlist[i]);
77
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
86                                          */
87                                 }
88                         }
89
90                         if(addrlist.Count == 0)
91                                 throw new SocketException(11001);
92
93                         he.AddressList = addrlist.ToArray(typeof(IPAddress)) as IPAddress[];
94                         return he;
95                 }
96         }
97 }
98