ISCSINameHelper.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace ISCSIConsole
  12. {
  13. public class ISCSINameHelper
  14. {
  15. /// <summary>
  16. /// Check if a name is a valid initiator or target name (a.k.a. iSCSI name)
  17. /// </summary>
  18. public static bool IsValidISCSIName(string name)
  19. {
  20. if (name.ToLower().StartsWith("iqn."))
  21. {
  22. return IsValidIQN(name);
  23. }
  24. else
  25. {
  26. return IsValidEUI(name);
  27. }
  28. }
  29. public static bool IsValidIQN(string name)
  30. {
  31. if (name.ToLower().StartsWith("iqn."))
  32. {
  33. if (name.Length > 12 && name[8] == '-' && name[11] == '.')
  34. {
  35. int year = Conversion.ToInt32(name.Substring(4, 4), -1);
  36. int month = Conversion.ToInt32(name.Substring(9, 2), -1);
  37. if (year != -1 && (month >= 1 && month <= 12))
  38. {
  39. string reversedDomain;
  40. string subQualifier = String.Empty;
  41. int index = name.IndexOf(":");
  42. if (index >= 12) // index cannot be non-negative and < 12
  43. {
  44. reversedDomain = name.Substring(12, index - 12);
  45. subQualifier = name.Substring(index + 1);
  46. return IsValidReversedDomainName(reversedDomain) && IsValidSubQualifier(subQualifier);
  47. }
  48. else
  49. {
  50. reversedDomain = name.Substring(12);
  51. return IsValidReversedDomainName(reversedDomain);
  52. }
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. public static bool IsValidReversedDomainName(string name)
  59. {
  60. string[] components = name.Split('.');
  61. if (components.Length < 1)
  62. {
  63. return false;
  64. }
  65. foreach (string component in components)
  66. {
  67. if (component.Length == 0 || component.StartsWith("-") || component.EndsWith("-"))
  68. {
  69. return false;
  70. }
  71. for (int index = 0; index < component.Length; index++)
  72. {
  73. bool isValid = (component[index] >= '0' && component[index] <= '9') ||
  74. (component[index] >= 'a' && component[index] <= 'z') ||
  75. (component[index] >= 'A' && component[index] <= 'Z') ||
  76. (component[index] == '-');
  77. if (!isValid)
  78. {
  79. return false;
  80. }
  81. }
  82. }
  83. return true;
  84. }
  85. public static bool IsValidSubQualifier(string subQualifier)
  86. {
  87. // RFC 3720: The owner of the domain name can assign everything after the reversed domain name as desired.
  88. // RFC 3720: iSCSI names are composed only of displayable characters.
  89. // RFC 3720: No whitespace characters are used in iSCSI names.
  90. // Note: String.Empty is a valid sub-qualifier.
  91. if (subQualifier.Contains(" "))
  92. {
  93. return false;
  94. }
  95. return true;
  96. }
  97. public static bool IsValidEUI(string name)
  98. {
  99. if (name.ToLower().StartsWith("eui.") && name.Length == 20)
  100. {
  101. string identifier = name.Substring(5);
  102. return OnlyHexChars(identifier);
  103. }
  104. return false;
  105. }
  106. public static bool OnlyHexChars(string value)
  107. {
  108. for (int index = 0; index < value.Length; index++)
  109. {
  110. bool isValid = (value[index] >= '0' && value[index] <= '9') ||
  111. (value[index] >= 'a' && value[index] <= 'f') ||
  112. (value[index] >= 'A' && value[index] <= 'F');
  113. if (!isValid)
  114. {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. }
  121. }