Skip to content

Commit 7604aed

Browse files
committed
SubnetCalculator - Working on IPv4-Splitter
1 parent b05aa84 commit 7604aed

11 files changed

Lines changed: 360 additions & 9 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using NETworkManager.Helpers;
2+
using System;
3+
using System.Globalization;
4+
using System.Net;
5+
using System.Windows.Data;
6+
7+
namespace NETworkManager.Converters
8+
{
9+
public sealed class ValidateSubnetCalculatorIPv4SplitterConverter : IMultiValueConverter
10+
{
11+
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
12+
{
13+
// if Validation.HasError is true...
14+
if ((bool)values[0] || (bool)values[1])
15+
return false;
16+
17+
string subnet = values[2] as string;
18+
string newSubnetmaskOrCIDR = values[3] as string;
19+
20+
// Catch null exceptions...
21+
if (string.IsNullOrEmpty(subnet) || string.IsNullOrEmpty(newSubnetmaskOrCIDR))
22+
return false;
23+
24+
// Get the cidr to compare...
25+
string subnetmaskOrCIDR = subnet.Split('/')[1];
26+
int cidr;
27+
28+
if (subnetmaskOrCIDR.Length < 3)
29+
cidr = int.Parse(subnetmaskOrCIDR);
30+
else
31+
cidr = SubnetmaskHelper.ConvertSubnetmaskToCidr(IPAddress.Parse(subnetmaskOrCIDR));
32+
33+
newSubnetmaskOrCIDR = newSubnetmaskOrCIDR.TrimStart('/');
34+
int newCidr;
35+
36+
if (newSubnetmaskOrCIDR.Length < 3)
37+
newCidr = int.Parse(newSubnetmaskOrCIDR);
38+
else
39+
newCidr = SubnetmaskHelper.ConvertSubnetmaskToCidr(IPAddress.Parse(newSubnetmaskOrCIDR));
40+
41+
// Compare
42+
return newCidr > cidr;
43+
}
44+
45+
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
46+
{
47+
throw new NotImplementedException();
48+
}
49+
}
50+
}

Source/NETworkManager/Models/Network/Subnet.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using NETworkManager.Helpers;
2+
using System.Collections.Generic;
23
using System.Net;
34

45
namespace NETworkManager.Models.Network
@@ -23,6 +24,15 @@ public static SubnetInfo CalculateIPv4Subnet(IPAddress ipv4Address, IPAddress su
2324
HostLastIP = IPv4AddressHelper.DecrementIPv4Address(broadcast, 1),
2425
HostIPs = totalIPs - 2
2526
};
26-
}
27+
}
28+
29+
public static List<SubnetInfo> SplitIPv4Subnet(IPAddress ipv45Address, IPAddress subnetmask, IPAddress newSubnetmask)
30+
{
31+
List<SubnetInfo> list = new List<SubnetInfo>();
32+
33+
34+
35+
return list;
36+
}
2737
}
2838
}

Source/NETworkManager/Models/Settings/SettingsInfo.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,34 @@ public List<string> SubnetCalculator_IPv4Calculator_SubnetHistory
10181018
SettingsChanged = true;
10191019
}
10201020
}
1021+
1022+
private List<string> _subnetCalculator_IPv4Splitter_SubnetHistory = new List<string>();
1023+
public List<string> SubnetCalculator_IPv4Splitter_SubnetHistory
1024+
{
1025+
get { return _subnetCalculator_IPv4Splitter_SubnetHistory; }
1026+
set
1027+
{
1028+
if (value == _subnetCalculator_IPv4Splitter_SubnetHistory)
1029+
return;
1030+
1031+
_subnetCalculator_IPv4Splitter_SubnetHistory = value;
1032+
SettingsChanged = true;
1033+
}
1034+
}
1035+
1036+
private List<string> _subnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory = new List<string>();
1037+
public List<string> SubnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory
1038+
{
1039+
get { return _subnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory; }
1040+
set
1041+
{
1042+
if (value == _subnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory)
1043+
return;
1044+
1045+
_subnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory = value;
1046+
SettingsChanged = true;
1047+
}
1048+
}
10211049
#endregion
10221050
#endregion
10231051

