TransactionRequest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ParameterCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 18);
  58. ushort ParameterOffset = LittleEndianConverter.ToUInt16(this.SMBParameters, 20);
  59. ushort DataCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 22);
  60. ushort DataOffset = 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, ParameterOffset, ParameterCount);
  84. TransData = ByteReader.ReadBytes(buffer, DataOffset, DataCount);
  85. }
  86. public override byte[] GetBytes(bool isUnicode)
  87. {
  88. byte SetupCount = (byte)(Setup.Length / 2);
  89. ushort ParameterCount = (ushort)TransParameters.Length;
  90. ushort DataCount = (ushort)TransData.Length;
  91. // WordCount + ByteCount are additional 3 bytes
  92. ushort ParameterOffset = (ushort)(SMB1Header.Length + 3 + (FixedSMBParametersLength + Setup.Length));
  93. if (this is Transaction2Request)
  94. {
  95. ParameterOffset += 1;
  96. }
  97. else
  98. {
  99. if (isUnicode)
  100. {
  101. ParameterOffset += (ushort)(Name.Length * 2 + 2);
  102. }
  103. else
  104. {
  105. ParameterOffset += (ushort)(Name.Length + 1);
  106. }
  107. }
  108. int padding1 = (4 - (ParameterOffset % 4)) % 4;
  109. ParameterOffset += (ushort)padding1;
  110. ushort DataOffset = (ushort)(ParameterOffset + ParameterCount);
  111. int padding2 = (4 - (DataOffset % 4)) % 4;
  112. DataOffset += (ushort)padding2;
  113. this.SMBParameters = new byte[FixedSMBParametersLength + Setup.Length];
  114. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, TotalParameterCount);
  115. LittleEndianWriter.WriteUInt16(this.SMBParameters, 2, TotalDataCount);
  116. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, MaxParameterCount);
  117. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, MaxDataCount);
  118. ByteWriter.WriteByte(this.SMBParameters, 8, MaxSetupCount);
  119. ByteWriter.WriteByte(this.SMBParameters, 9, Reserved1);
  120. LittleEndianWriter.WriteUInt16(this.SMBParameters, 10, (ushort)Flags);
  121. LittleEndianWriter.WriteUInt32(this.SMBParameters, 12, Timeout);
  122. LittleEndianWriter.WriteUInt16(this.SMBParameters, 16, Reserved2);
  123. LittleEndianWriter.WriteUInt16(this.SMBParameters, 18, ParameterCount);
  124. LittleEndianWriter.WriteUInt16(this.SMBParameters, 20, ParameterOffset);
  125. LittleEndianWriter.WriteUInt16(this.SMBParameters, 22, DataCount);
  126. LittleEndianWriter.WriteUInt16(this.SMBParameters, 24, DataOffset);
  127. ByteWriter.WriteByte(this.SMBParameters, 26, SetupCount);
  128. ByteWriter.WriteByte(this.SMBParameters, 27, Reserved3);
  129. ByteWriter.WriteBytes(this.SMBParameters, 28, Setup);
  130. int offset;
  131. if (this is Transaction2Request)
  132. {
  133. int nameLength = 1;
  134. this.SMBData = new byte[nameLength + padding1 + ParameterCount + padding2 + DataCount];
  135. offset = nameLength;
  136. }
  137. else
  138. {
  139. if (isUnicode)
  140. {
  141. int namePadding = 1;
  142. this.SMBData = new byte[namePadding + Name.Length * 2 + 2 + padding1 + ParameterCount + padding2 + DataCount];
  143. offset = namePadding;
  144. }
  145. else
  146. {
  147. this.SMBData = new byte[Name.Length + 1 + padding1 + ParameterCount + padding2 + DataCount];
  148. offset = 0;
  149. }
  150. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, Name);
  151. }
  152. ByteWriter.WriteBytes(this.SMBData, offset + padding1, TransParameters);
  153. ByteWriter.WriteBytes(this.SMBData, offset + padding1 + ParameterCount + padding2, TransData);
  154. return base.GetBytes(isUnicode);
  155. }
  156. public override CommandName CommandName
  157. {
  158. get
  159. {
  160. return CommandName.SMB_COM_TRANSACTION;
  161. }
  162. }
  163. }
  164. }