Skip to content

Commit 2a55df4

Browse files
committed
ARP-Table implemented
1 parent 8a7f40b commit 2a55df4

8 files changed

Lines changed: 193 additions & 34 deletions

File tree

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

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
using System;
66
using System.Collections.Generic;
77
using System.ComponentModel;
8+
using System.Diagnostics;
89
using System.Net;
910
using System.Net.NetworkInformation;
1011
using System.Runtime.InteropServices;
12+
using System.Threading.Tasks;
1113

1214
namespace NETworkManager.Models.Network
1315
{
14-
public static class ARPTable
16+
public class ARPTable
1517
{
18+
#region Variables
1619
// The max number of physical addresses.
1720
const int MAXLEN_PHYSADDR = 8;
1821

@@ -52,12 +55,23 @@ struct MIB_IPNETROW
5255
static extern int GetIpNetTable(IntPtr pIpNetTable, [MarshalAs(UnmanagedType.U4)] ref int pdwSize, bool bOrder);
5356

5457
[DllImport("IpHlpApi.dll", SetLastError = true, CharSet = CharSet.Auto)]
55-
internal static extern int FreeMibTable(IntPtr plpNetTable);
58+
internal static extern int FreeMibTable(IntPtr plpNetTable);
5659

5760
// The insufficient buffer error.
5861
const int ERROR_INSUFFICIENT_BUFFER = 122;
62+
#endregion
5963

60-
public static List<ARPTableInfo> GetARPTable()
64+
#region Events
65+
public event EventHandler UserHasCanceled;
66+
67+
protected virtual void OnUserHasCanceled()
68+
{
69+
UserHasCanceled?.Invoke(this, EventArgs.Empty);
70+
}
71+
#endregion
72+
73+
#region Methods
74+
public static List<ARPTableInfo> GetTable()
6175
{
6276
List<ARPTableInfo> list = new List<ARPTableInfo>();
6377

@@ -137,5 +151,48 @@ public static List<ARPTableInfo> GetARPTable()
137151
FreeMibTable(buffer);
138152
}
139153
}
154+
155+
public Task DeleteTableAsync()
156+
{
157+
return Task.Run(() => DeleteTable());
158+
}
159+
160+
public void DeleteTable()
161+
{
162+
string command = string.Format("netsh interface ip delete arpcache");
163+
164+
// Start process with elevated rights...
165+
ProcessStartInfo processStartInfo = new ProcessStartInfo()
166+
{
167+
Verb = "runas",
168+
FileName = "powershell.exe",
169+
Arguments = string.Format("-NoProfile -NoLogo -Command {0}", command)
170+
};
171+
172+
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
173+
174+
using (Process process = new Process())
175+
{
176+
process.StartInfo = processStartInfo;
177+
178+
try
179+
{
180+
process.Start();
181+
process.WaitForExit();
182+
}
183+
catch (Win32Exception win32ex)
184+
{
185+
switch (win32ex.NativeErrorCode)
186+
{
187+
case 1223:
188+
OnUserHasCanceled();
189+
break;
190+
default:
191+
throw;
192+
}
193+
}
194+
}
195+
}
196+
#endregion
140197
}
141198
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void ScanAsync(IPAddress[] ipAddresses, IPScannerOptions ipScannerOptions
102102

103103
if (ipScannerOptions.ResolveMACAddress)
104104
{
105-
macAddress = ARPTable.GetARPTable().Where(p => p.IPAddress.ToString() == ipAddress.ToString()).FirstOrDefault().MACAddress;
105+
macAddress = ARPTable.GetTable().Where(p => p.IPAddress.ToString() == ipAddress.ToString()).FirstOrDefault().MACAddress;
106106

107107
if (macAddress == null)
108108
macAddress = NetworkInterface.GetNetworkInterfaces().Where(p => p.IPv4Address.Contains(ipAddress)).FirstOrDefault().PhysicalAddress;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Net;
77
using System.Net.NetworkInformation;
88
using System.Threading.Tasks;
9-
using System.Windows;
109

1110
namespace NETworkManager.Models.Network
1211
{

‎Source/NETworkManager/Resources/Localization/Resources.de-DE.xaml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
<system:String x:Key="String_Header_LocalDevicesAndResources">Lokale Geräte und Ressourcen</system:String>
7979
<system:String x:Key="String_Header_Display">Anzeige</system:String>
8080
<system:String x:Key="String_Header_Filter">Filter</system:String>
81-
<system:String x:Key="String_Header_ARPTable">ARP-Table</system:String>
81+
<system:String x:Key="String_Header_ARPTable">ARP-Tabelle</system:String>
8282

8383
<!-- Normal strings -->
8484
<system:String x:Key="String_ProductName">NETworkManager</system:String>
@@ -316,6 +316,7 @@
316316
<system:String x:Key="String_Button_Connect">Verbinden</system:String>
317317
<system:String x:Key="String_Button_AddSessions">Sitzung hinzufügen...</system:String>
318318
<system:String x:Key="String_Button_Save">Speichern</system:String>
319+
<system:String x:Key="String_Button_Refresh">Aktualisieren</system:String>
319320

320321
<!-- Help -->
321322
<system:String x:Key="String_Help_ParameterHelp">Zeigt diesen Dialog an.</system:String>
@@ -391,7 +392,7 @@
391392
<system:String x:Key="String_ApplicationName_Traceroute">Traceroute</system:String>
392393
<system:String x:Key="String_ApplicationName_DNSLookup">DNS Lookup</system:String>
393394
<system:String x:Key="String_ApplicationName_RemoteDesktop">Remote Desktop</system:String>
394-
<system:String x:Key="String_ApplicationName_ARPTable">ARP-Table</system:String>
395+
<system:String x:Key="String_ApplicationName_ARPTable">ARP-Tabelle</system:String>
395396
<system:String x:Key="String_ApplicationName_Wiki">Wiki</system:String>
396397

397398
<!-- SettingsView.Name -->

‎Source/NETworkManager/Resources/Localization/Resources.en-US.xaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@
316316
<system:String x:Key="String_Button_Connect">Connect</system:String>
317317
<system:String x:Key="String_Button_AddSessions">Add session...</system:String>
318318
<system:String x:Key="String_Button_Save">Save</system:String>
319+
<system:String x:Key="String_Button_Refresh">Refresh</system:String>
319320

320321
<!-- Help -->
321322
<system:String x:Key="String_Help_ParameterHelp">Displays this dialog.</system:String>

‎Source/NETworkManager/ViewModels/Applications/ARPTableViewModel.cs‎

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
using NETworkManager.Models.Network;
2-
using NETworkManager.Models.Settings;
32
using NETworkManager.Helpers;
43
using System;
5-
using System.Collections.Generic;
64
using System.Windows;
75
using System.Windows.Input;
8-
using NETworkManager.Collections;
9-
using System.Net.NetworkInformation;
10-
using System.Windows.Threading;
11-
using System.Diagnostics;
12-
using System.Linq;
136
using System.ComponentModel;
147
using System.Windows.Data;
158
using System.Collections.ObjectModel;
@@ -19,7 +12,7 @@ namespace NETworkManager.ViewModels.Applications
1912
public class ARPTableViewModel : ViewModelBase
2013
{
2114
#region Variables
22-
private bool _isLoading = true;
15+
//private bool _isLoading = true;
2316

2417
private string _filter;
2518
public string Filter
@@ -71,6 +64,34 @@ public ARPTableInfo SelectedARPTableInfo
7164
OnPropertyChanged();
7265
}
7366
}
67+
68+
private bool _displayStatusMessage;
69+
public bool DisplayStatusMessage
70+
{
71+
get { return _displayStatusMessage; }
72+
set
73+
{
74+
if (value == _displayStatusMessage)
75+
return;
76+
77+
_displayStatusMessage = value;
78+
OnPropertyChanged();
79+
}
80+
}
81+
82+
private string _statusMessage;
83+
public string StatusMessage
84+
{
85+
get { return _statusMessage; }
86+
set
87+
{
88+
if (value == _statusMessage)
89+
return;
90+
91+
_statusMessage = value;
92+
OnPropertyChanged();
93+
}
94+
}
7495
#endregion
7596

7697
#region Contructor, load settings
@@ -91,11 +112,11 @@ public ARPTableViewModel()
91112
return info.IPAddress.ToString().IndexOf(filter, StringComparison.OrdinalIgnoreCase) > -1 || info.MACAddress.ToString().IndexOf(filter, StringComparison.OrdinalIgnoreCase) > -1;
92113
};
93114

94-
RefreshARPTableAction();
115+
Refresh();
95116

96117
LoadSettings();
97118

98-
_isLoading = false;
119+
//_isLoading = false;
99120
}
100121