Source/NETworkManager/NETworkManager.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
<Compile Include="3rdParty\Heijden.DNS\Structs.cs" />
168168
<Compile Include="Converters\TimestampToStringConverter.cs" />
169169
<Compile Include="Converters\NullableDateTimeToStringConverter.cs" />
170+
<Compile Include="Converters\ValidateSubnetCalculatorIPv4SplitterConverter.cs" />
170171
<Compile Include="Helpers\SingleInstanceHelper.cs" />
171172
<Compile Include="Models\Network\DNSLookup.cs" />
172173
<Compile Include="Models\Network\DNSLookupCompleteArgs.cs" />
@@ -180,10 +181,15 @@
180181
<Compile Include="Validators\SubnetValidator.cs" />
181182
<Compile Include="Validators\OpacityTextboxValidator.cs" />
182183
<Compile Include="ViewModels\Applications\DNSLookupViewModel.cs" />
184+
<Compile Include="ViewModels\Applications\ObserverableCollection.cs" />
185+
<Compile Include="ViewModels\Applications\SubnetCalculatorIPv4SplitterViewModel.cs" />
183186
<Compile Include="ViewModels\Applications\WikiOUILookupViewModel.cs" />
184187
<Compile Include="ViewModels\Help\HelpCommandLineViewModel.cs" />
185188
<Compile Include="ViewModels\Settings\SettingsApplicationDNSLookupViewModel.cs" />
186189
<Compile Include="ViewModels\Settings\SettingsApplicationWakeOnLANViewModel.cs" />
190+
<Compile Include="Views\Applications\SubnetCalculatorIPv4SplitterView.xaml.cs">
191+
<DependentUpon>SubnetCalculatorIPv4SplitterView.xaml</DependentUpon>
192+
</Compile>
187193
<Compile Include="Views\Applications\SubnetCalculatorView.xaml.cs">
188194
<DependentUpon>SubnetCalculatorView.xaml</DependentUpon>
189195
</Compile>
@@ -246,6 +252,10 @@
246252
<Generator>MSBuild:Compile</Generator>
247253
<SubType>Designer</SubType>
248254
</Page>
255+
<Page Include="Views\Applications\SubnetCalculatorIPv4SplitterView.xaml">
256+
<Generator>MSBuild:Compile</Generator>
257+
<SubType>Designer</SubType>
258+
</Page>
249259
<Page Include="Views\Applications\SubnetCalculatorView.xaml">
250260
<Generator>MSBuild:Compile</Generator>
251261
<SubType>Designer</SubType>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<system:String x:Key="String_Subnetmask">Subnetzmaske</system:String>
9797
<system:String x:Key="String_CIDR">CIDR</system:String>
9898
<system:String x:Key="String_SubnetmaskOrCIDR">Subnetmaske oder CIDR</system:String>
99+
<system:String x:Key="String_NewSubnetmaskOrCIDR">Neue Subnetmaske oder CIDR</system:String>
99100
<system:String x:Key="String_IPv4DefaultGateway">IPv4-Standardgateway</system:String>
100101
<system:String x:Key="String_DHCPEnabled">DHCP aktiviert</system:String>
101102
<system:String x:Key="String_DHCPServer">DHCP-Server</system:String>
@@ -235,6 +236,8 @@
235236
<system:String x:Key="String_Additionals">Additionals</system:String>
236237
<system:String x:Key="String_MessageSize">Nachrichtengröße</system:String>
237238
<system:String x:Key="String_Destination">Ziel</system:String>
239+
<system:String x:Key="String_EnterASubnetToCalculateIt">Geben Sie ein Subnetz ein, um es zu berechnen ...</system:String>
240+
<system:String x:Key="String_Subnet">Subnetz</system:String>
238241

239242
<!-- Buttons -->
240243
<system:String x:Key="String_Button_Change">Wechseln</system:String>
@@ -293,11 +296,11 @@
293296
<system:String x:Key="String_Watermark_ExampleMACAddressesOrVendor">01:23:45:67:89:AB; 01-23-45; AA11BB; 00F1A2C3D4E5; Intel Corp; Asus</system:String>
294297
<system:String x:Key="String_Watermark_EamplePortScanRange">22; 80; 443; 500 - 999; 8080</system:String>
295298
<system:String x:Key="String_Watermark_ExampleHostnameAndOrIPAddress">192.168.178.1; fritz.box; localhost</system:String>
296-
<system:String x:Key="String_Watermark_ExampleSubnetmaskOrCIDR">255.255.255.0 or /24</system:String>
299+
<system:String x:Key="String_Watermark_ExampleSubnetmaskOrCIDR">/24 oder 255.255.255.0</system:String>
297300
<system:String x:Key="String_Watermark_ExampleIPv4Gateway">192.168.178.1</system:String>
298301
<system:String x:Key="String_Watermark_ExampleIPv4DNSServer">8.8.8.8</system:String>
299302
<system:String x:Key="String_Watermark_EamplePortPortRangeOrService">22; 80; https; ldaps; 777 - 999; 8080</system:String>
300-
<system:String x:Key="String_Watermark_ExampleSubnet">192.168.178.133/26</system:String>
303+
<system:String x:Key="String_Watermark_ExampleSubnet">192.168.178.133/22 oder 192.168.178.133/255.255.252.0</system:String>
301304

