/// Argument must be an address binary formatted with dots as 00010001.10110110.00101011.00110101 protected string IncrementIP(string ipToInc) { string[] splittedIP = ipToInc.Split('.'); // For each byte for (var i = 3; i >= 0; i--) { // This byte has reached max value if (splittedIP[i] == "11111111") { // This is the bigger byte if (i == 0) { throw new Exception("Max value reached : 255.255.255.255. This method does not treat a maxValue (expect max value that an IPv4 can hold)"); } else { // Init right's bytes splittedIP[i] = "00000000"; // Next byte continue; } } else { // Inc current byte splittedIP[i] = BinaryTools.incrementBin(splittedIP[i]); break; } } // this returns a 32 bit long string (ip, binary, without dots) return(StringArrayToIP(splittedIP)); }
/// Converts a "192.168.1.0" address to his binary equivalent WITHOUT the dots (eg. a 32 characters long string (binary)) protected string DecimalIPtoBinIP(string ipToConvert) { string tempIP = string.Empty; string toConcat = string.Empty; for (int i = 0; i < ipToConvert.Split('.').Length; i++) { toConcat = BinaryTools.decimalToBin(ipToConvert.Split('.')[i]); tempIP = String.Concat(tempIP, toConcat); } return(tempIP); }
/// Argument is an IP address, as a string, binary formatted, with dots like "11000000.10101000.00000001.00000000". It returns a decimal IP address like "192.168.1.0" format (decimal) protected string BinIPtoDecimalIP(string stringNumberToConvert) { stringNumberToConvert = BinIPtoBinIPWithoutDots(stringNumberToConvert); return(String.Concat(BinaryTools.binToDecimal(stringNumberToConvert.Substring(0, 8)), '.', BinaryTools.binToDecimal(stringNumberToConvert.Substring(8, 8)), '.', BinaryTools.binToDecimal(stringNumberToConvert.Substring(16, 8)), '.', BinaryTools.binToDecimal(stringNumberToConvert.Substring(24, 8)))); }
/// This method is used to shred networkToShrink in multiple subnets with a maskCIDR mask. (ie This will shrink 192.168.0.0/23 in 192.168.0.0/24 and 192.168.1.0/24) public List <Subnet> Shrink(int newMaskCIDR) { // The specified mask must be inferior to the original mask if (this._maskCIDR >= newMaskCIDR) { Console.WriteLine("FATAL: You can't shrink in a larger subnet or same size. Exiting."); System.Environment.Exit(7); } /// Return value List <Subnet> shrunkSubnets = new List <Subnet>(); // IP address of initial network, binary formatted, without dots. Like "01011101010001100100100001000100" string initialNetworkBinIP = this.DecimalIPtoBinIP(_networkIP); // The "fix part" of the subnet address : every bits in the initial mask. These will never change. string fixPart = initialNetworkBinIP.Substring(0, this._maskCIDR); // The "net part" are the bits who are going to be part of the new network adresses for each resulting shrunk subnet string netPart = initialNetworkBinIP.Substring(this._maskCIDR, (newMaskCIDR - this._maskCIDR)); // The "ip range part" are the bits that are going to be out of the new masks string ipRangePart = initialNetworkBinIP.Substring(newMaskCIDR); /* * // We are going to iterate on all shrinked subnets to store them in a list, more specifically, we store their CIDR formatted addresses (like "192.168.1.0/24") * // To iterate on these network addresses, we just need to : * // - keep the fix part (eg keep initialNetworkBinIP bits) * // - increment the net part * // - set each bit of IP range part to 0 (eg keep initialNetworkBinIP bits) * // - concatenate '/' and newMaskCIDR to the resulting address * // We also need to calculate the last network address as an exit condition for the loop : * // - keep the fix part (eg keep initialNetworkBinIP bits) * // - set each bit of network part to 1 * // - set each but of IP range part to 0 (eg keep initialNetworkBinIP bits) */ char[] lastNetworkAddressArray = initialNetworkBinIP.ToCharArray(); for (int i = this._maskCIDR; i < newMaskCIDR; i++) { lastNetworkAddressArray[i] = '1'; } // Store the last address as a string string lastNetworkAddress = new string(lastNetworkAddressArray); // Incremented IP address all along the loop string currentBinIP = initialNetworkBinIP; // Add the first Subnet to the returned list using currentBinIP but correctly formatted : "192.168.1.0/26" (with new mask) to initialize the Subnet object shrunkSubnets.Add(new Subnet(String.Concat((BinIPtoDecimalIP(BinIPtoBinIPWithDots(currentBinIP))), '/', newMaskCIDR), ShrinkedSubnetType.first)); // Increment netPart netPart = BinaryTools.incrementBin(netPart).Substring(BinaryTools.incrementBin(netPart).Length - netPart.Length, netPart.Length); // Substring is used because increment bin always return an 8 bit string (or any other 8 multiples) // Create the new currentBinIP by concatenating fixPart netPart and ipRangePart currentBinIP = String.Concat(fixPart, netPart, ipRangePart); do { // Add a new none Subnet to the returned list using currentBinIP but correctly formatted : "192.168.1.0/26" (with new mask) to initialize the Subnet object shrunkSubnets.Add(new Subnet(String.Concat((BinIPtoDecimalIP(BinIPtoBinIPWithDots(currentBinIP))), '/', newMaskCIDR), ShrinkedSubnetType.none)); // Increment netPart netPart = BinaryTools.incrementBin(netPart).Substring(BinaryTools.incrementBin(netPart).Length - netPart.Length, netPart.Length); // Substring is used because increment bin always return an 8 bit string (or any other 8 multiples) // Create the new currentBinIP by concatenating fixPart netPart and ipRangePart currentBinIP = String.Concat(fixPart, netPart, ipRangePart); } while (currentBinIP != lastNetworkAddress); // Add the last Subnet to the returned list using currentBinIP but correctly formatted : "192.168.1.0/26" (with new mask) to initialize the Subnet object shrunkSubnets.Add(new Subnet(String.Concat((BinIPtoDecimalIP(BinIPtoBinIPWithDots(lastNetworkAddress))), '/', newMaskCIDR), ShrinkedSubnetType.last)); return(shrunkSubnets); }