コード例 #1
0
ファイル: UdpDetector.cs プロジェクト: ddonny/Network-Manager
 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;
 }
コード例 #2
0
 /// <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();
 }
コード例 #3
0
 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;
 }
コード例 #4
0
 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;
 }
コード例 #5
0
 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;
     }
 }
コード例 #6
0
 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;
 }
コード例 #7
0
 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();
 }
コード例 #8
0
 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;
 }