VendorSpecificInformationOption: added hex string parsing.

This commit is contained in:
Shreyas Zare
2020-11-14 16:44:28 +05:30
parent 8605862b3d
commit e84b4a3748

View File

@@ -17,12 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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