SMB2Session.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 SMBLibrary.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server
  13. {
  14. public class SMB2Session
  15. {
  16. private SMB2ConnectionState m_connection;
  17. private ulong m_sessionID;
  18. private string m_userName;
  19. // Key is TreeID
  20. private Dictionary<uint, ISMBShare> m_connectedTrees = new Dictionary<uint, ISMBShare>();
  21. private uint m_nextTreeID = 1; // TreeID uniquely identifies a tree connect within the scope of the session
  22. // Key is the persistent portion of the FileID
  23. private Dictionary<ulong, OpenFileObject> m_openFiles = new Dictionary<ulong, OpenFileObject>();
  24. // Key is the persistent portion of the FileID
  25. private Dictionary<ulong, OpenSearch> m_openSearches = new Dictionary<ulong, OpenSearch>();
  26. public SMB2Session(SMB2ConnectionState connecton, ulong sessionID, string userName)
  27. {
  28. m_connection = connecton;
  29. m_sessionID = sessionID;
  30. m_userName = userName;
  31. }
  32. private uint? AllocateTreeID()
  33. {
  34. for (uint offset = 0; offset < UInt32.MaxValue; offset++)
  35. {
  36. uint treeID = (uint)(m_nextTreeID + offset);
  37. if (treeID == 0 || treeID == 0xFFFFFFFF)
  38. {
  39. continue;
  40. }
  41. if (!m_connectedTrees.ContainsKey(treeID))
  42. {
  43. m_nextTreeID = (uint)(treeID + 1);
  44. return treeID;
  45. }
  46. }
  47. return null;
  48. }
  49. public uint? AddConnectedTree(ISMBShare share)
  50. {
  51. uint? treeID = AllocateTreeID();
  52. if (treeID.HasValue)
  53. {
  54. m_connectedTrees.Add(treeID.Value, share);
  55. }
  56. return treeID;
  57. }
  58. public ISMBShare GetConnectedTree(uint treeID)
  59. {
  60. if (m_connectedTrees.ContainsKey(treeID))
  61. {
  62. return m_connectedTrees[treeID];
  63. }
  64. else
  65. {
  66. return null;
  67. }
  68. }
  69. public void RemoveConnectedTree(uint treeID)
  70. {
  71. m_connectedTrees.Remove(treeID);
  72. }
  73. public void RemoveConnectedTrees()
  74. {
  75. m_connectedTrees.Clear();
  76. }
  77. public bool IsTreeConnected(uint treeID)
  78. {
  79. return m_connectedTrees.ContainsKey(treeID);
  80. }
  81. /// <param name="relativePath">Should include the path relative to the share</param>
  82. /// <returns>The persistent portion of the FileID</returns>
  83. public ulong? AddOpenFile(string relativePath)
  84. {
  85. return AddOpenFile(relativePath, null);
  86. }
  87. public ulong? AddOpenFile(string relativePath, Stream stream)
  88. {
  89. return AddOpenFile(relativePath, stream, false);
  90. }
  91. public ulong? AddOpenFile(string relativePath, Stream stream, bool deleteOnClose)
  92. {
  93. ulong? persistentID = m_connection.AllocatePersistentFileID();
  94. if (persistentID.HasValue)
  95. {
  96. m_openFiles.Add(persistentID.Value, new OpenFileObject(relativePath, stream, deleteOnClose));
  97. }
  98. return persistentID;
  99. }
  100. public OpenFileObject GetOpenFileObject(ulong fileID)
  101. {
  102. if (m_openFiles.ContainsKey(fileID))
  103. {
  104. return m_openFiles[fileID];
  105. }
  106. else
  107. {
  108. return null;
  109. }
  110. }
  111. public void RemoveOpenFile(ulong fileID)
  112. {
  113. Stream stream = m_openFiles[fileID].Stream;
  114. if (stream != null)
  115. {
  116. stream.Close();
  117. }
  118. m_openFiles.Remove(fileID);
  119. m_openSearches.Remove(fileID);
  120. }
  121. public OpenSearch AddOpenSearch(ulong fileID, List<FileSystemEntry> entries, int enumerationLocation)
  122. {
  123. OpenSearch openSearch = new OpenSearch(entries, enumerationLocation);
  124. m_openSearches.Add(fileID, openSearch);
  125. return openSearch;
  126. }
  127. public OpenSearch GetOpenSearch(ulong fileID)
  128. {
  129. OpenSearch openSearch;
  130. m_openSearches.TryGetValue(fileID, out openSearch);
  131. return openSearch;
  132. }
  133. public void RemoveOpenSearch(ulong fileID)
  134. {
  135. m_openSearches.Remove(fileID);
  136. }
  137. public string UserName
  138. {
  139. get
  140. {
  141. return m_userName;
  142. }
  143. }
  144. }
  145. }