Skip to content

Commit cd16829

Browse files
committed
Vendor/OUI lookup implemented, cleanup, bugfix
1 parent 5946ba6 commit cd16829

52 files changed

Lines changed: 761 additions & 233 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Source/NETworkManager/Converters/IPAddressToStringConverter.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
using System;
22
using System.Globalization;
3-
using System.Net;
43
using System.Windows.Data;
54

65
namespace NETworkManager.Converters
76
{
8-
public sealed class IPAddressToStringConverter : IValueConverter
7+
public sealed class NullOrEmptyToStringConverter : IValueConverter
98
{
109
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1110
{
12-
if (value == null)
11+
string str = value as string;
12+
13+
if (string.IsNullOrEmpty(str))
1314
return "-/-";
14-
15-
return ((IPAddress)value).ToString();
15+
16+
return str;
1617
}
1718

1819
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Net;
4+
using System.Windows.Data;
5+
6+
namespace NETworkManager.Converters
7+
{
8+
public sealed class IPAddressToStringConverter : IValueConverter
9+
{
10+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
11+
{
12+
if (value == null)
13+
return "-/-";
14+
15+
return ((IPAddress)value).ToString();
16+
}
17+
18+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
}
23+
}

‎Source/NETworkManager/Helpers/RegexHelper.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static class RegexHelper
1010

1111
// Match a MAC-Address 00-00-00-00-00-00-00
1212
public const string MACAddressRegex = @"^[A-Fa-f0-9]{12}$|^[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}$";
13+
public const string MACAddressFirst3BytesRegex = @"^[A-Fa-f0-9]{6}$|^[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}(:|-){1}[A-Fa-f0-9]{2}$";
1314

1415
// Match a Subnetmask like 255.255.255.0
1516
private const string SubnetmaskValues = @"(255|254|252|248|240|224|192|128|0)";

‎Source/NETworkManager/MainWindow.xaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
</Rectangle>
312312
<TextBlock Grid.Column="1" Text="{Binding TranslatedName}">
313313
<TextBlock.Style>
314-
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DefaultTextBlockStyle}">
314+
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource HeaderTextBlock}">
315315
<Setter Property="Foreground" Value="{DynamicResource GrayBrush3}" />
316316
<Setter Property="Margin" Value="0" />
317317
<Setter Property="VerticalAlignment" Value="Center" />

‎Source/NETworkManager/MainWindow.xaml.cs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public MainWindow()
178178

179179
// Load templates
180180
TemplateManager.LoadNetworkInterfaceConfigTemplates();
181-
TemplateManager.LoadWakeOnLanTemplates();
181+
TemplateManager.LoadWakeOnLANTemplates();
182182

183183
_isLoading = false;
184184
}
@@ -244,9 +244,9 @@ private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
244244
// Save templates
245245
if (TemplateManager.NetworkInterfaceConfigTemplatesChanged && !ImportExportManager.ForceRestart)
246246
TemplateManager.SaveNetworkInterfaceConfigTemplates();
247-
248-
if (TemplateManager.WakeOnLanTemplatesChanged && !ImportExportManager.ForceRestart)
249-
TemplateManager.SaveWakeOnLanTemplates();
247+
248+
if (TemplateManager.WakeOnLANTemplatesChanged && !ImportExportManager.ForceRestart)
249+
TemplateManager.SaveWakeOnLANTemplates();
250250

251251
// Save settings
252252
if (SettingsManager.Current.SettingsChanged && !ImportExportManager.ForceRestart)
@@ -271,6 +271,7 @@ private async void MetroWindowMain_Closing(object sender, CancelEventArgs e)
271271
PingView pingView;
272272
TracerouteView tracerouteView;
273273
DNSLookupView dnsLookupView;
274+
OUILookupView ouiLookupView;
274275

275276
private ApplicationView.Name? currentApplicationViewName = null;
276277

@@ -329,6 +330,12 @@ private void ChangeApplicationView(ApplicationView.Name name)
329330

330331
contentControlApplication.Content = dnsLookupView;
331332
break;
333+
case ApplicationView.Name.OUILookup:
334+
if (ouiLookupView == null)
335+
ouiLookupView = new OUILookupView();
336+
337+
contentControlApplication.Content = ouiLookupView;
338+
break;
332339
}
333340

334341
currentApplicationViewName = name;
@@ -614,7 +621,7 @@ private void CloseApplicationAction()
614621

