Ejemplo n.º 1
0
            public static bool PutUp()
            {
                if (!Check())
                {
                    return(false);
                }
                if (handle != IntPtr.Zero)
                {
                    return(true);
                }
                LoadingForm splash = LoadingForm.Create("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);
                }
            }