SMB2ConnectionState.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 delegate ulong? AllocatePersistentFileID();
  15. internal class SMB2ConnectionState : ConnectionState
  16. {
  17. // Key is SessionID
  18. private Dictionary<ulong, SMB2Session> m_sessions = new Dictionary<ulong, SMB2Session>();
  19. private ulong m_nextSessionID = 1;
  20. public AllocatePersistentFileID AllocatePersistentFileID;
  21. public SMB2ConnectionState(ConnectionState state, AllocatePersistentFileID allocatePersistentFileID) : base(state)
  22. {
  23. AllocatePersistentFileID = allocatePersistentFileID;
  24. }
  25. public ulong? AllocateSessionID()
  26. {
  27. for (ulong offset = 0; offset < UInt64.MaxValue; offset++)
  28. {
  29. ulong sessionID = (ulong)(m_nextSessionID + offset);
  30. if (sessionID == 0 || sessionID == 0xFFFFFFFF)
  31. {
  32. continue;
  33. }
  34. if (!m_sessions.ContainsKey(sessionID))
  35. {
  36. m_nextSessionID = (ulong)(sessionID + 1);
  37. return sessionID;
  38. }
  39. }
  40. return null;
  41. }
  42. public SMB2Session CreateSession(ulong sessionID, string userName, string machineName, byte[] sessionKey, object accessToken)
  43. {
  44. SMB2Session session = new SMB2Session(this, sessionID, userName, machineName, sessionKey, accessToken);
  45. lock (m_sessions)
  46. {
  47. m_sessions.Add(sessionID, session);
  48. }
  49. return session;
  50. }
  51. public SMB2Session GetSession(ulong sessionID)
  52. {
  53. SMB2Session session;
  54. m_sessions.TryGetValue(sessionID, out session);
  55. return session;
  56. }
  57. public void RemoveSession(ulong sessionID)
  58. {
  59. SMB2Session session;
  60. m_sessions.TryGetValue(sessionID, out session);
  61. if (session != null)
  62. {
  63. session.Close();
  64. lock (m_sessions)
  65. {
  66. m_sessions.Remove(sessionID);
  67. }
  68. }
  69. }
  70. public override void CloseSessions()
  71. {
  72. lock (m_sessions)
  73. {
  74. foreach (SMB2Session session in m_sessions.Values)
  75. {
  76. session.Close();
  77. }
  78. }
  79. m_sessions.Clear();
  80. }
  81. public override List<SessionInformation> GetSessionsInformation()
  82. {
  83. List<SessionInformation> result = new List<SessionInformation>();
  84. lock (m_sessions)
  85. {
  86. foreach (SMB2Session session in m_sessions.Values)
  87. {
  88. result.Add(new SessionInformation(this.ClientEndPoint, this.Dialect, session.UserName, session.MachineName, session.ListOpenFiles(), session.CreationDT));
  89. }
  90. }
  91. return result;
  92. }
  93. }
  94. }