ConnectionState.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) 2014-2020 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 System.Net.Sockets;
  11. using SMBLibrary.Authentication.GSSAPI;
  12. using SMBLibrary.NetBios;
  13. using Utilities;
  14. namespace SMBLibrary.Server
  15. {
  16. internal delegate void LogDelegate(Severity severity, string message);
  17. internal class ConnectionState
  18. {
  19. private Socket m_clientSocket;
  20. private IPEndPoint m_clientEndPoint;
  21. private NBTConnectionReceiveBuffer m_receiveBuffer;
  22. private BlockingQueue<SessionPacket> m_sendQueue;
  23. private DateTime m_creationDT;
  24. private DateTime m_lastReceiveDT;
  25. private Reference<DateTime> m_lastSendDTRef; // We must use a reference because the sender thread will keep using the original ConnectionState object
  26. private LogDelegate LogToServerHandler;
  27. public SMBDialect Dialect;
  28. public GSSContext AuthenticationContext;
  29. public ConnectionState(Socket clientSocket, IPEndPoint clientEndPoint, LogDelegate logToServerHandler)
  30. {
  31. m_clientSocket = clientSocket;
  32. m_clientEndPoint = clientEndPoint;
  33. m_receiveBuffer = new NBTConnectionReceiveBuffer();
  34. m_sendQueue = new BlockingQueue<SessionPacket>();
  35. m_creationDT = DateTime.UtcNow;
  36. m_lastReceiveDT = DateTime.UtcNow;
  37. m_lastSendDTRef = DateTime.UtcNow;
  38. LogToServerHandler = logToServerHandler;
  39. Dialect = SMBDialect.NotSet;
  40. }
  41. public ConnectionState(ConnectionState state)
  42. {
  43. m_clientSocket = state.ClientSocket;
  44. m_clientEndPoint = state.ClientEndPoint;
  45. m_receiveBuffer = state.ReceiveBuffer;
  46. m_sendQueue = state.SendQueue;
  47. m_creationDT = state.CreationDT;
  48. m_lastReceiveDT = state.LastReceiveDT;
  49. m_lastSendDTRef = state.LastSendDTRef;
  50. LogToServerHandler = state.LogToServerHandler;
  51. Dialect = state.Dialect;
  52. }
  53. /// <summary>
  54. /// Free all resources used by the active sessions in this connection
  55. /// </summary>
  56. public virtual void CloseSessions()
  57. {
  58. }
  59. public virtual List<SessionInformation> GetSessionsInformation()
  60. {
  61. return new List<SessionInformation>();
  62. }
  63. public void LogToServer(Severity severity, string message)
  64. {
  65. message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
  66. if (LogToServerHandler != null)
  67. {
  68. LogToServerHandler(severity, message);
  69. }
  70. }
  71. public void LogToServer(Severity severity, string message, params object[] args)
  72. {
  73. LogToServer(severity, String.Format(message, args));
  74. }
  75. public Socket ClientSocket
  76. {
  77. get
  78. {
  79. return m_clientSocket;
  80. }
  81. }
  82. public IPEndPoint ClientEndPoint
  83. {
  84. get
  85. {
  86. return m_clientEndPoint;
  87. }
  88. }
  89. public NBTConnectionReceiveBuffer ReceiveBuffer
  90. {
  91. get
  92. {
  93. return m_receiveBuffer;
  94. }
  95. }
  96. public BlockingQueue<SessionPacket> SendQueue
  97. {
  98. get
  99. {
  100. return m_sendQueue;
  101. }
  102. }
  103. public DateTime CreationDT
  104. {
  105. get
  106. {
  107. return m_creationDT;
  108. }
  109. }
  110. public DateTime LastReceiveDT
  111. {
  112. get
  113. {
  114. return m_lastReceiveDT;
  115. }
  116. }
  117. public DateTime LastSendDT
  118. {
  119. get
  120. {
  121. return LastSendDTRef.Value;
  122. }
  123. }
  124. internal Reference<DateTime> LastSendDTRef
  125. {
  126. get
  127. {
  128. return m_lastSendDTRef;
  129. }
  130. }
  131. public void UpdateLastReceiveDT()
  132. {
  133. m_lastReceiveDT = DateTime.UtcNow;
  134. }
  135. public void UpdateLastSendDT()
  136. {
  137. m_lastSendDTRef.Value = DateTime.UtcNow;
  138. }
  139. public string ConnectionIdentifier
  140. {
  141. get
  142. {
  143. if (ClientEndPoint != null)
  144. {
  145. return ClientEndPoint.Address + ":" + ClientEndPoint.Port;
  146. }
  147. return String.Empty;
  148. }
  149. }
  150. }
  151. }