-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (39 loc) · 1.4 KB
/
Program.cs
File metadata and controls
50 lines (39 loc) · 1.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
/* SPDX-License-Identifier: MIT
* Copyright (c) 2025 aneilmac
*
* A simple test program that waits for and displays incoming packets.
*/
using System.Runtime.Versioning;
using Microsoft.Extensions.Logging;
using NetWintun;
// Setup the logger
using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
Wintun.SetLogger(loggerFactory.CreateLogger<Program>());
// Create an adapter, which loads up the drivers
using var adapter = Adapter.Create("Basic", "Demo");
Console.WriteLine("Wintun version is: {0}", Wintun.GetRunningDriverVersion());
using var session = adapter.StartSession(Wintun.Constants.MaxRingCapacity);
Console.WriteLine("\nPRESS ANY KEY TO QUIT\n");
var cts = new CancellationTokenSource();
var task = Task.Run(async () =>
{
// Asynchronously await for incoming packets.
while (!cts.IsCancellationRequested)
{
var packet = await session.ReceivePacketAsync(cts.Token).ConfigureAwait(false);
var header = packet[..Math.Min(packet.Length, 20)];
Console.WriteLine("[{0:G}] Packet size: {1:000}. Header: {2}",
DateTime.Now, packet.Length, Convert.ToHexString(header));
}
}, cts.Token);
_ = Console.ReadLine();
try
{
await Task.WhenAll(cts.CancelAsync(), task).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Suppress cancellation exception
}
[SupportedOSPlatform("Windows")]
public partial class Program { }