-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalDeviceSelector.cs
482 lines (403 loc) · 16.3 KB
/
LocalDeviceSelector.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Net.NetworkInformation;
namespace Local_Device_Selector.NET
{
public static partial class Extensions
{
public static Boolean IsNull(this Object obj) =>
obj is String ? String.IsNullOrEmpty(obj as String) : obj == null;
public static Boolean IsHexString(this String hexStr) =>
hexStr.All(c => c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' || c >= '0' && c <= '9');
}
internal class LocalDeviceInfo
{
public readonly String ipAddress, macAddress;
public Boolean IsAvailable { get; private set; }
protected Boolean? IsSynchronized = null;
public String HostName { get; private set; }
public String Vendor { get; private set; }
public async Task<LocalDeviceInfo> RunProcessAsync(LocalDeviceInfo ldi)
{
var _ldi = ldi.MemberwiseClone() as LocalDeviceInfo;
await ldi.ProcessAsync(_ldi);
return ldi;
}
private async Task ProcessAsync(LocalDeviceInfo ldi)
{
String dns = ldi.HostName;
}
public LocalDeviceInfo(String IPAddress, String MACAddress)
{
ipAddress = IPAddress;
macAddress = MACAddress;
}
public override String ToString() =>
String.Format("ARP-Table Index: {0}\r\nIP-Address\t{1}\nMAC-Address\t{2}", LocalDeviceInfo.GetDeviceIndex(ipAddress), ipAddress, macAddress);
#region Static
protected static List<LocalDeviceInfo> deviceList;
//overload operator for comparison
public static Boolean operator ==(LocalDeviceInfo a, LocalDeviceInfo b) => EqualityComparer<LocalDeviceInfo>.Default.Equals(a, b);
public static Boolean operator !=(LocalDeviceInfo a, LocalDeviceInfo b) => !(a == b);
public static Boolean IsSerialized() =>
deviceList.TrueForAll(i => !i.IsSynchronized.IsNull());
public static List<LocalDeviceInfo> GetARP()
{
if (IsSerialized()) return deviceList;
var devList = new List<LocalDeviceInfo>();
foreach (var arp in FetchARPTable().Split(new Char[] { '\n', '\r' }))
{
//Parse out all the MAC / IP Address combinations
if (!arp.IsNull())
{
var parts = (from part in arp.Split(new Char[] { ' ', '\t' })
where !part.IsNull()
select part).ToArray();
if (parts.Length == 3)
{
devList.Add(new LocalDeviceInfo(parts[0], parts[1]));
}
//else throw new Exception("Could not parse ARP-Table...");
}
}
if(devList != deviceList)
{
deviceList = devList;
}
return deviceList;
}
async public static Task<LocalDeviceInfo> SyncDeviceAsync(LocalDeviceInfo ldi)
{
if (ldi.IsSynchronized.IsNull())
{
ldi.HostName = System.Net.Dns.GetHostEntryAsync(ldi.ipAddress).GetAwaiter().GetResult().HostName;
ldi.Vendor = await getVendor(ldi.macAddress);
PingReply pr = await new Ping().SendPingAsync(ldi.ipAddress);
ldi.IsAvailable = pr.Status == IPStatus.Success;
ldi.IsSynchronized = !(ldi.HostName.IsNull() && ldi.Vendor.IsNull());
}
return ldi;
}
async public static Task<List<LocalDeviceInfo>> SyncTableAsync()
{
deviceList.ForEach(dev => SyncDeviceAsync(dev).GetAwaiter());
return deviceList;
}
//This runs the "arp" utility in Windows to retrieve all the MAC / IP Address entries.
protected static String FetchARPTable()
{
String res = null;
try
{
var proc = Process.Start(new ProcessStartInfo("arp", "-a")
{
CreateNoWindow = true,
RedirectStandardOutput = true,
UseShellExecute = false
});
res = proc.StandardOutput.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("GetARPResults(): Error Retrieving 'arp -a' results", ex);
}
return res;
}
public static LocalDeviceInfo GetDeviceInfoByMAC(String macAddress) =>
GetARP().Where(dev => dev.macAddress.ToLowerInvariant() == macAddress.ToLowerInvariant()).Select(dev => dev).FirstOrDefault();
public static LocalDeviceInfo GetDeviceInfoByIP(String ipAddress) =>
GetARP().Where(dev => dev.ipAddress.ToLowerInvariant() == ipAddress.ToLowerInvariant()).Select(dev => dev).FirstOrDefault();
public static Int32 GetDeviceIndex(String ipAddress) =>
GetDeviceIndex(GetDeviceInfoByIP(ipAddress));
protected static Int32 GetDeviceIndex(LocalDeviceInfo dev)
{
var index = ~0;
for (var i = 0; i < deviceList.Count; i++) if (dev == deviceList[i]) index = i;
return index;
}
#region Mac-Address Lookup API
/* *************************
* Mac-Address Lookup API *
*****************************************************************************************
* All Documentations can be found here: http://www.macvendorlookup.com/mac-address-api ***
* ***************************************************************************************
*
* Return Value Descriptions:
* ***************************
* In all of the reponse formats, the order of the values are the same.
* A description of each value is below.
*
* startHex The start of the MAC address range the vendor owns in hexadecimal format
* endHex The end of the MAC address range the vendor owns in hexadecimal format
* startDec The start of the MAC address range the vendor owns in decimal format
* endDec The end of the MAC address range the vendor owns in decimal format
* company Company name of the vendor or manufacturer
* addressL1 First line of the address the company provided to IEEE
* addressL2 Second line of the address the company provided to IEEE
* addressL3 Third line of the address the company provided to IEEE
* country Country the company is located in
* type There are 3 different IEEE databases: oui24, oui36, and iab
*
*/
async protected static Task<String> getVendor(String macAddress)
{
String mac = macAddress.Replace("-", "").ToLower();
if (!mac.IsNull() && mac.Length < 6 && mac.IsHexString())
throw new FormatException("Exception occured in 'getVendor()'\n\nMAC-Address has a incorrect format!");
/* OUI => Organizationally Unique Identifier
* First 24 Bits of MAC-Address => 3 Bytes => 6 Character */
String oui = mac.Substring(0, 6);
String[] lookupResult = null;
try
{
lookupResult = new System.Net.WebClient().DownloadStringTaskAsync("http://www.macvendorlookup.com/api/v2/" + oui + "/pipe").GetAwaiter().GetResult().Split('|');
if (lookupResult.Count() != 10)
throw new Exception("macvendorlookup.com API Error");
return lookupResult[4];
}
catch (Exception e)
{
//handle api exception
}
return "Couldn't resolve device vendor";
}
#endregion
#endregion
}
public class DeviceSelector : Form
{
List<LocalDeviceInfo> arpTable;
Int32 LVIndex
{
get
{
if (deviceLV.SelectedItems.Count > 0)
return deviceLV.SelectedItems[0].Index;
return ~0;
}
}
async void RefreshDevices()
{
arpTable = LocalDeviceInfo.GetARP();
Int32 tableCount = arpTable.Count;
if (tableCount <= 0) MessageBox.Show("ARP-Table empty");
else
{
deviceLV.Items.Clear();
var tasks = new Task<LocalDeviceInfo>[tableCount];
for (var i = 0; i < tableCount; i++)
{
//tasks[i] = Task.Factory.StartNew<LocalDeviceInfo>(() => LocalDeviceInfo.SyncDeviceAsync(arpTable[i]).GetAwaiter().GetResult());
arpTable[i] = await LocalDeviceInfo.SyncDeviceAsync(arpTable[i]);
var items = new[]
{
new ListViewItem.ListViewSubItem()
{
Text = arpTable[i].ipAddress,
Tag = arpTable[i]
},
new ListViewItem.ListViewSubItem()
{
Text = arpTable[i].macAddress,
Tag = arpTable[i]
},
new ListViewItem.ListViewSubItem()
{
Text = arpTable[i].IsAvailable.ToString(),
Tag = arpTable[i],
ForeColor = arpTable[i].IsAvailable ? Color.LightGreen : Color.Red
}
};
if (deviceLV.InvokeRequired) deviceLV.Invoke(new Action( ()=> deviceLV.Items.Add(i.ToString()).SubItems.AddRange(items)));
else deviceLV.Items.Add(i.ToString()).SubItems.AddRange(items);
}
}
}
void Select()
{
}
void LoadFormEvent()
{
//refreshBtn.PerformClick();
}
private void ChangedDeviceListIndex()
{
/*var table = await LocalDeviceInfo.SyncTableAsync();
await Task.Run(() =>
{
SetSafeLabelText(vendorLbl, table[LVIndex].Vendor);
SetSafeLabelText(hostLbl, table[LVIndex].HostName);
});
*/
}
private void SetSafeLabelText(Label lbl, String txt)
{
if (lbl.InvokeRequired)
{
lbl.Invoke(new Action(() => lbl.Text = txt));
return;
}
lbl.Text = txt;
}
private void HandleControlEvents(Object sender, dynamic e)
{
//MessageBox.Show(sender.ToString());
var obj = sender as Control;
if (obj == refreshBtn)
RefreshDevices();
else if (obj == selectBtn)
Select();
else if (obj == deviceLV)
ChangedDeviceListIndex();
else if (obj == this)
LoadFormEvent();
else
throw new Exception("Unknown sender in 'HandleButtonEvent()'");
}
#region Draw GUI
System.ComponentModel.IContainer components = null;
public DeviceSelector()
{
InitializeForm();
}
protected override void Dispose(Boolean disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
ListView deviceLV;
Label hostLbl, vendorLbl;
Label[] labels;
GroupBox box;
Button refreshBtn, selectBtn;
//don't change anything here unless you know what you do...
void InitializeForm()
{
Application.EnableVisualStyles();
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
deviceLV = new ListView()
{
Size = new Size(0, 131),
Location = new Point(5, 5),
View = View.Details,
GridLines = true,
MultiSelect = false,
FullRowSelect = true,
AllowColumnReorder = false,
};
//deviceLV.SelectedIndexChanged += (() => );
//setup all columns to calculate the correct component sizes
var columns = new[]
{
new ColumnHeader()
{
Text = "Index",
Width = 50
},
new ColumnHeader()
{
Text = "IPv4-Address",
Width = 100,
TextAlign = HorizontalAlignment.Center
},
new ColumnHeader()
{
Text = "MAC-Address",
Width = 110,
TextAlign = HorizontalAlignment.Center
},
new ColumnHeader()
{
Text = "Available?",
Width = 69,
TextAlign = HorizontalAlignment.Center
}
};
deviceLV.Columns.AddRange(columns);
//Set now the correct size for the control
var countedSize = 0;
Array.ForEach<ColumnHeader>(deviceLV.Columns.Cast<ColumnHeader>().ToArray(), elem => countedSize += elem.Width);
deviceLV.Width = countedSize + (deviceLV.Columns.Count - 1 * 8) + 1;
var boldFont = new Font(DefaultFont, FontStyle.Bold);
labels = new[]
{
new Label
{
AutoSize = true,
Text = "Device Vendor",
Font = boldFont,
Location = new Point(57, 18),
},
new Label
{
AutoSize = true,
Text = "DNS Hostname",
Font = boldFont,
Location = new Point(50, 36),
}
};
vendorLbl = new Label()
{
AutoSize = true,
Text = "Unknown",
Location = new Point(labels[1].Location.X + 115, labels[0].Location.Y)
};
hostLbl = new Label()
{
AutoSize = true,
Text = "Unknown",
Location = new Point(vendorLbl.Location.X, labels[0].Location.Y + 17)
};
refreshBtn = new Button()
{
TabIndex = 0,
Text = "Refresh",
Size = new Size((Width / 2) - 17, 21),
Location = new Point(deviceLV.Location.X, deviceLV.Size.Height + 10)
};
selectBtn = new Button()
{
TabIndex = 1,
Text = "Select Device",
Size = new Size(refreshBtn.Width, 21),
Location = new Point((Width / 2) - 4, refreshBtn.Location.Y)
};
box = new GroupBox()
{
Text = "Current Selection",
Location = new Point(deviceLV.Location.X, refreshBtn.Location.Y + refreshBtn.Size.Height + 10),
Size = new Size(deviceLV.Width, 31 * 2)
};
//Setup form-controls events
var handler = new EventHandler(HandleControlEvents);
refreshBtn.Click += handler;
selectBtn.Click += handler;
Load += handler;
deviceLV.SelectedIndexChanged += handler;
box.Controls.AddRange(labels);
box.Controls.Add(vendorLbl);
box.Controls.Add(hostLbl);
Controls.Add(deviceLV);
Controls.Add(box);
Controls.Add(refreshBtn);
Controls.Add(selectBtn);
Width = deviceLV.Width + 26;
Height = deviceLV.Height + refreshBtn.Height + box.Height + 69;
box.SuspendLayout();
SuspendLayout();
}
#endregion
}
}