/
Subnetting.cs
256 lines (220 loc) · 8.79 KB
/
Subnetting.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
using System;
using System.Linq;
namespace ComputerScienceCalculator
{
public class Subnetting : IPAddress
{
public Subnetting(ref string[] IP, ref string[] mask, int subnets) : base(ref IP, ref mask, true)
{
_IP = "";
_mask = "";
for (int a = 0; a < IPConversions.octet; a++)
{
_IP += IP[a];
_mask += mask[a];
if (a < IPConversions.octet - 1)
{
_IP += ".";
_mask += ".";
}
}
_subnets = subnets;
LogicalAndcalculate();
}
private IPConversions converter;
private string _IP;
private string _mask;
private int _subnets;
private int _startingLocation;
private int _byteSpace;
private const int IPAddressSize = 35;
private bool _validated = false;
private string[,] subnetArray;
private string[] DecimalID;
private string[] BroadcastID;
private string[] HostRange;
private string[] StolenBits;
private string[] TotalHosts;
public string[,] makeSubnets()
{
if (_validated)
{
subnetArray = new string[_subnets, 3];
DecimalID = new string[_subnets];
BroadcastID = new string[_subnets];
HostRange = new string[_subnets];
StolenBits = new string[_subnets];
TotalHosts = new string[_subnets];
for (int i = 0; i < _subnets; i++)
{
string subnetID = Decimal.Convert_To_Binary(_byteSpace, i);
int count = 0;
subnetArray[i, 0] = "";
for (int j = 0; j < IPAddressSize; j++)
{
if (subnetAddress[j] == '.')
{
subnetArray[i, 0] += '.';
}
else if (j >= _startingLocation + 1 && count < subnetID.Length)
{
subnetArray[i, 0] += subnetID[count];
count++;
}
else
{
subnetArray[i, 0] += subnetAddress[j];
}
}
string[] temp = getFormattedString(subnetArray[i, 0], getDecimalSubnet(i), getBroadcast(i), getHostRange(i), i, _subnets);
subnetArray[i, 0] = temp[0];
subnetArray[i, 1] = temp[1];
subnetArray[i, 2] = temp[2];
}
return subnetArray;
}
return null;
}
private string[] getFormattedString(string SubnetID, string DecimalSubnet, string BroadCastAddress, string HostIdRange, int index, int length)
{
string[] output = new string[3];
string fmt = index.ToString();
for (int a = 0; fmt.Length < length.ToString().Length; a++)
fmt = ' ' + fmt;
output[0] = fmt + ": " + SubnetID + " (" + DecimalSubnet + ")";
output[1] = "\t\t" + "Broadcast ID: " + BroadCastAddress;
output[2] = "\t\t" + "Host Range: " + HostIdRange;
return output;
}
private string getDecimalSubnet(int index)
{
string[] subnetAddressArray = subnetArray[index, 0].Split('.', ' ', ':');
converter = new IPConversions(ref subnetAddressArray, IPType.Binary);
converter.ConvertAddress();
DecimalID[index] = converter.GetDecimalIp();
return converter.GetDecimalIp();
}
private string getBroadcast(int index)
{
int bitChangeCount = _startingLocation;
char[] tmp = subnetArray[index, 0].ToCharArray();
int counter = 0;
for (int i = bitChangeCount + 1; i < IPAddressSize; i++)
{
if (counter >= _byteSpace)
{
if (tmp[i] != '.')
tmp[i] = '1';
}
if (tmp[i] != '.')
counter++;
}
string[] binaryAddressArray = (new string(tmp)).Split('.', ':', ' ');
converter = new IPConversions(ref binaryAddressArray, IPType.Binary);
converter.ConvertAddress();
BroadcastID[index] = converter.GetDecimalIp();
return converter.GetDecimalIp();
}
private string getHostRange(int index)
{
char[] subnet = subnetArray[index, 0].ToCharArray();
subnet[IPAddressSize - 1] = '1';
string[] decimalAddressArray = BroadcastID[index].Split('.', ':', ' ');
converter = new IPConversions(ref decimalAddressArray, IPType.Decimal);
converter.ConvertAddress();
char[] subnet2 = converter.GetBinaryIp().ToCharArray();
subnet2[IPAddressSize - 1] = '0';
string[] binaryAddressArray = (new string(subnet)).Split('.', ' ', ':');
converter = new IPConversions(ref binaryAddressArray, IPType.Binary);
converter.ConvertAddress();
string lowRange = converter.GetDecimalIp();
string[] binaryAddressArray2 = (new string(subnet2)).Split('.', ':', ' ');
converter = new IPConversions(ref binaryAddressArray2, IPType.Binary);
converter.ConvertAddress();
string highRange = converter.GetDecimalIp();
HostRange[index] = lowRange + " - " + highRange;
return lowRange + " - " + highRange;
}
public string getBitsStolen()
{
return _byteSpace.ToString();
}
public string getTotalNumberOfHosts()
{
//Method 1 May be Flawed
/*
int starting = _startingLocation;
if (starting < 9)
starting++;
if (starting < 18)
starting++;
if (starting < 27)
starting++;
int numberOfHostBits = IPAddressSize - starting - _byteSpace - 1;
int numberOfHosts = (int)Math.Pow(2, numberOfHostBits) - 2;
return numberOfHosts.ToString();
*/
int numberOfHostBits = findHostSpace();
numberOfHostBits = (IPConversions.octet_size * IPConversions.octet) - numberOfHostBits;
int numberOfHosts = (int)Math.Pow(2, numberOfHostBits) - 2;
return numberOfHosts.ToString();
}
public string getIPAddressClass()
{
int First_Octet;
if (int.TryParse(FirstOctet, out First_Octet))
{
if (First_Octet > 0 && First_Octet < 127)
return "Class A";
else if (First_Octet >= 127 && First_Octet < 192)
return "Class B";
else if (First_Octet >= 192 && First_Octet < 224)
return "Class C";
else if (First_Octet >= 224 && First_Octet < 240)
return "Class D";
else if (First_Octet >= 240 && First_Octet < 255)
return "Class E";
}
return "Unknown Class";
}
private int findStartPosition()
{
int position = 0;
for (int i = 0; i < _subnetMaskAddress.Length; i++)
{
if (_subnetMaskAddress[i] == '1')
{
position = i;
if (_subnetMaskAddress[i + 1] == '.')
position++;
}
}
return position;
}
private int findHostSpace()
{
int position = 0;
int counter = 1;
for(int i =0; i < _subnetMaskAddress.Length; i++)
{
if (_subnetMaskAddress[i] == '1')
position = counter;
else if (_subnetMaskAddress[i] == '.')
counter--;
counter++;
}
return position + _byteSpace;
}
public bool validSubnetAmount()
{
int startPosition = findStartPosition();
_startingLocation = startPosition;
int byteNumber = (int)Math.Ceiling(Math.Log(_subnets, 2));
_byteSpace = byteNumber;
if (byteNumber + startPosition > IPAddressSize - 1 || _subnets == 1)
return false;
_validated = true;
return true;
}
}
}