//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2011-2014 All Rights Reserved.
//
// File: Utils.cs
//
// Contents: High Level API: Utils.
//
// Notes:
//
//----------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Net.NetworkInformation;
using Intel.Manageability.Exceptions;
using System.Text;
namespace Intel.Manageability.Utils
{
class Utils
{
private const string PASSWORD_EXPRESSION = @"^(?=(.*[a-z]){1,})(?=(.*[\d]){1,})(?=(.*[\W]){1,})(?!.*\s).{7,30}$";
#region Public Function
//The function get string and check if it is a valid IPv6.
public static bool ValidateIP6(string ip)
{
UriHostNameType tmpType = Uri.CheckHostName(ip);
if (tmpType == UriHostNameType.IPv6)
{
return true;
}
return false;
}
///
/// Get information about the AMT machine.
///
/// Information about the AMT machine, e.g. provisioning state, AMT version.
public static bool IsLocalHost(string host)
{
try
{
IPAddress parsedIPAddress;
// Check if the host name is local machine and get platform data by HECI.
// If the IP address is "127.0.0.1" (IPv4) or "::1" (IPv6), the address is of the local machine.
if (host == "127.0.0.1" || host == "::1" || host == "localhost")
{
return true;
}
// If the host name is a IP address
if (IPAddress.TryParse(host, out parsedIPAddress))
{
// Obtain a reference to all network interfaces in the machine.
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
IPInterfaceProperties ipProperties;
foreach (NetworkInterface networkInterface in networkInterfaces)
{
ipProperties = networkInterface.GetIPProperties();
foreach (IPAddressInformation unicastAddress in ipProperties.UnicastAddresses)
{
// If the machine name equals to one of the interface's IP Address.
if (unicastAddress.Address.ToString() == host)
{
return true;
}
}
}
}
else // If the host name is a FQDN - check if it is the local machine name.
{
if (host.Substring(0, host.IndexOf('.')).ToLower() == Environment.MachineName.ToLower())
{
return true;
}
}
return false;
}
catch (Exception ex)
{
throw new ManageabilityException("Failed to get host location. Failure reason: " + ex.Message);
}
}
public static bool ValidateFQDN(string fqdn)
{
//check for a '.'
bool dotExists = false;
for (int i = 0; i < fqdn.Length; i++)
{
char c = fqdn[i];
if ((c == '.'))
{
dotExists = true;
if (i >= fqdn.Length - 2)
return false;
continue;
}
if (!(LegalHostnameChar(c)))
return false;
if (i == (fqdn.Length - 1))
if (((c >= '0') && (c <= '9')) || (c == '.'))
return false;
}
if (!dotExists)
return false;
bool ret = Regex.IsMatch(fqdn, "(?=^.{1,254}$)(^(?:(?!\\d+\\.|-)[a-zA-Z0-9_#/\\-]{1,63}(?
/// Prior to Release 5.1 platformGUID return half of the numbers reversed.
/// this function return the GUID in correct format
/// Converts string with 32 contiguous digits GUID format into a 16 length byte array
///
/// String with 32 contiguous digits GUID format
/// A 16 length byte array representing the given string
public static byte[] ConvertGuidStringToByteArray(string guidString)
{
string newGuid = guidString.Replace("-", "");
byte[] guidByteArray = new byte[newGuid.Length / 2];
for (int i = 0; i < guidByteArray.Length; i++)
{
Byte.TryParse(
newGuid.Substring((i * 2), 2),
NumberStyles.AllowHexSpecifier,
null,
out guidByteArray[i]);
}
return guidByteArray;
}
//get description of value of enum and return the correct value in the enum
public static object ConvertStringToEnumValue(string value, Type enumType)
{
string[] names = Enum.GetNames(enumType);
foreach (string name in names)
{
if (ConvertEnumValueToString((Enum)Enum.Parse(enumType, name)).Equals(value))
{
return Enum.Parse(enumType, name);
}
}
throw new ManageabilityException("Failed to convert the value to " + enumType.Name);
}
//get value of enum and return its description
public static string ConvertEnumValueToString(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
public static string ExtractAddressFromWsmanConnection(string connectionAddress)
{
string cutAdd = connectionAddress.StartsWith("https://") ? connectionAddress.Substring(8, connectionAddress.Length - 8) :
connectionAddress.Substring(7, connectionAddress.Length - 7);
return cutAdd.Substring(0, cutAdd.LastIndexOf(':'));
}
#endregion
#region Private function
private static bool LegalHostnameChar(char c)
{
if ((c >= 'a' && c <= 'z') || ((c >= 'A' && c <= 'Z')) || ((c >= '0' && c <= '9')))
{
return true;
}
if ((c == '-') || (c == '_') || (c == '/') || (c == '#'))
{
return true;
}
return false;
}
#endregion
}
public static class HLAPIExtentions
{
public static string ToStringAMTFormat(this Version version)
{
return new Version(version.Major, version.Minor, version.Revision, version.Build).ToString();// change the order as needed
}
}
}