Пример #1
0
        /// <summary>
        /// Gets the DHCP option string.
        /// </summary>
        /// <param name="dhcpOption">The DHCP option.</param>
        /// <param name="dhcpOptionValue">The DHCP option value.</param>
        /// <param name="dhcpRequestListFormat">The DHCP request list format.</param>
        /// <param name="logger">The logger.</param>
        /// <returns>System.String.</returns>
        /// <autogeneratedoc />
        public static string GetDhcpOptionString(
            DhcpOption dhcpOption, byte[] dhcpOptionValue,
            DhcpRequestListFormat dhcpRequestListFormat = DhcpRequestListFormat.StringNewlineIndentedSeparated,
            IPureLogger logger = null
            )
        {
            try
            {
                if (dhcpOptionValue == null)
                {
                    return("Null");
                }

                if (dhcpOption == DhcpOption.ParamReqList)
                {
                    return(GetDhcpRequestListString(dhcpOptionValue, dhcpRequestListFormat, logger));
                }

                switch (DhcpOptionTypes[dhcpOption])
                {
                case DhcpOptionType.IPAddress:
                    return(new IPAddress(dhcpOptionValue).ToString());

                case DhcpOptionType.PhysicalAddressSkip1DashString:
                    return(new PhysicalAddress(dhcpOptionValue.Skip(1).ToArray()).ToDashString());

                case DhcpOptionType.PhysicalAddressSkip1ColonString:
                    return(new PhysicalAddress(dhcpOptionValue.Skip(1).ToArray()).ToColonString());

                case DhcpOptionType.PhysicalAddressDashString:
                    return(new PhysicalAddress(dhcpOptionValue).ToDashString());

                case DhcpOptionType.PhysicalAddressColonString:
                    return(new PhysicalAddress(dhcpOptionValue).ToColonString());

                case DhcpOptionType.MessageTypeString:
                    return(MessageTypeString.GetName((MessageType)dhcpOptionValue[0]));

                case DhcpOptionType.SafeString:
                    return(ByteUtility.GetSafeString(dhcpOptionValue));

                case DhcpOptionType.SafeBytes:
                    return(ByteUtility.PrintSafeBytes(dhcpOptionValue));

                case DhcpOptionType.UInt16:
                    return(BitConverter.ToUInt16(dhcpOptionValue, 0).ToString());

                case DhcpOptionType.UInt32:
                    return(BitConverter.ToUInt32(dhcpOptionValue, 0).ToString());

                case DhcpOptionType.UInt16Network:
                    return(IPAddress.NetworkToHostOrder(BitConverter.ToUInt16(dhcpOptionValue, 0)).ToString());

                case DhcpOptionType.UInt32Network:
                    return(IPAddress.NetworkToHostOrder(BitConverter.ToUInt32(dhcpOptionValue, 0)).ToString());
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Invalid DhcpOption: {dhcpOption}", dhcpOption);
            }

            return(string.Empty);
        }
Пример #2
0
        /// <summary>
        /// Gets the DHCP request list string.
        /// </summary>
        /// <param name="dhcpOptionValue">The DHCP option value.</param>
        /// <param name="dhcpRequestListFormat">The DHCP request list format.</param>
        /// <param name="logger">The logger.</param>
        /// <returns>System.String.</returns>
        /// <autogeneratedoc />
        public static string GetDhcpRequestListString(byte[] dhcpOptionValue,
                                                      DhcpRequestListFormat dhcpRequestListFormat, IPureLogger logger = null)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var requestOption in dhcpOptionValue)
            {
                DhcpOption dhcpRequestOption = (DhcpOption)requestOption;

                switch (dhcpRequestListFormat)
                {
                case DhcpRequestListFormat.StringCommaSeparated:
                    sb.Append($"{dhcpRequestOption.ToString()}, ");
                    break;

                case DhcpRequestListFormat.StringNewlineSeparated:
                    sb.Append($"{Environment.NewLine}{dhcpRequestOption.ToString()}");
                    break;

                case DhcpRequestListFormat.StringNewlineIndentedSeparated:
                    sb.Append($"{Environment.NewLine}    {dhcpRequestOption.ToString()}");
                    break;

                case DhcpRequestListFormat.HexValueCommaSeparated:
                    sb.Append($"{ByteUtility.ByteToHex((byte) dhcpRequestOption)}, ");
                    break;

                case DhcpRequestListFormat.HexValueSpaceSeparated:
                    sb.Append($"{ByteUtility.ByteToHex((byte) dhcpRequestOption)} ");
                    break;

                case DhcpRequestListFormat.HexValueDashSeparated:
                    sb.Append($"{ByteUtility.ByteToHex((byte) dhcpRequestOption)}-");
                    break;

                case DhcpRequestListFormat.DecimalValueCommaSeparated:
                    sb.Append($"{(byte) dhcpRequestOption}, ");
                    break;

                case DhcpRequestListFormat.DecimalValueSpaceSeparated:
                    sb.Append($"{(byte) dhcpRequestOption} ");
                    break;
                }
            }

            // Remove trailing space
            if (sb.Length > 1)
            {
                if (sb[sb.Length - 1] == ' ' || sb[sb.Length - 1] == '-')
                {
                    sb.Length--;
                }
            }

            // Remove trailing comma
            if (sb.Length > 1)
            {
                if (sb[sb.Length - 1] == ',')
                {
                    sb.Length--;
                }
            }

            return(sb.ToString());
        }