302305
<!-- ShowProgress -->
303306
<system:String x:Key="String_ProgessHeader_ConfigureNetworkInterface">Konfiguriere Netzwerkinterface</system:String>

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
<system:String x:Key="String_IPv4Address">IPv4-Address</system:String>
9696
<system:String x:Key="String_Subnetmask">Subnetmask</system:String>
9797
<system:String x:Key="String_CIDR">CIDR</system:String>
98-
<system:String x:Key="String_SubnetmaskOrCIDR">Subnetmask oder CIDR</system:String>
98+
<system:String x:Key="String_SubnetmaskOrCIDR">Subnetmask or CIDR</system:String>
99+
<system:String x:Key="String_NewSubnetmaskOrCIDR">New subnetmask or CIDR</system:String>
99100
<system:String x:Key="String_IPv4DefaultGateway">IPv4-Default-Gateway</system:String>
100101
<system:String x:Key="String_DHCPEnabled">DHCP enabled</system:String>
101102
<system:String x:Key="String_DHCPServer">DHCP server</system:String>
@@ -235,7 +236,9 @@
235236
<system:String x:Key="String_Additionals">Additionals</system:String>
236237
<system:String x:Key="String_MessageSize">Message size</system:String>
237238
<system:String x:Key="String_Destination">Destination</system:String>
238-
239+
<system:String x:Key="String_EnterASubnetToCalculateIt">Enter a subnet to calculate it...</system:String>
240+
<system:String x:Key="String_Subnet">Subnet</system:String>
241+
239242
<!-- Buttons -->
240243
<system:String x:Key="String_Button_Change">Change</system:String>
241244
<system:String x:Key="String_Button_Default">Default</system:String>
@@ -293,11 +296,11 @@
293296
<system:String x:Key="String_Watermark_ExampleMACAddressesOrVendor">01:23:45:67:89:AB; 01-23-45; AA11BB; 00F1A2C3D4E5; Intel Corp; Asus</system:String>
294297
<system:String x:Key="String_Watermark_EamplePortScanRange">22; 80; 443; 500 - 999; 8080</system:String>
295298
<system:String x:Key="String_Watermark_ExampleHostnameAndOrIPAddress">192.168.178.1; fritz.box</system:String>
296-
<system:String x:Key="String_Watermark_ExampleSubnetmaskOrCIDR">255.255.255.0 or /24</system:String>
299+
<system:String x:Key="String_Watermark_ExampleSubnetmaskOrCIDR">/24 or 255.255.255.0</system:String>
297300
<system:String x:Key="String_Watermark_ExampleIPv4Gateway">192.168.178.1</system:String>
298301
<system:String x:Key="String_Watermark_ExampleIPv4DNSServer">8.8.8.8</system:String>
299302
<system:String x:Key="String_Watermark_EamplePortPortRangeOrService">22; 80; https; ldaps; 777 - 999; 8080</system:String>
300-
<system:String x:Key="String_Watermark_ExampleSubnet">192.168.178.133/26</system:String>
303+
<system:String x:Key="String_Watermark_ExampleSubnet">192.168.178.133/22 or 192.168.178.133/255.255.252.0</system:String>
301304

