SMB1ConnectionState.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.Server
  12. {
  13. public class SMB1ConnectionState : ConnectionState
  14. {
  15. public int MaxBufferSize;
  16. public bool LargeRead;
  17. public bool LargeWrite;
  18. // Key is UID
  19. private Dictionary<ushort, SMB1Session> m_sessions = new Dictionary<ushort, SMB1Session>();
  20. private ushort m_nextUID = 1; // UID MUST be unique within an SMB connection
  21. private ushort m_nextTID = 1; // TID MUST be unique within an SMB connection
  22. private ushort m_nextFID = 1; // FID MUST be unique within an SMB connection
  23. // Key is PID (PID MUST be unique within an SMB connection)
  24. private Dictionary<uint, ProcessStateObject> m_processStateList = new Dictionary<uint, ProcessStateObject>();
  25. public SMB1ConnectionState(ConnectionState state) : base(state)
  26. {
  27. }
  28. /// <summary>
  29. /// An open UID MUST be unique within an SMB connection.
  30. /// The value of 0xFFFE SHOULD NOT be used as a valid UID. All other possible values for a UID, excluding zero (0x0000), are valid.
  31. /// </summary>
  32. public ushort? AllocateUserID()
  33. {
  34. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  35. {
  36. ushort userID = (ushort)(m_nextUID + offset);
  37. if (userID == 0 || userID == 0xFFFE || userID == 0xFFFF)
  38. {
  39. continue;
  40. }
  41. if (!m_sessions.ContainsKey(userID))
  42. {
  43. m_nextUID = (ushort)(userID + 1);
  44. return userID;
  45. }
  46. }
  47. return null;
  48. }
  49. public SMB1Session CreateSession(ushort userID, string userName, string machineName)
  50. {
  51. SMB1Session session = new SMB1Session(this, userID, userName, machineName);
  52. m_sessions.Add(userID, session);
  53. return session;
  54. }
  55. /// <returns>null if all UserID values have already been allocated</returns>
  56. public SMB1Session CreateSession(string userName, string machineName)
  57. {
  58. ushort? userID = AllocateUserID();
  59. if (userID.HasValue)
  60. {
  61. return CreateSession(userID.Value, userName, machineName);
  62. }
  63. return null;
  64. }
  65. public SMB1Session GetSession(ushort userID)
  66. {
  67. SMB1Session session;
  68. m_sessions.TryGetValue(userID, out session);
  69. return session;
  70. }
  71. public void RemoveSession(ushort userID)
  72. {
  73. m_sessions.Remove(userID);
  74. }
  75. /// <summary>
  76. /// An open TID MUST be unique within an SMB connection.
  77. /// The value 0xFFFF MUST NOT be used as a valid TID. All other possible values for TID, including zero (0x0000), are valid.
  78. /// </summary>
  79. public ushort? AllocateTreeID()
  80. {
  81. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  82. {
  83. ushort treeID = (ushort)(m_nextTID + offset);
  84. if (treeID == 0 || treeID == 0xFFFF)
  85. {
  86. continue;
  87. }
  88. if (!IsTreeIDAllocated(treeID))
  89. {
  90. m_nextTID = (ushort)(treeID + 1);
  91. return treeID;
  92. }
  93. }
  94. return null;
  95. }
  96. private bool IsTreeIDAllocated(ushort treeID)
  97. {
  98. foreach (SMB1Session session in m_sessions.Values)
  99. {
  100. if (session.GetConnectedTree(treeID) != null)
  101. {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. /// <summary>
  108. /// A FID returned from an Open or Create operation MUST be unique within an SMB connection.
  109. /// The value 0xFFFF MUST NOT be used as a valid FID. All other possible values for FID, including zero (0x0000) are valid.
  110. /// </summary>
  111. /// <returns></returns>
  112. public ushort? AllocateFileID()
  113. {
  114. for (ushort offset = 0; offset < UInt16.MaxValue; offset++)
  115. {
  116. ushort fileID = (ushort)(m_nextFID + offset);
  117. if (fileID == 0 || fileID == 0xFFFF)
  118. {
  119. continue;
  120. }
  121. if (!IsFileIDAllocated(fileID))
  122. {
  123. m_nextFID = (ushort)(fileID + 1);
  124. return fileID;
  125. }
  126. }
  127. return null;
  128. }
  129. private bool IsFileIDAllocated(ushort fileID)
  130. {
  131. foreach (SMB1Session session in m_sessions.Values)
  132. {
  133. if (session.GetOpenFileObject(fileID) != null)
  134. {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. public ProcessStateObject GetProcessState(uint processID)
  141. {
  142. if (m_processStateList.ContainsKey(processID))
  143. {
  144. return m_processStateList[processID];
  145. }
  146. else
  147. {
  148. return null;
  149. }
  150. }
  151. /// <summary>
  152. /// Get or Create process state
  153. /// </summary>
  154. public ProcessStateObject ObtainProcessState(uint processID)
  155. {
  156. if (m_processStateList.ContainsKey(processID))
  157. {
  158. return m_processStateList[processID];
  159. }
  160. else
  161. {
  162. ProcessStateObject processState = new ProcessStateObject();
  163. m_processStateList[processID] = processState;
  164. return processState;
  165. }
  166. }
  167. public uint? GetMaxDataCount(uint processID)
  168. {
  169. ProcessStateObject processState = GetProcessState(processID);
  170. if (processState != null)
  171. {
  172. return processState.MaxDataCount;
  173. }
  174. else
  175. {
  176. return null;
  177. }
  178. }
  179. }
  180. }