WIP
parent
7304747952
commit
a002c4b0a1
|
@ -3,6 +3,13 @@ using ln.types.net;
|
|||
using ln.types.odb.attributes;
|
||||
namespace ln.dhcp
|
||||
{
|
||||
public enum DHCPLeaseState
|
||||
{
|
||||
RELEASED,
|
||||
OFFERED,
|
||||
BOUND,
|
||||
DECLINED
|
||||
}
|
||||
public class DHCPLease
|
||||
{
|
||||
[DocumentID]
|
||||
|
@ -19,6 +26,8 @@ namespace ln.dhcp
|
|||
public DateTimeOffset ValidThrough { get; set; }
|
||||
public DateTimeOffset Released { get; set; }
|
||||
|
||||
public DHCPLeaseState LeaseState { get; set; } = DHCPLeaseState.OFFERED;
|
||||
|
||||
private DHCPLease()
|
||||
{}
|
||||
|
||||
|
|
|
@ -19,17 +19,17 @@ namespace ln.dhcp
|
|||
|
||||
public Pool Pool { get; private set; } = new Pool(8);
|
||||
|
||||
ODBCollection<DHCPServerInterface> interfaces;
|
||||
public DHCPServerInterface[] Interfaces => interfaces.ToArray();
|
||||
public ODBCollection<DHCPServerInterface> serverInterfacesCollection;
|
||||
public DHCPServerInterface[] Interfaces => serverInterfacesCollection.ToArray();
|
||||
|
||||
ODBCollection<IPPool> ippools;
|
||||
public IPPool[] IPPools => ippools.ToArray();
|
||||
public ODBCollection<IPPool> ippoolsCollection;
|
||||
public IPPool[] IPPools => ippoolsCollection.ToArray();
|
||||
|
||||
ODBCollection<Option> defaultOptions;
|
||||
public Option[] DefaultOptions => defaultOptions.ToArray();
|
||||
public ODBCollection<Option> defaultOptionsCollection;
|
||||
public Option[] DefaultOptions => defaultOptionsCollection.ToArray();
|
||||
|
||||
ODBCollection<DHCPLease> leases;
|
||||
public DHCPLease[] Leases => leases.ToArray();
|
||||
public ODBCollection<DHCPLease> leasesCollection;
|
||||
public DHCPLease[] Leases => leasesCollection.ToArray();
|
||||
|
||||
Dictionary<DHCPServerInterface, DHCPListener> listeners = new Dictionary<DHCPServerInterface, DHCPListener>();
|
||||
|
||||
|
@ -52,10 +52,10 @@ namespace ln.dhcp
|
|||
logLease = new FileLogger(Path.Combine(StoragePath, "lease.log"));
|
||||
|
||||
odb = new ODB(StoragePath);
|
||||
defaultOptions = odb.GetCollection<Option>();
|
||||
ippools = odb.GetCollection<IPPool>();
|
||||
interfaces = odb.GetCollection<DHCPServerInterface>();
|
||||
leases = odb.GetCollection<DHCPLease>();
|
||||
defaultOptionsCollection = odb.GetCollection<Option>();
|
||||
ippoolsCollection = odb.GetCollection<IPPool>();
|
||||
serverInterfacesCollection = odb.GetCollection<DHCPServerInterface>();
|
||||
leasesCollection = odb.GetCollection<DHCPLease>();
|
||||
|
||||
leasesAPI = new LeasesAPI(this);
|
||||
}
|
||||
|
@ -64,9 +64,9 @@ namespace ln.dhcp
|
|||
{
|
||||
lock (this)
|
||||
{
|
||||
if (!IsStarted && interfaces.Count > 0)
|
||||
if (!IsStarted && serverInterfacesCollection.Count > 0)
|
||||
{
|
||||
foreach (DHCPServerInterface serverInterface in interfaces)
|
||||
foreach (DHCPServerInterface serverInterface in serverInterfacesCollection)
|
||||
StartInterface(serverInterface);
|
||||
|
||||
IsStarted = true;
|
||||
|
@ -77,7 +77,7 @@ namespace ln.dhcp
|
|||
{
|
||||
lock (this)
|
||||
{
|
||||
foreach (DHCPServerInterface serverInterface in interfaces)
|
||||
foreach (DHCPServerInterface serverInterface in serverInterfacesCollection)
|
||||
StartInterface(serverInterface);
|
||||
|
||||
IsStarted = false;
|
||||
|
@ -86,7 +86,7 @@ namespace ln.dhcp
|
|||
|
||||
public void EnsureInterface(IPv4 interfaceAddr)
|
||||
{
|
||||
foreach (DHCPServerInterface serverInterface in interfaces)
|
||||
foreach (DHCPServerInterface serverInterface in serverInterfacesCollection)
|
||||
if (serverInterface.InterfaceAddress.Equals(interfaceAddr))
|
||||
return;
|
||||
|
||||
|
@ -152,7 +152,7 @@ namespace ln.dhcp
|
|||
DHCPServerInterface serverInterface = new DHCPServerInterface(ip);
|
||||
lock (this)
|
||||
{
|
||||
if (!interfaces.Insert(serverInterface))
|
||||
if (!serverInterfacesCollection.Insert(serverInterface))
|
||||
throw new ArgumentException(nameof(ip),String.Format("DHCPServer already has interface for IP {0}",ip));
|
||||
if (IsStarted)
|
||||
StartInterface(serverInterface);
|
||||
|
@ -162,7 +162,7 @@ namespace ln.dhcp
|
|||
{
|
||||
lock (this)
|
||||
{
|
||||
if (!ippools.Insert(pool))
|
||||
if (!ippoolsCollection.Insert(pool))
|
||||
throw new ArgumentException(nameof(pool), String.Format("DHCPServer could not insert new pool {0}", pool));
|
||||
}
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ namespace ln.dhcp
|
|||
|
||||
private void CollectDefaultOptions(DHCPServerInterface serverInterface,BootPPacket packet)
|
||||
{
|
||||
foreach (Option defaultOption in defaultOptions)
|
||||
foreach (Option defaultOption in defaultOptionsCollection)
|
||||
packet.SetOption(defaultOption);
|
||||
|
||||
foreach (Option defaultOption in serverInterface.DefaultOptions)
|
||||
|
@ -226,9 +226,11 @@ namespace ln.dhcp
|
|||
if (lease.ValidThrough < reservationTimeout)
|
||||
{
|
||||
lease.ValidThrough = reservationTimeout;
|
||||
lease.LeaseState = DHCPLeaseState.OFFERED;
|
||||
Commit(lease);
|
||||
logLease.Log(LogLevel.INFO, "[OFFERED] {0} {1} {2}", lease.ClientMAC, lease.ClientIP, lease.ClientName);
|
||||
}
|
||||
|
||||
SendOffer(serverInterface, lease, packet);
|
||||
}
|
||||
}
|
||||
|
@ -284,9 +286,11 @@ namespace ln.dhcp
|
|||
if (lease.ValidThrough < validThrough)
|
||||
{
|
||||
lease.ValidThrough = validThrough;
|
||||
Commit(lease);
|
||||
}
|
||||
lease.LeaseState = DHCPLeaseState.BOUND;
|
||||
Commit(lease);
|
||||
logLease.Log(LogLevel.INFO, "[ACK ] {0} {1} {2}", lease.ClientMAC, lease.ClientIP, lease.ClientName);
|
||||
|
||||
SendACK(serverInterface, lease, packet);
|
||||
}
|
||||
else
|
||||
|
@ -297,9 +301,23 @@ namespace ln.dhcp
|
|||
}
|
||||
private void DHCPDecline(DHCPServerInterface serverInterface, BootPPacket packet)
|
||||
{
|
||||
DHCPLease lease = leasesAPI.FindLease(serverInterface.InterfaceAddress, packet.CHAddr);
|
||||
if (lease != null)
|
||||
{
|
||||
lease.Released = DateTimeOffset.Now;
|
||||
lease.LeaseState = DHCPLeaseState.DECLINED;
|
||||
Commit(lease);
|
||||
}
|
||||
}
|
||||
private void DHCPRelease(DHCPServerInterface serverInterface, BootPPacket packet)
|
||||
{
|
||||
DHCPLease lease = leasesAPI.FindLease(serverInterface.InterfaceAddress, packet.CHAddr);
|
||||
if (lease != null)
|
||||
{
|
||||
lease.Released = DateTimeOffset.Now;
|
||||
lease.LeaseState = DHCPLeaseState.RELEASED;
|
||||
Commit(lease);
|
||||
}
|
||||
}
|
||||
|
||||
private IPv4 AcquireIP(IPPool ippool)
|
||||
|
@ -318,9 +336,9 @@ namespace ln.dhcp
|
|||
}
|
||||
}
|
||||
|
||||
public bool Commit(IPPool ippool) => ippools.Upsert(ippool);
|
||||
public bool Commit(DHCPServerInterface serverInterface) => interfaces.Upsert(serverInterface);
|
||||
public bool Commit(DHCPLease lease) => leases.Upsert(lease);
|
||||
public bool Commit(IPPool ippool) => ippoolsCollection.Upsert(ippool);
|
||||
public bool Commit(DHCPServerInterface serverInterface) => serverInterfacesCollection.Upsert(serverInterface);
|
||||
public bool Commit(DHCPLease lease) => leasesCollection.Upsert(lease);
|
||||
|
||||
public class LeasesAPI
|
||||
{
|
||||
|
@ -340,7 +358,7 @@ namespace ln.dhcp
|
|||
foreach (DHCPLease lease in dhcp.Leases)
|
||||
{
|
||||
if ((lease.ValidThrough + acceptedOverdue) < now)
|
||||
dhcp.leases.Delete(lease);
|
||||
dhcp.leasesCollection.Delete(lease);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -353,7 +371,7 @@ namespace ln.dhcp
|
|||
Query.Equals<DHCPLease>("ClientMAC", mac),
|
||||
Query.Equals<DHCPLease>("ServerInterface", serverInterface)
|
||||
);
|
||||
DHCPLease lease = dhcp.leases.Query(queryLease).FirstOrDefault();
|
||||
DHCPLease lease = dhcp.leasesCollection.Query(queryLease).FirstOrDefault();
|
||||
return lease;
|
||||
}
|
||||
}
|
||||
|
@ -361,7 +379,7 @@ namespace ln.dhcp
|
|||
{
|
||||
lock (this)
|
||||
{
|
||||
DHCPLease lease = dhcp.leases.Query(Query.Equals<DHCPLease>("ClientIP", ip)).FirstOrDefault();
|
||||
DHCPLease lease = dhcp.leasesCollection.Query(Query.Equals<DHCPLease>("ClientIP", ip)).FirstOrDefault();
|
||||
return lease;
|
||||
}
|
||||
}
|
||||
|
@ -378,7 +396,7 @@ namespace ln.dhcp
|
|||
{
|
||||
lock (this)
|
||||
{
|
||||
dhcp.leases.Delete(lease);
|
||||
dhcp.leasesCollection.Delete(lease);
|
||||
dhcp.logLease.Log(LogLevel.INFO, "[REMOVE] {0} {1} {2}", lease.ClientMAC, lease.ClientIP, lease.ClientName);
|
||||
}
|
||||
}
|
||||
|
|
24
Option.cs
24
Option.cs
|
@ -57,7 +57,7 @@ namespace ln.dhcp
|
|||
byte[] slice = bytes.Slice(offset, len);
|
||||
offset += len;
|
||||
|
||||
if (factories.ContainsKey(bytes[offset]))
|
||||
if (factories.ContainsKey(tag))
|
||||
return factories[tag](tag, slice);
|
||||
else
|
||||
return new Option(tag, slice);
|
||||
|
@ -85,7 +85,7 @@ namespace ln.dhcp
|
|||
AddFactory(0x0C, (tag, bytes) => new TextOption(tag, bytes)); // Host Name
|
||||
AddFactory(0x0F, (tag, bytes) => new TextOption(tag, bytes)); // Domain Name
|
||||
|
||||
AddFactory(0x32, (tag, bytes) => new IPv4Option(tag, bytes)); // Requested IP
|
||||
AddFactory(0x32, (tag, bytes) => new DHCPRequestedIPOption(bytes)); // Requested IP
|
||||
AddFactory(0x33, (tag, bytes) => new DHCPLeaseTimeOption(bytes));// Lease Time
|
||||
|
||||
AddFactory(0x35, (tag, bytes) => new DHCPMessageTypeOption(bytes[0])); // DHCP Message Type
|
||||
|
@ -190,6 +190,11 @@ namespace ln.dhcp
|
|||
public DHCPRequestedIPOption(IPv4 ip)
|
||||
: base(0x32, ip)
|
||||
{ }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("DHCPRequestedIP={0}",IP);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -206,6 +211,11 @@ namespace ln.dhcp
|
|||
public DHCPServerIdentifierOption(IPv4 ip)
|
||||
: base(0x36, ip)
|
||||
{ }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("DHCPServerIdentifier={0}",IP);
|
||||
}
|
||||
}
|
||||
|
||||
public class TextOption : Option
|
||||
|
@ -267,6 +277,11 @@ namespace ln.dhcp
|
|||
{
|
||||
Integer = (int)(validThrough - DateTimeOffset.Now).TotalSeconds;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("DHCPLeaseTime={0}s",Integer);
|
||||
}
|
||||
}
|
||||
|
||||
public class DHCPMessageTypeOption : Option
|
||||
|
@ -291,6 +306,11 @@ namespace ln.dhcp
|
|||
{
|
||||
DHCPMessageType = (DHCPMessageType)value;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("DHCPMessageType={0}",DHCPMessageType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue