initial commit

This commit is contained in:
Baipyrus 2022-05-15 17:25:52 +02:00
commit e46cfc36b5
3 changed files with 237 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/*
obj/*

227
Program.cs Normal file
View File

@ -0,0 +1,227 @@
using System;
using System.Collections;
namespace VLSM {
class Program {
static void Main(string[] args) {
// Get user input for the network address
Console.Write("Enter the network address: ");
string networkAddress = Console.ReadLine();
// Convert the network address to a single binary value
string[] networkAddressParts = networkAddress.Split('.');
if (networkAddressParts.Length != 4) {
Console.WriteLine("Invalid network address.");
return;
} else {
networkAddress = "";
for (int i = 0; i < 4; i++) {
// Convert each octet to binary
int octet = Convert.ToInt32(networkAddressParts[i]);
string octetBinary = Convert.ToString(octet, 2);
// Pad the binary string with zeros to 8 bits
octetBinary = octetBinary.PadLeft(8, '0');
// Add the binary string to the subnet mask
networkAddress += octetBinary;
}
}
// Get user input for the subnet mask
Console.Write("Enter the subnet mask: ");
string subnetMask = Console.ReadLine();
// Convert the subnet mask to a single binary value
if (subnetMask[0] == '/') {
int subnetPrefix = Convert.ToInt32(subnetMask.Substring(1));
string tempMask = "";
for (int i = 0; i < subnetPrefix; i++)
tempMask += "1";
for (int i = subnetPrefix; i < 32; i++)
tempMask += "0";
subnetMask = tempMask;
} else {
string[] subnetMaskParts = subnetMask.Split('.');
if (subnetMaskParts.Length != 4) {
Console.WriteLine("Invalid subnet mask.");
return;
} else {
subnetMask = "";
for (int i = 0; i < 4; i++) {
// Convert each octet to binary
int octet = Convert.ToInt32(subnetMaskParts[i]);
string octetBinary = Convert.ToString(octet, 2);
// Pad the binary string with zeros to 8 bits
octetBinary = octetBinary.PadLeft(8, '0');
// Add the binary string to the subnet mask
subnetMask += octetBinary;
}
}
}
// Output the network address and subnet mask, then pause the program
// Console.WriteLine("Network address: " + networkAddress);
// Console.WriteLine("Subnet mask: " + subnetMask);
// Console.ReadKey();
// Get user input for the number of hosts repeatedly
Console.Clear();
ArrayList hostList = new ArrayList();
while (true) {
Console.Write("Enter the number of hosts: ");
string input = Console.ReadLine();
if (input == "")
break;
int numberOfHosts = Convert.ToInt32(input);
if (numberOfHosts < 2) {
Console.WriteLine("The number of hosts must be greater than 1.");
continue;
}
hostList.Add(numberOfHosts+2);
}
// Sort the list of hosts in descending order
hostList.Sort();
hostList.Reverse();
// Exit if the sum of hosts is greater than maxHosts
int sumOfHosts = 0;
int bitCountDown = subnetMask.Length - 1;
while (subnetMask[bitCountDown] == '0')
bitCountDown--;
int maxHosts = (int)Math.Pow(2, subnetMask.Length - bitCountDown - 1);
// Console.WriteLine("The maximum number of hosts is: " + maxHosts);
// Console.ReadKey();
foreach (int host in hostList)
sumOfHosts += host;
if (sumOfHosts > maxHosts) {
Console.WriteLine("The sum of hosts must be less than or equal to {0}.", maxHosts);
Console.ReadKey();
return;
}
// Calculate each subnet by their hosts contained in hostList
Console.Clear();
ArrayList subnetList = new ArrayList();
string previous = "";
int totalHostsProvided = 0;
foreach (int host in hostList) {
string sM_ = subnetMask;
// Remove all the zeros at the end of sM_
int i = sM_.Length - 1;
while (sM_[i] == '0')
i--;
sM_ = sM_.Substring(0, i + 1);
// Calculate the number of zeroes in sM_
int numberOfZeroes = subnetMask.Length - 1 - i;
// Add remaining subnetbits to sM_
int hBit = (int)Math.Ceiling(Math.Log(host, 2));
int bits = numberOfZeroes - hBit;
int nOH = (int)Math.Pow(2, hBit);
totalHostsProvided += nOH;
for (int j = 0; j < bits; j++)
sM_ += "1";
// Add zeroes to the end of sM_ until there are 32 characters in it
while (sM_.Length < 32)
sM_ += '0';
// Calculate the network address
string nA_ = networkAddress.Substring(0, i+1);
if (previous == "") {
for (int j = 0; j < bits; j++)
previous += "0";
nA_ += previous;
// Console.WriteLine("Previous: {0}; Pre.Int: {1}; ", previous, Convert.ToInt32(previous, 2));
} else {
// Add 1 to the binary value of previous
int preConversionLength = previous.Length;
int previousInt = Convert.ToInt32(previous, 2);
// Console.WriteLine("#1 Previous: {0}; Pre.Int: {1}; ", previous, previousInt);
previousInt++;
previous = Convert.ToString(previousInt, 2);
// Convert 'previousInt' to binary, add 'preConversionLength' many
// zeroes to the end of the string and save it in 'previous'
previous = Convert.ToString(previousInt, 2);
previous = previous.PadLeft(preConversionLength, '0');
// Add zeroes to the end of previous until there are 'bits' many characters in it
while (previous.Length < bits)
previous += '0';
// Console.WriteLine("#2 Previous: {0}; Pre.Int: {1}; ", previous, previousInt);
nA_ += previous;
}
// Copy nA_ to bA_ and add ones to the end of it until there are 32 characters in it
string bA_ = nA_;
while (bA_.Length < 32)
bA_ += '1';
// Add zeroes to the end of nA_ until there are 32 characters in it
while (nA_.Length < 32)
nA_ += '0';
subnetList.Add(new Subnet(nA_, bA_, sM_, nOH-2));
// Console.WriteLine();
// Console.ReadKey();
}
// Output the subnets
foreach (Subnet subnet in subnetList) {
Console.WriteLine("Needed Hosts: {0}", subnet.numberOfHosts);
// Convert subnet.subnetMask to a subnetprefix
Console.WriteLine("Subnetmask: {0}/{1}", subnet.subnetMask, subnet.subnetPrefix);
Console.WriteLine("Network address: {0}", subnet.networkAddress);
Console.WriteLine("Broadcast address: {0}", subnet.broadcastAddress);
Console.WriteLine();
}
float AMNASU = ((float)totalHostsProvided*100) / maxHosts;
float SNASU = ((float)sumOfHosts*100) / totalHostsProvided;
Console.WriteLine("About {0:000.00}% of available major network address space is used.", AMNASU);
Console.WriteLine("About {0:000.00}% of subnetted network address space is used.", SNASU);
Console.ReadKey();
}
// Class to contain a subnet.
// Each subnet has a network address, broadcast adress, subnet mask and number of hosts.
class Subnet {
public string networkAddress, broadcastAddress, subnetMask;
public int numberOfHosts, subnetPrefix = 0;
// Constructor
public Subnet(string nA_, string bA_, string sM_, int nH_) {
for (int i = 0; i < sM_.Length; i++)
if (sM_[i] == '1')
subnetPrefix++;
// Split network address, broadcast address and subnet mask into octets
// and convert each octet to decimal
// then join all octets with a '.'
string[] nA_Octets = new string[4];
string[] bA_Octets = new string[4];
string[] sM_Octets = new string[4];
for (int i = 0; i < 32; i++) {
int index = (int) i / 8;
nA_Octets[index] = nA_Octets[index] + nA_[i];
bA_Octets[index] = bA_Octets[index] + bA_[i];
sM_Octets[index] = sM_Octets[index] + sM_[i];
}
networkAddress = "";
broadcastAddress = "";
subnetMask = "";
for (int i = 0; i < 4; i++) {
int nA_Octet = Convert.ToInt32(nA_Octets[i], 2);
int bA_Octet = Convert.ToInt32(bA_Octets[i], 2);
int sM_Octet = Convert.ToInt32(sM_Octets[i], 2);
bool iL3 = i < 3;
networkAddress += nA_Octet + ((iL3) ? "." : "");
broadcastAddress += bA_Octet + ((iL3) ? "." : "");
subnetMask += sM_Octet + ((iL3) ? "." : "");
}
// networkAddress = nA_;
// broadcastAddress = bA_;
// subnetMask = sM_;
numberOfHosts = nH_;
}
}
}
}

8
Subnetting.csproj Normal file
View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>