302305
<!-- ShowProgress -->
303306
<system:String x:Key="String_ProgessHeader_ConfigureNetworkInterface">Configure Network Interface</system:String>
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
using NETworkManager.Models.Network;
2+
using NETworkManager.Models.Settings;
3+
using System.Collections.Generic;
4+
using System.Net;
5+
using System.Windows.Input;
6+
using NETworkManager.Helpers;
7+
using System.Windows;
8+
using System.Collections.ObjectModel;
9+
10+
namespace NETworkManager.ViewModels.Applications
11+
{
12+
public class SubnetCalculatorIPv4SplitterViewModel : ViewModelBase
13+
{
14+
#region Variables
15+
private bool _isLoading = true;
16+
17+
private string _subnet;
18+
public string Subnet
19+
{
20+
get { return _subnet; }
21+
set
22+
{
23+
if (value == _subnet)
24+
return;
25+
26+
_subnet = value;
27+
OnPropertyChanged();
28+
}
29+
}
30+
31+
private List<string> _subnetHistory = new List<string>();
32+
public List<string> SubnetHistory
33+
{
34+
get { return _subnetHistory; }
35+
set
36+
{
37+
if (value == _subnetHistory)
38+
return;
39+
40+
if (!_isLoading)
41+
SettingsManager.Current.SubnetCalculator_IPv4Splitter_SubnetHistory = value;
42+
43+
_subnetHistory = value;
44+
OnPropertyChanged();
45+
}
46+
}
47+
48+
private string _newSubnetmaskOrCIDR;
49+
public string NewSubnetmaskOrCIDR
50+
{
51+
get { return _newSubnetmaskOrCIDR; }
52+
set
53+
{
54+
if (value == _newSubnetmaskOrCIDR)
55+
return;
56+
57+
_newSubnetmaskOrCIDR = value;
58+
OnPropertyChanged();
59+
}
60+
}
61+
62+
private List<string> _newSubnetmaskOrCIDRHistory = new List<string>();
63+
public List<string> NewSubnetmaskOrCIDRHistory
64+
{
65+
get { return _newSubnetmaskOrCIDRHistory; }
66+
set
67+
{
68+
if (value == _newSubnetmaskOrCIDRHistory)
69+
return;
70+
71+
if (!_isLoading)
72+
SettingsManager.Current.SubnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory = value;
73+
74+
_newSubnetmaskOrCIDRHistory = value;
75+
OnPropertyChanged();
76+
}
77+
}
78+
79+
private ObservableCollection<SubnetInfo> _splitResult = new ObservableCollection<SubnetInfo>();
80+
public ObservableCollection<SubnetInfo> SplitResult
81+
{
82+
get { return _splitResult; }
83+
set
84+
{
85+
if (value == _splitResult)
86+
return;
87+
88+
_splitResult = value;
89+
}
90+
}
91+
#endregion
92+
93+
#region Constructor, load settings
94+
public SubnetCalculatorIPv4SplitterViewModel()
95+
{
96+
LoadSettings();
97+
98+
_isLoading = false;
99+
}
100+
101+
private void LoadSettings()
102+
{
103+
if (SettingsManager.Current.SubnetCalculator_IPv4Splitter_SubnetHistory != null)
104+
SubnetHistory = new List<string>(SettingsManager.Current.SubnetCalculator_IPv4Splitter_SubnetHistory);
105+
106+
if (SettingsManager.Current.SubnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory != null)
107+
NewSubnetmaskOrCIDRHistory = new List<string>(SettingsManager.Current.SubnetCalculator_IPv4Splitter_NewSubnetmaskOrCIDRHistory);
108+
}
109+
#endregion
110+
111+
#region ICommands
112+
public ICommand SplitIPv4SubnetCommand
113+
{
114+
get { return new RelayCommand(p => SplitIPv4SubnetAction()); }
115+
}
116+
#endregion
117+
118+
#region Methods
119+
private void SplitIPv4SubnetAction()
120+
{
121+
string[] subnet = Subnet.Trim().Split('/');
122+
123+
string subnetmask = subnet[1];
124+
125+
// Convert CIDR to subnetmask
126+
if (subnetmask.Length < 3)
127+
subnetmask = Subnetmask.GetFromCidr(int.Parse(subnet[1])).Subnetmask;
128+
129+
SubnetInfo subnetInfo = Models.Network.Subnet.CalculateIPv4Subnet(IPAddress.Parse(subnet[0]), IPAddress.Parse(subnetmask));
130+
131+
SubnetHistory = new List<string>(HistoryListHelper.Modify(SubnetHistory, Subnet, SettingsManager.Current.Application_HistoryListEntries));
132+
NewSubnetmaskOrCIDRHistory = new List<string>(HistoryListHelper.Modify(NewSubnetmaskOrCIDRHistory, NewSubnetmaskOrCIDR, SettingsManager.Current.Application_HistoryListEntries));
133+
}
134+
#endregion
135+
}
136+
}

Source/NETworkManager/Views/Applications/SubnetCalculatorIPv4CalculatorView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@
114114
</Grid>
115115
</ScrollViewer>
116116
</StackPanel>
117-
<TextBlock Grid.Row="2" FontSize="18" Foreground="{DynamicResource GrayBrush3}" Text="Calculate your subnet..." Visibility="{Binding IsDetailsVisible, Converter={StaticResource BooleanReverseToVisibilityConverter}}" Style="{StaticResource HeaderTextBlock}" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" TextAlignment="Center" Margin="10,0" />
117+
<TextBlock Grid.Row="2" FontSize="18" Foreground="{DynamicResource GrayBrush3}" Text="{DynamicResource String_EnterASubnetToCalculateIt}" Visibility="{Binding IsDetailsVisible, Converter={StaticResource BooleanReverseToVisibilityConverter}}" Style="{StaticResource HeaderTextBlock}" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" TextAlignment="Center" Margin="10,0" />
118118
</Grid>
119119
</UserControl>

0 commit comments

Comments
 (0)