DhcpOption: Implemented support for generic option and TftpServerAddress option.

This commit is contained in:
Shreyas Zare
2022-12-24 12:05:50 +05:30
parent 9562964b82
commit 3f9f7db65b

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using DnsServerCore.Dhcp.Options;
using System;
using System.Globalization;
using System.IO;
using TechnitiumLibrary.IO;
@@ -105,6 +106,7 @@ namespace DnsServerCore.Dhcp
DomainSearch = 119,
ClasslessStaticRoute = 121,
CAPWAPAccessControllerAddresses = 138,
TftpServerAddress = 150,
End = 255
}
@@ -119,6 +121,28 @@ namespace DnsServerCore.Dhcp
#region constructor
public DhcpOption(DhcpOptionCode code, string hexValue)
{
if (hexValue is null)
throw new ArgumentNullException(nameof(hexValue));
_code = code;
if (hexValue.Contains(':'))
_value = ParseColonHexString(hexValue);
else
_value = Convert.FromHexString(hexValue);
}
public DhcpOption(DhcpOptionCode code, byte[] value)
{
if (value is null)
throw new ArgumentNullException(nameof(value));
_code = code;
_value = value;
}
protected DhcpOption(DhcpOptionCode code, Stream s)
{
_code = code;
@@ -223,6 +247,9 @@ namespace DnsServerCore.Dhcp
case DhcpOptionCode.CAPWAPAccessControllerAddresses:
return new CAPWAPAccessControllerOption(s);
case DhcpOptionCode.TftpServerAddress:
return new TftpServerAddressOption(s);
case DhcpOptionCode.Pad:
case DhcpOptionCode.End:
return new DhcpOption(optionCode);
@@ -233,6 +260,38 @@ namespace DnsServerCore.Dhcp
}
}
protected static byte[] ParseColonHexString(string value)
{
int i;
int j = -1;
string strHex;
int b;
using (MemoryStream mS = new MemoryStream())
{
while (true)
{
i = value.IndexOf(':', j + 1);
if (i < 0)
i = value.Length;
strHex = value.Substring(j + 1, i - j - 1);
if (!int.TryParse(strHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b) || (b < byte.MinValue) || (b > byte.MaxValue))
throw new InvalidDataException("VendorSpecificInformation option data must be a colon (:) separated hex string.");
mS.WriteByte((byte)b);
if (i == value.Length)
break;
j = i;
}
return mS.ToArray();
}
}
#endregion
#region internal
@@ -321,6 +380,9 @@ namespace DnsServerCore.Dhcp
public DhcpOptionCode Code
{ get { return _code; } }
public byte[] RawValue
{ get { return _value; } }
#endregion
}
}