LoginRequestPDU.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 LoginRequestPDU : ISCSIPDU
  14. {
  15. public bool Transit; // indicates that the initiator is ready to transit to the next stage
  16. public bool Continue; // indicates that the text (set of key=value pairs) in this Login Request is not complete
  17. public byte CurrentStage; // 0..3
  18. public byte NextStage; // 0..3
  19. public byte VersionMax;
  20. public byte VersionMin;
  21. public ulong ISID;
  22. public ushort TSIH;
  23. public ushort CID; // ConnectionID
  24. public uint CmdSN;
  25. public uint ExpStatSN;
  26. public string LoginParametersText = String.Empty; // A key=value pair can start in one PDU and continue on the next
  27. public LoginRequestPDU() : base()
  28. {
  29. OpCode = ISCSIOpCodeName.LoginRequest;
  30. ImmediateDelivery = true;
  31. }
  32. public LoginRequestPDU(byte[] buffer) : base(buffer)
  33. {
  34. Transit = Final; // the Transit bit replaces the Final bit
  35. Continue = (OpCodeSpecificHeader[0] & 0x40) != 0;
  36. CurrentStage = (byte)((OpCodeSpecificHeader[0] & 0x0C) >> 2);
  37. NextStage = (byte)(OpCodeSpecificHeader[0] & 0x03);
  38. VersionMax = OpCodeSpecificHeader[1];
  39. VersionMin = OpCodeSpecificHeader[2];
  40. ISID = (ulong)BigEndianConverter.ToUInt32(LUNOrOpCodeSpecific, 0) << 16 | BigEndianConverter.ToUInt16(LUNOrOpCodeSpecific, 4);
  41. TSIH = BigEndianConverter.ToUInt16(LUNOrOpCodeSpecific, 6);
  42. CID = BigEndianConverter.ToUInt16(OpCodeSpecific, 0);
  43. CmdSN = BigEndianConverter.ToUInt32(OpCodeSpecific, 4);
  44. ExpStatSN = BigEndianConverter.ToUInt32(OpCodeSpecific, 8);
  45. LoginParametersText = Encoding.ASCII.GetString(Data);
  46. }
  47. public override byte[] GetBytes()
  48. {
  49. if (Transit)
  50. {
  51. Final = true; // the Transit bit replaces the Final bit
  52. }
  53. if (Continue)
  54. {
  55. OpCodeSpecificHeader[0] |= 0x40;
  56. }
  57. OpCodeSpecificHeader[0] |= (byte)(CurrentStage << 2);
  58. OpCodeSpecificHeader[0] |= NextStage;
  59. OpCodeSpecificHeader[1] = VersionMax;
  60. OpCodeSpecificHeader[2] = VersionMin;
  61. Array.Copy(BigEndianConverter.GetBytes(ISID), 2, LUNOrOpCodeSpecific, 0, 6);
  62. Array.Copy(BigEndianConverter.GetBytes(TSIH), 0, LUNOrOpCodeSpecific, 6, 2);
  63. Array.Copy(BigEndianConverter.GetBytes(CID), 0, OpCodeSpecific, 0, 2);
  64. Array.Copy(BigEndianConverter.GetBytes(CmdSN), 0, OpCodeSpecific, 4, 4);
  65. Array.Copy(BigEndianConverter.GetBytes(ExpStatSN), 0, OpCodeSpecific, 8, 4);
  66. Data = ASCIIEncoding.ASCII.GetBytes(LoginParametersText);
  67. return base.GetBytes();
  68. }
  69. public KeyValuePairList<string, string> LoginParameters
  70. {
  71. set
  72. {
  73. LoginParametersText = KeyValuePairUtils.ToNullDelimitedString(value);
  74. }
  75. }
  76. }
  77. }