SCSICommandPDU.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 ISCSI
  12. {
  13. public class SCSICommandPDU : ISCSIPDU
  14. {
  15. public bool Read;
  16. public bool Write;
  17. public byte TaskAttributes;
  18. public LUNStructure LUN;
  19. public uint ExpectedDataTransferLength; // in bytes (for the whole operation and not just this command)
  20. public uint CmdSN;
  21. public uint ExpStatSN;
  22. public byte[] CommandDescriptorBlock;
  23. public SCSICommandPDU() : base()
  24. {
  25. OpCode = ISCSIOpCodeName.SCSICommand;
  26. }
  27. public SCSICommandPDU(byte[] buffer) : base(buffer)
  28. {
  29. Read = (OpCodeSpecificHeader[0] & 0x40) != 0;
  30. Write = (OpCodeSpecificHeader[0] & 0x20) != 0;
  31. TaskAttributes = (byte)(OpCodeSpecificHeader[0] & 0x7);
  32. LUN = new LUNStructure(LUNOrOpCodeSpecific, 0);
  33. ExpectedDataTransferLength = BigEndianConverter.ToUInt32(OpCodeSpecific, 0);
  34. CmdSN = BigEndianConverter.ToUInt32(OpCodeSpecific, 4);
  35. ExpStatSN = BigEndianConverter.ToUInt32(OpCodeSpecific, 8);
  36. CommandDescriptorBlock = ByteReader.ReadBytes(OpCodeSpecific, 12, 16);
  37. }
  38. public override byte[] GetBytes()
  39. {
  40. if (Read)
  41. {
  42. OpCodeSpecificHeader[0] |= 0x40;
  43. }
  44. if (Write)
  45. {
  46. OpCodeSpecificHeader[0] |= 0x20;
  47. }
  48. OpCodeSpecificHeader[0] |= TaskAttributes;
  49. LUNOrOpCodeSpecific = LUN.GetBytes();
  50. Array.Copy(BigEndianConverter.GetBytes(ExpectedDataTransferLength), 0, OpCodeSpecific, 0, 4);
  51. Array.Copy(BigEndianConverter.GetBytes(CmdSN), 0, OpCodeSpecific, 4, 4);
  52. Array.Copy(BigEndianConverter.GetBytes(ExpStatSN), 0, OpCodeSpecific, 8, 4);
  53. Array.Copy(CommandDescriptorBlock, 0, OpCodeSpecific, 12, CommandDescriptorBlock.Length);
  54. return base.GetBytes();
  55. }
  56. }
  57. }