NegotiateRequest.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.IO;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_NEGOTIATE Request
  15. /// </summary>
  16. public class NegotiateRequest : SMB1Command
  17. {
  18. public const int SupportedBufferFormat = 0x02;
  19. // Data:
  20. public List<string> Dialects = new List<string>();
  21. public NegotiateRequest() : base()
  22. {
  23. }
  24. public NegotiateRequest(byte[] buffer, int offset) : base(buffer, offset, false)
  25. {
  26. int dataOffset = 0;
  27. while (dataOffset < this.SMBData.Length)
  28. {
  29. byte bufferFormat = ByteReader.ReadByte(this.SMBData, ref dataOffset);
  30. if (bufferFormat != SupportedBufferFormat)
  31. {
  32. throw new InvalidDataException("Unsupported Buffer Format");
  33. }
  34. string dialect = ByteReader.ReadNullTerminatedAnsiString(this.SMBData, dataOffset);
  35. Dialects.Add(dialect);
  36. dataOffset += dialect.Length + 1;
  37. }
  38. }
  39. public override byte[] GetBytes(bool isUnicode)
  40. {
  41. int length = 0;
  42. foreach (string dialect in this.Dialects)
  43. {
  44. length += 1 + dialect.Length + 1;
  45. }
  46. this.SMBParameters = new byte[0];
  47. this.SMBData = new byte[length];
  48. int offset = 0;
  49. foreach (string dialect in this.Dialects)
  50. {
  51. ByteWriter.WriteByte(this.SMBData, offset, 0x02);
  52. ByteWriter.WriteAnsiString(this.SMBData, offset + 1, dialect, dialect.Length);
  53. ByteWriter.WriteByte(this.SMBData, offset + 1 + dialect.Length, 0x00);
  54. offset += 1 + dialect.Length + 1;
  55. }
  56. return base.GetBytes(isUnicode);
  57. }
  58. public override CommandName CommandName
  59. {
  60. get
  61. {
  62. return CommandName.SMB_COM_NEGOTIATE;
  63. }
  64. }
  65. }
  66. }