ConnectionState.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.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. public Socket ClientSocket;
  20. public IPEndPoint ClientEndPoint;
  21. public NBTConnectionReceiveBuffer ReceiveBuffer;
  22. public BlockingQueue<SessionPacket> SendQueue;
  23. protected LogDelegate LogToServerHandler;
  24. public SMBDialect Dialect;
  25. public GSSContext AuthenticationContext;
  26. public ConnectionState(LogDelegate logToServerHandler)
  27. {
  28. ReceiveBuffer = new NBTConnectionReceiveBuffer();
  29. SendQueue = new BlockingQueue<SessionPacket>();
  30. LogToServerHandler = logToServerHandler;
  31. Dialect = SMBDialect.NotSet;
  32. }
  33. public ConnectionState(ConnectionState state)
  34. {
  35. ClientSocket = state.ClientSocket;
  36. ClientEndPoint = state.ClientEndPoint;
  37. ReceiveBuffer = state.ReceiveBuffer;
  38. SendQueue = state.SendQueue;
  39. LogToServerHandler = state.LogToServerHandler;
  40. Dialect = state.Dialect;
  41. }
  42. /// <summary>
  43. /// Free all resources used by the active sessions in this connection
  44. /// </summary>
  45. public virtual void CloseSessions()
  46. {
  47. }
  48. public virtual List<SessionInformation> GetSessionsInformation()
  49. {
  50. return new List<SessionInformation>();
  51. }
  52. public void LogToServer(Severity severity, string message)
  53. {
  54. message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
  55. if (LogToServerHandler != null)
  56. {
  57. LogToServerHandler(severity, message);
  58. }
  59. }
  60. public void LogToServer(Severity severity, string message, params object[] args)
  61. {
  62. LogToServer(severity, String.Format(message, args));
  63. }
  64. public string ConnectionIdentifier
  65. {
  66. get
  67. {
  68. if (ClientEndPoint != null)
  69. {
  70. return ClientEndPoint.Address + ":" + ClientEndPoint.Port;
  71. }
  72. return String.Empty;
  73. }
  74. }
  75. }
  76. }