/// <summary> /// Initialization of the Class variable nessecary for building the subnet /// </summary> /// <param name="AddressClass"></param> /// <param name="SubnetNumber"></param> public ClassCSubnetBuilder(SubnetMask NetMask, IpAddress BaseAddress) { _SubnetNumber = 1; // We are casting to a byte here because for class A and class B the count will be greater than 255, but in every other case it will be <255 _HostsPerSubnet = NetMask.UsableHostsPerSubnet; _SampleAddress = BaseAddress; }
/// <summary> /// Uses the ammount of bits bowrowed to determine the subnetmask /// </summary> /// <returns></returns> private SubnetMask GetSubnetAddress(AddressClass AddrClass, int BitsBorrowed, int AddressBits) { // Use the address class to determine what the defaul subnet mask is byte[] BaseAddress; // Builts a base subnet with the address class switch (AddrClass) { case AddressClass.A: BaseAddress = new byte[] { 255, 0, 0, 0 }; break; case AddressClass.B: BaseAddress = new byte[] { 255, 255, 0, 0 }; break; case AddressClass.C: BaseAddress = new byte[] { 255, 255, 255, 0 }; break; default: BaseAddress = new byte[] { 255, 255, 255, 0 }; break; } int FirstIndexOfZero = Array.IndexOf(BaseAddress, 0); // Get local resource dictionary var assembly = typeof(IpAddress).GetTypeInfo().Assembly; string[] Resources = assembly.GetManifestResourceNames(); // Load resources based on address class Stream fileStream = assembly.GetManifestResourceStream("Network.SubnetData." + AddrClass.ToString() + ".json"); StreamReader Reader = new StreamReader(fileStream); // Parse json from resources string contents = Reader.ReadToEnd(); dynamic JsonData = JsonConvert.DeserializeObject(contents); // Determine Address class string AddressString = ""; AddressString = JsonData[BitsBorrowed.ToString()]; // Build the subnet mask object that will contain the nessecary information to build the addressing scheme SubnetMask ReturnAddress = new SubnetMask(AddressString); ReturnAddress.BitsBorrowed = BitsBorrowed; ReturnAddress.UsableHostsPerSubnet = AddressesPerSubnet(AddressBits); ReturnAddress.AddressesPerSubnet = IntPow(2, BitsBorrowed); return(ReturnAddress); }
public IpAddress AndAddress(IpAddress Ip, SubnetMask Subnetmask) { AddressParser Parser = new AddressParser(); byte[] AndedAddress = new byte[4]; for (int i = 0; i < Ip.AddressArray.Length; i++) { AndedAddress[i] = (byte)(Ip.AddressArray[i] & Subnetmask.AddressArray[i]); } IpAddress ReturnAddress = new IpAddress(AndedAddress); return ReturnAddress; }
public IpAddress AndAddress(IpAddress Ip, SubnetMask Subnetmask) { AddressParser Parser = new AddressParser(); byte[] AndedAddress = new byte[4]; for (int i = 0; i < Ip.AddressArray.Length; i++) { AndedAddress[i] = (byte)(Ip.AddressArray[i] & Subnetmask.AddressArray[i]); } IpAddress ReturnAddress = new IpAddress(AndedAddress); return(ReturnAddress); }
private void AndValues_Click(object sender, RoutedEventArgs e) { try { string IpAddressInput = IpAddressTextBox.Text; string SubnetmaskInput = SubnetMaskTextBox.Text; Ander AddressAnder = new Ander(); IpAddress InputIpAddress = new IpAddress(IpAddressInput); SubnetMask InputNetmask = new SubnetMask(SubnetmaskInput); IpAddress AndedAddress = AddressAnder.AndAddress(InputIpAddress, InputNetmask); OutputTextBox.Text = AndedAddress.ToString(); } catch (Exception) { Helpers.MessageBox MessageBox = new Helpers.MessageBox("Invalid input"); MessageBox.Show(); } }
/// <summary> /// Uses the ammount of bits bowrowed to determine the subnetmask /// </summary> /// <returns></returns> private SubnetMask GetSubnetAddress(AddressClass AddrClass, int BitsBorrowed, int AddressBits) { // Use the address class to determine what the defaul subnet mask is byte[] BaseAddress; // Builts a base subnet with the address class switch (AddrClass) { case AddressClass.A: BaseAddress = new byte[] { 255, 0, 0, 0 }; break; case AddressClass.B: BaseAddress = new byte[] { 255, 255, 0, 0 }; break; case AddressClass.C: BaseAddress = new byte[] { 255, 255, 255, 0 }; break; default: BaseAddress = new byte[] { 255, 255, 255, 0 }; break; } int FirstIndexOfZero = Array.IndexOf(BaseAddress, 0); // Get local resource dictionary var assembly = typeof(IpAddress).GetTypeInfo().Assembly; string[] Resources = assembly.GetManifestResourceNames(); // Load resources based on address class Stream fileStream = assembly.GetManifestResourceStream("Network.SubnetData." + AddrClass.ToString() + ".json"); StreamReader Reader = new StreamReader(fileStream); // Parse json from resources string contents = Reader.ReadToEnd(); dynamic JsonData = JsonConvert.DeserializeObject(contents); // Determine Address class string AddressString = ""; AddressString = JsonData[BitsBorrowed.ToString()]; // Build the subnet mask object that will contain the nessecary information to build the addressing scheme SubnetMask ReturnAddress = new SubnetMask(AddressString); ReturnAddress.BitsBorrowed = BitsBorrowed; ReturnAddress.UsableHostsPerSubnet = AddressesPerSubnet(AddressBits); ReturnAddress.AddressesPerSubnet = IntPow(2, BitsBorrowed); return ReturnAddress; }
public NetworkBuilder(NetworkInfo Info) { //// Get the nessecary data to build the subnetting information //int NumberOfHostsNeeded = Info.NumberOfHosts; //// The nessecary address class for the number of subnets and the required hosts per subnet //AddressClass InetClass = DetermineNessecaryAddressClass(NumberOfHostsNeeded); //// Uses 2^x to determine how many bits need to be borrowed //int BitsToBorrow = DetermineBitsToBorrow(NumberOfHostsNeeded, InetClass); //int BitsForAddressSpace = DetermineBitsForAddressSpace(BitsToBorrow, InetClass); //int RequiredSubnets = DetermineNumberOfSubnets(BitsToBorrow, InetClass); int BitsToBorrow = DetermineBitsToBorrow(Info.RequiredSubnets); AddressClass InetClass = DetermineNessecaryAddressClass(Info.NumberOfHosts); int AddressSpaceBits = DetermineBitsForAddressSpace(BitsToBorrow, InetClass); int RequiredSubnets = IntPow(2, BitsToBorrow); // Gets the subnet mask using data resources basedo n the required bits to borrow SubnetMask NetMask = GetSubnetAddress(InetClass, BitsToBorrow, AddressSpaceBits); IpAddress SampleAddress = new IpAddress(Info.SampleAddress); // The program has different functionallity based on different address class // For Class C it will be able to build the entire subnet out // For the other classes it will display generic subnetting information but not build a subnet switch (InetClass) { case AddressClass.A: BuiltNetwork = new FullNetwork(); // Gather network information for Class A //ClassAandBBuilder ClassABuilder = new ClassAandBBuilder(); //ClassABuilder.NetMask = NetMask; //ClassABuilder.HostsPerSubnet = IntPow(2, (AddressSpaceBits - 2) ); //ClassABuilder.NumberOfSubents = IntPow(2, BitsToBorrow); //BuiltNetwork.ClassAorBBuilder = ClassABuilder; BuiltNetwork = new FullNetwork(); BuiltNetwork.BitsBorrowed = BitsToBorrow; BuiltNetwork.Class = InetClass; BuiltNetwork.NetMask = NetMask; BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow); BuiltNetwork.AddressSpace = IntPow(2, AddressSpaceBits); BuiltNetwork.UsableHosts = IntPow(2, AddressSpaceBits) - 2; break; case AddressClass.B: //BuiltNetwork = new FullNetwork(); //ClassAandBBuilder ClassBBuilder = new ClassAandBBuilder(); //ClassBBuilder.NetMask = NetMask; //ClassBBuilder.HostsPerSubnet = IntPow(2, AddressSpaceBits); //ClassBBuilder.NumberOfSubents = IntPow(2, BitsToBorrow); //BuiltNetwork.ClassAorBBuilder = ClassBBuilder; BuiltNetwork = new FullNetwork(); BuiltNetwork.BitsBorrowed = BitsToBorrow; BuiltNetwork.Class = InetClass; BuiltNetwork.NetMask = NetMask; BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow); BuiltNetwork.AddressSpace = IntPow(2, AddressSpaceBits); BuiltNetwork.UsableHosts = IntPow(2, AddressSpaceBits) - 2; break; case AddressClass.C: // Gather network information for Class C int SubnetCount = 0; BuiltNetwork = new FullNetwork(); // Begin the subnet building process ClassCSubnetBuilder ClassCBuilder = new ClassCSubnetBuilder(NetMask, SampleAddress); // Subtraction by one because it is a based of zero while (SubnetCount < RequiredSubnets) { SubnetCount += 1; Subnetwork Subnet = ClassCBuilder.NextSubnet(); BuiltNetwork.Subnets.Enqueue(Subnet); } BuiltNetwork.BitsBorrowed = BitsToBorrow; BuiltNetwork.Class = InetClass; BuiltNetwork.NetMask = NetMask; BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow); BuiltNetwork.AddressSpace = (IntPow(2, AddressSpaceBits) * BuiltNetwork.NumberOfSubnets); BuiltNetwork.UsableHosts = IntPow(2, AddressSpaceBits) - 2; break; default: break; } }