101122
private void LoadSettings()
@@ -105,16 +126,42 @@ private void LoadSettings()
105126
#endregion
106127

107128
#region ICommands & Actions
108-
public ICommand RefreshARPTableCommand
129+
public ICommand RefreshCommand
109130
{
110-
get { return new RelayCommand(p => RefreshARPTableAction()); }
131+
get { return new RelayCommand(p => RefreshAction()); }
111132
}
112133

113-
private void RefreshARPTableAction()
134+
private void RefreshAction()
114135
{
115-
ARPTable.Clear();
136+
DisplayStatusMessage = false;
116137

117-
Models.Network.ARPTable.GetARPTable().ForEach(x => ARPTable.Add(x));
138+
Refresh();
139+
}
140+
141+
public ICommand DeleteCommand
142+
{
143+
get { return new RelayCommand(p => DeleteAction()); }
144+
}
145+
146+
private async void DeleteAction()
147+
{
148+
DisplayStatusMessage = false;
149+
150+
try
151+
{
152+
ARPTable arpTable = new ARPTable();
153+
154+
arpTable.UserHasCanceled += ArpTable_UserHasCanceled;
155+
156+
await arpTable.DeleteTableAsync();
157+
158+
Refresh();
159+
}
160+
catch (Exception ex)
161+
{
162+
StatusMessage = ex.Message;
163+
DisplayStatusMessage = true;
164+
}
118165
}
119166

