/// <summary>Stop the DHCP server.</summary> public void stop() { dhcpsvr.Dispose(); state = STATE_TERMINATED; pool.stop(); sendNotify(DhcpNotify.NotifyType.CONTROL, "RTDhcpServer is terminated."); }
private void CleanupAndRetry() { lock (m_Lock) { if (!m_Disposed) { // stop server if (m_Server != null) { m_Server.OnStatusChange -= server_OnStatusChange; m_Server.OnTrace -= server_OnTrace; m_Server.Dispose(); m_Server = null; } // initiate retry timer m_RetryTimer.Change(RetryTime, Timeout.Infinite); } } }
static void Main(string[] args) { var server = new DHCPServer(); server.ServerName = "SharpDHCPServer"; server.OnDataReceived += Request; Console.WriteLine("Running DHCP server. Press enter to stop it."); Console.ReadLine(); server.Dispose(); }
static void Main(string[] args) { var server = new DHCPServer(); server.ServerName = "SharpDHCPServer"; server.OnDataReceived += Request; server.BroadcastAddress = IPAddress.Parse("192.168.1.255"); server.Start(); Console.WriteLine("Running DHCP server. Press enter to stop it."); Console.ReadLine(); server.Dispose(); }
static void Main(string[] args) { var lst = NetworkInterface.GetAllNetworkInterfaces(); var eth0If = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(x => x.Name == "USB_ETH"); var server = new DHCPServer(); server.ServerName = "SharpDHCPServer"; server.OnDataReceived += Request; server.BroadcastAddress = IPAddress.Broadcast; server.SendDhcpAnswerNetworkInterface = eth0If; server.Start(); Console.WriteLine("Running DHCP server. Press enter to stop it."); Console.ReadLine(); server.Dispose(); }
//Based on ClusterM's Sharp-DHCP-Server //Credit and thanks goes to ClusterM, and jogibear9988 public void Run(IBackgroundTaskInstance taskInstance) { var server = new DHCPServer(new System.Net.IPAddress(new byte[] { 192, 168, 137, 1 })); server.ServerName = "sharpDHCPWifiToEthernet"; server.BroadcastAddress = IPAddress.Broadcast.ToString(); BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); taskInstance.Canceled += (o, e) => { server.Dispose(); deferral.Complete(); }; server.OnDataReceived += (sender, dhcpRequest) => { try { var type = dhcpRequest.GetMsgType(); var ip = new System.Net.IPAddress(new byte[] { 192, 168, 137, 11 }); var replyOptions = new DHCPReplyOptions(); replyOptions.SubnetMask = IPAddress.Parse("255.255.255.0"); replyOptions.DomainName = server.ServerName; replyOptions.ServerIdentifier = IPAddress.Parse("192.168.137.1"); replyOptions.RouterIP = IPAddress.Parse("192.168.137.1"); replyOptions.DomainNameServers = new IPAddress[] { IPAddress.Parse("8.8.8.8"), IPAddress.Parse("8.8.4.4") }; if (type == DHCPMsgType.DHCPDISCOVER) { dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPOFFER, ip, replyOptions); } if (type == DHCPMsgType.DHCPREQUEST) { dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, ip, replyOptions); } } catch { } }; server.Start(); }