615622
private void RestartApplication()
616623
{
617-
Process.Start(Assembly.GetExecutingAssembly().Location);
624+
Process.Start(ConfigurationManager.Current.ApplicationPath);
618625

619626
_closeApplication = true;
620627
Close();

‎Source/NETworkManager/Models/Network/IPScanner.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,25 @@ public void ScanAsync(IPAddress[] ipAddresses, IPScannerOptions ipScannerOptions
9797

9898
// ARP
9999
PhysicalAddress macAddress = null;
100+
string vendor = string.Empty;
100101

101102
if (ipScannerOptions.ResolveMACAddress)
102103
{
103104
try
104105
{
106+
// Get mac
105107
if (Dns.GetHostName() == hostname.Substring(0, hostname.IndexOf('.')))
106-
macAddress = NetworkInterface.GetNetworkInterfaces().Where(p => p.IPv4Address.Contains(ipAddress)).FirstOrDefault().PhysicalAddress;
108+
macAddress = NetworkInterface.GetNetworkInterfaces().Where(p => p.IPv4Address.Contains(ipAddress)).First().PhysicalAddress;
107109
else
108110
macAddress = IPNetTableHelper.GetAllDevicesOnLAN().Where(p => p.Key.ToString() == ipAddress.ToString()).ToDictionary(p => p.Key, p => p.Value).First().Value;
111+
112+
// Vendor lookup
113+
vendor = OUILookup.Lookup(macAddress).Vendor;
109114
}
110115
catch { }
111116
}
112117

113-
OnHostFound(new IPScannerHostFoundArgs(pingInfo, hostname, macAddress));
118+
OnHostFound(new IPScannerHostFoundArgs(pingInfo, hostname, macAddress, vendor));
114119

115120
break;
116121
}

‎Source/NETworkManager/Models/Network/IPScannerHostFoundArgs.cs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ public class IPScannerHostFoundArgs : System.EventArgs
77
public PingInfo PingInfo { get; set; }
88
public string Hostname { get; set; }
99
public PhysicalAddress MACAddress { get; set; }
10+
public string Vendor { get; set; }
1011

1112
public IPScannerHostFoundArgs()
1213
{
1314

1415
}
1516

16-
public IPScannerHostFoundArgs(PingInfo pingInfo, string hostname, PhysicalAddress macAddress)
17+
public IPScannerHostFoundArgs(PingInfo pingInfo, string hostname, PhysicalAddress macAddress, string vendor)
1718
{
1819
PingInfo = pingInfo;
1920
Hostname = hostname;
2021
MACAddress = macAddress;
21-
}
22+
Vendor = vendor;
23+
}
2224
}
2325
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
using System.Net.NetworkInformation;
1+
using System.Net.NetworkInformation;
32

43
namespace NETworkManager.Models.Network
54
{
@@ -8,22 +7,24 @@ public class IPScannerHostInfo
87
public PingInfo PingInfo { get; set; }
98
public string Hostname { get; set; }
109
public PhysicalAddress MACAddress { get; set; }
10+
public string Vendor { get; set; }
1111

1212
public IPScannerHostInfo()
1313
{
1414

1515
}
1616

17-
public IPScannerHostInfo(PingInfo pingInfo, string hostname, PhysicalAddress macAddress)
17+
public IPScannerHostInfo(PingInfo pingInfo, string hostname, PhysicalAddress macAddress, string vendor)
1818
{
1919
PingInfo = pingInfo;
2020
Hostname = hostname;
2121
MACAddress = macAddress;
22+
Vendor = vendor;
2223
}
23-
24+
2425
public static IPScannerHostInfo Parse(IPScannerHostFoundArgs e)
2526
{
26-
return new IPScannerHostInfo(e.PingInfo, e.Hostname, e.MACAddress);
27+
return new IPScannerHostInfo(e.PingInfo, e.Hostname, e.MACAddress, e.Vendor);
2728
}
2829
}
2930
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace NETworkManager.Models.Network
2+
{
3+
public class OUIInfo
4+
{
5+
public string MACAddress { get; set; }
6+
public string Vendor { get; set; }
7+
8+
public OUIInfo()
9+
{
10+
11+
}
12+
13+
public OUIInfo(string macAddress, string vendor)
14+
{
15+
MACAddress = macAddress;
16+
Vendor = vendor;
17+
}
18+
}
19+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using System.IO;
5+
using NETworkManager.Models.Settings;
6+
using System.Text.RegularExpressions;
7+
using System.Net.NetworkInformation;
8+
9+
namespace NETworkManager.Models.Network
10+
{
11+
public static class OUILookup
12+
{
13+
#region Variables
14+
private static string OUIPath = Path.Combine(ConfigurationManager.Current.StartupPath, "Resources", "oui.txt");
15+
16+
public static Dictionary<string, string> OUIDict = new Dictionary<string, string>();
17+
18+
#endregion
19+
20+
#region Methods
21+
static OUILookup()
22+
{
23+
// Load list from resource folder (.txt-file)
24+
foreach (string line in File.ReadAllLines(OUIPath))
25+
{
26+
string[] ouiData = line.Split('|');
27+
28+
if (!string.IsNullOrEmpty(ouiData[0]) && !OUIDict.Keys.Contains(ouiData[0]))
29+
OUIDict.Add(ouiData[0], ouiData[1]);
30+
}
31+
}
32+
33+
public static Task<OUIInfo> LookupAsync(string macAddressOrFirst3Bytes)
34+
{
35+
return Task.Run(() => Lookup(macAddressOrFirst3Bytes));
36+
}
37+
38+
public static OUIInfo Lookup(string macAddressOrFirst3Bytes)
39+
{
40+
string ouiKey = Regex.Replace(macAddressOrFirst3Bytes, "-|:", "").Substring(0,6);
41+
42+
string vendor = string.Empty;
43+
OUIDict.TryGetValue(ouiKey, out vendor);
44+
45+
return new OUIInfo(ouiKey, vendor);
46+
}
47+
48+
public static Task<OUIInfo> LookupAsync(PhysicalAddress macAddress)
49+
{
50+
return Task.Run(() => Lookup(macAddress));
51+
}
52+
53+
public static OUIInfo Lookup(PhysicalAddress macAddress)
54+
{
55+
return Lookup(macAddress.ToString());
56+
}
57+
#endregion
58+
}
59+
}

0 commit comments

Comments
 (0)