//Metoda, ki namenjena preverjanju vnosa XXX.XXX.XXX.XXX in YYY.YYY.YYY.YYY public static bool ParseWithIPv4Subnet(string ipInput, out IPAddress ipAddress, out IPAddress subnetMask) { //Preverjanje, če je naslov neveljaven if (!IPAddress.TryParse(ipInput, out ipAddress) || ipAddress.AddressFamily != AddressFamily.InterNetwork) { WriteLine("Input is invalid IPv4 address."); ipAddress = subnetMask = null; return(false); } WriteLine("Enter valid IPv4 subnet mask:"); string input = ReadLine(); //Preverjanje, če je maska veljavna if (input != null && IPAddress.TryParse(input, out subnetMask) && ipAddress.AddressFamily == AddressFamily.InterNetwork && SubnetMask.IsValid(subnetMask)) { return(true); } WriteLine("Input is invalid IPv4 subnet mask."); ipAddress = subnetMask = null; return(false); }
/// <summary> /// Metoda namenjena preverjanju vnosa XXX.XXX.XXX.XXX/YY /// </summary> /// <param name="parts">deli vhodnega niza</param> /// <param name="ipAddress">izhodni ip naslov</param> /// <param name="subnetMask">izhodna maska</param> /// <returns>rezultat preverjanja veljavnosti</returns> public static bool ParseWithCIDR(string[] parts, out IPAddress ipAddress, out IPAddress subnetMask) { ipAddress = null; subnetMask = null; //Preverimo, če je smo dobili 2 dela if (parts.Length != 2) { return(false); } //Preverimo veljavnost IP naslova if (!IPAddress.TryParse(parts[0], out ipAddress) || ipAddress.AddressFamily != AddressFamily.InterNetwork) { WriteLine("Input is invalid IPv4 address."); ipAddress = null; return(false); } //Preverimo veljavnost CIDR pripone int cidr; if (!int.TryParse(parts[1], out cidr) || !Enumerable.Range(0, 32 + 1).Contains(cidr)) { WriteLine("Input is invalid CIDR extension."); return(false); } subnetMask = SubnetMask.CreateByNetBitLength(cidr); return(true); }
//Izpišemo masko podmrežja v obeh oblikah. public static void WriteNetmask(IPAddress subnetMask) { Write("Netmask: ".PadRight(RightPadding)); ForegroundColor = Blue; string netMask = $"{subnetMask} = {SubnetMask.GetNetBitLength(subnetMask)}"; Write(netMask.PadRight(RightPadding)); ForegroundColor = Red; SetCursorPosition(ThirdRow, CursorTop); Write(subnetMask.ToBinary()); ResetColor(); WriteLine(); }
//Oblikujemo izhod programa static void WriteMainOutput(IPAddress ipAddress, IPAddress subnetMask) { WriteLine("Input interpreted as:"); WriteStandardFormat("Address: ", ipAddress); WriteNetmask(subnetMask); WriteStandardFormat("Wildcard: ", SubnetMask.GetWildCard(subnetMask)); WriteLine(); WriteLine("=>"); WriteLine(); WriteNetwork(ipAddress, subnetMask); WriteStandardFormat("Broadcast: ", ipAddress.GetBroadcastAddress(subnetMask)); IPAddress hostMin = ipAddress.GetNetworkAddress(subnetMask).Increment(), hostMax = ipAddress.GetBroadcastAddress(subnetMask).Decrement(); WriteStandardFormat("HostMin: ", hostMin); WriteStandardFormat("HostMax: ", hostMax); WriteAvailableHosts(subnetMask, ipAddress); }
//Oblikujemo izpis za omrežje, kjer so dodatno obrvani prvi biti glede na razred IP naslova. public static void WriteNetwork(IPAddress ipAddress, IPAddress subnetMask) { var networkAddress = ipAddress.GetNetworkAddress(subnetMask); var ipClass = networkAddress.GetClass(); Write("Network: ".PadRight(RightPadding)); ForegroundColor = Blue; string network = $"{networkAddress}/{SubnetMask.GetNetBitLength(subnetMask)}"; Write(network.PadRight(RightPadding)); ForegroundColor = DarkGray; int numOfColoredBits = (int)ipClass + 1; if (numOfColoredBits == 5) { numOfColoredBits--; } SetCursorPosition(ThirdRow, CursorTop); if (ipClass > IPClass.Undefined && ipClass < IPClass.Invalid) { WriteWithColoredStartChars(networkAddress.ToBinary(), numOfColoredBits); } else { Write(networkAddress.ToBinary()); } Write(" ("); ForegroundColor = Green; if (ipClass > IPClass.Undefined && ipClass < IPClass.Invalid) { Write($"Class {ipClass}"); } else { Write(ipClass); } ForegroundColor = DarkGray; Write(")"); ResetColor(); WriteLine(); }
//Izpišemo koliko uporabnih naslovov je na voljo in še ali omrežje spada med nejavne naslove. public static void WriteAvailableHosts(IPAddress subnetMask, IPAddress ipAddress) { Write("Hosts/Net: ".PadRight(RightPadding)); ForegroundColor = Blue; Write($"{SubnetMask.NumberOfUsableHosts(subnetMask)}".PadRight(RightPadding)); ForegroundColor = DarkBlue; string specialOutput = ""; if (ipAddress.IsPrivate()) { specialOutput = "(Private Internet)"; } else if (ipAddress.Equals(IPAddress.Loopback)) { specialOutput = "(LOCALHOST)"; } SetCursorPosition(ThirdRow, CursorTop); Write(specialOutput); ResetColor(); WriteLine(); }