private void DecimalIpAddressTextBox_KeyUp(object sender, KeyEventArgs e) { string RawInput; RawInput = DecimalIpAddressTextBox.Text.Trim(); // CIDR regex string pattern = @"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"; Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); if (rgx.IsMatch(RawInput) == true) { try { IpAddress Address = new IpAddress(RawInput); BinaryIpAddressTextBox.Text = Address.ToBinaryString(); } catch (Exception) { BinaryIpAddressTextBox.Text = "Invalid Address"; } } else { BinaryIpAddressTextBox.Text = ""; } }
/// <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> /// Returns the next subnet in the sequence /// </summary> /// <returns></returns> public Subnetwork NextSubnet() { // Will be returned to the network buidler class Subnetwork ReturnNetwork = new Subnetwork(); byte[] BaseAddress = _SampleAddress.AddressArray; // Calculate the nessecary information to build the IPs on the subnet byte lastUsable = Convert.ToByte(_HostsPerSubnet * _SubnetNumber - 2); byte broadCastAddress = Convert.ToByte(lastUsable + 1); // HORRID HACK // I am Elijah Ellis Please remember my name as I may not // The subnet ID here is a special case because it requires math using subtraction byte subnetId; if (_SubnetNumber == 1) { // I am sorry mom subnetId = 0; } else { subnetId = Convert.ToByte(Math.Abs(_HostsPerSubnet - broadCastAddress - 1)); } byte firstUsable = Convert.ToByte(subnetId + 1); // TODO: Possibly refactor this section by using the BaseAddress away, but I am scared that it might mutate the contents of the array inproperly // Build Subnet ID and attach it to the return network byte[] NetworkIdArray = BaseAddress; NetworkIdArray[3] = subnetId; IpAddress NetworkID = new IpAddress(NetworkIdArray); ReturnNetwork.NetworkId = NetworkID; // Build Broadcast address object byte[] BroadCastAddressArray = BaseAddress; BroadCastAddressArray[3] = broadCastAddress; IpAddress BroadcastAddress = new IpAddress(BroadCastAddressArray); ReturnNetwork.BroadcastAddress = BroadcastAddress; // Build the first usuable byte[] FirstUsuableArray = BaseAddress; FirstUsuableArray[3] = firstUsable; IpAddress FirstUsable = new IpAddress(FirstUsuableArray); ReturnNetwork.FirstUsable = FirstUsable; // Built the last usable address object byte[] LastUsuableArray = BaseAddress; LastUsuableArray[3] = lastUsable; IpAddress LastUsable = new IpAddress(LastUsuableArray); ReturnNetwork.LastUsable = LastUsable; // Increment the subnet number for the next calculation _SubnetNumber += 1; return ReturnNetwork; }
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(); } }
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; } }
/* Private members */ /// <summary> /// Initializes the base address /// </summary> /// <param name="baseAddress"></param> /// <returns></returns> private IpAddress ParseBaseAddress(IpAddress baseAddress) { throw new NotImplementedException(); }
// Test static void PrintAddressInformation(IpAddress ip) { Console.WriteLine(ip.Address); foreach (byte oct in ip.AddressArray) { Console.WriteLine(oct.ToString()); } }
/// <summary> /// Returns the next subnet in the sequence /// </summary> /// <returns></returns> public Subnetwork NextSubnet() { // Will be returned to the network buidler class Subnetwork ReturnNetwork = new Subnetwork(); byte[] BaseAddress = _SampleAddress.AddressArray; // Calculate the nessecary information to build the IPs on the subnet byte lastUsable = Convert.ToByte(_HostsPerSubnet * _SubnetNumber - 2); byte broadCastAddress = Convert.ToByte(lastUsable + 1); // HORRID HACK // I am Elijah Ellis Please remember my name as I may not // The subnet ID here is a special case because it requires math using subtraction byte subnetId; if (_SubnetNumber == 1) { // I am sorry mom subnetId = 0; } else { subnetId = Convert.ToByte(Math.Abs(_HostsPerSubnet - broadCastAddress - 1)); } byte firstUsable = Convert.ToByte(subnetId + 1); // TODO: Possibly refactor this section by using the BaseAddress away, but I am scared that it might mutate the contents of the array inproperly // Build Subnet ID and attach it to the return network byte[] NetworkIdArray = BaseAddress; NetworkIdArray[3] = subnetId; IpAddress NetworkID = new IpAddress(NetworkIdArray); ReturnNetwork.NetworkId = NetworkID; // Build Broadcast address object byte[] BroadCastAddressArray = BaseAddress; BroadCastAddressArray[3] = broadCastAddress; IpAddress BroadcastAddress = new IpAddress(BroadCastAddressArray); ReturnNetwork.BroadcastAddress = BroadcastAddress; // Build the first usuable byte[] FirstUsuableArray = BaseAddress; FirstUsuableArray[3] = firstUsable; IpAddress FirstUsable = new IpAddress(FirstUsuableArray); ReturnNetwork.FirstUsable = FirstUsable; // Built the last usable address object byte[] LastUsuableArray = BaseAddress; LastUsuableArray[3] = lastUsable; IpAddress LastUsable = new IpAddress(LastUsuableArray); ReturnNetwork.LastUsable = LastUsable; // Increment the subnet number for the next calculation _SubnetNumber += 1; return(ReturnNetwork); }