Prechádzať zdrojové kódy

Code refactoring: moved Session inside ConnectionParameters

Tal Aloni 8 rokov pred
rodič
commit
ec6ba4a18b

+ 27 - 14
ISCSI/ISCSI.Client/ClientHelper.cs

@@ -15,8 +15,9 @@ namespace ISCSI.Client
     public class ClientHelper
     {
         /// <param name="targetName">Set to null for discovery session</param>
-        internal static LoginRequestPDU GetFirstStageLoginRequest(string initiatorName, string targetName, ISCSISession session, ConnectionParameters connection)
+        internal static LoginRequestPDU GetFirstStageLoginRequest(string initiatorName, string targetName, ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             LoginRequestPDU request = new LoginRequestPDU();
             request.InitiatorTaskTag = session.GetNextTaskTag();
             request.ISID = session.ISID;
@@ -51,8 +52,9 @@ namespace ISCSI.Client
             return request;
         }
 
-        internal static LoginRequestPDU GetSecondStageLoginRequest(LoginResponsePDU firstStageResponse, ISCSISession session, ConnectionParameters connection, bool isDiscovery)
+        internal static LoginRequestPDU GetSecondStageLoginRequest(LoginResponsePDU firstStageResponse, ConnectionParameters connection, bool isDiscovery)
         {
+            ISCSISession session = connection.Session;
             LoginRequestPDU request = new LoginRequestPDU();
             request.ISID = firstStageResponse.ISID;
             request.TSIH = firstStageResponse.TSIH;
@@ -86,8 +88,9 @@ namespace ISCSI.Client
             return request;
         }
 
-        internal static LoginRequestPDU GetSingleStageLoginRequest(string initiatorName, string targetName, ISCSISession session, ConnectionParameters connection)
+        internal static LoginRequestPDU GetSingleStageLoginRequest(string initiatorName, string targetName, ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             LoginRequestPDU request = new LoginRequestPDU();
             request.ISID = session.ISID;
             request.TSIH = session.TSIH;
@@ -130,8 +133,9 @@ namespace ISCSI.Client
             return request;
         }
 
-        internal static void UpdateOperationalParameters(KeyValuePairList<string, string> loginParameters, ISCSISession session, ConnectionParameters connection)
+        internal static void UpdateOperationalParameters(KeyValuePairList<string, string> loginParameters, ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             string value = loginParameters.ValueOf("MaxRecvDataSegmentLength");
             if (value != null)
             {
@@ -249,8 +253,9 @@ namespace ISCSI.Client
             }
         }
 
-        internal static LogoutRequestPDU GetLogoutRequest(ISCSISession session, ConnectionParameters connection)
+        internal static LogoutRequestPDU GetLogoutRequest(ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             LogoutRequestPDU request = new LogoutRequestPDU();
             request.ReasonCode = LogoutReasonCode.CloseTheSession;
             request.InitiatorTaskTag = session.GetNextTaskTag();
@@ -260,8 +265,9 @@ namespace ISCSI.Client
             return request;
         }
 
-        internal static TextRequestPDU GetSendTargetsRequest(ISCSISession session, ConnectionParameters connection)
+        internal static TextRequestPDU GetSendTargetsRequest(ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             TextRequestPDU request = new TextRequestPDU();
             request.Text = "SendTargets=All";
             request.InitiatorTaskTag = session.GetNextTaskTag();
@@ -271,8 +277,9 @@ namespace ISCSI.Client
             return request;
         }
 
-        internal static SCSICommandPDU GetReportLUNsCommand(ISCSISession session, ConnectionParameters connection, uint allocationLength)
+        internal static SCSICommandPDU GetReportLUNsCommand(ConnectionParameters connection, uint allocationLength)
         {
+            ISCSISession session = connection.Session;
             SCSICommandDescriptorBlock reportLUNs = SCSICommandDescriptorBlock.Create(SCSIOpCodeName.ReportLUNs);
             reportLUNs.TransferLength = allocationLength;
             
@@ -286,8 +293,9 @@ namespace ISCSI.Client
             return scsiCommand;
         }
 
-        internal static SCSICommandPDU GetReadCapacity10Command(ISCSISession session, ConnectionParameters connection, ushort LUN)
+        internal static SCSICommandPDU GetReadCapacity10Command(ConnectionParameters connection, ushort LUN)
         {
+            ISCSISession session = connection.Session;
             SCSICommandDescriptorBlock readCapacity10 = SCSICommandDescriptorBlock.Create(SCSIOpCodeName.ReadCapacity10);
             readCapacity10.TransferLength = ReadCapacity10Parameter.Length;
 
@@ -302,8 +310,9 @@ namespace ISCSI.Client
             return scsiCommand;
         }
 
-        internal static SCSICommandPDU GetReadCapacity16Command(ISCSISession session, ConnectionParameters connection, ushort LUN)
+        internal static SCSICommandPDU GetReadCapacity16Command(ConnectionParameters connection, ushort LUN)
         {
+            ISCSISession session = connection.Session;
             SCSICommandDescriptorBlock serviceActionIn = SCSICommandDescriptorBlock.Create(SCSIOpCodeName.ServiceActionIn);
             serviceActionIn.ServiceAction = ServiceAction.ReadCapacity16;
             serviceActionIn.TransferLength = ReadCapacity16Parameter.Length;
@@ -319,8 +328,9 @@ namespace ISCSI.Client
             return scsiCommand;
         }
 
-        internal static SCSICommandPDU GetRead16Command(ISCSISession session, ConnectionParameters connection, ushort LUN, ulong sectorIndex, uint sectorCount, int bytesPerSector)
+        internal static SCSICommandPDU GetRead16Command(ConnectionParameters connection, ushort LUN, ulong sectorIndex, uint sectorCount, int bytesPerSector)
         {
+            ISCSISession session = connection.Session;
             SCSICommandDescriptorBlock read16 = SCSICommandDescriptorBlock.Create(SCSIOpCodeName.Read16);
             read16.LogicalBlockAddress64 = sectorIndex;
             read16.TransferLength = sectorCount;
@@ -336,8 +346,9 @@ namespace ISCSI.Client
             return scsiCommand;
         }
 
-        internal static SCSICommandPDU GetWrite16Command(ISCSISession session, ConnectionParameters connection, ushort LUN, ulong sectorIndex, byte[] data, int bytesPerSector)
+        internal static SCSICommandPDU GetWrite16Command(ConnectionParameters connection, ushort LUN, ulong sectorIndex, byte[] data, int bytesPerSector)
         {
+            ISCSISession session = connection.Session;
             SCSICommandDescriptorBlock write16 = SCSICommandDescriptorBlock.Create(SCSIOpCodeName.Write16);
             write16.LogicalBlockAddress64 = sectorIndex;
             write16.TransferLength = (uint)(data.Length / bytesPerSector);
@@ -358,7 +369,7 @@ namespace ISCSI.Client
             return scsiCommand;
         }
 
-        internal static List<SCSIDataOutPDU> GetWriteData(ISCSISession session, ConnectionParameters connection, ushort LUN, ulong sectorIndex, byte[] data, int bytesPerSector, ReadyToTransferPDU readyToTransfer)
+        internal static List<SCSIDataOutPDU> GetWriteData(ConnectionParameters connection, ushort LUN, ulong sectorIndex, byte[] data, int bytesPerSector, ReadyToTransferPDU readyToTransfer)
         {
             List<SCSIDataOutPDU> result = new List<SCSIDataOutPDU>();
             // if readyToTransfer.DesiredDataTransferLength <= connection.TargetMaxRecvDataSegmentLength we must send multiple Data-Out PDUs
@@ -382,8 +393,9 @@ namespace ISCSI.Client
             return result;
         }
 
-        internal static NOPOutPDU GetPingRequest(ISCSISession session, ConnectionParameters connection)
+        internal static NOPOutPDU GetPingRequest(ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             // Microsoft iSCSI Target v3.1 expects that CmdSN won't be incremented after this request regardless of whether the ImmediateDelivery bit is set or not,
             // So we set the ImmediateDelivery bit to work around the issue.
             NOPOutPDU request = new NOPOutPDU();
@@ -396,8 +408,9 @@ namespace ISCSI.Client
             return request;
         }
 
-        internal static NOPOutPDU GetPingResponse(NOPInPDU request, ISCSISession session, ConnectionParameters connection)
+        internal static NOPOutPDU GetPingResponse(NOPInPDU request, ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             NOPOutPDU response = new NOPOutPDU();
             // If the Initiator Task Tag contains 0xffffffff, the I bit MUST be set to 1 and the CmdSN is not advanced after this PDU is sent.
             response.ImmediateDelivery = true;

+ 9 - 0
ISCSI/ISCSI.Client/ConnectionParameters.cs

@@ -12,6 +12,7 @@ namespace ISCSI.Client
 {
     internal class ConnectionParameters
     {
+        public ISCSISession Session;
         public ushort CID; // connection ID, generated by the initiator
         
         public int InitiatorMaxRecvDataSegmentLength = ISCSIClient.DeclaredParameters.MaxRecvDataSegmentLength;
@@ -19,5 +20,13 @@ namespace ISCSI.Client
 
         public bool StatusNumberingStarted;
         public uint ExpStatSN;
+
+        public string ConnectionIdentifier
+        {
+            get
+            {
+                return String.Format("ISID={0},TSIH={1},CID={2}", Session == null ? "0" : Session.ISID.ToString("x"), Session == null ? "0" : Session.TSIH.ToString("x"), this.CID.ToString("x"));
+            }
+        }
     }
 }

+ 20 - 28
ISCSI/ISCSI.Client/ISCSIClient.cs

@@ -19,7 +19,6 @@ namespace ISCSI.Client
 {
     public partial class ISCSIClient
     {
-        private ISCSISession m_session = new ISCSISession();
         private ConnectionParameters m_connection = new ConnectionParameters();
 
         private string m_initiatorName;
@@ -79,26 +78,27 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            m_session.ISID = ClientHelper.GetRandomISID();
-            m_connection.CID = m_session.GetNextCID();
+            m_connection.Session = new ISCSISession();
+            m_connection.Session.ISID = ClientHelper.GetRandomISID();
+            m_connection.CID = m_connection.Session.GetNextCID();
             // p.s. It's possible to perform a single stage login (stage 1 to stage 3, tested against Microsoft iSCSI Target v3.1)
-            LoginRequestPDU request = ClientHelper.GetFirstStageLoginRequest(m_initiatorName, targetName, m_session, m_connection);
+            LoginRequestPDU request = ClientHelper.GetFirstStageLoginRequest(m_initiatorName, targetName, m_connection);
             SendPDU(request);
             LoginResponsePDU response = WaitForPDU(request.InitiatorTaskTag) as LoginResponsePDU;
             if (response != null && response.Status == LoginResponseStatusName.Success)
             {
-                m_session.TSIH = response.TSIH;
+                m_connection.Session.TSIH = response.TSIH;
                 // Status numbering starts with the Login response to the first Login request of the connection
                 m_connection.StatusNumberingStarted = true;
                 m_connection.ExpStatSN = response.StatSN + 1;
 
-                request = ClientHelper.GetSecondStageLoginRequest(response, m_session, m_connection, targetName == null);
+                request = ClientHelper.GetSecondStageLoginRequest(response, m_connection, targetName == null);
                 SendPDU(request);
                 response = WaitForPDU(request.InitiatorTaskTag) as LoginResponsePDU;
                 if (response != null && response.Status == LoginResponseStatusName.Success)
                 {
                     KeyValuePairList<string, string> loginParameters = KeyValuePairUtils.GetKeyValuePairList(response.LoginParametersText);
-                    ClientHelper.UpdateOperationalParameters(loginParameters, m_session, m_connection);
+                    ClientHelper.UpdateOperationalParameters(loginParameters, m_connection);
                     return true;
                 }
             }
@@ -111,7 +111,7 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            LogoutRequestPDU request = ClientHelper.GetLogoutRequest(m_session, m_connection);
+            LogoutRequestPDU request = ClientHelper.GetLogoutRequest(m_connection);
             SendPDU(request);
             LogoutResponsePDU response = WaitForPDU(request.InitiatorTaskTag) as LogoutResponsePDU;
             return (response != null && response.Response == LogoutResponse.ClosedSuccessfully);
@@ -123,7 +123,7 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            TextRequestPDU request = ClientHelper.GetSendTargetsRequest(m_session, m_connection);
+            TextRequestPDU request = ClientHelper.GetSendTargetsRequest(m_connection);
             SendPDU(request);
             TextResponsePDU response = WaitForPDU(request.InitiatorTaskTag) as TextResponsePDU;
             if (response != null && response.Final)
@@ -148,7 +148,7 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            SCSICommandPDU reportLUNs = ClientHelper.GetReportLUNsCommand(m_session, m_connection, ReportLUNsParameter.MinimumAllocationLength);
+            SCSICommandPDU reportLUNs = ClientHelper.GetReportLUNsCommand(m_connection, ReportLUNsParameter.MinimumAllocationLength);
             SendPDU(reportLUNs);
             SCSIDataInPDU data = WaitForPDU(reportLUNs.InitiatorTaskTag) as SCSIDataInPDU;
             if (data != null && data.StatusPresent && data.Status == SCSIStatusCodeName.Good)
@@ -156,7 +156,7 @@ namespace ISCSI.Client
                 uint requiredAllocationLength = ReportLUNsParameter.GetRequiredAllocationLength(data.Data);
                 if (requiredAllocationLength > ReportLUNsParameter.MinimumAllocationLength)
                 {
-                    reportLUNs = ClientHelper.GetReportLUNsCommand(m_session, m_connection, requiredAllocationLength);
+                    reportLUNs = ClientHelper.GetReportLUNsCommand(m_connection, requiredAllocationLength);
                     m_clientSocket.Send(reportLUNs.GetBytes());
                     data = WaitForPDU(reportLUNs.InitiatorTaskTag) as SCSIDataInPDU;
 
@@ -188,7 +188,7 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            SCSICommandPDU readCapacity = ClientHelper.GetReadCapacity10Command(m_session, m_connection, LUN);
+            SCSICommandPDU readCapacity = ClientHelper.GetReadCapacity10Command(m_connection, LUN);
             SendPDU(readCapacity);
             // SCSIResponsePDU with CheckCondition could be returned in case of an error
             SCSIDataInPDU data = WaitForPDU(readCapacity.InitiatorTaskTag) as SCSIDataInPDU;
@@ -201,7 +201,7 @@ namespace ISCSI.Client
                     return (ulong)(capacity.ReturnedLBA + 1) * capacity.BlockLengthInBytes;
                 }
 
-                readCapacity = ClientHelper.GetReadCapacity16Command(m_session, m_connection, LUN);
+                readCapacity = ClientHelper.GetReadCapacity16Command(m_connection, LUN);
                 m_clientSocket.Send(readCapacity.GetBytes());
                 data = WaitForPDU(readCapacity.InitiatorTaskTag) as SCSIDataInPDU;
                 if (data != null && data.StatusPresent && data.Status == SCSIStatusCodeName.Good)
@@ -222,7 +222,7 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            SCSICommandPDU readCommand = ClientHelper.GetRead16Command(m_session, m_connection, LUN, sectorIndex, sectorCount, bytesPerSector);
+            SCSICommandPDU readCommand = ClientHelper.GetRead16Command(m_connection, LUN, sectorIndex, sectorCount, bytesPerSector);
             SendPDU(readCommand);
             // RFC 3720: Data payload is associated with a specific SCSI command through the Initiator Task Tag
             SCSIDataInPDU data = WaitForPDU(readCommand.InitiatorTaskTag) as SCSIDataInPDU;
@@ -252,12 +252,12 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            SCSICommandPDU writeCommand = ClientHelper.GetWrite16Command(m_session, m_connection, LUN, sectorIndex, data, bytesPerSector);
+            SCSICommandPDU writeCommand = ClientHelper.GetWrite16Command(m_connection, LUN, sectorIndex, data, bytesPerSector);
             SendPDU(writeCommand);
             ISCSIPDU response = WaitForPDU(writeCommand.InitiatorTaskTag);
             while (response is ReadyToTransferPDU)
             {
-                List<SCSIDataOutPDU> requestedData = ClientHelper.GetWriteData(m_session, m_connection, LUN, sectorIndex, data, bytesPerSector, (ReadyToTransferPDU)response);
+                List<SCSIDataOutPDU> requestedData = ClientHelper.GetWriteData(m_connection, LUN, sectorIndex, data, bytesPerSector, (ReadyToTransferPDU)response);
                 foreach (SCSIDataOutPDU dataOut in requestedData)
                 {
                     SendPDU(dataOut);
@@ -281,7 +281,7 @@ namespace ISCSI.Client
             {
                 throw new InvalidOperationException("iSCSI client is not connected");
             }
-            NOPOutPDU request = ClientHelper.GetPingRequest(m_session, m_connection);
+            NOPOutPDU request = ClientHelper.GetPingRequest(m_connection);
             SendPDU(request);
             NOPInPDU response = WaitForPDU(request.InitiatorTaskTag) as NOPInPDU;
             return response != null;
@@ -427,7 +427,7 @@ namespace ISCSI.Client
                 if (((NOPInPDU)pdu).TargetTransferTag != 0xFFFFFFFF)
                 {
                     // Send NOP-OUT
-                    NOPOutPDU response = ClientHelper.GetPingResponse((NOPInPDU)pdu, m_session, m_connection);
+                    NOPOutPDU response = ClientHelper.GetPingResponse((NOPInPDU)pdu, m_connection);
                     SendPDU(response);
                     return;
                 }
@@ -481,11 +481,11 @@ namespace ISCSI.Client
                     PDUHelper.SetExpStatSN(request, m_connection.ExpStatSN);
                 }
                 m_clientSocket.Send(request.GetBytes());
-                Log("[{0}][SendPDU] Sent request to target, Operation: {1}, Size: {2}", this.ConnectionIdentifier, (ISCSIOpCodeName)request.OpCode, request.Length);
+                Log("[{0}][SendPDU] Sent request to target, Operation: {1}, Size: {2}", m_connection.ConnectionIdentifier, (ISCSIOpCodeName)request.OpCode, request.Length);
             }
             catch (SocketException ex)
             {
-                Log("[{0}][SendPDU] Failed to send PDU to target (Operation: {1}, Size: {2}), SocketException: {3}", this.ConnectionIdentifier, (ISCSIOpCodeName)request.OpCode, request.Length, ex.Message);
+                Log("[{0}][SendPDU] Failed to send PDU to target (Operation: {1}, Size: {2}), SocketException: {3}", m_connection.ConnectionIdentifier, (ISCSIOpCodeName)request.OpCode, request.Length, ex.Message);
                 m_isConnected = false;
             }
             catch (ObjectDisposedException)
@@ -494,14 +494,6 @@ namespace ISCSI.Client
             }
         }
 
-        public string ConnectionIdentifier
-        {
-            get
-            {
-                return String.Format("ISID={0},TSIH={1},CID={2}", m_session.ISID.ToString("x"), m_session.TSIH.ToString("x"), m_connection.CID.ToString("x"));
-            }
-        }
-
         public bool IsConnected
         {
             get

+ 9 - 0
ISCSI/ISCSI.Server/ConnectionParameters.cs

@@ -30,6 +30,7 @@ namespace ISCSI.Server
 
     internal class ConnectionParameters
     {
+        public ISCSISession Session;
         public ushort CID; // connection ID, generated by the initiator
         public string InitiatorName = String.Empty;
         public IPEndPoint InitiatorEndPoint;
@@ -82,5 +83,13 @@ namespace ISCSI.Server
         {
             n_transfers.Remove(transferTag);
         }
+
+        public string ConnectionIdentifier
+        {
+            get
+            {
+                return String.Format("ISID={0},TSIH={1},CID={2}", Session == null ? "0" : Session.ISID.ToString("x"), Session == null ? "0" : Session.TSIH.ToString("x"), this.CID.ToString("x"));
+            }
+        }
     }
 }

+ 8 - 6
ISCSI/ISCSI.Server/ConnectionState.cs

@@ -26,7 +26,6 @@ namespace ISCSI.Server
         public byte[] ReceiveBuffer = new byte[ReceiveBufferSize]; // immediate receive buffer
         public byte[] ConnectionBuffer = new byte[0]; // we append the receive buffer here until we have a complete PDU
 
-        public ISCSISession Session;
         public ConnectionParameters ConnectionParameters = new ConnectionParameters();
 
         public CountdownLatch RunningSCSICommands = new CountdownLatch();
@@ -44,21 +43,24 @@ namespace ISCSI.Server
         {
             get
             {
-                return GetConnectionIdentifier(Session, ConnectionParameters);
+                return ConnectionParameters.ConnectionIdentifier;
             }
         }
 
-        public ISCSITarget Target
+        public ISCSISession Session
         {
             get
             {
-                return Session.Target;
+                return ConnectionParameters.Session;
             }
         }
 
-        public static string GetConnectionIdentifier(ISCSISession session, ConnectionParameters connection)
+        public ISCSITarget Target
         {
-            return String.Format("ISID={0},TSIH={1},CID={2}", session == null ? "0" : session.ISID.ToString("x"), session == null ? "0" : session.TSIH.ToString("x"), connection.CID.ToString("x"));
+            get
+            {
+                return Session.Target;
+            }
         }
     }
 }

