TransactionRequest.cs 7.7 KB

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