forked from Hawkynt/C--FrameworkExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
System.Net.IPAdressExtensions.cs
151 lines (135 loc) · 5.41 KB
/
System.Net.IPAdressExtensions.cs
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
#region (c)2010-2042 Hawkynt
/*
This file is part of Hawkynt's .NET Framework extensions.
Hawkynt's .NET Framework extensions are free software:
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hawkynt's .NET Framework extensions is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hawkynt's .NET Framework extensions.
If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
#if NET45
using System.Threading.Tasks;
#endif
#if NET40
using System.Diagnostics.Contracts;
#else
using System.Diagnostics;
#endif
namespace System.Net {
internal static partial class IPAdressExtensions {
/// <summary>
/// Determines whether the specified IPAdress is loopback.
/// </summary>
/// <param name="this">The this.</param>
/// <returns>
/// <c>true</c> if the specified IPAdress is loopback; otherwise, <c>false</c>.
/// </returns>
public static bool IsLoopback(this IPAddress @this)
=> IPAddress.IsLoopback(@this) || new[] { IPAddress.Any, IPAddress.IPv6Any, IPAddress.Loopback, IPAddress.IPv6Loopback }.Contains(@this)
;
/// <summary>
/// Gets the host name for the given ip-adress.
/// </summary>
/// <param name="this">This IPAdress.</param>
/// <returns>The host name or <c>null</c>.</returns>
public static string GetHostName(this IPAddress @this) {
#if NET40
Contract.Requires(@this != null);
#else
Debug.Assert(@this != null);
#endif
try {
var hostEntry = Dns.GetHostEntry(@this);
return hostEntry.HostName;
} catch (Exception) {
return null;
}
}
#if NET45
/// <summary>
/// Gets the host name for the given ip-adress.
/// </summary>
/// <param name="This">This IPAdress.</param>
/// <returns>The host name or <c>null</c>.</returns>
public static async Task<string> GetHostNameAsync(this IPAddress This) {
Contract.Requires(This != null);
try {
var hostEntry = await Dns.GetHostEntryAsync(This);
return hostEntry.HostName;
} catch (Exception) {
return null;
}
}
#endif
public static Tuple<bool, string, PingReply, Exception> Ping(this IPAddress @this, uint retryCount = 0, TimeSpan? timeout = null, PingOptions options = null) {
const int ttl = 128;
const bool dontFragment = true;
if (options == null)
options = new PingOptions(ttl, dontFragment);
if (timeout == null)
timeout = TimeSpan.FromSeconds(5);
var receiveBuffer = new byte[32];
Tuple<bool, string, PingReply, Exception> result;
using (var ping = new Ping()) {
do {
try {
var pingReply = ping.Send(@this, (int)timeout.Value.TotalMilliseconds, receiveBuffer, options);
//make sure we dont have a null reply
if (pingReply == null) {
result = Tuple.Create(false, "Got empty packet.", (PingReply)null, (Exception)null);
continue;
}
switch (pingReply.Status) {
case IPStatus.Success:
return Tuple.Create(true, (string)null, pingReply, (Exception)null);
case IPStatus.TimedOut: {
result = Tuple.Create(false, "Remote connection timed out.", pingReply, (Exception)null);
break;
}
case IPStatus.DestinationHostUnreachable: {
result = Tuple.Create(false, "Destination host unreachable.", pingReply, (Exception)null);
break;
}
case IPStatus.DestinationNetworkUnreachable: {
result = Tuple.Create(false, "Destination network unreachable.", pingReply, (Exception)null);
break;
}
case IPStatus.DestinationPortUnreachable: {
result = Tuple.Create(false, "Destination port unreachable.", pingReply, (Exception)null);
break;
}
case IPStatus.DestinationUnreachable: {
result = Tuple.Create(false, "Destination unreachable.", pingReply, (Exception)null);
break;
}
case IPStatus.TtlExpired: {
result = Tuple.Create(false, "Too many hops.", pingReply, (Exception)null);
break;
}
default: {
result = Tuple.Create(false, pingReply.Status.ToString(), pingReply, (Exception)null);
break;
}
}
} catch (PingException e) {
result = Tuple.Create(false, e.Message, (PingReply)null, (Exception)e);
} catch (SocketException e) {
result = Tuple.Create(false, e.Message, (PingReply)null, (Exception)e);
}
} while (retryCount-- > 0);
}
return result;
}
}
}