From f08b489476dd8334be7b6512e7fe10a1aa853865 Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 24 Dec 2022 12:07:34 +0530 Subject: [PATCH] Scope: implemented support for TFTP server address and generic options. --- DnsServerCore/Dhcp/Scope.cs | 104 +++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/DnsServerCore/Dhcp/Scope.cs b/DnsServerCore/Dhcp/Scope.cs index a2cd9afb..9ff638a7 100644 --- a/DnsServerCore/Dhcp/Scope.cs +++ b/DnsServerCore/Dhcp/Scope.cs @@ -76,8 +76,10 @@ namespace DnsServerCore.Dhcp IReadOnlyCollection _staticRoutes; IReadOnlyDictionary _vendorInfo; IReadOnlyCollection _capwapAcIpAddresses; + IReadOnlyCollection _tftpServerAddreses; //advanced options + IReadOnlyCollection _genericOptions; IReadOnlyCollection _exclusions; readonly ConcurrentDictionary _reservedLeases = new ConcurrentDictionary(); bool _allowOnlyReservedLeases; @@ -130,6 +132,7 @@ namespace DnsServerCore.Dhcp case 5: case 6: case 7: + case 8: _name = bR.ReadShortString(); _enabled = bR.ReadBoolean(); @@ -299,6 +302,40 @@ namespace DnsServerCore.Dhcp } } + if (version >= 8) + { + int count = bR.ReadByte(); + if (count > 0) + { + IPAddress[] tftpServerAddreses = new IPAddress[count]; + + for (int i = 0; i < count; i++) + tftpServerAddreses[i] = IPAddressExtension.ReadFrom(bR); + + _tftpServerAddreses = tftpServerAddreses; + } + } + + if (version >= 8) + { + int count = bR.ReadByte(); + if (count > 0) + { + DhcpOption[] genericOptions = new DhcpOption[count]; + + for (int i = 0; i < count; i++) + { + DhcpOptionCode code = (DhcpOptionCode)bR.ReadByte(); + short length = bR.ReadInt16(); + byte[] value = bR.ReadBytes(length); + + genericOptions[i] = new DhcpOption(code, value); + } + + _genericOptions = genericOptions; + } + } + { int count = bR.ReadByte(); if (count > 0) @@ -1121,6 +1158,27 @@ namespace DnsServerCore.Dhcp options.Add(new CAPWAPAccessControllerOption(_capwapAcIpAddresses)); break; + + case DhcpOptionCode.TftpServerAddress: + if (_tftpServerAddreses is not null) + options.Add(new TftpServerAddressOption(_tftpServerAddreses)); + + break; + + default: + if (_genericOptions is not null) + { + foreach (DhcpOption genericOption in _genericOptions) + { + if (optionCode == genericOption.Code) + { + options.Add(genericOption); + break; + } + } + } + + break; } } } @@ -1429,7 +1487,7 @@ namespace DnsServerCore.Dhcp public void WriteTo(BinaryWriter bW) { bW.Write(Encoding.ASCII.GetBytes("SC")); - bW.Write((byte)7); //version + bW.Write((byte)8); //version bW.WriteShortString(_name); bW.Write(_enabled); @@ -1576,6 +1634,34 @@ namespace DnsServerCore.Dhcp capwapAcIpAddress.WriteTo(bW); } + if (_tftpServerAddreses is null) + { + bW.Write((byte)0); + } + else + { + bW.Write(Convert.ToByte(_tftpServerAddreses.Count)); + + foreach (IPAddress tftpServerAddress in _tftpServerAddreses) + tftpServerAddress.WriteTo(bW); + } + + if (_genericOptions is null) + { + bW.Write((byte)0); + } + else + { + bW.Write(Convert.ToByte(_genericOptions.Count)); + + foreach (DhcpOption genericOption in _genericOptions) + { + bW.Write((byte)genericOption.Code); + bW.Write(Convert.ToInt16(genericOption.RawValue.Length)); + bW.Write(genericOption.RawValue); + } + } + if (_exclusions is null) { bW.Write((byte)0); @@ -1898,6 +1984,22 @@ namespace DnsServerCore.Dhcp } } + public IReadOnlyCollection TftpServerAddresses + { + get { return _tftpServerAddreses; } + set + { + ValidateIpv4(value, nameof(TftpServerAddresses)); + _tftpServerAddreses = value; + } + } + + public IReadOnlyCollection GenericOptions + { + get { return _genericOptions; } + set { _genericOptions = value; } + } + public IReadOnlyCollection Exclusions { get { return _exclusions; }