From e84b4a3748e4a0feeafeb53d60f68303217f034c Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 14 Nov 2020 16:44:28 +0530 Subject: [PATCH] VendorSpecificInformationOption: added hex string parsing. --- .../VendorSpecificInformationOption.cs | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/DnsServerCore/Dhcp/Options/VendorSpecificInformationOption.cs b/DnsServerCore/Dhcp/Options/VendorSpecificInformationOption.cs index a0ca2280..9280da42 100644 --- a/DnsServerCore/Dhcp/Options/VendorSpecificInformationOption.cs +++ b/DnsServerCore/Dhcp/Options/VendorSpecificInformationOption.cs @@ -17,12 +17,14 @@ along with this program. If not, see . */ +using System; +using System.Globalization; using System.IO; using TechnitiumLibrary.IO; namespace DnsServerCore.Dhcp.Options { - class VendorSpecificInformationOption : DhcpOption + public class VendorSpecificInformationOption : DhcpOption { #region variables @@ -32,6 +34,12 @@ namespace DnsServerCore.Dhcp.Options #region constructor + public VendorSpecificInformationOption(string hexInfo) + : base(DhcpOptionCode.VendorSpecificInformation) + { + _information = ParseHexString(hexInfo); + } + public VendorSpecificInformationOption(byte[] information) : base(DhcpOptionCode.VendorSpecificInformation) { @@ -44,6 +52,42 @@ namespace DnsServerCore.Dhcp.Options #endregion + #region private + + private static byte[] ParseHexString(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 protected protected override void ParseOptionValue(Stream s) @@ -58,6 +102,15 @@ namespace DnsServerCore.Dhcp.Options #endregion + #region public + + public override string ToString() + { + return BitConverter.ToString(_information).Replace("-", ":"); + } + + #endregion + #region properties public byte[] Information