-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
323 lines (286 loc) · 12.4 KB
/
Program.cs
File metadata and controls
323 lines (286 loc) · 12.4 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using Mono.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace PingLog
{
public class Program
{
static bool _continue = false;
public static bool ShouldContinue
{
get => _continue;
set { _continue = value; }
}
static ManualResetEvent _pinging = new(false);
public static ManualResetEvent WhilePinging
{
get => _pinging;
set { _pinging = value; }
}
static bool _showHelp = false;
public static bool ShowHelp
{
get => _showHelp;
set { _showHelp = value; }
}
static bool _endless = false;
public static bool Endless
{
get => _endless;
set { _endless = value; }
}
static ulong _maxMessages = 4;
public static ulong MaximumPackages
{
get => _maxMessages;
set { _maxMessages = value; }
}
static ushort _size = 32;
public static ushort PackageSize
{
get => _size;
set { _size = value; }
}
static bool _fragment = false;
public static bool AllowPackageFragmentation
{
get => _fragment;
set { _fragment = value; }
}
static byte _ttl = 128;
public static byte Ttl
{
get => _ttl;
set { _ttl = value; }
}
static int _timeoutResponse = 4000;
public static int ResponseTimeout
{
get => _timeoutResponse;
set { _timeoutResponse = value; }
}
static int _timeoutRequest = 1000;
public static int RequestTimeout
{
get => _timeoutRequest;
set { _timeoutRequest = value; }
}
static AddressFamily _protocol;
public static AddressFamily ProtocolVersion
{
get => _protocol;
set { _protocol = value; }
}
static bool _hideOutput;
public static bool ShouldHideOutput
{
get => _hideOutput;
set { _hideOutput = value; }
}
#nullable enable
static string? _source;
public static string? SourceFile
{
get => _source;
set { _source = value; }
}
static string? _destination;
public static string? DestinationFolder
{
get => _destination;
set { _destination = value; }
}
#nullable disable
static Dictionary<int, (IPAddress, Dictionary<string, ulong>, HashSet<long>)> _results;
public static Dictionary<int, (IPAddress Address, Dictionary<string, ulong> MessagesCounter, HashSet<long> RoundtripTimeValues)> Results
{
get => _results;
set { _results = value; }
}
static int _fieldWidth = 0;
public static int AddressFieldWidth
{
get => _fieldWidth;
set { _fieldWidth = value; }
}
static ConcurrentHashSet<PingTask> _tasks;
public static ConcurrentHashSet<PingTask> Tasks
{
get => _tasks;
set { _tasks = value; }
}
static string Name => typeof(Program).Assembly.GetName().Name;
static HashSet<string> _extra;
static void Main(string[] args)
{
Console.CancelKeyPress += Console_CancelKeyPress;
Task main = MainAsync(args);
main.Wait();
}
private static async Task MainAsync(string[] args)
{
#region args
var p = new OptionSet()
{
$"Usage: {Name}\t[-t] [-n=<nubmer>] [-l=<size>] [-f] [-i=<TTL>]\n\t\t[-w=<timeout>] [-h] [-4] [-6] [-W=<timeout>]\n\t\t[-s=<file>] [[-d] | [-d=<directory>]] <hostlist>",
"",
"Options:",
{ "?|help", Resources.Resource.description_help,
v => ShowHelp = true
},
{ "t", $"Specifies {Name} continue sending echo Request messages to the destination until interrupted. " +
$"To display statistic and continue, press CTRL+BREAK (CTRL+\\ on Linux). " +
$"To interrupt, display statistics and quit, press CTRL+C. " +
$"Will display message if specified.",
v => Endless = true
},
{ "n=", $"Specifies the number of echo Request messages be sent. " +
$"The default is {MaximumPackages}. " +
$"The maximum is 18,446,744,073,709,551,615.",
(ulong v) => MaximumPackages = v
},
{ "l=", $"Specifies the length, in bytes, of the Data field in the echo Request messages. " +
$"The default is {PackageSize}. " +
$"The maximum size is 65,527.",
(ushort v) => PackageSize = v
},
{ "f", $"Specifies that echo Request messages are sent with the Do not Fragment flag in the IP header set to 1 (available on IPv4 only).",
v => AllowPackageFragmentation = true
},
{ "i=", $"Specifies the value of the Time To Live (TTL) field in the IP header for echo Request messages sent. " +
$"The default is {Ttl}. " +
$"The maximum TTL is 255.",
(byte v) => Ttl = v
},
{ "w=", $"Specifies the amount of time, in milliseconds, to wait for the echo Reply message corresponding to a given echo Request message. " +
$"The default time-out is {ResponseTimeout} ({TimeSpan.FromMilliseconds(ResponseTimeout).TotalSeconds} sec). " +
$"The maximum time-out is 2,147,483,647 (~25 days).",
(int v) => ResponseTimeout = v
},
{ "h", $"Specifies {Name} to hide the results of attempts to send echo Request messages. " +
$"Will display message if specified.",
v => ShouldHideOutput = true
},
{ "s=", $"Specifies path of the file of destinations list. " +
$"Full and relative paths available. " +
$"Will display message if specified.",
(string v) => SourceFile = v
},
{ "d:", $"Enables writing results of attempts to send echo Request messages to a file. " +
$"The default path is current working directory. " +
$"Full and relative paths available. " +
$"Will display message if specified.",
(string v) =>
{
if (v == null || v.Length == 0) DestinationFolder = Directory.GetCurrentDirectory();
else DestinationFolder = v;
}
},
{ "W=", $"Specifies the amount of time, in milliseconds, to wait between sending each new echo Request message. " +
$"The default time-out is {RequestTimeout} ({TimeSpan.FromMilliseconds(RequestTimeout).TotalSeconds} sec). " +
$"The maximum time-out is 2,147,483,647 (~25 days).",
(int v) => RequestTimeout = v
},
{ "4", $"Specifies IPv4 used to ping. " +
$"This parameter is only required to identify the target host by name.",
v => ProtocolVersion = AddressFamily.InterNetwork
},
{ "6", $"Specifies IPv6 used to ping. " +
$"This parameter is only required to identify the target host by name.",
v => ProtocolVersion = AddressFamily.InterNetworkV6
},
};
#endregion
try { _extra = p.Parse(args).ToHashSet(); }
catch (OptionException e)
{
Console.Write($"{Name}: {e.Message} Try `{Name} --help' for more information.");
return;
}
if (SourceFile != null) Read_SourceFile();
if (DestinationFolder != null)
{
Console.Write($"Key d specified");
string dirFullPath = Path.GetFullPath(DestinationFolder);
if (!Directory.Exists(dirFullPath))
{
Console.Write($", but directory \"{dirFullPath}\" is not exist. {Name} will try to create directory... ");
try { Directory.CreateDirectory(dirFullPath); }
catch (Exception e)
{
Console.WriteLine($"{e.Message}");
return;
};
Console.Write($"Success");
};
DestinationFolder = dirFullPath;
Console.WriteLine($". {Name} will logging output to files in directory \"{DestinationFolder}{Path.DirectorySeparatorChar}\".");
};
if (ShowHelp || _extra.Count == 0)
{
p.WriteOptionDescriptions(Console.Out);
return;
}
if (ShouldHideOutput) Console.WriteLine($"Key h specified. {Name} will hide output for each package.");
if (Endless) Console.WriteLine($"Key t specified. {Name} will continue sending echo Request messages to the destination until interrupted. " +
$"To display statistic and continue, press CTRL+BREAK (CTRL+\\ on Linux). " +
$"To interrupt, display statistics and quit, press CTRL+C.");
if (DestinationFolder != null || ShouldHideOutput || Endless) Console.WriteLine();
Tasks = new ConcurrentHashSet<PingTask>();
ShouldContinue = true;
Results = new Dictionary<int, (IPAddress Address, Dictionary<string, ulong> MessagesCounter, HashSet<long> RoundtripTimeValues)>();
foreach (string s in _extra) Tasks.Add(new PingTask(s));
foreach (var t in Tasks) { new Task(async () => t.Run()).Start(); }
WhilePinging.WaitOne();
ShowResults();
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
if (e.SpecialKey == ConsoleSpecialKey.ControlC)
{
ShouldContinue = false;
if (Tasks != null) foreach (var task in Tasks) if (task == null) Tasks.Remove(task);
}
if (e.SpecialKey == ConsoleSpecialKey.ControlBreak)
{
ShowResults();
Console.WriteLine();
}
}
public static void ShowResults()
{
foreach (var kv in Results)
if (kv.Value.MessagesCounter["Sent"] > 0)
{
Console.WriteLine($"\tPackages to {kv.Value.Address}:" +
$"\n\t\tSent = {kv.Value.MessagesCounter["Sent"]}" +
$", Received = {kv.Value.MessagesCounter["Received"]}" +
$", Lost = {kv.Value.MessagesCounter["Lost"]}" +
", ({0:0.##}% loss)", ((float)kv.Value.MessagesCounter["Lost"] / (float)kv.Value.MessagesCounter["Sent"]) * 100);
if (kv.Value.RoundtripTimeValues.Count > 0)
Console.WriteLine($"\tTime-outs to {kv.Value.Address}:" +
$"\n\t\tMinimum = {kv.Value.RoundtripTimeValues.Min()}ms" +
$", Maximum = {kv.Value.RoundtripTimeValues.Max()}ms" +
", Average = {0:0.##}ms", kv.Value.RoundtripTimeValues.Average());
}
}
private static void Read_SourceFile()
{
if (!File.Exists(SourceFile))
{
Console.WriteLine($"Key s specified, but file \"{SourceFile}\" does not exist. {Name} will ignore this key.");
return;
}
string line;
var source = new StreamReader(SourceFile);
while ((line = source.ReadLine()) != null) _extra.Add(line);
}
}
}