ConnectionManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (C) 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.Net;
  10. using Utilities;
  11. namespace SMBLibrary.Server
  12. {
  13. internal class ConnectionManager
  14. {
  15. private List<ConnectionState> m_activeConnections = new List<ConnectionState>();
  16. public void AddConnection(ConnectionState connection)
  17. {
  18. lock (m_activeConnections)
  19. {
  20. m_activeConnections.Add(connection);
  21. }
  22. }
  23. public bool RemoveConnection(ConnectionState connection)
  24. {
  25. lock (m_activeConnections)
  26. {
  27. int connectionIndex = m_activeConnections.IndexOf(connection);
  28. if (connectionIndex >= 0)
  29. {
  30. m_activeConnections.RemoveAt(connectionIndex);
  31. return true;
  32. }
  33. return false;
  34. }
  35. }
  36. public void ReleaseConnection(ConnectionState connection)
  37. {
  38. connection.SendQueue.Stop();
  39. SocketUtils.ReleaseSocket(connection.ClientSocket);
  40. connection.CloseSessions();
  41. RemoveConnection(connection);
  42. }
  43. public void ReleaseConnection(IPEndPoint clientEndPoint)
  44. {
  45. ConnectionState connection = FindConnection(clientEndPoint);
  46. if (connection != null)
  47. {
  48. ReleaseConnection(connection);
  49. }
  50. }
  51. /// <summary>
  52. /// Some broken NATs will reply to TCP KeepAlive even after the client initiating the connection has long gone,
  53. /// This methods prevent such connections from hanging around indefinitely by sending an unsolicited ECHO response to make sure the connection is still alive.
  54. /// </summary>
  55. public void SendSMBKeepAlive(TimeSpan inactivityDuration)
  56. {
  57. List<ConnectionState> connections = new List<ConnectionState>(m_activeConnections);
  58. foreach (ConnectionState connection in connections)
  59. {
  60. if (connection.LastReceiveDT.Add(inactivityDuration) < DateTime.UtcNow &&
  61. connection.LastSendDT.Add(inactivityDuration) < DateTime.UtcNow)
  62. {
  63. if (connection is SMB1ConnectionState)
  64. {
  65. // [MS-CIFS] Clients SHOULD, at minimum, send an SMB_COM_ECHO to the server every few minutes.
  66. // This means that an unsolicited SMB_COM_ECHO reply is not likely to be sent on a connection that is alive.
  67. SMBLibrary.SMB1.SMB1Message echoReply = SMB1.EchoHelper.GetUnsolicitedEchoReply();
  68. SMBServer.EnqueueMessage(connection, echoReply);
  69. }
  70. else if (connection is SMB2ConnectionState)
  71. {
  72. SMBLibrary.SMB2.EchoResponse echoResponse = SMB2.EchoHelper.GetUnsolicitedEchoResponse();
  73. SMBServer.EnqueueResponse(connection, echoResponse);
  74. }
  75. }
  76. }
  77. }
  78. public void ReleaseAllConnections()
  79. {
  80. List<ConnectionState> connections = new List<ConnectionState>(m_activeConnections);
  81. foreach (ConnectionState connection in connections)
  82. {
  83. ReleaseConnection(connection);
  84. }
  85. }
  86. private ConnectionState FindConnection(IPEndPoint clientEndPoint)
  87. {
  88. lock (m_activeConnections)
  89. {
  90. for (int index = 0; index < m_activeConnections.Count; index++)
  91. {
  92. if (m_activeConnections[index].ClientEndPoint.Equals(clientEndPoint))
  93. {
  94. return m_activeConnections[index];
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. public List<SessionInformation> GetSessionsInformation()
  101. {
  102. List<SessionInformation> result = new List<SessionInformation>();
  103. lock (m_activeConnections)
  104. {
  105. foreach (ConnectionState connection in m_activeConnections)
  106. {
  107. List<SessionInformation> sessions = connection.GetSessionsInformation();
  108. result.AddRange(sessions);
  109. }
  110. }
  111. return result;
  112. }
  113. }
  114. }