Dhcp6 parser

This commit is contained in:
Jöran Malek 2023-11-30 15:28:01 +01:00
parent bb2b357d8f
commit 6bc18cbb54
2 changed files with 152 additions and 50 deletions

View file

@ -22,50 +22,62 @@ public record struct KeaDhcp4Lease(
{ {
public static KeaDhcp4Lease Parse(in SepReader.Row row) public static KeaDhcp4Lease Parse(in SepReader.Row row)
{ {
var address = IPAddress.Parse(row[0].Span); KeaDhcp4Lease result = new();
PhysicalAddress hwaddr = PhysicalAddress.None; for (int i = 0; i < row.ColCount; i++)
if (row[1].Span is { IsEmpty: false } physical)
{ {
hwaddr = PhysicalAddress.Parse(physical); var span = row[i].Span;
} switch (i)
string? clientId = row[2].ToString(); {
uint validLifetime = uint.Parse(row[3].Span); case 0:
DateTimeOffset expire = DateTimeOffset.FromUnixTimeSeconds(unchecked((long)ulong.Parse(row[4].Span))); result.Address = IPAddress.Parse(span);
uint subnetId = uint.Parse(row[5].Span); break;
bool fqdnFwd = sbyte.Parse(row[6].Span) != 0;
bool fqdnRev = sbyte.Parse(row[7].Span) != 0;
string hostname = KeaDhcpLease.Unescape(row[8].Span);
uint state = 0; case 1 when !span.IsWhiteSpace():
if (row.ColCount > 9) result.HWAddr = PhysicalAddress.Parse(span);
{ break;
state = uint.Parse(row[9].Span);
case 2:
result.ClientId = span.ToString();
break;
case 3:
result.ValidLifetime = uint.Parse(span);
break;
case 4:
result.Expire = DateTimeOffset.FromUnixTimeSeconds(unchecked((long)ulong.Parse(span)));
break;
case 5:
result.SubnetId = uint.Parse(span);
break;
case 6:
result.FqdnFwd = byte.Parse(span) != 0;
break;
case 7:
result.FqdnRev = byte.Parse(span) != 0;
break;
case 8:
result.Hostname = KeaDhcpLease.Unescape(span);
break;
case 9:
result.State = uint.Parse(span);
break;
case 10:
result.UserContext = KeaDhcpLease.Unescape(span);
break;
case 11:
result.PoolId = uint.Parse(span);
break;
}
} }
string? userContext = default; return result;
if (row.ColCount > 10)
{
userContext = KeaDhcpLease.Unescape(row[10].Span);
}
uint poolId = 0;
if (row.ColCount > 11)
{
poolId = uint.Parse(row[11].Span);
}
return new(
address,
hwaddr,
clientId,
validLifetime,
expire,
subnetId,
fqdnFwd,
fqdnRev,
hostname,
state,
userContext,
poolId);
} }
} }

View file

@ -1,23 +1,113 @@
using System.Net;
using System.Net.NetworkInformation;
using nietras.SeparatedValues;
namespace pdns_dhcp.Kea; namespace pdns_dhcp.Kea;
// ref: https://github.com/isc-projects/kea/blob/Kea-2.5.3/src/lib/dhcpsrv/csv_lease_file6.h // ref: https://github.com/isc-projects/kea/blob/Kea-2.5.3/src/lib/dhcpsrv/csv_lease_file6.h
public record struct KeaDhcp6Lease( public record struct KeaDhcp6Lease(
string Address, IPAddress Address,
string DUId, string DUId,
uint ValidLifetime, uint ValidLifetime,
ulong Expire, DateTimeOffset Expire,
string SubnetId, uint SubnetId,
uint PrefLifetime, uint PrefLifetime,
LeaseType LeaseType, LeaseType LeaseType,
uint IAId, uint IAId,
byte PrefixLen, byte PrefixLen,
byte FqdnFwd, bool FqdnFwd,
byte FqdnRev, bool FqdnRev,
string Hostname, string Hostname,
string HWAddr, PhysicalAddress HWAddr,
uint State, uint State,
string UserContext, string? UserContext,
ushort? HWType, ushort? HWType,
uint? HWAddrSource, uint? HWAddrSource,
uint PoolId uint PoolId)
); {
public static KeaDhcp6Lease Parse(in SepReader.Row row)
{
KeaDhcp6Lease result = new();
for (int i = 0; i < row.ColCount; i++)
{
var span = row[i].Span;
switch (i)
{
case 0:
result.Address = IPAddress.Parse(span);
break;
case 1:
result.DUId = span.ToString();
break;
case 2:
result.ValidLifetime = uint.Parse(span);
break;
case 3:
result.Expire = DateTimeOffset.FromUnixTimeSeconds(unchecked((long)ulong.Parse(span)));
break;
case 4:
result.SubnetId = uint.Parse(span);
break;
case 5:
result.PrefLifetime = uint.Parse(span);
break;
case 6:
result.LeaseType = (LeaseType)byte.Parse(span);
break;
case 7:
result.IAId = uint.Parse(span);
break;
case 8:
result.PrefixLen = byte.Parse(span);
break;
case 9:
result.FqdnFwd = byte.Parse(span) != 0;
break;
case 10:
result.FqdnRev = byte.Parse(span) != 0;
break;
case 11:
result.Hostname = KeaDhcpLease.Unescape(span);
break;
case 12 when !span.IsWhiteSpace():
result.HWAddr = PhysicalAddress.Parse(span);
break;
case 13:
result.State = uint.Parse(span);
break;
case 14 when !span.IsWhiteSpace():
result.UserContext = KeaDhcpLease.Unescape(span);
break;
case 15:
result.HWType = ushort.Parse(span);
break;
case 16:
result.HWAddrSource = uint.Parse(span);
break;
case 17:
result.PoolId = uint.Parse(span);
break;
}
}
return result;
}
}