/// <summary> /// Checks if the given String is an valid ip-address /// </summary> /// <param name="arg_sIP">IP-String</param> /// <param name="arg_ipNotation">IP-notation</param> /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param> /// <returns>true/false validated/not</returns> protected static bool ValidateIP(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) { bool bValidated = false; ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char)))); try { switch (arg_ipNotation) { case IPNotation.IPv4Decimal: case IPNotation.IPv4Binary: bValidated = arlIP.Count == 4; break; case IPNotation.IPv4DecimalCIDR: case IPNotation.IPv4BinaryCIDR: bValidated = arlIP.Count == 5; break; case IPNotation.IPv6Hexadecimal: case IPNotation.IPv6Binary: bValidated = arlIP.Count == 8; break; case IPNotation.IPv6HexadecimalCIDR: case IPNotation.IPv6BinaryCIDR: bValidated = arlIP.Count == 9; break; case IPNotation.IPv6IPv4Decimal: case IPNotation.IPv6IPv4Binary: bValidated = arlIP.Count == 6; break; case IPNotation.IPv6IPv4DecimalCIDR: case IPNotation.IPv6IPv4BinaryCIDR: bValidated = arlIP.Count == 7; break; default: break; } if (!bValidated) { throw new Exception("IP-Address has wrong element count"); } //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0); //don't check the subnet element i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count); i++) { string sIPPart = arlIP[i].ToString().Replace(" ", ""); int iIPPart = 0; switch (arg_ipNotation) { case IPNotation.IPv4Decimal: case IPNotation.IPv4DecimalCIDR: case IPNotation.IPv6IPv4Decimal: case IPNotation.IPv6IPv4DecimalCIDR: while (sIPPart.Length < 3) sIPPart = "0" + sIPPart; iIPPart = Convert.ToInt32(sIPPart, 10); if (iIPPart < 256) bValidated = true; else bValidated = false; break; case IPNotation.IPv4Binary: case IPNotation.IPv4BinaryCIDR: case IPNotation.IPv6IPv4Binary: case IPNotation.IPv6IPv4BinaryCIDR: while (sIPPart.Length < 8) sIPPart = "0" + sIPPart; iIPPart = Convert.ToInt32(sIPPart, 2); if (iIPPart < 256) bValidated = true; else bValidated = false; break; case IPNotation.IPv6Hexadecimal: case IPNotation.IPv6HexadecimalCIDR: while (sIPPart.Length < 4) sIPPart = "0" + sIPPart; iIPPart = Convert.ToInt32(sIPPart, 16); if (iIPPart < 65536) bValidated = true; else bValidated = false; break; case IPNotation.IPv6Binary: case IPNotation.IPv6BinaryCIDR: while (sIPPart.Length < 16) sIPPart = "0" + sIPPart; iIPPart = Convert.ToInt32(sIPPart, 2); if (iIPPart < 65536) bValidated = true; else bValidated = false; break; default: break; } if (!bValidated) { throw new Exception(string.Format("IP-Address element {0}({1}) has wrong format", i, sIPPart)); } } } catch (Exception LastError) { System.Diagnostics.Debug.WriteLine(LastError.Message); bValidated = false; throw LastError; } return bValidated; }
/// <summary> /// Adds Zeroes to given IP-Address, so it fits in the textfield /// </summary> /// <param name="arg_sIP">IP-String</param> /// <param name="arg_ipNotation">IP-notation</param> /// <param name="arg_arlDelimeter">Delimeter to parse IPString</param> /// <returns>IP-Address with Spaces</returns> protected static string MakeValidZeroes(string arg_sIP, IPNotation arg_ipNotation, ArrayList arg_arlDelimeter) { ArrayList arlIP = new ArrayList(arg_sIP.Split((char[])arg_arlDelimeter.ToArray(typeof(char)))); //don't check the 1st 2 elemnt if its IPv4 in IPv6-notation for (int i = (arg_ipNotation.ToString().IndexOf("IPv6IPv4") == 0 ? 2 : 0); //don't check the subnet element i < (arg_ipNotation.ToString().IndexOf("CIDR") > 0 ? arlIP.Count - 1 : arlIP.Count); i++) { switch (arg_ipNotation) { case IPNotation.IPv4Decimal: case IPNotation.IPv4DecimalCIDR: case IPNotation.IPv6IPv4Decimal: case IPNotation.IPv6IPv4DecimalCIDR: while (arlIP[i].ToString().Length < 3) arlIP[i] = "0" + arlIP[i].ToString(); break; case IPNotation.IPv4Binary: case IPNotation.IPv4BinaryCIDR: case IPNotation.IPv6IPv4Binary: case IPNotation.IPv6IPv4BinaryCIDR: while (arlIP[i].ToString().Length < 8) arlIP[i] = "0" + arlIP[i].ToString(); break; case IPNotation.IPv6Hexadecimal: case IPNotation.IPv6HexadecimalCIDR: while (arlIP[i].ToString().Length < 4) arlIP[i] = "0" + arlIP[i].ToString(); break; case IPNotation.IPv6Binary: case IPNotation.IPv6BinaryCIDR: while (arlIP[i].ToString().Length < 16) arlIP[i] = "0" + arlIP[i].ToString(); break; default: break; } } return IPAddressTextBox.MakeIP((string[])arlIP.ToArray(typeof(string)), arg_ipNotation); }