PDUHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. namespace ISCSI.Server
  11. {
  12. public class PDUHelper
  13. {
  14. public static uint? GetCmdSN(ISCSIPDU pdu)
  15. {
  16. if (pdu is NOPOutPDU)
  17. {
  18. return ((NOPOutPDU)pdu).CmdSN;
  19. }
  20. else if (pdu is SCSICommandPDU)
  21. {
  22. return ((SCSICommandPDU)pdu).CmdSN;
  23. }
  24. else if (pdu is LoginRequestPDU)
  25. {
  26. return ((LoginRequestPDU)pdu).CmdSN;
  27. }
  28. else if (pdu is TextRequestPDU)
  29. {
  30. return ((TextRequestPDU)pdu).CmdSN;
  31. }
  32. else if (pdu is LogoutRequestPDU)
  33. {
  34. return ((LogoutRequestPDU)pdu).CmdSN;
  35. }
  36. return null;
  37. }
  38. public static void SetStatSN(ISCSIPDU pdu, uint statSN)
  39. {
  40. if (pdu is NOPInPDU)
  41. {
  42. ((NOPInPDU)pdu).StatSN = statSN;
  43. }
  44. else if (pdu is SCSIResponsePDU)
  45. {
  46. ((SCSIResponsePDU)pdu).StatSN = statSN;
  47. }
  48. else if (pdu is LoginResponsePDU)
  49. {
  50. ((LoginResponsePDU)pdu).StatSN = statSN;
  51. }
  52. else if (pdu is TextResponsePDU)
  53. {
  54. ((TextResponsePDU)pdu).StatSN = statSN;
  55. }
  56. else if (pdu is SCSIDataInPDU && ((SCSIDataInPDU)pdu).StatusPresent) // RFC 3720: StatSN [..] only have meaningful content if the S bit is set to 1
  57. {
  58. ((SCSIDataInPDU)pdu).StatSN = statSN;
  59. }
  60. else if (pdu is LogoutResponsePDU)
  61. {
  62. ((LogoutResponsePDU)pdu).StatSN = statSN;
  63. }
  64. else if (pdu is ReadyToTransferPDU)
  65. {
  66. ((ReadyToTransferPDU)pdu).StatSN = statSN;
  67. }
  68. else if (pdu is RejectPDU)
  69. {
  70. ((RejectPDU)pdu).StatSN = statSN;
  71. }
  72. }
  73. public static void SetExpCmdSN(ISCSIPDU pdu, uint expCmdSN, uint maxCmdSN)
  74. {
  75. if (pdu is NOPInPDU)
  76. {
  77. ((NOPInPDU)pdu).ExpCmdSN = expCmdSN;
  78. ((NOPInPDU)pdu).MaxCmdSN = maxCmdSN;
  79. }
  80. else if (pdu is SCSIResponsePDU)
  81. {
  82. ((SCSIResponsePDU)pdu).ExpCmdSN = expCmdSN;
  83. ((SCSIResponsePDU)pdu).MaxCmdSN = maxCmdSN;
  84. }
  85. else if (pdu is LoginResponsePDU)
  86. {
  87. ((LoginResponsePDU)pdu).ExpCmdSN = expCmdSN;
  88. ((LoginResponsePDU)pdu).MaxCmdSN = maxCmdSN;
  89. }
  90. else if (pdu is TextResponsePDU)
  91. {
  92. ((TextResponsePDU)pdu).ExpCmdSN = expCmdSN;
  93. ((TextResponsePDU)pdu).MaxCmdSN = maxCmdSN;
  94. }
  95. else if (pdu is SCSIDataInPDU)
  96. {
  97. ((SCSIDataInPDU)pdu).ExpCmdSN = expCmdSN;
  98. ((SCSIDataInPDU)pdu).MaxCmdSN = maxCmdSN;
  99. }
  100. else if (pdu is LogoutResponsePDU)
  101. {
  102. ((LogoutResponsePDU)pdu).ExpCmdSN = expCmdSN;
  103. ((LogoutResponsePDU)pdu).MaxCmdSN = maxCmdSN;
  104. }
  105. else if (pdu is ReadyToTransferPDU)
  106. {
  107. ((ReadyToTransferPDU)pdu).ExpCmdSN = expCmdSN;
  108. ((ReadyToTransferPDU)pdu).MaxCmdSN = maxCmdSN;
  109. }
  110. else if (pdu is RejectPDU)
  111. {
  112. ((RejectPDU)pdu).ExpCmdSN = expCmdSN;
  113. ((RejectPDU)pdu).MaxCmdSN = maxCmdSN;
  114. }
  115. }
  116. }
  117. }