private bool ValidateConfigs() { string ip = tapIPAddress.Text; string subnet = tapSubnet.Text; string gateway = tapGateway.Text; string dns = tapDnsServer.Text; if (Regex.IsMatch(tapSubnet.Text, @"^\s*$")) { tapSubnet.Text = "255.255.255.0"; } if (!IP.ValidateIPv4Mask(ref subnet)) { new BalloonTip("Warning", "Invalid IPv4 subnet mask", tapSubnet, BalloonTip.ICON.WARNING); return(false); } tapSubnet.Text = subnet; if (!IP.ValidateIPv4(ref ip, subnet)) { new BalloonTip("Warning", "Invalid IPv4 address", tapIPAddress, BalloonTip.ICON.WARNING); return(false); } tapIPAddress.Text = ip; if (!IP.ValidateIPv4(ref gateway)) { new BalloonTip("Warning", "Invalid IPv4 address", tapGateway, BalloonTip.ICON.WARNING); return(false); } tapGateway.Text = gateway; if (!IP.CheckIfSameNetwork(ip, gateway, subnet)) { new BalloonTip("Warning", "The gateway IPv4 address is not in the same natework", tapGateway, BalloonTip.ICON.WARNING); return(false); } if (IP.CheckIfSameNetwork(ip, gateway, "255.255.255.255")) { new BalloonTip("Warning", "Default gateway can't be the same as the local address.", tapGateway, BalloonTip.ICON.WARNING); return(false); } if (!IP.ValidateIPv4(ref dns)) { new BalloonTip("Warning", "Invalid IPv4 address", tapDnsServer, BalloonTip.ICON.WARNING); return(false); } tapDnsServer.Text = dns; return(true); }
private bool ValidateRoute() { IPAddress ipAddress = new IPAddress(0); string destination = routeDestination.Text; string prefix = routePrefix.Text; string gateway = routeGateway.Text; if (ipVersion == 4) { if (prefix == "") { prefix = "255.255.255.255"; } if (gateway == "") { gateway = "0.0.0.0"; } if (destination == "" || !IPAddress.TryParse(destination, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(destination, "240.0.0.0", "240.0.0.0")) { new BalloonTip("Warning", "Invalid IPv4 address", routeDestination, BalloonTip.ICON.WARNING); return(false); } if (!IP.ValidateIPv4Mask(ref prefix)) { new BalloonTip("Warning", "Invalid IPv4 subnet mask", routePrefix, BalloonTip.ICON.WARNING); return(false); } routeDestination.Text = IP.GetNetwork(destination, prefix); routePrefix.Text = prefix; if (!IPAddress.TryParse(gateway, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1 && IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.0.0.0") && !IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.255.255.255") || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.255.255.255") || IP.CheckIfSameNetwork(gateway, "224.0.0.0", "224.0.0.0") || IP.CheckIfSameNetwork(gateway, "240.0.0.0", "240.0.0.0")) { new BalloonTip("Warning", "Invalid IPv4 gateway address", routeGateway, BalloonTip.ICON.WARNING); return(false); } routeGateway.Text = gateway; } else { if (prefix == "") { prefix = "128"; } if (gateway == "") { gateway = "::"; } if (destination == "" || !IPAddress.TryParse(destination, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { new BalloonTip("Warning", "Invalid IPv6 address", routeDestination, BalloonTip.ICON.WARNING); return(false); } if (!IP.ValidateIPv6Prefix(ref prefix)) { new BalloonTip("Warning", "Invalid IPv6 subnet prefix length. Value must between 0 and 128 (inclusive)", routePrefix, BalloonTip.ICON.WARNING); return(false); } routeDestination.Text = IP.GetNetwork(destination, prefix); routePrefix.Text = prefix; if (!IP.ValidateIPv6(ref gateway)) // && !Regex.IsMatch(gateway, @"^(::1|::)$") && IPAddress.TryParse(gateway, out ipAddress) && ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { new BalloonTip("Warning", "Invalid IPv6 address", routeGateway, BalloonTip.ICON.WARNING); return(false); } routeGateway.Text = gateway; } if (routeInterface.SelectedIndex == -1) { new BalloonTip("Information", "Select the interface through which to route", routeInterface, BalloonTip.ICON.INFO); return(false); } if (routeMetric.Text == "") { if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1) { routeMetric.Text = "0"; } else { routeMetric.Text = "1"; } } if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0) { if (ushort.Parse(routeMetric.Text) == 0) { new BalloonTip("Warning", "Route metric must be a value between 1 and 9999 (inclusive)", routeMetric, BalloonTip.ICON.WARNING); return(false); } } List <Iphlpapi.Route> routes = Iphlpapi.GetRoutes(Iphlpapi.FAMILY.AF_UNSPEC); if (routes.Where((i) => IPAddress.Parse(i.Destination).Equals(IPAddress.Parse(routeDestination.Text)) && (ipVersion == 4 ? IPAddress.Parse(i.Prefix).Equals(IPAddress.Parse(routePrefix.Text)) : int.Parse(i.Prefix) == int.Parse(routePrefix.Text)) && IPAddress.Parse(i.Gateway).Equals(IPAddress.Parse(routeGateway.Text)) && i.InterfaceIndex.ToString() == Regex.Replace(routeInterface.Text, @"^(\d+) .*$", "$1")).Count() > 0) { new BalloonTip("Error", "Route already exists", button1, BalloonTip.ICON.ERROR); return(false); } return(true); }
private bool ValidateRoute() { IPAddress ipAddress = new IPAddress(0); string destination = routeDestination.Text; string prefix = routePrefix.Text; string gateway = routeGateway.Text; if (ipVersion == 4) { if (prefix == "") { prefix = "255.255.255.255"; } if (gateway == "") { gateway = "0.0.0.0"; } if (destination == "" || !IPAddress.TryParse(destination, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(destination, "240.0.0.0", "240.0.0.0")) { new BalloonTip("Warning", "Invalid IPv4 address", routeDestination, BalloonTip.ICON.WARNING); return(false); } if (!IP.ValidateIPv4Mask(ref prefix)) { new BalloonTip("Warning", "Invalid IPv4 subnet mask", routePrefix, BalloonTip.ICON.WARNING); return(false); } routeDestination.Text = IP.GetNetwork(destination, prefix); routePrefix.Text = prefix; if (!IPAddress.TryParse(gateway, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1 && IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.0.0.0") && !IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.255.255.255") || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.255.255.255") || IP.CheckIfSameNetwork(gateway, "224.0.0.0", "224.0.0.0") || IP.CheckIfSameNetwork(gateway, "240.0.0.0", "240.0.0.0")) { new BalloonTip("Warning", "Invalid IPv4 gateway address", routeGateway, BalloonTip.ICON.WARNING); return(false); } routeGateway.Text = gateway; } else { if (prefix == "") { prefix = "128"; } if (gateway == "") { gateway = "::"; } if (destination == "" || !IPAddress.TryParse(destination, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { new BalloonTip("Warning", "Invalid IPv6 address", routeDestination, BalloonTip.ICON.WARNING); return(false); } if (!IP.ValidateIPv6Prefix(prefix)) { new BalloonTip("Warning", "Invalid IPv6 subnet prefix length. Value must between 0 and 128 (inclusive)", routePrefix, BalloonTip.ICON.WARNING); return(false); } routeDestination.Text = IP.GetNetwork(destination, prefix); routePrefix.Text = prefix; if (!IP.ValidateIPv6(ref gateway)) // && !Regex.IsMatch(gateway, @"^(::1|::)$") && IPAddress.TryParse(gateway, out ipAddress) && ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { new BalloonTip("Warning", "Invalid IPv6 address", routeGateway, BalloonTip.ICON.WARNING); return(false); } routeGateway.Text = gateway; } if (routeInterface.SelectedIndex == -1) { new BalloonTip("Information", "Select the interface through which to route", routeInterface, BalloonTip.ICON.INFO); return(false); } if (routeMetric.Text == "") { if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1) { routeMetric.Text = "0"; } else { routeMetric.Text = "1"; } } if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0) { if (ushort.Parse(routeMetric.Text) == 0) { new BalloonTip("Warning", "Route metric must be a value between 1 and 9999 (inclusive)", routeMetric, BalloonTip.ICON.WARNING); return(false); } } return(true); }
private bool ValidateRoute(ref string destination, ref string prefix, ref string gateway, int ipVersion) { IPAddress ipAddress = new IPAddress(0); if (ipVersion == 4) { if (prefix == "") { return(false); } if (gateway == "") { return(false); } if (destination == "" || !IPAddress.TryParse(destination, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(destination, "240.0.0.0", "240.0.0.0")) { return(false); } if (!IP.ValidateIPv4Mask(ref prefix)) { return(false); } destination = IP.GetNetwork(destination, prefix); if (!IPAddress.TryParse(gateway, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1 && IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.0.0.0") && !IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.255.255.255") || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(gateway, "0.0.0.0", "255.255.255.255") || IP.CheckIfSameNetwork(gateway, "224.0.0.0", "224.0.0.0") || IP.CheckIfSameNetwork(gateway, "240.0.0.0", "240.0.0.0")) { return(false); } } else { if (prefix == "") { return(false); } if (gateway == "") { return(false); } if (destination == "" || !IPAddress.TryParse(destination, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { return(false); } if (!IP.ValidateIPv6Prefix(ref prefix)) { return(false); } destination = IP.GetNetwork(destination, prefix); if (!IP.ValidateIPv6(ref gateway)) { return(false); } } return(true); }
private bool ValidateConfigs() { IPAddress ipAddress = new IPAddress(0); if (loadIPv4 > 0 && NotConnectedIPv4Interfaces > 0 && defaultInterfaceMode.SelectedIndex == 0 || loadIPv4 > 0 && defaultInterfaceMode.SelectedIndex == 1) { if (defaultIPv4Interface.SelectedIndex == -1) { tabControl1.SelectTab(0); new BalloonTip("Information", "Select the default IPv4 interface through which to route", defaultIPv4Interface, BalloonTip.ICON.INFO); return(false); } if (defaultIPv4GatewayMode.SelectedIndex != 3) { string ipv4Gateway = defaultIPv4Gateway.Text; if (ipv4Gateway == "") { ipv4Gateway = "0.0.0.0"; } if (!IPAddress.TryParse(ipv4Gateway, out ipAddress) || ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork || Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1 && IP.CheckIfSameNetwork(ipv4Gateway, "0.0.0.0", "255.0.0.0") && !IP.CheckIfSameNetwork(ipv4Gateway, "0.0.0.0", "255.255.255.255") || Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0 && IP.CheckIfSameNetwork(ipv4Gateway, "0.0.0.0", "255.255.255.255") || IP.CheckIfSameNetwork(ipv4Gateway, "224.0.0.0", "224.0.0.0") || IP.CheckIfSameNetwork(ipv4Gateway, "240.0.0.0", "240.0.0.0")) { tabControl1.SelectTab(0); new BalloonTip("Warning", "Invalid IPv4 gateway address", defaultIPv4Gateway, BalloonTip.ICON.WARNING); return(false); } defaultIPv4Gateway.Text = ipv4Gateway; } } if (loadIPv6 > 0 && NotConnectedIPv6Interfaces > 0 && defaultInterfaceMode.SelectedIndex == 0 || loadIPv6 > 0 && defaultInterfaceMode.SelectedIndex == 1) { if (defaultIPv6Interface.SelectedIndex == -1) { tabControl1.SelectTab(1); new BalloonTip("Information", "Select the default IPv6 interface through which to route", defaultIPv6Interface, BalloonTip.ICON.INFO); return(false); } if (defaultIPv6GatewayMode.SelectedIndex != 3) { string ipv6Gateway = defaultIPv6Gateway.Text; if (ipv6Gateway == "") { ipv6Gateway = "0.0.0.0"; } if (!IP.ValidateIPv6(ref ipv6Gateway)) // && !Regex.IsMatch(gateway, @"^(::1|::)$") && IPAddress.TryParse(gateway, out ipAddress) && ipAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { tabControl1.SelectTab(1); new BalloonTip("Warning", "Invalid IPv6 gateway address", defaultIPv6Gateway, BalloonTip.ICON.WARNING); return(false); } defaultIPv6Gateway.Text = ipv6Gateway; } } return(true); }