TransactionResponse.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 2014 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 SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_TRANSACTION Response
  15. /// </summary>
  16. public class TransactionResponse : SMB1Command
  17. {
  18. public const int FixedSMBParametersLength = 20;
  19. // Parameters:
  20. public ushort TotalParameterCount;
  21. public ushort TotalDataCount;
  22. public ushort Reserved1;
  23. //ushort ParameterCount;
  24. //ushort ParameterOffset;
  25. public ushort ParameterDisplacement;
  26. //ushort DataCount;
  27. //ushort DataOffset;
  28. public ushort DataDisplacement;
  29. //byte SetupCount; // In 2-byte words
  30. public byte Reserved2;
  31. public byte[] Setup;
  32. // Data:
  33. // Padding (alignment to 4 byte boundary)
  34. public byte[] TransParameters; // Trans_Parameters
  35. // Padding (alignment to 4 byte boundary)
  36. public byte[] TransData; // Trans_Data
  37. public TransactionResponse() : base()
  38. {
  39. Setup = new byte[0];
  40. TransParameters = new byte[0];
  41. TransData = new byte[0];
  42. }
  43. public TransactionResponse(byte[] buffer, int offset) : base(buffer, offset, false)
  44. {
  45. TotalParameterCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
  46. TotalDataCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 2);
  47. Reserved1 = LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  48. ushort parameterCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 6);
  49. ushort parameterOffset = LittleEndianConverter.ToUInt16(this.SMBParameters, 8);
  50. ParameterDisplacement = LittleEndianConverter.ToUInt16(this.SMBParameters, 10);
  51. ushort dataCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 12);
  52. ushort dataOffset = LittleEndianConverter.ToUInt16(this.SMBParameters, 14);
  53. DataDisplacement = LittleEndianConverter.ToUInt16(this.SMBParameters, 16);
  54. byte setupCount = ByteReader.ReadByte(this.SMBParameters, 18);
  55. Reserved2 = ByteReader.ReadByte(this.SMBParameters, 19);
  56. Setup = ByteReader.ReadBytes(this.SMBParameters, 20, setupCount * 2);
  57. TransParameters = ByteReader.ReadBytes(buffer, parameterOffset, parameterCount);
  58. TransData = ByteReader.ReadBytes(buffer, dataOffset, dataCount);
  59. }
  60. public override byte[] GetBytes(bool isUnicode)
  61. {
  62. if (TransData.Length > UInt16.MaxValue)
  63. {
  64. throw new ArgumentException("Invalid Trans_Data length");
  65. }
  66. byte setupCount = (byte)(Setup.Length / 2);
  67. ushort parameterCount = (ushort)TransParameters.Length;
  68. ushort dataCount = (ushort)TransData.Length;
  69. // WordCount + ByteCount are additional 3 bytes
  70. ushort parameterOffset = (ushort)(SMB1Header.Length + 3 + (FixedSMBParametersLength + Setup.Length));
  71. int padding1 = (4 - (parameterOffset %4)) % 4;
  72. parameterOffset += (ushort)padding1;
  73. ushort dataOffset = (ushort)(parameterOffset + parameterCount);
  74. int padding2 = (4 - (dataOffset % 4)) % 4;
  75. dataOffset += (ushort)padding2;
  76. this.SMBParameters = new byte[FixedSMBParametersLength + Setup.Length];
  77. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, TotalParameterCount);
  78. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, TotalDataCount);
  79. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, Reserved1);
  80. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, parameterCount);
  81. LittleEndianWriter.WriteUInt16(this.SMBParameters, 8, parameterOffset);
  82. LittleEndianWriter.WriteUInt16(this.SMBParameters, 10, ParameterDisplacement);
  83. LittleEndianWriter.WriteUInt16(this.SMBParameters, 12, dataCount);
  84. LittleEndianWriter.WriteUInt16(this.SMBParameters, 14, dataOffset);
  85. LittleEndianWriter.WriteUInt16(this.SMBParameters, 16, DataDisplacement);
  86. ByteWriter.WriteByte(this.SMBParameters, 18, setupCount);
  87. ByteWriter.WriteByte(this.SMBParameters, 19, Reserved2);
  88. ByteWriter.WriteBytes(this.SMBParameters, 20, Setup);
  89. this.SMBData = new byte[parameterCount + dataCount + padding1 + padding2];
  90. ByteWriter.WriteBytes(this.SMBData, padding1, TransParameters);
  91. ByteWriter.WriteBytes(this.SMBData, padding1 + parameterCount + padding2, TransData);
  92. return base.GetBytes(isUnicode);
  93. }
  94. public override CommandName CommandName
  95. {
  96. get
  97. {
  98. return CommandName.SMB_COM_TRANSACTION;
  99. }
  100. }
  101. public static int CalculateMessageSize(int setupLength, int trans2ParametersLength, int trans2DataLength)
  102. {
  103. int parameterOffset = SMB1Header.Length + 3 + (FixedSMBParametersLength + setupLength);
  104. int padding1 = (4 - (parameterOffset %4)) % 4;
  105. parameterOffset += padding1;
  106. int dataOffset = (parameterOffset + trans2ParametersLength);
  107. int padding2 = (4 - (dataOffset % 4)) % 4;
  108. int messageParametersLength = FixedSMBParametersLength + setupLength;
  109. int messageDataLength = trans2ParametersLength + trans2DataLength + padding1 + padding2;
  110. // WordCount + ByteCount are additional 3 bytes
  111. return SMB1Header.Length + messageParametersLength + messageDataLength + 3;
  112. }
  113. }
  114. }