123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Utilities;
- namespace ISCSIConsole
- {
- public class ISCSINameHelper
- {
-
-
-
- public static bool IsValidISCSIName(string name)
- {
- if (name.ToLower().StartsWith("iqn."))
- {
- return IsValidIQN(name);
- }
- else
- {
- return IsValidEUI(name);
- }
- }
- public static bool IsValidIQN(string name)
- {
- if (name.ToLower().StartsWith("iqn."))
- {
- if (name.Length > 12 && name[8] == '-' && name[11] == '.')
- {
- int year = Conversion.ToInt32(name.Substring(4, 4), -1);
- int month = Conversion.ToInt32(name.Substring(9, 2), -1);
- if (year != -1 && (month >= 1 && month <= 12))
- {
- string reversedDomain;
- string subQualifier = String.Empty;
- int index = name.IndexOf(":");
- if (index >= 12)
- {
- reversedDomain = name.Substring(12, index - 12);
- subQualifier = name.Substring(index + 1);
- return IsValidReversedDomainName(reversedDomain) && IsValidSubQualifier(subQualifier);
- }
- else
- {
- reversedDomain = name.Substring(12);
- return IsValidReversedDomainName(reversedDomain);
- }
- }
- }
- }
- return false;
- }
- public static bool IsValidReversedDomainName(string name)
- {
- string[] components = name.Split('.');
- if (components.Length < 1)
- {
- return false;
- }
- foreach (string component in components)
- {
- if (component.Length == 0 || component.StartsWith("-") || component.EndsWith("-"))
- {
- return false;
- }
- for (int index = 0; index < component.Length; index++)
- {
- bool isValid = (component[index] >= '0' && component[index] <= '9') ||
- (component[index] >= 'a' && component[index] <= 'z') ||
- (component[index] >= 'A' && component[index] <= 'Z') ||
- (component[index] == '-');
- if (!isValid)
- {
- return false;
- }
- }
- }
- return true;
- }
- public static bool IsValidSubQualifier(string subQualifier)
- {
-
-
-
-
- if (subQualifier.Contains(" "))
- {
- return false;
- }
- return true;
- }
- public static bool IsValidEUI(string name)
- {
- if (name.ToLower().StartsWith("eui.") && name.Length == 20)
- {
- string identifier = name.Substring(5);
- return OnlyHexChars(identifier);
- }
- return false;
- }
- public static bool OnlyHexChars(string value)
- {
- for (int index = 0; index < value.Length; index++)
- {
- bool isValid = (value[index] >= '0' && value[index] <= '9') ||
- (value[index] >= 'a' && value[index] <= 'f') ||
- (value[index] >= 'A' && value[index] <= 'F');
- if (!isValid)
- {
- return false;
- }
- }
- return true;
- }
- }
- }
|