TransactionRequest.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (C) 2014-2017 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 Request
  15. /// </summary>
  16. public class TransactionRequest : SMB1Command
  17. {
  18. public const int FixedSMBParametersLength = 28;
  19. // Parameters:
  20. public ushort TotalParameterCount;
  21. public ushort TotalDataCount;
  22. public ushort MaxParameterCount;
  23. public ushort MaxDataCount;
  24. public byte MaxSetupCount;
  25. public byte Reserved1;
  26. public TransactionFlags Flags;
  27. public uint Timeout;
  28. public ushort Reserved2;
  29. // ushort ParameterCount;
  30. // ushort ParameterOffset;
  31. // ushort DataCount;
  32. // ushort DataOffset;
  33. // byte SetupCount; // In 2-byte words
  34. public byte Reserved3;
  35. public byte[] Setup;
  36. // Data:
  37. public string Name; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  38. // Padding (alignment to 4 byte boundary)
  39. public byte[] TransParameters; // Trans_Parameters
  40. // Padding (alignment to 4 byte boundary)
  41. public byte[] TransData; // Trans_Data
  42. public TransactionRequest() : base()
  43. {
  44. Name = String.Empty;
  45. }
  46. public TransactionRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  47. {
  48. TotalParameterCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
  49. TotalDataCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 2);
  50. MaxParameterCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  51. MaxDataCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 6);
  52. MaxSetupCount = ByteReader.ReadByte(this.SMBParameters, 8);
  53. Reserved1 = ByteReader.ReadByte(this.SMBParameters, 9);
  54. Flags = (TransactionFlags)LittleEndianConverter.ToUInt16(this.SMBParameters, 10);
  55. Timeout = LittleEndianConverter.ToUInt32(this.SMBParameters, 12);
  56. Reserved2 = LittleEndianConverter.ToUInt16(this.SMBParameters, 16);
  57. ushort transParameterCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 18);
  58. ushort transParameterOffset = LittleEndianConverter.ToUInt16(this.SMBParameters, 20);
  59. ushort transDataCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 22);
  60. ushort transDataOffset = LittleEndianConverter.ToUInt16(this.SMBParameters, 24);
  61. byte setupCount = ByteReader.ReadByte(this.SMBParameters, 26);
  62. Reserved3 = ByteReader.ReadByte(this.SMBParameters, 27);
  63. Setup = ByteReader.ReadBytes(this.SMBParameters, 28, setupCount * 2);
  64. if (this.SMBData.Length > 0) // Workaround, Some SAMBA clients will set ByteCount to 0 (Popcorn Hour A-400)
  65. {
  66. int dataOffset = 0;
  67. if (this is Transaction2Request)
  68. {
  69. Name = String.Empty;
  70. int nameLength = 1;
  71. dataOffset += nameLength;
  72. }
  73. else
  74. {
  75. if (isUnicode)
  76. {
  77. int namePadding = 1;
  78. dataOffset += namePadding;
  79. }
  80. Name = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  81. }
  82. }
  83. TransParameters = ByteReader.ReadBytes(buffer, transParameterOffset, transParameterCount);
  84. TransData = ByteReader.ReadBytes(buffer, transDataOffset, transDataCount);
  85. }
  86. public override byte[] GetBytes(bool isUnicode)
  87. {
  88. if (Setup.Length % 2 > 0)
  89. {
  90. throw new Exception("Setup length must be a multiple of 2");
  91. }
  92. byte setupCount = (byte)(Setup.Length / 2);
  93. ushort transParameterCount = (ushort)TransParameters.Length;
  94. ushort transDataCount = (ushort)TransData.Length;
  95. // WordCount + ByteCount are additional 3 bytes
  96. int nameLength;
  97. int namePadding;
  98. if (this is Transaction2Request)
  99. {
  100. namePadding = 0;
  101. nameLength = 1;
  102. }
  103. else
  104. {
  105. if (isUnicode)
  106. {
  107. namePadding = 1;
  108. nameLength = Name.Length * 2 + 2;
  109. }
  110. else
  111. {
  112. namePadding = 0;
  113. nameLength = Name.Length + 1;
  114. }
  115. }
  116. ushort transParameterOffset = (ushort)(SMB1Header.Length + 3 + (FixedSMBParametersLength + Setup.Length + namePadding + nameLength));
  117. int padding1 = (4 - (transParameterOffset % 4)) % 4;
  118. transParameterOffset += (ushort)padding1;
  119. ushort transDataOffset = (ushort)(transParameterOffset + transParameterCount);
  120. int padding2 = (4 - (transDataOffset % 4)) % 4;
  121. transDataOffset += (ushort)padding2;
  122. this.SMBParameters = new byte[FixedSMBParametersLength + Setup.Length];
  123. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, TotalParameterCount);
  124. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, TotalDataCount);
  125. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, MaxParameterCount);
  126. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, MaxDataCount);
  127. ByteWriter.WriteByte(this.SMBParameters, 8, MaxSetupCount);
  128. ByteWriter.WriteByte(this.SMBParameters, 9, Reserved1);
  129. LittleEndianWriter.WriteUInt16(this.SMBParameters, 10, (ushort)Flags);
  130. LittleEndianWriter.WriteUInt32(this.SMBParameters, 12, Timeout);
  131. LittleEndianWriter.WriteUInt16(this.SMBParameters, 16, Reserved2);
  132. LittleEndianWriter.WriteUInt16(this.SMBParameters, 18, transParameterCount);
  133. LittleEndianWriter.WriteUInt16(this.SMBParameters, 20, transParameterOffset);
  134. LittleEndianWriter.WriteUInt16(this.SMBParameters, 22, transDataCount);
  135. LittleEndianWriter.WriteUInt16(this.SMBParameters, 24, transDataOffset);
  136. ByteWriter.WriteByte(this.SMBParameters, 26, setupCount);
  137. ByteWriter.WriteByte(this.SMBParameters, 27, Reserved3);
  138. ByteWriter.WriteBytes(this.SMBParameters, 28, Setup);
  139. int offset;
  140. this.SMBData = new byte[namePadding + nameLength + padding1 + transParameterCount + padding2 + transDataCount];
  141. offset = namePadding;
  142. if (this is Transaction2Request)
  143. {
  144. offset += nameLength;
  145. }
  146. else
  147. {
  148. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, Name);
  149. }
  150. ByteWriter.WriteBytes(this.SMBData, offset + padding1, TransParameters);
  151. ByteWriter.WriteBytes(this.SMBData, offset + padding1 + transParameterCount + padding2, TransData);
  152. return base.GetBytes(isUnicode);
  153. }
  154. public override CommandName CommandName
  155. {
  156. get
  157. {
  158. return CommandName.SMB_COM_TRANSACTION;
  159. }
  160. }
  161. }
  162. }