Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Dns.cs « System.Net « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ab29a95b039f4458a624a59c3bc2b4d839344a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// System.Net.Dns.cs
//
// Author: Mads Pultz (mpultz@diku.dk)
// Author: Lawrence Pit (loz@cable.a2000.nl)
//
// (C) Mads Pultz, 2001

using System;
using System.Net.Sockets;
using System.Text;
using System.Collections;
using System.Threading;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging;

namespace System.Net {
        public sealed class Dns {

		private Dns () {}

                private delegate IPHostEntry GetHostByNameCallback (string hostName);
                private delegate IPHostEntry ResolveCallback (string hostName);
                
                public static IAsyncResult BeginGetHostByName (string hostName,
                	AsyncCallback requestCallback, object stateObject)
               	{
                        if (hostName == null)
                                throw new ArgumentNullException();
			GetHostByNameCallback c = new GetHostByNameCallback (GetHostByName);
			return c.BeginInvoke (hostName, requestCallback, stateObject);
                }

                public static IAsyncResult BeginResolve (string hostName,
                	AsyncCallback requestCallback, object stateObject)
                {
                        if (hostName == null)
                                throw new ArgumentNullException();
			ResolveCallback c = new ResolveCallback (Resolve);
			return c.BeginInvoke (hostName, requestCallback, stateObject);
                }
                
                public static IPHostEntry EndGetHostByName (IAsyncResult asyncResult) {
			if (asyncResult == null)
				throw new ArgumentNullException ("asyncResult");
			AsyncResult async = (AsyncResult) asyncResult;
			GetHostByNameCallback cb = (GetHostByNameCallback) async.AsyncDelegate;
			asyncResult.AsyncWaitHandle.WaitOne ();
			return cb.EndInvoke(asyncResult);
                }

                public static IPHostEntry EndResolve (IAsyncResult asyncResult) {
			if (asyncResult == null)
				throw new ArgumentNullException ("asyncResult");
			AsyncResult async = (AsyncResult) asyncResult;
			ResolveCallback cb = (ResolveCallback) async.AsyncDelegate;
			asyncResult.AsyncWaitHandle.WaitOne ();
			return cb.EndInvoke(asyncResult);
                }
                                
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static bool GetHostByName_internal(string host, out string h_name, out string[] h_aliases, out string[] h_addr_list);

                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern static bool GetHostByAddr_internal(string addr, out string h_name, out string[] h_aliases, out string[] h_addr_list);
                
                private static IPHostEntry hostent_to_IPHostEntry(string h_name, string[] h_aliases, string[] h_addrlist) {
                        IPHostEntry he = new IPHostEntry();
                        IPAddress[] addrlist = new IPAddress[h_addrlist.Length];
                        
                        he.HostName=h_name;
                        he.Aliases=h_aliases;
                        for(int i=0; i<h_addrlist.Length; i++) {
                                addrlist[i]=IPAddress.Parse(h_addrlist[i]);
                        }
                        he.AddressList=addrlist;

                        return(he);
                }

                public static IPHostEntry GetHostByAddress(IPAddress address) {
                        if (address == null)
                                throw new ArgumentNullException();
                        return GetHostByAddress(address.ToString());
                }
                
                public static IPHostEntry GetHostByAddress(string address) {
                        if (address == null) {
                                throw new ArgumentNullException();
                        }
                        
                        string h_name;
                        string[] h_aliases, h_addrlist;
                        
                        bool ret = GetHostByAddr_internal(address, out h_name,
                                                          out h_aliases,
                                                          out h_addrlist);
                        if (ret == false) {
                                throw new SocketException(11001);
                        }
                        
                        return(hostent_to_IPHostEntry(h_name, h_aliases,
                                                      h_addrlist));
                }

                public static IPHostEntry GetHostByName(string hostName) {
                        if (hostName == null)
                                throw new ArgumentNullException();
                        
                        string h_name;
                        string[] h_aliases, h_addrlist;
                        
                        bool ret = GetHostByName_internal(hostName, out h_name,
                                                          out h_aliases,
                                                          out h_addrlist);
                        if (ret == false)
                                throw new SocketException(11001);

                        return(hostent_to_IPHostEntry(h_name, h_aliases,
                                                      h_addrlist));
                }
                
                /// <summary>
                /// This method returns the host name associated with the local host.
                /// </summary>
                public static string GetHostName() {
                        IPHostEntry h = GetHostByAddress("127.0.0.1");
                        return h.HostName;
                }
                
                /// <summary>
                /// This method resolves a DNS-style host name or IP
                /// address.
                /// </summary>
                /// <param name=hostName>
                /// A string containing either a DNS-style host name (e.g.
                /// www.go-mono.com) or IP address (e.g. 129.250.184.233).
                /// </param>
                public static IPHostEntry Resolve(string hostName) {
                        if (hostName == null)
                                throw new ArgumentNullException();

			return GetHostByName (hostName);
                }
        }
}