SCSICommandPDU.cs 2.3 KB

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