private void NewDibblerServerProcess() { Debug.Assert(DhcpServiceProcess == null); // Generate the DHCPv6 server config string DaemonDirectory = Path.Combine(Program.MyProgramDir, "dibbler_server"); Directory.CreateDirectory(DaemonDirectory); // Won't fail if the directory already exists, or will create a new one string DibblerServerConfigPath = Path.Combine(DaemonDirectory, "server.conf"); using (FileStream ConfigFile = File.OpenWrite(DibblerServerConfigPath)) { Byte[] content = new UTF8Encoding(true).GetBytes(RenderDibblerServerConfig()); ConfigFile.Write(content, 0, content.Length); ConfigFile.SetLength(content.Length); } // Run the DHCPv6 process DhcpServiceProcess = new Process { StartInfo = new ProcessStartInfo { FileName = Path.Combine(Program.MyProgramDir, "dibbler-server.exe"), Arguments = "run", WorkingDirectory = DaemonDirectory } }; DhcpServiceProcess.Start(); // Add routes foreach (var ServedInterface in Configuration.ServedInterfaceList) { using (PowerShell PowerShellInstance = PowerShell.Create()) { var cmd = PowerShellInstance.AddScript("param([String] $prefix, [int] $iface, [String] $gw) New-NetRoute -DestinationPrefix $prefix -InterfaceIndex $iface -AddressFamily IPv6 -NextHop $gw -Publish Yes -PolicyStore ActiveStore -Confirm:$false"); PowerShellInstance.AddParameter("prefix", GetServedSubnet(ServedInterface.NetworkId).ToString() + "/112"); PowerShellInstance.AddParameter("iface", ServedInterface.Index); PowerShellInstance.AddParameter("gw", "::"); PowerShellInstance.Invoke(); foreach (var PowerShellError in PowerShellInstance.Streams.Error.ReadAll()) { EventLog.WriteEntry(Program.MyName, string.Format("Error(s) occured while adding a route. More information: {0}", PowerShellError.ToString()), EventLogEntryType.Error); } } } }
private void KillDibblerServerProcess() { Debug.Assert(DhcpServiceProcess != null); // Remove routes foreach (var ServedInterface in Configuration.ServedInterfaceList) { using (PowerShell PowerShellInstance = PowerShell.Create()) { PowerShellInstance.AddScript("param([String] $prefix, [int] $iface, [String] $gw) Remove-NetRoute -DestinationPrefix @($prefix) -InterfaceIndex $iface -AddressFamily IPv6 -NextHop @($gw) -Publish Yes -Confirm:$false"); PowerShellInstance.AddParameter("prefix", GetServedSubnet(ServedInterface.NetworkId).ToString() + "/112"); PowerShellInstance.AddParameter("iface", ServedInterface.Index); PowerShellInstance.AddParameter("gw", "::"); PowerShellInstance.Invoke(); foreach (var PowerShellError in PowerShellInstance.Streams.Error.ReadAll()) { EventLog.WriteEntry(Program.MyName, string.Format("Error(s) occured while removing the route. More information: {0}", PowerShellError.ToString()), EventLogEntryType.Error); } } } // Kill the DHCPv6 process try { DhcpServiceProcess.Kill(); } catch (System.InvalidOperationException) { EventLog.WriteEntry(Program.MyName, "Could not kill dibbler-server (DHCPv6 daemon). This might cause problems. Please stop the service, kill the rest of lingering daemons and restart the service again.", EventLogEntryType.Warning); } finally { DhcpServiceProcess = null; } }