ReadWriteResponseHelper.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 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 SMBLibrary.Authentication;
  10. using SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. public class ReadWriteResponseHelper
  15. {
  16. internal static SMB2Command GetReadResponse(ReadRequest request, ISMBShare share, SMB2ConnectionState state)
  17. {
  18. SMB2Session session = state.GetSession(request.Header.SessionID);
  19. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  20. if (openFile == null)
  21. {
  22. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  23. }
  24. byte[] data;
  25. NTStatus readStatus = NTFileSystemHelper.ReadFile(out data, openFile, (long)request.Offset, (int)request.ReadLength, state);
  26. if (readStatus != NTStatus.STATUS_SUCCESS)
  27. {
  28. return new ErrorResponse(request.CommandName, readStatus);
  29. }
  30. ReadResponse response = new ReadResponse();
  31. response.Data = data;
  32. return response;
  33. }
  34. internal static SMB2Command GetWriteResponse(WriteRequest request, ISMBShare share, SMB2ConnectionState state)
  35. {
  36. SMB2Session session = state.GetSession(request.Header.SessionID);
  37. OpenFileObject openFile = session.GetOpenFileObject(request.FileId.Persistent);
  38. if (openFile == null)
  39. {
  40. return new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED);
  41. }
  42. int numberOfBytesWritten;
  43. NTStatus writeStatus = NTFileSystemHelper.WriteFile(out numberOfBytesWritten, openFile, (long)request.Offset, request.Data, state);
  44. if (writeStatus != NTStatus.STATUS_SUCCESS)
  45. {
  46. return new ErrorResponse(request.CommandName, writeStatus);
  47. }
  48. WriteResponse response = new WriteResponse();
  49. response.Count = (uint)numberOfBytesWritten;
  50. return response;
  51. }
  52. }
  53. }