ConnectionState.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 object AuthenticationContext;
  31. public ConnectionState(LogDelegate logToServerHandler)
  32. {
  33. ReceiveBuffer = new NBTConnectionReceiveBuffer();
  34. LogToServerHandler = logToServerHandler;
  35. ServerDialect = SMBDialect.NotSet;
  36. }
  37. public ConnectionState(ConnectionState state)
  38. {
  39. ClientSocket = state.ClientSocket;
  40. ClientEndPoint = state.ClientEndPoint;
  41. ReceiveBuffer = state.ReceiveBuffer;
  42. LogToServerHandler = state.LogToServerHandler;
  43. ServerDialect = state.ServerDialect;
  44. }
  45. public void LogToServer(Severity severity, string message)
  46. {
  47. message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
  48. if (LogToServerHandler != null)
  49. {
  50. LogToServerHandler(severity, message);
  51. }
  52. }
  53. public void LogToServer(Severity severity, string message, params object[] args)
  54. {
  55. LogToServer(severity, String.Format(message, args));
  56. }
  57. public string ConnectionIdentifier
  58. {
  59. get
  60. {
  61. if (ClientEndPoint != null)
  62. {
  63. return ClientEndPoint.Address + ":" + ClientEndPoint.Port;
  64. }
  65. return String.Empty;
  66. }
  67. }
  68. }
  69. }