ConnectionState.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.NetBios;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public delegate void LogDelegate(Severity severity, string message);
  16. public enum SMBDialect
  17. {
  18. NotSet,
  19. NTLM012, // NT LM 0.12
  20. SMB202, // SMB 2.0.2
  21. SMB210, // SMB 2.1
  22. }
  23. public class ConnectionState
  24. {
  25. public Socket ClientSocket;
  26. public IPEndPoint ClientEndPoint;
  27. public NBTConnectionReceiveBuffer ReceiveBuffer;
  28. protected LogDelegate LogToServerHandler;
  29. public SMBDialect ServerDialect;
  30. public ConnectionState(LogDelegate logToServerHandler)
  31. {
  32. ReceiveBuffer = new NBTConnectionReceiveBuffer();
  33. LogToServerHandler = logToServerHandler;
  34. ServerDialect = SMBDialect.NotSet;
  35. }
  36. public ConnectionState(ConnectionState state)
  37. {
  38. ClientSocket = state.ClientSocket;
  39. ClientEndPoint = state.ClientEndPoint;
  40. ReceiveBuffer = state.ReceiveBuffer;
  41. LogToServerHandler = state.LogToServerHandler;
  42. ServerDialect = state.ServerDialect;
  43. }
  44. public void LogToServer(Severity severity, string message)
  45. {
  46. message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
  47. if (LogToServerHandler != null)
  48. {
  49. LogToServerHandler(severity, message);
  50. }
  51. }
  52. public void LogToServer(Severity severity, string message, params object[] args)
  53. {
  54. LogToServer(severity, String.Format(message, args));
  55. }
  56. public string ConnectionIdentifier
  57. {
  58. get
  59. {
  60. if (ClientEndPoint != null)
  61. {
  62. return ClientEndPoint.Address + ":" + ClientEndPoint.Port;
  63. }
  64. return String.Empty;
  65. }
  66. }
  67. }
  68. }