forked from NtsFranz/Spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestIPFetching.cs
567 lines (482 loc) · 16.9 KB
/
QuestIPFetching.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using EchoVRAPI;
using Newtonsoft.Json;
using Spark.Properties;
namespace Spark
{
public class QuestIPFetching
{
private static Thread IPSearchthread1;
private static Thread IPSearchthread2;
// The max number of physical addresses.
const int MAXLEN_PHYSADDR = 8;
// Define the MIB_IPNETROW structure.
[StructLayout(LayoutKind.Sequential)]
struct MIB_IPNETROW
{
[MarshalAs(UnmanagedType.U4)] public int dwIndex;
[MarshalAs(UnmanagedType.U4)] public int dwPhysAddrLen;
[MarshalAs(UnmanagedType.U1)] public byte mac0;
[MarshalAs(UnmanagedType.U1)] public byte mac1;
[MarshalAs(UnmanagedType.U1)] public byte mac2;
[MarshalAs(UnmanagedType.U1)] public byte mac3;
[MarshalAs(UnmanagedType.U1)] public byte mac4;
[MarshalAs(UnmanagedType.U1)] public byte mac5;
[MarshalAs(UnmanagedType.U1)] public byte mac6;
[MarshalAs(UnmanagedType.U1)] public byte mac7;
[MarshalAs(UnmanagedType.U4)] public int dwAddr;
[MarshalAs(UnmanagedType.U4)] public int dwType;
}
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
public static IPAddress QuestIP = null;
public static bool IPPingThread1Done = false;
public static bool IPPingThread2Done = false;
// Declare the GetIpNetTable function.
[DllImport("IpHlpApi.dll")]
[return: MarshalAs(UnmanagedType.U4)]
static extern int GetIpNetTable(
IntPtr pIpNetTable,
[MarshalAs(UnmanagedType.U4)] ref int pdwSize,
bool bOrder);
[DllImport("IpHlpApi.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int FreeMibTable(IntPtr plpNetTable);
// The insufficient buffer error.
const int ERROR_INSUFFICIENT_BUFFER = 122;
public static string GetLocalIP()
{
using Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0);
socket.Connect("8.8.8.8", 65530);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
return endPoint != null ? endPoint.Address.ToString() : "";
}
public static List<IPAddress> GetLocalIPAddresses()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
return host.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToList();
}
public static List<IPAddress> GetPossibleLocalIPs()
{
List<IPAddress> myIps = GetLocalIPAddresses();
List<IPAddress> ips = new List<IPAddress>();
foreach (IPAddress ip in myIps)
{
for (byte i = 0; i < 255; i++)
{
List<byte> orig = ip.GetAddressBytes().SkipLast(1).ToList();
orig.Add(i);
ips.Add(new IPAddress(orig.ToArray()));
}
}
return ips;
}
public static void GetCurrentIPAndPingNetwork()
{
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces().Where(ni => ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)))
{
GatewayIPAddressInformation? addr = adapter.GetIPProperties().GatewayAddresses.FirstOrDefault();
if (addr != null && !addr.Address.ToString().Equals("0.0.0.0"))
{
foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
{
if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine("PC IP Address: " + unicastIPAddressInformation.Address);
Console.Write("PC Subnet Mask: " + unicastIPAddressInformation.IPv4Mask + "\n Searching for Quest on network...");
PingNetworkIPs(unicastIPAddressInformation.Address, unicastIPAddressInformation.IPv4Mask);
}
}
}
}
}
public static async Task GetCurrentIPAndPingNetworkAsync()
{
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces().Where(ni => ni.OperationalStatus == OperationalStatus.Up && (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)))
{
GatewayIPAddressInformation? addr = adapter.GetIPProperties().GatewayAddresses.FirstOrDefault();
if (addr != null && !addr.Address.ToString().Equals("0.0.0.0"))
{
foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
{
if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine("PC IP Address: " + unicastIPAddressInformation.Address);
Console.Write("PC Subnet Mask: " + unicastIPAddressInformation.IPv4Mask + "\n Searching for Quest on network...");
uint ipAddress = BitConverter.ToUInt32(unicastIPAddressInformation.Address.GetAddressBytes(), 0);
uint ipMaskV4 = BitConverter.ToUInt32(unicastIPAddressInformation.IPv4Mask.GetAddressBytes(), 0);
uint broadCastIpAddress = ipAddress | ~ipMaskV4;
IPAddress start = new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
byte leastSigByte = unicastIPAddressInformation.Address.GetAddressBytes().Last();
int range = 255 - leastSigByte;
List<IPAddress> pingReplyTasks = Enumerable.Range(0, range)
.Select(x =>
{
byte[] bb = start.GetAddressBytes();
bb[3] = (byte)x;
IPAddress destIp = new IPAddress(bb);
return destIp;
})
.ToList();
IEnumerable<Task<PingReply>> tasks = pingReplyTasks.Select(ip => new Ping().SendPingAsync(ip, 4000));
PingReply[] results = await Task.WhenAll(tasks);
}
}
}
}
}
public static async void PingIPList(List<IPAddress> IPs, int threadID)
{
IEnumerable<Task<PingReply>> tasks = IPs.Select(ip => new Ping().SendPingAsync(ip, 4000));
PingReply[] results = await Task.WhenAll(tasks);
switch (threadID)
{
case 1:
IPPingThread1Done = true;
break;
case 2:
IPPingThread2Done = true;
break;
default:
break;
}
}
public static void PingNetworkIPs(IPAddress address, IPAddress mask)
{
uint ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
uint ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
uint broadCastIpAddress = ipAddress | ~ipMaskV4;
IPAddress start = new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
byte leastSigByte = address.GetAddressBytes().Last();
int range = 255 - leastSigByte;
List<IPAddress> pingReplyTasks = Enumerable.Range(leastSigByte, range)
.Select(x =>
{
byte[] bb = start.GetAddressBytes();
bb[3] = (byte)x;
IPAddress destIp = new IPAddress(bb);
return destIp;
})
.ToList();
List<IPAddress> pingReplyTasks2 = Enumerable.Range(0, leastSigByte - 1)
.Select(x =>
{
byte[] bb = start.GetAddressBytes();
bb[3] = (byte)x;
IPAddress destIp = new IPAddress(bb);
return destIp;
})
.ToList();
IPSearchthread1 = new Thread(() => PingIPList(pingReplyTasks, 1));
IPSearchthread2 = new Thread(() => PingIPList(pingReplyTasks2, 2));
IPPingThread1Done = false;
IPPingThread2Done = false;
IPSearchthread1.Start();
IPSearchthread2.Start();
}
public static async Task PingNetworkIPsAsync(IPAddress address, IPAddress mask)
{
}
public static void CheckARPTable()
{
int bytesNeeded = 0;
// The result from the API call.
int result = GetIpNetTable(IntPtr.Zero, ref bytesNeeded, false);
// Call the function, expecting an insufficient buffer.
if (result != ERROR_INSUFFICIENT_BUFFER)
{
// Throw an exception.
throw new Exception();
}
// Allocate the memory, do it in a try/finally block, to ensure
// that it is released.
IntPtr buffer = IntPtr.Zero;
// Allocate the memory.
buffer = Marshal.AllocCoTaskMem(bytesNeeded);
// Make the call again. If it did not succeed, then
// raise an error.
result = GetIpNetTable(buffer, ref bytesNeeded, false);
// If the result is not 0 (no error), then throw an exception.
if (result != 0)
{
// Throw an exception.
throw new Exception();
}
// Now we have the buffer, we have to marshal it. We can read
// the first 4 bytes to get the length of the buffer.
int entries = Marshal.ReadInt32(buffer);
// Increment the memory pointer by the size of the int.
IntPtr currentBuffer = new IntPtr(buffer.ToInt64() +
Marshal.SizeOf(typeof(int)));
// Allocate an array of entries.
MIB_IPNETROW[] table = new MIB_IPNETROW[entries];
// Cycle through the entries.
for (int index = 0; index < entries; index++)
{
// Call PtrToStructure, getting the structure information.
table[index] = (MIB_IPNETROW)Marshal.PtrToStructure(new IntPtr(currentBuffer.ToInt64() + (index * Marshal.SizeOf(typeof(MIB_IPNETROW)))), typeof(MIB_IPNETROW));
}
for (int index = 0; index < entries; index++)
{
MIB_IPNETROW row = table[index];
if (row.mac0 == 0x2C && row.mac1 == 0x26 && row.mac2 == 0x17)
{
QuestIP = new IPAddress(BitConverter.GetBytes(row.dwAddr));
break;
}
}
// Release the memory.
FreeMibTable(buffer);
}
public static async Task<List<IPAddress>> CheckARPTableAsync(bool onlyQuests = true)
{
int bytesNeeded = 0;
// The result from the API call.
int result = GetIpNetTable(IntPtr.Zero, ref bytesNeeded, false);
// Call the function, expecting an insufficient buffer.
if (result != ERROR_INSUFFICIENT_BUFFER)
{
// Throw an exception.
throw new Exception();
}
// Allocate the memory, do it in a try/finally block, to ensure
// that it is released.
// Allocate the memory.
IntPtr buffer = Marshal.AllocCoTaskMem(bytesNeeded);
// Make the call again. If it did not succeed, then
// raise an error.
result = GetIpNetTable(buffer, ref bytesNeeded, false);
// If the result is not 0 (no error), then throw an exception.
if (result != 0)
{
// Throw an exception.
throw new Exception();
}
// Now we have the buffer, we have to marshal it. We can read
// the first 4 bytes to get the length of the buffer.
int entries = Marshal.ReadInt32(buffer);
// Increment the memory pointer by the size of the int.
IntPtr currentBuffer = new IntPtr(buffer.ToInt64() + Marshal.SizeOf(typeof(int)));
// Allocate an array of entries.
MIB_IPNETROW[] table = new MIB_IPNETROW[entries];
// Cycle through the entries.
for (int index = 0; index < entries; index++)
{
// Call PtrToStructure, getting the structure information.
table[index] = (MIB_IPNETROW)Marshal.PtrToStructure(new IntPtr(currentBuffer.ToInt64() + (index * Marshal.SizeOf(typeof(MIB_IPNETROW)))), typeof(MIB_IPNETROW));
}
List<IPAddress> ips = new List<IPAddress>();
for (int index = 0; index < entries; index++)
{
MIB_IPNETROW row = table[index];
if (!onlyQuests || (row.mac0 == 0x2C && row.mac1 == 0x26 && row.mac2 == 0x17))
{
IPAddress ip = new IPAddress(BitConverter.GetBytes(row.dwAddr));
if (!ips.Contains(ip)) ips.Add(ip);
}
}
// Release the memory.
FreeMibTable(buffer);
return ips;
}
/// <summary>
/// https://stackoverflow.com/a/31492250
/// </summary>
private static Task<int> RunProcessAsync(Process process)
{
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
process.Exited += (s, ea) => tcs.SetResult(process.ExitCode);
process.OutputDataReceived += (s, ea) => Console.WriteLine(ea.Data);
process.ErrorDataReceived += (s, ea) => Console.WriteLine("ERR: " + ea.Data);
bool started = process.Start();
if (!started)
{
//you may allow for the process to be re-used (started = false)
//but I'm not sure about the guarantees of the Exited event in such a case
throw new InvalidOperationException("Could not start process: " + process);
}
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return tcs.Task;
}
public static void ClearARPCache()
{
try
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C netsh interface ip delete arpcache",
Verb = "runas",
UseShellExecute = true
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit(500);
Thread.Sleep(20);
}
catch
{
// ignored
}
}
public static async Task ClearARPCacheAsync()
{
try
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C netsh interface ip delete arpcache",
Verb = "runas",
UseShellExecute = true
};
process.StartInfo = startInfo;
process.Start();
await process.WaitForExitAsync();
}
catch
{
// ignored
}
}
// /// <summary>
// /// Waits asynchronously for the process to exit.
// /// </summary>
// /// <param name="process">The process to wait for cancellation.</param>
// /// <param name="cancellationToken">A cancellation token. If invoked, the task will return
// /// immediately as canceled.</param>
// /// <returns>A Task representing waiting for the process to end.</returns>
// public static Task WaitForExitAsync(this Process process,
// CancellationToken cancellationToken = default(CancellationToken))
// {
// if (process.HasExited) return Task.CompletedTask;
//
// var tcs = new TaskCompletionSource<object>();
// process.EnableRaisingEvents = true;
// process.Exited += (sender, args) => tcs.TrySetResult(null);
// if(cancellationToken != default(CancellationToken))
// cancellationToken.Register(() => tcs.SetCanceled());
//
// return process.HasExited ? Task.CompletedTask : tcs.Task;
// }
public static async Task<List<(IPAddress, string)>> PingEchoVRAPIAsync(IReadOnlyCollection<IPAddress> ips, int maxConcurrency = 1000, IProgress<float> progress = null)
{
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(2);
int count = ips.Count;
int finished = 0;
SemaphoreSlim throttler = new SemaphoreSlim(initialCount: maxConcurrency);
IEnumerable<Task<string>> tasks = ips.Select(async ip =>
{
// do an async wait until we can schedule again
await throttler.WaitAsync();
string s = null;
try
{
HttpResponseMessage response = await client.GetAsync($"http://{ip}:6721/session");
s = await response.Content.ReadAsStringAsync();
}
catch (Exception)
{
// ignored
}
finished += 1;
progress?.Report((float)finished/count);
throttler.Release();
return s;
});
string[] results = await Task.WhenAll(tasks);
return ips.Zip(results).ToList();
}
/// <summary>
/// Finds a Quest local IP address on the same network
/// </summary>
/// <returns>The IP address</returns>
public static string FindQuestIP(IProgress<string> progress)
{
try
{
string QuestStatusLabel = Resources.Searching_for_Quest_on_network;
QuestIP = null;
ClearARPCache();
CheckARPTable();
int count = 0;
string statusDots = "";
if (QuestIP == null)
{
GetCurrentIPAndPingNetwork();
while (QuestIP == null && (!IPPingThread1Done || !IPPingThread2Done))
{
if (count % 16 == 0)
{
statusDots = "";
}
else if (count % 4 == 0)
{
statusDots += ".";
}
count++;
progress.Report(QuestStatusLabel + statusDots);
Thread.Sleep(50);
CheckARPTable();
}
IPSearchthread1 = null;
IPSearchthread2 = null;
if (QuestIP != null)
{
progress.Report(Resources.QuestIPFetching_FindQuestIP_Found_Quest_on_network_);
}
else
{
Thread.Sleep(1000);
CheckARPTable();
if (QuestIP != null)
{
progress.Report(Resources.QuestIPFetching_FindQuestIP_Found_Quest_on_network_);
}
else
{
progress.Report(Resources.Failed_to_find_Quest_on_network_);
}
}
}
else
{
progress.Report(Resources.QuestIPFetching_FindQuestIP_Found_Quest_on_network_);
}
}
finally
{
}
Thread.Sleep(500);
return QuestIP == null ? "127.0.0.1" : QuestIP.ToString();
// TODO set Program.echoVRIP
}
public static async Task<List<IPAddress>> FindAllQuestIPs(IProgress<List<IPAddress>> progress)
{
// await ClearARPCacheAsync();
List<IPAddress> ips = await CheckARPTableAsync();
progress.Report(ips);
await GetCurrentIPAndPingNetworkAsync();
ips.AddRange(await CheckARPTableAsync());
ips = ips.Distinct().ToList();
progress.Report(ips);
// IEnumerable<SimpleFrame> frames = results.Select(s=> s != null ? JsonConvert.DeserializeObject<SimpleFrame>(s) : null);
return ips;
}
}
}