SMB2ConnectionState.cs 3.2 KB

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