ISCSIServer.Logout.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 2012-2018 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 Utilities;
  10. namespace ISCSI.Server
  11. {
  12. public partial class ISCSIServer
  13. {
  14. private ISCSIPDU GetLogoutResponsePDU(LogoutRequestPDU request, ConnectionParameters connection)
  15. {
  16. Log(Severity.Verbose, "[{0}] Logout Request", connection.ConnectionIdentifier);
  17. if (connection.Session.IsDiscovery && request.ReasonCode != LogoutReasonCode.CloseTheSession)
  18. {
  19. // RFC 3720: Discovery-session: The target MUST ONLY accept [..] logout request with the reason "close the session"
  20. RejectPDU reject = new RejectPDU();
  21. reject.Reason = RejectReason.ProtocolError;
  22. reject.Data = ByteReader.ReadBytes(request.GetBytes(), 0, 48);
  23. return reject;
  24. }
  25. else
  26. {
  27. List<ConnectionState> connectionsToClose = new List<ConnectionState>();
  28. if (request.ReasonCode == LogoutReasonCode.CloseTheSession)
  29. {
  30. connectionsToClose = m_connectionManager.GetSessionConnections(connection.Session);
  31. }
  32. else if (request.ReasonCode == LogoutReasonCode.CloseTheConnection)
  33. {
  34. // RFC 3720: A Logout for a CID may be performed on a different transport connection when the TCP connection for the CID has already been terminated.
  35. ConnectionState existingConnection = m_connectionManager.FindConnection(connection.Session, request.CID);
  36. if (existingConnection != null)
  37. {
  38. connectionsToClose.Add(existingConnection);
  39. }
  40. else
  41. {
  42. return ServerResponseHelper.GetLogoutResponsePDU(request, LogoutResponse.CIDNotFound);
  43. }
  44. }
  45. else if (request.ReasonCode == LogoutReasonCode.RemoveTheConnectionForRecovery)
  46. {
  47. return ServerResponseHelper.GetLogoutResponsePDU(request, LogoutResponse.ConnectionRecoveryNotSupported);
  48. }
  49. else
  50. {
  51. // Unknown LogoutRequest ReasonCode
  52. RejectPDU reject = new RejectPDU();
  53. reject.Reason = RejectReason.ProtocolError;
  54. reject.Data = ByteReader.ReadBytes(request.GetBytes(), 0, 48);
  55. return reject;
  56. }
  57. foreach (ConnectionState connectionToClose in connectionsToClose)
  58. {
  59. // Wait for pending I/O to complete.
  60. connectionToClose.RunningSCSICommands.WaitUntilZero();
  61. if (connectionToClose.ConnectionParameters != connection)
  62. {
  63. SocketUtils.ReleaseSocket(connectionToClose.ClientSocket);
  64. }
  65. m_connectionManager.RemoveConnection(connectionToClose);
  66. }
  67. if (request.ReasonCode == LogoutReasonCode.CloseTheSession)
  68. {
  69. Log(Severity.Verbose, "[{0}] Session has been closed", connection.Session.SessionIdentifier);
  70. m_sessionManager.RemoveSession(connection.Session, SessionTerminationReason.Logout);
  71. }
  72. return ServerResponseHelper.GetLogoutResponsePDU(request, LogoutResponse.ClosedSuccessfully);
  73. // connection will be closed after a LogoutResponsePDU has been sent.
  74. }
  75. }
  76. }
  77. }