-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
64 lines (54 loc) · 1.78 KB
/
Program.cs
File metadata and controls
64 lines (54 loc) · 1.78 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
using System;
using CommandLine;
namespace IPK_Project2;
static class Program {
static void Main(string[] args) {
// Handle CTRL+C
Console.CancelKeyPress += HandleCancelEvent;
// Parse arguments
ParserResult<Cli> cli = Parser.Default.ParseArguments<Cli>(args);
cli.WithNotParsed(_ => { Environment.Exit(0); });
// Validate arguments
cli.WithParsed(o => {
if (o is { Port: not null, PortDestination: not null } or { Port: not null, PortSource: not null } or
{ PortDestination: not null, PortSource: not null }) {
Error.Exit(
"Invalid combination of arguments. -p --port-destination and --port-source cannot be used in combination");
}
if ((o.Port != null || o.PortDestination != null || o.PortSource != null) &&
o is { Tcp: false, Udp: false }) {
Error.Exit(
"Invalid combination of arguments. -p --port-destination and --port-source have to be used in combination with --tcp or --udp");
}
});
// Validate interface
CheckInterface(cli.Value);
try {
Sniffer.Sniff(cli.Value);
} catch (Exception e) {
Error.Exit(e.Message);
}
}
// Handle CTRL+C
private static void HandleCancelEvent(object? sender, ConsoleCancelEventArgs eventArgs) {
eventArgs.Cancel = true;
Sniffer.Close();
eventArgs.Cancel = false;
}
// Print available interfaces if the value for -i or --interface was not specified or the interface does nto exist
private static void CheckInterface(Cli args) {
try {
if (args.Interface == null) {
Interface.ListNetworkInterfaces();
Environment.Exit(0);
}
if (!Interface.InterfaceExists(args.Interface)) {
Error.Print($"Interface {args.Interface} does not exist");
Interface.ListNetworkInterfaces();
Environment.Exit(1);
}
} catch (Exception e) {
Error.Exit(e.Message);
}
}
}