SessionSetupAndXResponse.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_SESSION_SETUP_ANDX Response
  15. /// </summary>
  16. public class SessionSetupAndXResponse : SMBAndXCommand
  17. {
  18. public const int ParametersLength = 6;
  19. // Parameters:
  20. //CommandName AndXCommand;
  21. //byte AndXReserved;
  22. //ushort AndXOffset;
  23. public SessionSetupAction Action;
  24. // Data:
  25. public string NativeOS; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  26. public string NativeLanMan; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  27. public string PrimaryDomain; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  28. public SessionSetupAndXResponse() : base()
  29. {
  30. NativeOS = String.Empty;
  31. NativeLanMan = String.Empty;
  32. PrimaryDomain = String.Empty;
  33. }
  34. public SessionSetupAndXResponse(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  35. {
  36. Action = (SessionSetupAction)LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  37. int dataOffset = 0;
  38. if (isUnicode)
  39. {
  40. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  41. // Note: SMBData starts at an odd offset.
  42. dataOffset++;
  43. }
  44. NativeOS = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  45. NativeLanMan = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  46. PrimaryDomain = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  47. }
  48. public override byte[] GetBytes(bool isUnicode)
  49. {
  50. this.SMBParameters = new byte[ParametersLength];
  51. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, (ushort)Action);
  52. int offset = 0;
  53. if (isUnicode)
  54. {
  55. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  56. // Note: SMBData starts at an odd offset.
  57. int padding = 1;
  58. this.SMBData = new byte[padding + NativeOS.Length * 2 + NativeLanMan.Length * 2 + PrimaryDomain.Length * 2 + 6];
  59. offset = padding;
  60. }
  61. else
  62. {
  63. this.SMBData = new byte[NativeOS.Length + NativeLanMan.Length + PrimaryDomain.Length + 3];
  64. }
  65. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeOS);
  66. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeLanMan);
  67. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, PrimaryDomain);
  68. return base.GetBytes(isUnicode);
  69. }
  70. public override CommandName CommandName
  71. {
  72. get
  73. {
  74. return CommandName.SMB_COM_SESSION_SETUP_ANDX;
  75. }
  76. }
  77. }
  78. }