forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhoIsClient.cs
More file actions
163 lines (138 loc) · 4.24 KB
/
WhoIsClient.cs
File metadata and controls
163 lines (138 loc) · 4.24 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Waher.Networking.Sniffers;
namespace Waher.Networking.WHOIS
{
/// <summary>
/// WHOIS client, as defined in RFC 3912:
/// https://tools.ietf.org/html/rfc3912
/// </summary>
public partial class WhoIsClient : TextTcpClient
{
private readonly StringBuilder receivedText = new StringBuilder();
private readonly AutoResetEvent received = new AutoResetEvent(false);
private readonly ManualResetEvent closed = new ManualResetEvent(false);
/// <summary>
/// Default WHOIS Port = 43
/// </summary>
public const int DefaultWhoIsPort = 43;
/// <summary>
/// WHOIS client, as defined in RFC 3912:
/// https://tools.ietf.org/html/rfc3912
/// </summary>
/// <param name="Sniffers">Sniffers.</param>
public WhoIsClient(params ISniffer[] Sniffers)
: this(Encoding.ASCII, Sniffers)
{
}
/// <summary>
/// WHOIS client, as defined in RFC 3912:
/// https://tools.ietf.org/html/rfc3912
/// </summary>
/// <param name="Encoding">Text encoding to use. (Default=<see cref="Encoding.ASCII"/>)</param>
/// <param name="Sniffers">Sniffers.</param>
public WhoIsClient(Encoding Encoding, params ISniffer[] Sniffers)
: base(Encoding, Sniffers)
{
}
/// <summary>
/// Queries an Internet Registry about an entity.
/// </summary>
/// <param name="InternetRegistry">Domain name of Internet Registry to query.</param>
/// <param name="Entity">Entity to be queried.</param>
/// <returns>WHOIS Information</returns>
public async Task<string> DoQuery(string InternetRegistry, string Entity)
{
this.received.Reset();
await this.ConnectAsync(InternetRegistry, DefaultWhoIsPort);
this.received.WaitOne(500);
lock (this.receivedText)
{
this.receivedText.Clear();
}
this.closed.Reset();
await this.SendAsync(Entity + "\r\n");
this.closed.WaitOne(5000);
string s;
lock (this.receivedText)
{
s = this.receivedText.ToString();
this.receivedText.Clear();
}
return s.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
}
/// <summary>
/// Method called when text data has been received.
/// </summary>
/// <param name="Data">Text data received.</param>
protected override Task<bool> TextDataReceived(string Data)
{
lock (this.receivedText)
{
this.receivedText.Append(Data);
}
this.received.Set();
return Task.FromResult<bool>(true);
}
/// <summary>
/// Method called when the connection has been disconnected.
/// </summary>
protected override void Disconnected()
{
this.closed.Set();
base.Disconnected();
}
/// <summary>
/// Disposes of the object. The underlying <see cref="TcpClient"/> is either disposed directly, or when asynchronous
/// operations have ceased.
/// </summary>
public override void Dispose()
{
base.Dispose();
this.received.Dispose();
this.closed.Dispose();
this.receivedText.Clear();
}
/// <summary>
/// Queries an Internet Registry about an entity.
/// </summary>
/// <param name="InternetRegistry">Domain name of Internet Registry to query.</param>
/// <param name="Entity">Entity to be queried.</param>
/// <returns>WHOIS Information</returns>
public static async Task<string> Query(string InternetRegistry, string Entity)
{
using (WhoIsClient Client = new WhoIsClient())
{
return await Client.DoQuery(InternetRegistry, Entity);
}
}
/// <summary>
/// Queries the corresponding Internet Registry about an IP Address.
/// </summary>
/// <param name="Address">IP Address to query</param>
/// <returns>WHOIS Information, if available, null if not.</returns>
public static async Task<string> Query(IPAddress Address)
{
switch (Address.AddressFamily)
{
case AddressFamily.InterNetwork:
byte[] Bytes = Address.GetAddressBytes();
byte b = Bytes[0];
if (b >= ipv4ToWhoIsService.Length)
return null;
WhoIsIpv4ServiceEnum Service = ipv4ToWhoIsService[b];
string InternetRegistry = ipv4WhoIsServices[(int)Service];
using (WhoIsClient Client = new WhoIsClient())
{
return await Client.DoQuery(InternetRegistry, Address.ToString());
}
default:
return null;
}
}
}
}