+ 25 - 22
ISCSI/ISCSI.Server/ISCSIServer.Login.cs

@@ -14,23 +14,23 @@ namespace ISCSI.Server
 {
     public partial class ISCSIServer
     {
-        private LoginResponsePDU GetLoginResponsePDU(LoginRequestPDU request, ref ISCSISession session, ConnectionParameters connection)
+        private LoginResponsePDU GetLoginResponsePDU(LoginRequestPDU request, ConnectionParameters connection)
         {
             if (request.Continue)
             {
                 connection.AddTextToSequence(request.InitiatorTaskTag, request.LoginParametersText);
-                return GetPartialLoginResponsePDU(request, ref session, connection);
+                return GetPartialLoginResponsePDU(request, connection);
             }
             else
             {
                 string text = connection.AddTextToSequence(request.InitiatorTaskTag, request.LoginParametersText);
                 connection.RemoveTextSequence(request.InitiatorTaskTag);
                 KeyValuePairList<string, string> loginParameters = KeyValuePairUtils.GetKeyValuePairList(text);
-                return GetFinalLoginResponsePDU(request, loginParameters, ref session, connection);
+                return GetFinalLoginResponsePDU(request, loginParameters, connection);
             }
         }
 
-        private LoginResponsePDU GetPartialLoginResponsePDU(LoginRequestPDU request, ref ISCSISession session, ConnectionParameters connection)
+        private LoginResponsePDU GetPartialLoginResponsePDU(LoginRequestPDU request, ConnectionParameters connection)
         {
             LoginResponsePDU response = new LoginResponsePDU();
             response.Transit = false;
@@ -46,8 +46,7 @@ namespace ISCSI.Server
             response.InitiatorTaskTag = request.InitiatorTaskTag;
             if (request.Transit)
             {
-                string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
-                Log(Severity.Warning, "[{0}] Initiator error: Received login request with both Transit and Continue set to true", connectionIdentifier);
+                Log(Severity.Warning, "[{0}] Initiator error: Received login request with both Transit and Continue set to true", connection.ConnectionIdentifier);
                 response.Status = LoginResponseStatusName.InitiatorError;
                 return response;
             }
@@ -55,7 +54,7 @@ namespace ISCSI.Server
             return response;
         }
 
-        private LoginResponsePDU GetFinalLoginResponsePDU(LoginRequestPDU request, KeyValuePairList<string, string> requestParameters, ref ISCSISession session, ConnectionParameters connection)
+        private LoginResponsePDU GetFinalLoginResponsePDU(LoginRequestPDU request, KeyValuePairList<string, string> requestParameters, ConnectionParameters connection)
         {
             LoginResponsePDU response = new LoginResponsePDU();
             response.Transit = request.Transit;
@@ -75,33 +74,34 @@ namespace ISCSI.Server
 
             bool isFirstLoginRequest = false; // first login request in login phase
             bool isNewSession = false;
-            if (session == null)
+            if (connection.Session == null)
             {
                 if (request.TSIH == 0)
                 {
                     // For a new session, the request TSIH is zero,
                     // As part of the response, the target generates a TSIH.
-                    session = m_sessionManager.StartSession(request.ISID);
-                    Log(Severity.Verbose, "[{0}] Session has been started", session.SessionIdentifier);
-                    session.CommandNumberingStarted = true;
-                    session.ExpCmdSN = request.CmdSN;
+                    connection.Session = m_sessionManager.StartSession(request.ISID);
+                    Log(Severity.Verbose, "[{0}] Session has been started", connection.Session.SessionIdentifier);
+                    connection.Session.CommandNumberingStarted = true;
+                    connection.Session.ExpCmdSN = request.CmdSN;
                     isNewSession = true;
                 }
                 else
                 {
-                    session = m_sessionManager.FindSession(request.ISID, request.TSIH);
-                    if (session == null)
+                    ISCSISession existingSession = m_sessionManager.FindSession(request.ISID, request.TSIH);
+                    if (existingSession == null)
                     {
                         response.TSIH = request.TSIH;
                         response.Status = LoginResponseStatusName.SessionDoesNotExist;
                         return response;
                     }
+                    connection.Session = existingSession;
                 }
                 isFirstLoginRequest = true;
             }
-            response.TSIH = session.TSIH;
+            response.TSIH = connection.Session.TSIH;
 
-            string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
+            string connectionIdentifier = connection.ConnectionIdentifier;
             response.Status = LoginResponseStatusName.Success;
 
             if (isFirstLoginRequest)
@@ -117,6 +117,7 @@ namespace ISCSI.Server
                 }
             }
 
+            ISCSISession session = connection.Session;
             if (isNewSession)
             {
                 string sessionType = requestParameters.ValueOf("SessionType");
@@ -191,8 +192,8 @@ namespace ISCSI.Server
             }
             else if (request.CurrentStage == 1)
             {
-                UpdateOperationalParameters(requestParameters, session, connection);
-                response.LoginParameters = GetLoginResponseOperationalParameters(session, connection);
+                UpdateOperationalParameters(requestParameters, connection);
+                response.LoginParameters = GetLoginResponseOperationalParameters(connection);
 
                 if (request.Transit)
                 {
@@ -217,12 +218,13 @@ namespace ISCSI.Server
             return response;
         }
 
-        private static void UpdateOperationalParameters(KeyValuePairList<string, string> loginParameters, ISCSISession session, ConnectionParameters connectionParameters)
+        private static void UpdateOperationalParameters(KeyValuePairList<string, string> loginParameters, ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             string value = loginParameters.ValueOf("MaxRecvDataSegmentLength");
             if (value != null)
             {
-                connectionParameters.InitiatorMaxRecvDataSegmentLength = Convert.ToInt32(value);
+                connection.InitiatorMaxRecvDataSegmentLength = Convert.ToInt32(value);
             }
 
             value = loginParameters.ValueOf("MaxConnections");
@@ -286,12 +288,13 @@ namespace ISCSI.Server
             }
         }
 
-        private static KeyValuePairList<string, string> GetLoginResponseOperationalParameters(ISCSISession session, ConnectionParameters connectionParameters)
+        private static KeyValuePairList<string, string> GetLoginResponseOperationalParameters(ConnectionParameters connection)
         {
+            ISCSISession session = connection.Session;
             KeyValuePairList<string, string> loginParameters = new KeyValuePairList<string, string>();
             loginParameters.Add("HeaderDigest", "None");
             loginParameters.Add("DataDigest", "None");
-            loginParameters.Add("MaxRecvDataSegmentLength", connectionParameters.TargetMaxRecvDataSegmentLength.ToString());
+            loginParameters.Add("MaxRecvDataSegmentLength", connection.TargetMaxRecvDataSegmentLength.ToString());
             if (!session.IsDiscovery)
             {
                 loginParameters.Add("MaxConnections", session.MaxConnections.ToString());

+ 3 - 3
ISCSI/ISCSI.Server/ISCSIServer.PDUProcessor.cs

@@ -75,7 +75,7 @@ namespace ISCSI.Server
                             Log(Severity.Verbose, "[{0}] Implicit logout completed", state.ConnectionIdentifier);
                         }
                     }
-                    LoginResponsePDU response = GetLoginResponsePDU(request, ref state.Session, state.ConnectionParameters);
+                    LoginResponsePDU response = GetLoginResponsePDU(request, state.ConnectionParameters);
                     if (state.Session != null && state.Session.IsFullFeaturePhase)
                     {
                         state.ConnectionParameters.CID = request.CID;
@@ -201,7 +201,7 @@ namespace ISCSI.Server
                         Log(Severity.Debug, "[{0}] SCSIDataOutPDU: Target transfer tag: {1}, LUN: {2}, Buffer offset: {3}, Data segment length: {4}, DataSN: {5}, Final: {6}", state.ConnectionIdentifier, request.TargetTransferTag, (ushort)request.LUN, request.BufferOffset, request.DataSegmentLength, request.DataSN, request.Final);
                         try
                         {
-                            readyToTransferPDUs = TargetResponseHelper.GetReadyToTransferPDUs(request, state.Session, state.ConnectionParameters, out commandsToExecute);
+                            readyToTransferPDUs = TargetResponseHelper.GetReadyToTransferPDUs(request, state.ConnectionParameters, out commandsToExecute);
                         }
                         catch (InvalidTargetTransferTagException ex)
                         {
@@ -217,7 +217,7 @@ namespace ISCSI.Server
                     {
                         SCSICommandPDU command = (SCSICommandPDU)pdu;
                         Log(Severity.Debug, "[{0}] SCSICommandPDU: CmdSN: {1}, LUN: {2}, Data segment length: {3}, Expected Data Transfer Length: {4}, Final: {5}", state.ConnectionIdentifier, command.CmdSN, (ushort)command.LUN, command.DataSegmentLength, command.ExpectedDataTransferLength, command.Final);
-                        readyToTransferPDUs = TargetResponseHelper.GetReadyToTransferPDUs(command, state.Session, state.ConnectionParameters, out commandsToExecute);
+                        readyToTransferPDUs = TargetResponseHelper.GetReadyToTransferPDUs(command, state.ConnectionParameters, out commandsToExecute);
                     }
                     foreach (ReadyToTransferPDU readyToTransferPDU in readyToTransferPDUs)
                     {

+ 5 - 5
ISCSI/ISCSI.Server/TargetResponseHelper.cs

@@ -17,13 +17,13 @@ namespace ISCSI.Server
 {
     internal class TargetResponseHelper
     {
-        internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSICommandPDU command, ISCSISession session, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
+        internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSICommandPDU command, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
         {
             // We return either SCSIResponsePDU or List<SCSIDataInPDU>
             List<ReadyToTransferPDU> responseList = new List<ReadyToTransferPDU>();
             commandsToExecute = new List<SCSICommandPDU>();
-            
-            string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
+
+            ISCSISession session = connection.Session;
 
             if (command.Write && command.DataSegmentLength < command.ExpectedDataTransferLength)
             {
@@ -71,12 +71,12 @@ namespace ISCSI.Server
             return responseList;
         }
 
-        internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSIDataOutPDU request, ISCSISession session, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
+        internal static List<ReadyToTransferPDU> GetReadyToTransferPDUs(SCSIDataOutPDU request, ConnectionParameters connection, out List<SCSICommandPDU> commandsToExecute)
         {
             List<ReadyToTransferPDU> responseList = new List<ReadyToTransferPDU>();
             commandsToExecute = new List<SCSICommandPDU>();
 
-            string connectionIdentifier = ConnectionState.GetConnectionIdentifier(session, connection);
+            ISCSISession session = connection.Session;
             TransferEntry transfer = connection.GetTransferEntry(request.TargetTransferTag);
             if (transfer == null)
             {