-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVLSM_Calc.cs
404 lines (342 loc) · 15.5 KB
/
VLSM_Calc.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
using System;
using System.Collections.Generic;
namespace VLSM_Calculator
{
/* Author: Domninik Krzywański
* Website: wolfror.com
* -------------------------
* Autor: Dominik Krzywański
* Strona internetowa: wolfror.com
*/
class VLSM_Calc
{
static readonly string[] Masks = new string[] {"0.0.0.0", "128.0.0.0", "192.0.0.0", "224.0.0.0", "240.0.0.0", "248.0.0.0", "252.0.0.0", "254.0.0.0",
"255.0.0.0", "255.128.0.0", "255.192.0.0", "255.224.0.0", "255.240.0.0", "255.248.0.0", "255.252.0.0", "255.254.0.0",
"255.255.0.0","255.255.128.0", "255.255.192.0", "255.255.224.0", "255.255.240.0", "255.255.248.0", "255.255.252.0",
"255.255.254.0", "255.255.255.0", "255.255.255.128", "255.255.255.192", "255.255.255.224", "255.255.255.240",
"255.255.255.248", "255.255.255.252", "255.255.255.254", "255.255.255.255" }; //List of masks
static bool FirstVLSM;
static List<VlsmObject> ListOfNetworks = new List<VlsmObject>(); //Calculated VLSM
static void Main()
{
Console.WriteLine("Welcome in \"VLSM Calculator\""); //Greeting user
Console.WriteLine("Provide ip address: ");
string IPLine = Console.ReadLine(); //Get ip
Console.WriteLine("Provide mask: ");
string MaskLine = Console.ReadLine(); //Get mask
Console.WriteLine("Provide hosts: ");
string HostLine = Console.ReadLine(); //Get hosts to calculate
Console.WriteLine(); //Break line
int[] HostSorted = HostsSorting(HostLine); //Sorting hosts into an array
int Mask = MaskCheck(MaskLine); //Mask conversion
int Overflow = OverflowCheck(HostSorted, Mask); //Checking how many addresses will be without
string IP = IpCheck(IPLine); //Optional ip address repair
FirstVLSM = true;
for (int i = 0; i < Overflow; i++)
{
if (i == 0)
{
ListOfNetworks.Add(Calculate_VLSM(IP, Mask, HostSorted[i])); //First network to calculate
}
else
{
ListOfNetworks.Add(Calculate_VLSM(ListOfNetworks[i - 1].Broadcast, Mask, HostSorted[i])); //Next nets to calculate
}
}
foreach (VlsmObject item in ListOfNetworks) //Showing calculated data
{
Console.WriteLine("-------------------");
Console.WriteLine("Network: " + item.Network);
Console.WriteLine("Mask: " + item.Mask);
Console.WriteLine("First Host: " + item.FirstHost);
Console.WriteLine("Last Host: " + item.LastHost);
Console.WriteLine("Broadcast: " + item.Broadcast);
Console.WriteLine("Wanted host: " + item.WantedHost);
}
Console.WriteLine("\nClick enter to close app");
Console.ReadLine(); //Lock to prevent the application from closing without user interaction
}
static VlsmObject Calculate_VLSM(string IpAddress, int Mask, int Host)
{
string Network, FirstHost, LastHost, Broadcast; //Variables to return
int Foct, Soct, Toct, Fouroct, rest, VlsmMask, OverFlowTest; //Foct stands for first octet
string[] Testing = IpAddress.Split('.'); //Split ip address into octets
#region Overflow calculation
int tempMask = 0; //Sum of needed hosts
int HostsInTheTempMask = 2; //The number of hosts on the network needed to accommodate the previously entered number of hosts
while (tempMask < 31 - Mask)
{
HostsInTheTempMask *= 2;
tempMask++;
}
#endregion
#region Main calculation
if (Mask >= 8 && Mask <= 30)
{
int ManyHosts = 2; //Number of wanted hosts in network
int MaskAddFromHosts = 0; //Mask for choosen host
while (ManyHosts - 2 < Host) //Calculating how many hosts is needed to fit the previously entered number of hosts
{
ManyHosts *= 2;
MaskAddFromHosts++;
}
OverFlowTest = ManyHosts;
if (31 - MaskAddFromHosts >= Mask && HostsInTheTempMask >= OverFlowTest && Host > 0)
{
VlsmMask = (31 - MaskAddFromHosts);
ManyHosts -= 3;
Foct = int.Parse(Testing[0]);
Soct = int.Parse(Testing[1]);
Toct = int.Parse(Testing[2]);
Fouroct = int.Parse(Testing[3]);
if (Fouroct == 255) //First converting octets
{
Toct += 1;
Fouroct = 0;
if (Toct == 256)
{
Toct = 0;
Soct += 1;
}
}
else if (FirstVLSM == false)
{
Fouroct += 1; //Fourth octet increment for all subnets except the first
}
Network = (Foct.ToString() + "." + Soct.ToString() + "." + Toct.ToString() + "." + Fouroct.ToString()); //Creating Network variable from octets
if (Fouroct == 255) //Second converting octets
{
Toct += 1;
Fouroct = 0;
if (Toct == 256)
{
Toct = 0;
Soct += 1;
}
}
else
{
Fouroct += 1;
}
FirstHost = (Foct.ToString() + "." + Soct.ToString() + "." + Toct.ToString() + "." + Fouroct.ToString()); //Creating First Host variable from octets
ManyHosts += Fouroct;
if (ManyHosts >= 256) //Third converting octets
{
rest = ManyHosts / 256;
Toct += rest;
if (Toct >= 256)
{
Soct += Toct / 256;
Toct -= ((Toct / 256) * 256);
}
Fouroct = ManyHosts - (rest * 256);
}
else
{
Fouroct = ManyHosts;
}
LastHost = (Foct.ToString() + "." + Soct.ToString() + "." + Toct.ToString() + "." + Fouroct.ToString()); //Creating Lasy Host variable from octets
if (Fouroct == 255) //Fourth converting octets
{
Fouroct = 0;
Toct++;
if (Toct == 255)
{
Toct = 0;
Soct++;
}
}
else
{
Fouroct++;
}
Broadcast = (Foct.ToString() + "." + Soct.ToString() + "." + Toct.ToString() + "." + Fouroct.ToString()); //Creating Broadcast variable from octets
}
else //Returning overflow VlsmObject
{
Network = ("Overflow");
VlsmMask = (0);
FirstHost = ("Overflow");
LastHost = ("Overflow");
Broadcast = ("Overflow");
}
}
else //Returning error VlsmObject
{
Network = ("Error");
VlsmMask = (0);
FirstHost = ("Error");
LastHost = ("Error");
Broadcast = ("Error");
}
#endregion
VlsmObject VO = new VlsmObject(Network, VlsmMask, FirstHost, LastHost, Broadcast, Host);
FirstVLSM = false;
return VO;
}
static int[] HostsSorting(string HostsToSort)
{
string[] OriginalHostArray = HostsToSort.Split(',');
int[] SortedHostArray = new int[OriginalHostArray.Length]; //Creating array to return
for (int a = 0; a < OriginalHostArray.Length; a++)
{
SortedHostArray[a] = int.Parse(OriginalHostArray[a]); //Converting hosts from string to int
}
Array.Sort(SortedHostArray); //Sorting array - from the smallest host to the largest
Array.Reverse(SortedHostArray); //Reversing array - from the largest host to the smallest
return SortedHostArray;
}
static int MaskCheck(string MaskLine)
{
int IpMask; //Int to return
if (MaskLine.Contains(".")) //Checking if the mask looks like this: 255.255.255.0 or 11111111.11111111.11111111.00000000
{
string[] Testing = MaskLine.Split('.');
if (Testing[0].Length == 8)//Checking if the mask looks like this: 11111111.11111111.11111111.00000000
{
int Foct, Soct, Toct, Fouroct;
Foct = Convert.ToInt32(Testing[0], 2);
Soct = Convert.ToInt32(Testing[1], 2);
Toct = Convert.ToInt32(Testing[2], 2);
Fouroct = Convert.ToInt32(Testing[3], 2);
IpMask = Foct + Soct + Toct + Fouroct;
}
else
{
IpMask = Array.IndexOf(Masks, MaskLine); //Searching mask in masks array
}
}
else if (MaskLine.Contains("/")) //Checking if the mask looks like this: /24
{
IpMask = int.Parse(MaskLine.Replace("/", string.Empty)); //Deleting "/" from mask
}
else //Mask propably looks like this: 24
{
IpMask = int.Parse(MaskLine); //Converting from string to int
}
return IpMask;
}
static string IpCheck(string IpAddress)
{
string[] Testing = IpAddress.Split('.'); //Spliting IP address
string IpToReturn = IpAddress;
bool UserError = false;
for (int i = 0; i < Testing.Length; i++) //The loop fixes the ip address from errors such as too many octets or letters instead of numbers
{
try
{
if (!string.IsNullOrEmpty(Testing[i]))
{
int x = int.Parse(Testing[i]);
}
}
catch
{
Testing[i] = "0";
UserError = true;
}
}
if (UserError == true)
{
string[] ErrorArray = new string[4];
for (int i = 0; i < 4; i++)
{
if (i < Testing.Length)
{
ErrorArray[i] = Testing[i];
}
else
{
ErrorArray[i] = "0";
}
}
IpToReturn = ErrorArray[0] + "." + ErrorArray[1] + "." + ErrorArray[2] + "." + ErrorArray[3]; //Creating ip address from octets
Testing = IpToReturn.Split('.');
}
if (Testing[0].Length == 8) //Checking if the ip address looks like this: 11000000.00000011.10010101.00000000
{
int Foct = Convert.ToInt32(Testing[0], 2); //Convert first octet from binary to decimal
int Soct = Convert.ToInt32(Testing[1], 2); //Convert second octet from binary to decimal
int Toct = Convert.ToInt32(Testing[2], 2); //Convert third octet from binary to decimal
int Fouroct = Convert.ToInt32(Testing[3], 2); //Convert fourth octet from binary to decimal
IpToReturn = Foct + "." + Soct + "." + Toct + "." + Fouroct; //Creating ip address from octets
}
else if (IpAddress.Contains(".")) //Checking if the ip address is full. If the address is incomplete, it will be supplemented.
{
if (Testing.Length == 2)
{
if (string.IsNullOrEmpty(Testing[1]))
{
IpToReturn += "0";
}
IpToReturn += ".0.0";
}
else if (Testing.Length == 3)
{
if (string.IsNullOrEmpty(Testing[2]))
{
IpToReturn += "0";
}
IpToReturn += ".0";
}
else if (Testing.Length == 4)
{
if (string.IsNullOrEmpty(Testing[3]))
{
IpToReturn += "0";
}
}
}
else
{
IpToReturn += ".0.0.0";
}
return IpToReturn;
}
static int OverflowCheck(int[] ArrayHost, int Mask) //Calculating the number of hosts without overflowing
{
int HostsWithoutOverFlow = 0; //Number of hosts without overflowing
int MainMask = 2; //Number of free hosts in mask
int HostOver = 0; //Sum of busy network hosts
for (int i = 0; i <= 30 - Mask; i++)
{
MainMask *= 2;
} //Counts how many hosts are in the mask, including network and broadcast
foreach (int item in ArrayHost)
{
if (MainMask <= HostOver) //Breaking the loop when overflowing occurs
{
break;
}
int ManyHosts = 2; //The number of hosts on the network needed to accommodate the previously entered number of hosts
int MaskAddFromHosts = 0; //Sum of needed hosts
while (ManyHosts - 2 < item)
{
ManyHosts *= 2;
MaskAddFromHosts++;
}
HostOver += ManyHosts;
HostsWithoutOverFlow++;
}
return HostsWithoutOverFlow;
}
}
public class VlsmObject //An object containing all the necessary data
{
public string Network { get; set; }
public int Mask { get; set; }
public string FirstHost { get; set; }
public string LastHost { get; set; }
public string Broadcast { get; set; }
public int WantedHost { get; set; }
public VlsmObject(string network, int mask, string first_host, string last_host, string broadcast, int wanted_host)
{
Network = network;
Mask = mask;
FirstHost = first_host;
LastHost = last_host;
Broadcast = broadcast;
WantedHost = wanted_host;
}
}
}