SMB2ConnectionState.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. public 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)
  43. {
  44. SMB2Session session = new SMB2Session(this, sessionID, userName, machineName);
  45. m_sessions.Add(sessionID, session);
  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. m_sessions.Remove(sessionID);
  57. }
  58. public void ClearSessions()
  59. {
  60. m_sessions.Clear();
  61. }
  62. }
  63. }