/// <summary> /// Checks if there is any TAP interface ready to be used (disconnected or connected with a valid handle) /// </summary> /// <returns></returns> public static bool Check() { LoadingForm splash = new LoadingForm("Checking TAP interface ..."); if (handle != IntPtr.Zero) { IntPtr pTapVersion = Marshal.AllocHGlobal(2000); uint bytesReturned = 0; if (Kernel32.DeviceIoControl(handle, TAP_IOCTL_GET_VERSION, pTapVersion, 2000, pTapVersion, 2000, ref bytesReturned, IntPtr.Zero)) { Marshal.FreeHGlobal(pTapVersion); Connected = true; splash.Stop(); return true; } else { Marshal.FreeHGlobal(pTapVersion); handle = IntPtr.Zero; } } // not connected; seeking available interface Connected = false; Guid = null; string cfgKeyPath = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"; string nameKeyPath = @"SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}"; RegistryKey key = Registry.LocalMachine.OpenSubKey(cfgKeyPath); foreach (string name in key.GetSubKeyNames()) { try { if ((string)Registry.LocalMachine.OpenSubKey(cfgKeyPath + @"\" + name).GetValue("ComponentId") == componentID) { string netCfgInstanceId = (string)Registry.LocalMachine.OpenSubKey(cfgKeyPath + @"\" + name).GetValue("NetCfgInstanceId"); string friendlyName = (string)Registry.LocalMachine.OpenSubKey(nameKeyPath + @"\" + netCfgInstanceId + @"\Connection").GetValue("Name"); if (NetworkInterface.GetAdapterStatus(friendlyName) == NetworkInterface.Status.MediaDisconnected || NetworkInterface.GetAdapterStatus(friendlyName) == NetworkInterface.Status.Disconnected) { Guid = netCfgInstanceId; FriendlyName = friendlyName; splash.Stop(); return true; } } } catch { } } splash.Stop(); return false; }
public static bool Start() { Global.WriteLog("UDP Detector is starting.", true); if (!Dependencies.RunWinPcapService(null, true)) { Global.WriteLog("UDP Detector failed to start.", true); Global.ShowTrayTip("UDP Detector", "Failed to start", System.Windows.Forms.ToolTipIcon.Error); return false; } LoadingForm splash = new LoadingForm("Initializing ..."); interfaceWorkers.Clear(); foreach (string guid in Dependencies.WinPcapDevices) { interfaceWorkers.Add(new InterfaceWorker(guid)); splash.UpdateStatus("Initializing " + interfaceWorkers.Last().Name + " ..."); new Thread(new ThreadStart(interfaceWorkers.Last().ReceivePackets)).Start(); interfaceWorkers.Last().Initialized.Wait(10000); } splash.Stop(); Dependencies.WinPcapInUse.Reset(Dependencies.WinPcapInUse.CurrentCount + 1); Global.WriteLog("UDP Detector: started"); Global.ShowTrayTip("UDP Detector", "Started", System.Windows.Forms.ToolTipIcon.Info); return true; }
/// <summary> /// HACK: metrics are sometimes ignored or the effect is delayed /// </summary> /// <param name="guid"></param> void MakeInterfacePrimary(string guid) { LoadingForm splash = new LoadingForm("Retrieving interfaces ..."); foreach (NetworkInterface nic in Global.NetworkInterfaces.Values) { splash.UpdateStatus("Configuring interface \"" + nic.Name + "\" ..."); if (nic.Guid == guid) { nic.SetInterfaceMetric("1"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) nic.EditIPv4Gateway(ip.Address, "1"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) nic.EditIPv4Gateway(ip.Address, "1"); // on Windows 8.1 occasionaly breaks routing until restart or ??? internal Windows stuff //foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) // Iphlpapi.EditRoute("0.0.0.0", "0.0.0.0", ip.Address, nic.Index.ToString(), "0"); //foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) // Iphlpapi.EditRoute("::", "0", ip.Address, nic.Index.ToString(), "0"); } else { nic.SetInterfaceMetric("4000"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) nic.EditIPv4Gateway(ip.Address, "4000"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) nic.EditIPv4Gateway(ip.Address, "4000"); // on Windows 8.1 occasionaly breaks routing until restart or ??? internal Windows stuff //foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) // Iphlpapi.EditRoute("0.0.0.0", "0.0.0.0", ip.Address, nic.Index.ToString(), "4000"); //foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) // Iphlpapi.EditRoute("::", "0", ip.Address, nic.Index.ToString(), "4000"); } } splash.Stop(); Program.Refresh(); }
public static bool Start(IEnumerable<NetworkInterface> requiredNics) { Global.WriteLog("Load Balancer is starting.", true); Status.Update(State.Starting); if (requiredNics.Count() == 0) { Global.WriteLog("Load Balancer can't start without any physical interface.", true); Global.ShowTrayTip("Load Balancer", "Can't start whitout any physical interface", System.Windows.Forms.ToolTipIcon.Error); Status.Update(State.Failed); return false; } if (!Jobs.Extensions.Dependencies.Check()) { Status.Update(State.Failed); return false; } if (!TapInterface.PutUp()) { Global.WriteLog("Load Balancer failed to connect to " + TapInterface.FriendlyName, true); Global.ShowTrayTip("Load Balancer", "Failed to connect to " + TapInterface.FriendlyName, System.Windows.Forms.ToolTipIcon.Error); Status.Update(State.Failed); return false; } if (!Dependencies.RunWinPcapService(requiredNics, true)) { Global.WriteLog("Load Balancer failed to start because some interfaces were not captured by WinPcap.", true); Global.ShowTrayTip("Load Balancer", "Failed to start", System.Windows.Forms.ToolTipIcon.Error); TapInterface.PutDown(); Status.Update(State.Failed); return false; } Interfaces = requiredNics; // start LB threads LoadingForm splash = new LoadingForm("Initializing ..."); foreach (NetworkInterface nic in Global.NetworkInterfaces.Values) if (nic.Guid != TapInterface.Guid && (nic.IPv4Gateway.Count > 0 || nic.IPv6Gateway.Count > 0)) { splash.UpdateStatus("Configuring " + nic.Name + " ..."); nic.SetInterfaceMetric("4000"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) nic.EditIPv4Gateway(ip.Address, "4000"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) nic.EditIPv6Gateway(ip.Address, "4000"); } splash.UpdateStatus("Initializing " + TapInterface.FriendlyName + " ..."); physicalWorkers.Clear(); tapWorker = new TapWorker(TapInterface.Guid, TapInterface.FriendlyName, TapInterface.Mac, Global.Config.LoadBalancer.IPv4LocalAddresses.First().Address, Global.Config.LoadBalancer.IPv4GatewayAddresses.First().Address); new Thread(new ThreadStart(tapWorker.ReceivePackets)).Start(); tapWorker.Initialized.Wait(1000); foreach (NetworkInterface nic in requiredNics) { splash.UpdateStatus("Initializing " + nic.Name + " ..."); physicalWorkers.Add(new PhysicalWorker(nic.Guid, nic.Name, nic.Mac, nic.IPv4Address.First().Address, nic.IPv4Address.First().Subnet, nic.DefaultIPv4GatewayMac, nic.DefaultIPv4Gateway)); new Thread(new ThreadStart(physicalWorkers.Last().ReceivePackets)).Start(); physicalWorkers.Last().Initialized.Wait(10000); } int mtu; MTU = requiredNics.Min((i) => int.TryParse(i.IPv4Mtu, out mtu) ? mtu : 1500); Global.WriteLog("Load Balancer: Negociated MTU = " + MTU); MSS = (ushort)(MTU - 40); splash.Stop(); Dependencies.WinPcapInUse.Reset(Dependencies.WinPcapInUse.CurrentCount + 1); Global.WriteLog("Load Balancer: started"); Global.ShowTrayTip("Load Balancer", "Started", System.Windows.Forms.ToolTipIcon.Info); Status.Update(State.Running); new Thread(new ThreadStart(CheckUp)).Start(); return true; }
public static bool Stop() { Status.Update(State.Stopping); TapInterface.PutDown(); LoadingForm splash = new LoadingForm("Stopping Load Balancer ..."); if (physicalWorkers.Count() > 0) { for (int i = physicalWorkers.Count() - 1; i >= 0; i--) { splash.UpdateStatus("Stop capturing from " + physicalWorkers[i].Name + " ..."); physicalWorkers[i].Stop(); physicalWorkers.RemoveAt(i); } } splash.UpdateStatus("Stop capturing from " + tapWorker.Name + " ..."); tapWorker.Stop(); splash.UpdateStatus("Clearing routing table ..."); RoutingTable.Clear(); Dependencies.WinPcapInUse.Signal(); splash.Stop(); Global.WriteLog("Load Balancer stopped.", true); Status.Update(State.Stopped); return true; }
public static bool Install() { DialogResult dialogResult = MessageBox.Show("Do you want to create a TAP network interface?", "TAP Driver Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.No) return false; LoadingForm splash = new LoadingForm("Decompressing TAP driver..."); Directory.CreateDirectory("Temp"); File.WriteAllBytes(@"Temp\TAP Driver.zip", Network_Manager.Properties.Resources.TAP_Driver); Lib.IO.Compression.UnZip(@"Temp\TAP Driver.zip", "Temp"); // Vista driver version does not install correctly on Vista, so Win2K is used string driverPath; if (Environment.Is64BitOperatingSystem) if (Environment.OSVersion.Version.CompareTo(new Version("6.1")) > -1) driverPath = @"Temp\TAP Driver\Vista 64-bit\OemVista.inf"; else driverPath = @"Temp\TAP Driver\XP 64-bit\OemWin2k.inf"; else if (Environment.OSVersion.Version.CompareTo(new Version("6.1")) > -1) driverPath = @"Temp\TAP Driver\Vista 32-bit\OemVista.inf"; else driverPath = @"Temp\TAP Driver\XP 32-bit\OemWin2k.inf"; splash.UpdateStatus("Installing TAP driver ..."); if (Setupapi.InstallInfDriver(driverPath, componentID, "Network Manager TAP Adapter")) { File.Delete("TAP Driver.zip"); splash.Stop(); MessageBox.Show("TAP network interface was successfully created.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } else { File.Delete("TAP Driver.zip"); splash.Stop(); MessageBox.Show("TAP network interface failed to create.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
public static bool Uninstall() { string cfgKeyPath = @"SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"; string nameKeyPath = @"SYSTEM\CurrentControlSet\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}"; string tapList = ""; RegistryKey key = Registry.LocalMachine.OpenSubKey(cfgKeyPath); foreach (string name in key.GetSubKeyNames()) { try { if ((string)Registry.LocalMachine.OpenSubKey(cfgKeyPath + @"\" + name).GetValue("ComponentId") == componentID) { string netCfgInstanceId = (string)Registry.LocalMachine.OpenSubKey(cfgKeyPath + @"\" + name).GetValue("NetCfgInstanceId"); string friendlyName = (string)Registry.LocalMachine.OpenSubKey(nameKeyPath + @"\" + netCfgInstanceId + @"\Connection").GetValue("Name"); tapList += friendlyName + "\n"; } } catch { } } if (tapList == "") { MessageBox.Show("No TAP interface was found.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Information); return false; } DialogResult dialogResult = MessageBox.Show("This will remove the following network interfaces:\n\n" + tapList + "\nDo you want to remove all these TAP network interfaces?", "TAP Driver Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.No) return false; LoadingForm splash = new LoadingForm("Uninstalling TAP driver ..."); if (Setupapi.UninstallDevice(componentID)) { splash.Stop(); MessageBox.Show("Uninstallation was successful.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Information); return true; } else { splash.Stop(); MessageBox.Show("Uninstallation failed.", "TAP Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
public static bool PutDown() { if (handle == IntPtr.Zero) return true; LoadingForm splash = new LoadingForm("Disconnecting from \"" + FriendlyName + "\" ..."); uint bytesReturned = 0; IntPtr pBuffer = Marshal.AllocHGlobal(4); Marshal.Copy(BitConverter.GetBytes(0), 0, pBuffer, 4); bool deviceStatus = Kernel32.DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS, pBuffer, 4, pBuffer, 4, ref bytesReturned, IntPtr.Zero); Marshal.FreeHGlobal(pBuffer); Kernel32.CloseHandle(handle); handle = IntPtr.Zero; splash.Stop(); return deviceStatus; }
public static bool PutUp() { if (!Check()) return false; if (handle != IntPtr.Zero) return true; LoadingForm splash = new LoadingForm("Connecting to \"" + FriendlyName + "\" ..."); string duplicateName; if ((duplicateName = NetworkInterface.CheckIfIPv4Used(Global.Config.LoadBalancer.IPv4LocalAddresses[0].Address, Guid)) != null) { splash.Stop(); Global.WriteLog("TAP Interface: IP address " + Global.Config.LoadBalancer.IPv4LocalAddresses[0].Address + " already used by \"" + duplicateName + "\""); MessageBox.Show("\"" + FriendlyName + "\" can't use the IP address " + Global.Config.LoadBalancer.IPv4LocalAddresses[0].Address + " because it's already used by \"" + duplicateName + "\".\n\n Set a different IPv4 address in Control Panel>Tools>Load balancing>Advanced.", "TAP Interface", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } handle = Kernel32.CreateFile(@"\\.\Global\" + Guid + ".tap", Kernel32.FILE_READ_DATA | Kernel32.FILE_WRITE_DATA, Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, Kernel32.FILE_ATTRIBUTE_SYSTEM | Kernel32.FILE_FLAG_OVERLAPPED, IntPtr.Zero); if (handle == Kernel32.INVALID_HANDLE_VALUE) { uint errorCode = Kernel32.GetLastError(); splash.Stop(); Global.WriteLog("TAP Interface: failed to connect to " + FriendlyName + ": " + Kernel32.GetLastErrorMessage(errorCode)); MessageBox.Show("Failed to connect to " + FriendlyName + ":\n" + Kernel32.GetLastErrorMessage(errorCode), "TAP Interface", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } // set TAP status to connected uint bytesReturned = 0; IntPtr pStatus = Marshal.AllocHGlobal(4); Marshal.Copy(BitConverter.GetBytes(1), 0, pStatus, 4); bool deviceStatus = Kernel32.DeviceIoControl(handle, TAP_IOCTL_SET_MEDIA_STATUS, pStatus, 4, pStatus, 4, ref bytesReturned, IntPtr.Zero); Marshal.FreeHGlobal(pStatus); // get TAP MAC address bytesReturned = 0; IntPtr pMac = Marshal.AllocHGlobal(8); bool macRetrieved = Kernel32.DeviceIoControl(handle, TAP_IOCTL_GET_MAC, pMac, 12, pMac, 12, ref bytesReturned, IntPtr.Zero); byte[] mac = new byte[bytesReturned]; Marshal.Copy(pMac, mac, 0, (int)bytesReturned); Mac = BitConverter.ToString(mac).Replace('-', ':'); Marshal.FreeHGlobal(pMac); // configure TAP splash.UpdateStatus("Configuring " + FriendlyName + " ..."); List<Iphlpapi.Adapter> adapters = Iphlpapi.GetAdapters(Iphlpapi.FAMILY.AF_UNSPEC); Iphlpapi.Adapter tap = adapters.Find(i => i.Guid == Guid); if (tap == null) { splash.Stop(); Global.WriteLog("TAP Interface: couldn't find " + FriendlyName + " index"); MessageBox.Show("Couldn't find " + FriendlyName + " index.", "TAP Interface", MessageBoxButtons.OK, MessageBoxIcon.Error); PutDown(); return false; } Index = tap.InterfaceIndex; if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0) NetworkInterface.SetInterfaceMetric(Mac, "1"); else NetworkInterface.SetInterfaceMetric(Index, "1"); NetworkInterface.SetNetBios(Guid, false); NetworkInterface.ClearIPv4Addresses(Mac, Global.Config.LoadBalancer.IPv4LocalAddresses[0].Address, Global.Config.LoadBalancer.IPv4LocalAddresses[0].Subnet); NetworkInterface.ClearGateways(Index); NetworkInterface.ClearIPv4DnsServers(Mac); if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0) NetworkInterface.AddIPv4Gateway(Mac, Global.Config.LoadBalancer.IPv4GatewayAddresses[0].Address, Global.Config.LoadBalancer.IPv4GatewayAddresses[0].GatewayMetric.ToString()); else NetworkInterface.AddIPv4Gateway(Index, Global.Config.LoadBalancer.IPv4GatewayAddresses[0].Address, Global.Config.LoadBalancer.IPv4GatewayAddresses[0].GatewayMetric.ToString()); NetworkInterface.SetIPv4DnsServer(FriendlyName, Global.Config.LoadBalancer.IPv4DnsAddresses[0]); splash.Stop(); if (handle != IntPtr.Zero && handle != Kernel32.INVALID_HANDLE_VALUE && deviceStatus && macRetrieved) return true; else return false; }
/// <summary> /// Makes sure that the "NetGroup Packet Filter" service status is "Running" and that all specified interfaces are captured. /// </summary> /// <param name="requiredNics">If this is null it only alerts the user about any non-captured interfaces and returns true</param> /// <returns></returns> public static bool RunWinPcapService(IEnumerable<NetworkInterface> requiredNics = null, bool verbose = false) { LoadingForm splash = null; if (verbose) splash = new LoadingForm("Searching \"NetGroup Packet Filter\" service ..."); if (ServiceController.GetDevices().Where(i => i.ServiceName == "NPF").Count() == 0) { if (verbose) splash.Stop(); MessageBox.Show("\"NetGroup Packet Filter\" service was not installed by WinPcap.", "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } using (ServiceController service = new ServiceController("NPF")) { bool isCapturingOptional = false; if (requiredNics == null) { requiredNics = Global.NetworkInterfaces.Values.ToList(); isCapturingOptional = true; } if (service.Status != ServiceControllerStatus.Running) { if (verbose) splash.UpdateStatus("Starting \"NetGroup Packet Filter\" service ..."); if (!RestartNpfService()) { if (verbose) splash.Stop(); return false; } } try { service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(0)); } // update status catch { } if (service.Status == ServiceControllerStatus.Running) { // get captured devices if (verbose) splash.UpdateStatus("Getting devices captured by WinPcap ..."); WinPcapDevices = GetWinPcapDevs(); List<NetworkInterface> nonCapturedNics = requiredNics.Where(i => WinPcapDevices.Find(j => j.Contains(i.Guid)) == null).ToList(); List<NetworkInterface> capturedNics = requiredNics.Where(i => WinPcapDevices.Find(j => j.Contains(i.Guid)) != null).ToList(); List<NetworkInterface> supportedNonCapturedNics = nonCapturedNics.Where(i => i.Type != NetworkInterface.AdapterType.Ppp && i.Type != NetworkInterface.AdapterType.Wwanpp && i.Type != NetworkInterface.AdapterType.Wwanpp2).ToList(); // if there are NICs not captured by WinPcap that are supported if (supportedNonCapturedNics.Count > 0) { if (verbose) splash.UpdateStatus("Restarting \"NetGroup Packet Filter\" service ..."); // if WinPcap is not in use if (Dependencies.WinPcapInUse.CurrentCount == 0) if (!RestartNpfService()) { if (verbose) splash.Stop(); return false; } // get captured devices again if (verbose) splash.UpdateStatus("Recheck devices captured by WinPcap ..."); WinPcapDevices = GetWinPcapDevs(); nonCapturedNics = requiredNics.Where(i => WinPcapDevices.Find(j => j.Contains(i.Guid)) == null).ToList(); capturedNics = requiredNics.Where(i => WinPcapDevices.Find(j => j.Contains(i.Guid)) != null).ToList(); supportedNonCapturedNics = nonCapturedNics.Where(i => i.Type != NetworkInterface.AdapterType.Ppp && i.Type != NetworkInterface.AdapterType.Wwanpp && i.Type != NetworkInterface.AdapterType.Wwanpp2).ToList(); } // gat captured devices names list string nonCapturedNicsNames = null; string capturedNicsNames = null; string supportedNonCapturedNicsNames = null; foreach (NetworkInterface nic in nonCapturedNics) nonCapturedNicsNames += nic.Name + "\n"; foreach (NetworkInterface nic in capturedNics) capturedNicsNames += nic.Name + "\n"; foreach (NetworkInterface nic in supportedNonCapturedNics) supportedNonCapturedNicsNames += nic.Name + "\n"; if (verbose) splash.Stop(); // if there are NICs still not captured by WinPcap if (nonCapturedNics.Count > 0) { // if some non-captured NICs are still supported by WinPcap if (supportedNonCapturedNics.Count > 0) { if (isCapturingOptional) { DialogResult result = MessageBox.Show("The following interfaces are not captured by WinPcap:\n\n" + nonCapturedNicsNames + "\n\n" + "Only the following interfaces are captured by WinPcap:\n\n" + capturedNicsNames + "\n\n" + "Do you want to continue?", "WinPcap Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) return true; else return false; } else { MessageBox.Show("The following interfaces are not captured by WinPcap and can not continue:\n\n" + nonCapturedNicsNames + "\n\n" + "Only the following interfaces are captured by WinPcap:\n\n" + capturedNicsNames, "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else { if (isCapturingOptional) { DialogResult result = MessageBox.Show("The following interfaces are not captured by WinPcap because WinPcap does not support PPP related interfaces:\n\n" + nonCapturedNicsNames + "\n\n" + "Only the following interfaces are captured by WinPcap:\n\n" + capturedNicsNames + "\n\n" + "Do you want to continue?", "WinPcap Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) return true; else return false; } else { MessageBox.Show("The following interfaces are not captured by WinPcap because WinPcap does not support PPP related interfaces, and can not continue:\n\n" + nonCapturedNicsNames + "\n\n" + "Only the following interfaces are captured by WinPcap:\n\n" + capturedNicsNames, "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } // all NICs are captured return true; } // should never be reached MessageBox.Show("\"NetGroup Packet Filter\" service is not running.", "WinPcap Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
private void button5_Click(object sender, EventArgs e) { LoadingForm splash = new LoadingForm("Initializing ..."); Global.Config.LoadBalancer.ExcludedInterfacesForWindows.Clear(); List<NetworkInterface> loadBalancingInterfaces = new List<NetworkInterface>(); foreach (Control control in windowsInterfaces.Controls) if (!((CheckBox)control).Checked) Global.Config.LoadBalancer.ExcludedInterfacesForWindows.Add((string)((CheckBox)control).Tag); else loadBalancingInterfaces.Add(Global.NetworkInterfaces[(string)((CheckBox)control).Tag]); Global.Config.Save(); // Configure high metrics on interfaces that are not used foreach (NetworkInterface nic in Global.NetworkInterfaces.Values) if (!loadBalancingInterfaces.Any(i => i.Guid == nic.Guid) && (nic.IPv4Gateway.Count > 0 || nic.IPv6Gateway.Count > 0)) { splash.UpdateStatus("Configuring " + nic.Name + " ..."); nic.SetInterfaceMetric("4000"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) nic.EditIPv4Gateway(ip.Address, "4000"); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) nic.EditIPv6Gateway(ip.Address, "4000"); } // Configure the used interfaces with the specified metrics foreach (NetworkInterface nic in loadBalancingInterfaces) { splash.UpdateStatus("Configuring " + nic.Name + " ..."); nic.SetInterfaceMetric(textBox1.Text); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) nic.EditIPv4Gateway(ip.Address, textBox2.Text); foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) nic.EditIPv6Gateway(ip.Address, textBox2.Text); } splash.Stop(); Program.Refresh(); }
private void buttonApply_Click(object sender, EventArgs e) { if (!ValidateConfigs()) return; // validation passed; starting configuring Hide(); GadgetForm.AutoRefreshAllowed = false; splash = new LoadingForm("Configuring interface \"" + nic.Name + "\" ..."); // XP sets gateway metric along with interface metric, if gwmetric is auto, so we do this first if (interfaceMetric.Text != nic.InterfaceMetric.ToString()) nic.SetInterfaceMetric(interfaceMetric.Text); if (nic.IPv4Enabled) { if (dhcpIPEnabled.Checked || ipv4Address.Count == 0) nic.SetDhcp(1 + Convert.ToInt32(dhcpDnsEnabled.Checked || ipv4Dns.Count == 0)); else { if (nic.Dhcpv4Enabled > 0) nic.SetIPv4Address(ipv4Address[0].Address, ipv4Address[0].Subnet); if (ipv4Address.Count > 0) { foreach (NetworkInterface.IPHostAddress ip in ipv4Address) { if (nic.IPv4Address.Find((i) => i.Address == ip.Address) != null && nic.IPv4Address.Find((i) => i.Address == ip.Address).Subnet == ip.Subnet && nic.Dhcpv4Enabled == 0) continue; if (nic.IPv4Address.Find((i) => i.Address == ip.Address) != null || nic.Dhcpv4Enabled > 0) nic.DeleteIPv4Address(ip.Address, ip.Subnet); nic.AddIPv4Address(ip.Address, ip.Subnet); } foreach (NetworkInterface.IPHostAddress ip in nic.IPv4Address) if (ipv4Address.Find((i) => i.Address == ip.Address) == null) nic.DeleteIPv4Address(ip.Address, ip.Subnet); foreach (NetworkInterface.IPGatewayAddress ip in ipv4Gateway) { if (nic.IPv4Gateway.Find((i) => i.Address == ip.Address) != null && nic.IPv4Gateway.Find((i) => i.Address == ip.Address).GatewayMetric == ip.GatewayMetric && nic.Dhcpv4Enabled == 0) continue; if (nic.IPv4Gateway.Find((i) => i.Address == ip.Address) != null || nic.Dhcpv4Enabled > 0) nic.DeleteIPv4Gateway(ip.Address); nic.AddIPv4Gateway(ip.Address, ip.GatewayMetric.ToString()); } foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv4Gateway) if (ipv4Gateway.Find((i) => i.Address == ip.Address) == null) nic.DeleteIPv4Gateway(ip.Address); } } foreach (string dns in ipv4Dns) { if (nic.IPv4DnsServer.Contains(dns) && nic.Dhcpv4Enabled < NetworkInterface.Dhcp.IPnDns) continue; nic.AddIPv4DnsServer(dns); } foreach (string dns in nic.IPv4DnsServer) if (!ipv4Dns.Contains(dns)) nic.DeleteIPv4DnsServer(dns); if (netbiosEnabled.Checked == false && nic.NetbiosEnabled != NetworkInterface.Netbios.Disabled) nic.SetNetBios(false); if (netbiosEnabled.Checked && nic.NetbiosEnabled == NetworkInterface.Netbios.Disabled) nic.SetNetBios(true); if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1) if (ipv4Mtu.Text != nic.IPv4Mtu) nic.SetIPv4Mtu(ipv4Mtu.Text); } if (nic.IPv6Enabled) { foreach (NetworkInterface.IPHostAddress ip in ipv6Address) { if (nic.IPv6Address.Contains(ip.Address, ip.Subnet)) continue; nic.AddIPv6Address(ip.Address, ip.Subnet); } foreach (NetworkInterface.IPHostAddress ip in nic.IPv6Address.All) if (ipv6Address.Find((i) => i.Address == ip.Address) == null) nic.DeleteIPv6Address(ip.Address); foreach (NetworkInterface.IPGatewayAddress ip in ipv6Gateway) { if (nic.IPv6Gateway.Contains(new NetworkInterface.IPGatewayAddress(ip.Address, ip.GatewayMetric))) continue; nic.AddIPv6Gateway(ip.Address, ip.GatewayMetric.ToString()); } foreach (NetworkInterface.IPGatewayAddress ip in nic.IPv6Gateway) if (ipv6Gateway.Find((i) => i.Address == ip.Address) == null) nic.DeleteIPv6Gateway(ip.Address); foreach (string ip in ipv6Dns) { if (nic.IPv6DnsServer.Contains(ip)) continue; nic.AddIPv6DnsServer(ip); } foreach (string ip in nic.IPv6DnsServer) if (!ipv6Dns.Contains(ip)) nic.DeleteIPv6DnsServer(ip); if (routerDiscoveryEnabled.Checked != nic.IPv6RouterDiscoveryEnabled) nic.SetRouterDiscovery(routerDiscoveryEnabled.Checked); if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1) if (ipv6Mtu.Text != nic.IPv6Mtu) nic.SetIPv6Mtu(ipv6Mtu.Text); } splash.UpdateStatus("Waiting for changes to be applied ..."); timer.AutoReset = false; timer.Interval = 10000; timer.Elapsed += timer_Elapsed; timer.Start(); System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged; }