120167
public ICommand CopySelectedIPAddressCommand
@@ -137,5 +184,22 @@ private void CopySelectedMACAddressAction()
137184
Clipboard.SetText(MACAddressHelper.GetDefaultFormat(SelectedARPTableInfo.MACAddress.ToString()));
138185
}
139186
#endregion
187+
188+
#region Methods
189+
private void Refresh()
190+
{
191+
ARPTable.Clear();
192+
193+
Models.Network.ARPTable.GetTable().ForEach(x => ARPTable.Add(x));
194+
}
195+
#endregion
196+
197+
#region Events
198+
private void ArpTable_UserHasCanceled(object sender, EventArgs e)
199+
{
200+
StatusMessage = Application.Current.Resources["String_CanceledByUser"] as string;
201+
DisplayStatusMessage = true;
202+
}
203+
#endregion
140204
}
141205
}

‎Source/NETworkManager/ViewModels/Applications/NetworkInterfaceViewModel.cs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,7 @@ public async void ApplyNetworkInterfaceConfigAction()
798798
await progressDialogController.CloseAsync();
799799
}
800800
}
801-
802-
private void NetworkInterface_UserHasCanceled(object sender, EventArgs e)
803-
{
804-
StatusMessage = Application.Current.Resources["String_CanceledByUser"] as string;
805-
DisplayStatusMessage = true;
806-
}
807-
801+
808802
public ICommand AddProfileCommand
809803
{
810804
get { return new RelayCommand(p => AddProfileAction()); }
@@ -862,5 +856,13 @@ private async void DeleteProfileAction()
862856
NetworkInterfaceProfileManager.RemoveProfile(SelectedProfile);
863857
}
864858
#endregion
859+
860+
#region Events
861+
private void NetworkInterface_UserHasCanceled(object sender, EventArgs e)
862+
{
863+
StatusMessage = Application.Current.Resources["String_CanceledByUser"] as string;
864+
DisplayStatusMessage = true;
865+
}
866+
#endregion
865867
}
866868
}

