static void Main(string[] args) { var logger = new ConsoleLogger(); DhcpServer server; try { var serverSettings = new DhcpServerSettings { ServerIp = IPAddress.Parse(args[0]) }; server = new DhcpServer(serverSettings); logger.AddSource(server); server.Discovered += ShowMessage; server.Requested += ShowMessage; server.Start(); while (true) { ; } } catch (Exception e) { Console.WriteLine(e.Message); } }
public bool setIP(string ip) { Trace.TraceInformation("Interface IP Changing"); for (int i = 0; i < 5; i++) { if (IcsManager.setIpWMI(connectionGuid, ip, "255.255.255.0")) { Trace.TraceInformation("Interface IP Changed"); break; } else { Trace.TraceInformation("Interface IP Change failed"); } Thread.Sleep(2000); } string trimmedIp = ip.TrimEnd("0123456789".ToCharArray()); InternetAddress privateAddress = InternetAddress.Parse(ip); dhcpManager.DhcpInterfaceAddress = IPAddress.Parse(ip); dhcpManager.StartAddress = InternetAddress.Parse(trimmedIp + "1"); dhcpManager.EndAddress = InternetAddress.Parse(trimmedIp + "255"); dhcpManager.Subnet = InternetAddress.Parse("255.255.255.0"); dhcpManager.Gateway = privateAddress; dhcpManager.DnsServers.Add(privateAddress); for (int i = 1; i < 5; i++) { try { Socket sock = dhcpManager.configureSocket(); sock.Close(); dhcpManager.Start(); return(true); } catch (SocketException e) { if (e.ErrorCode == 10049) { Trace.TraceInformation("Interface IP waiting for ready."); dhcpManager.Stop(); Thread.Sleep(2000); } } } return(false); }
static void Main(string[] args) { DhcpServerConfigurationSection dhcpConfig = ConfigurationManager.GetSection("dhcpServer") as DhcpServerConfigurationSection; DhcpServer server = new DhcpServer(); if (dhcpConfig != null) { if (dhcpConfig != null) { if (dhcpConfig.NetworkInterface >= 0) { server.DhcpInterface = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[dhcpConfig.NetworkInterface]; } server.StartAddress = InternetAddress.Parse(dhcpConfig.StartAddress.Trim()); server.EndAddress = InternetAddress.Parse(dhcpConfig.EndAddress.Trim()); server.Subnet = InternetAddress.Parse(dhcpConfig.Subnet.Trim()); server.Gateway = InternetAddress.Parse(dhcpConfig.Gateway.Trim()); server.LeaseDuration = dhcpConfig.LeaseDuration; server.OfferTimeout = dhcpConfig.OfferTimeout; server.DnsSuffix = dhcpConfig.DnsSuffix; server.StartAddress = InternetAddress.Parse("2.0.4.1"); server.EndAddress = InternetAddress.Parse("2.0.4.10"); server.Subnet = InternetAddress.Parse("255.255.255.0"); foreach (InternetAddressElement dnsServer in dhcpConfig.DnsServers) { server.DnsServers.Add(InternetAddress.Parse(dnsServer.IPAddress.Trim())); } foreach (PhysicalAddressElement macAllow in dhcpConfig.MacAllowList) { if (macAllow.PhysicalAddress.Trim() == "*") { server.ClearAcls(); server.AllowAny = true; break; } else { server.AddAcl(PhysicalAddress.Parse(macAllow.PhysicalAddress), false); } } foreach (PhysicalAddressElement macDeny in dhcpConfig.MacDenyList) { if (macDeny.PhysicalAddress.Trim() == "*") { server.ClearAcls(); server.AllowAny = false; break; } else { server.AddAcl(PhysicalAddress.Parse(macDeny.PhysicalAddress), true); } } foreach (PhysicalAddressMappingElement macReservation in dhcpConfig.MacReservationList) { server.addReservation(macReservation.PhysicalAddress, macReservation.IPAddress); } } server.Start(); Console.WriteLine("DHCP Service Running."); Console.WriteLine("Hit [Enter] to Terminate."); Console.ReadLine(); } }