-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
118 lines (102 loc) · 4.42 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System;
using System.Diagnostics;
using System.Configuration;
using WinDHCP.Library.Configuration;
using WinDHCP.Library;
using NetworkInterface = System.Net.NetworkInformation.NetworkInterface;
namespace WinDHCP
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(String[] args)
{
DhcpServerConfigurationSection dhcpConfig = ConfigurationManager.GetSection("dhcpServer") as DhcpServerConfigurationSection;
DhcpServer server = new DhcpServer();
if (dhcpConfig != null)
{
if (dhcpConfig.NetworkInterface >= 0)
{
server.DhcpInterface = 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;
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.Reservations.Add(PhysicalAddress.Parse(macReservation.PhysicalAddress), InternetAddress.Parse(macReservation.IPAddress));
}
}
if (args.Length > 0 && (ContainsSwitch(args, "console") || ContainsSwitch(args, "debug")))
{
if (ContainsSwitch(args, "debug"))
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
}
DhcpHost host = new DhcpHost(server);
host.ManualStart(args);
Console.WriteLine("DHCP Service Running.");
Console.WriteLine("Hit [Enter] to Terminate.");
Console.ReadLine();
host.ManualStop();
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new DhcpHost(server) };
ServiceBase.Run(ServicesToRun);
}
}
private static Boolean ContainsSwitch(String[] args, String switchStr)
{
foreach (String arg in args)
{
if (arg.StartsWith("--") && arg.Length > 2 && switchStr.StartsWith(arg.Substring(2), StringComparison.OrdinalIgnoreCase) ||
(arg.StartsWith("/") || arg.StartsWith("-")) && arg.Length > 1 && switchStr.StartsWith(arg.Substring(1), StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
}
}