‎Source/NETworkManager/Views/Applications/ARPTableView.xaml‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
xmlns:Validator="clr-namespace:NETworkManager.Validators"
76
xmlns:Converter="clr-namespace:NETworkManager.Converters"
87
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
9-
xmlns:Control="clr-namespace:NETworkManager.Controls"
108
xmlns:IconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
11-
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
129
mc:Ignorable="d">
1310
<UserControl.Resources>
11+
<Converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
1412
<Converter:PhysicalAddressToStringConverter x:Key="PhysicalAddressToStringConverter" />
1513
</UserControl.Resources>
1614
<Grid Margin="10">
@@ -22,9 +20,12 @@
2220
<RowDefinition Height="Auto"/>
2321
</Grid.RowDefinitions>
2422
<TextBlock Grid.Row="0" Style="{StaticResource HeaderTextBlock}" Text="{DynamicResource String_Header_Filter}" />
25-
<TextBox Grid.Row="1" x:Name="txtFilter" Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_Filter}" Margin="0,0,0,20" />
23+
<StackPanel Grid.Row="1" Margin="0,0,0,20">
24+
<TextBox x:Name="txtFilter" Text="{Binding Filter, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" mah:TextBoxHelper.Watermark="{DynamicResource String_Watermark_Filter}" />
25+
<TextBlock Foreground="{DynamicResource AccentColorBrush}" Text="{Binding StatusMessage}" Visibility="{Binding DisplayStatusMessage, Converter={StaticResource BooleanToVisibilityConverter}}" Style="{DynamicResource DefaultTextBlock}" Margin="0,10,0,0" />
26+
</StackPanel>
2627
<TextBlock Grid.Row="2" Style="{StaticResource HeaderTextBlock}" Text="{DynamicResource String_Header_ARPTable}" />
27-
<DataGrid Grid.Row="3" Style="{StaticResource MetroDataGrid}" FontSize="14" mah:ControlsHelper.ContentCharacterCasing="Normal" ItemsSource="{Binding ARPTableView}" SelectedItem="{Binding SelectedARPTableInfo}" CanUserResizeColumns="False" VerticalScrollBarVisibility="Auto" AutoGenerateColumns="false" IsReadOnly="True">
28+
<DataGrid Grid.Row="3" Style="{StaticResource MetroDataGrid}" FontSize="14" mah:ControlsHelper.ContentCharacterCasing="Normal" ItemsSource="{Binding ARPTableView}" SelectedItem="{Binding SelectedARPTableInfo}" CanUserResizeColumns="False" VerticalScrollBarVisibility="Auto" AutoGenerateColumns="false" IsReadOnly="True" Margin="0,0,0,10">
2829
<DataGrid.Resources>
2930
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource DataGridScrollBar}" />
3031
<ContextMenu x:Key="RowContextMenu" Opened="ContextMenu_Opened" MinWidth="150">
@@ -71,5 +72,39 @@
7172
<DataGridTextColumn Header="{DynamicResource String_MACAddress}" Binding="{Binding MACAddress, Converter={StaticResource PhysicalAddressToStringConverter}}" MinWidth="200" />
7273
</DataGrid.Columns>
7374
</DataGrid>
75+
<StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right">
76+
<Button Command="{Binding DeleteCommand}" IsDefault="True" Style="{StaticResource ImageWithTextButton}">
77+
<Button.Content>
78+
<Grid>
79+
<Grid.ColumnDefinitions>
80+
<ColumnDefinition Width="Auto" />
81+
<ColumnDefinition Width="*" />
82+
</Grid.ColumnDefinitions>
83+
<Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
84+
<Rectangle.OpacityMask>
85+
<VisualBrush Stretch="Uniform" Visual="{IconPacks:PackIconMaterial Kind=Close}" />
86+
</Rectangle.OpacityMask>
87+
</Rectangle>
88+
<TextBlock Grid.Column="1" Text="{DynamicResource String_Button_Delete}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
89+
</Grid>
90+
</Button.Content>
91+
</Button>
92+
<Button Command="{Binding RefreshCommand}" IsDefault="True" Style="{StaticResource ImageWithTextButton}" Margin="10,0,0,0">
93+
<Button.Content>
94+
<Grid>
95+
<Grid.ColumnDefinitions>
96+
<ColumnDefinition Width="Auto" />
97+
<ColumnDefinition Width="*" />
98+
</Grid.ColumnDefinitions>
99+
<Rectangle Width="20" Height="20" Fill="{DynamicResource GrayBrush3}" Margin="10,5,0,5">
100+
<Rectangle.OpacityMask>
101+
<VisualBrush Stretch="Uniform" Visual="{IconPacks:PackIconMaterial Kind=Refresh}" />
102+
</Rectangle.OpacityMask>
103+
</Rectangle>
104+
<TextBlock Grid.Column="1" Text="{DynamicResource String_Button_Refresh}" Style="{StaticResource DefaultTextBlock}" TextAlignment="Center" Margin="10,5" />
105+
</Grid>
106+
</Button.Content>
107+
</Button>
108+
</StackPanel>
74109
</Grid>
75110
</UserControl>

0 commit comments

Comments
 (0)