public void ProcessCurrentBuffer(byte[] currentBuffer, StateObject state) { Socket clientSocket = state.ClientSocket; if (state.ConnectionBuffer.Length == 0) { state.ConnectionBuffer = currentBuffer; } else { state.ConnectionBuffer = ByteUtils.Concatenate(state.ConnectionBuffer, currentBuffer); } // we now have all PDU bytes received so far in state.ConnectionBuffer int bytesLeftInBuffer = state.ConnectionBuffer.Length; while (bytesLeftInBuffer >= 8) { int bufferOffset = state.ConnectionBuffer.Length - bytesLeftInBuffer; int pduLength = ISCSIPDU.GetPDULength(state.ConnectionBuffer, bufferOffset); if (pduLength > bytesLeftInBuffer) { Log(LogLevel.Debug, "[{0}][ProcessCurrentBuffer] Bytes left in receive buffer: {1}", state.ConnectionIdentifier, bytesLeftInBuffer); break; } else { byte[] pduBytes = ByteReader.ReadBytes(state.ConnectionBuffer, bufferOffset, pduLength); bytesLeftInBuffer -= pduLength; ISCSIPDU pdu = null; try { pdu = ISCSIPDU.GetPDU(pduBytes); } catch (Exception ex) { Log(LogLevel.Error, "[{0}][ProcessCurrentBuffer] Failed to read PDU (Exception: {1})", state.ConnectionIdentifier, ex.Message); RejectPDU reject = new RejectPDU(); reject.Reason = RejectReason.InvalidPDUField; reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48); TrySendPDU(state, reject); } if (pdu != null) { if (pdu.GetType() == typeof(ISCSIPDU)) { Log(LogLevel.Warning, "[{0}][ProcessCurrentBuffer] Unsupported PDU (0x{1})", state.ConnectionIdentifier, pdu.OpCode.ToString("X")); // Unsupported PDU RejectPDU reject = new RejectPDU(); reject.InitiatorTaskTag = pdu.InitiatorTaskTag; reject.Reason = RejectReason.CommandNotSupported; reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48); TrySendPDU(state, reject); } else { ProcessPDU(pdu, state); } } if (!clientSocket.Connected) { // Do not continue to process the buffer if the other side closed the connection if (bytesLeftInBuffer > 0) { Log(LogLevel.Warning, "[{0}][ProcessCurrentBuffer] Buffer processing aborted, bytes left in receive buffer: {1}", state.ConnectionIdentifier, bytesLeftInBuffer); } return; } } } if (bytesLeftInBuffer > 0) { state.ConnectionBuffer = ByteReader.ReadBytes(state.ConnectionBuffer, state.ConnectionBuffer.Length - bytesLeftInBuffer, bytesLeftInBuffer); } else { state.ConnectionBuffer = new byte[0]; } }
public void ProcessCurrentBuffer(byte[] currentBuffer, StateObject state) { if (state.ConnectionBuffer.Length == 0) { state.ConnectionBuffer = currentBuffer; } else { state.ConnectionBuffer = ByteUtils.Concatenate(state.ConnectionBuffer, currentBuffer); } // we now have all PDU bytes received so far in state.ConnectionBuffer int bytesLeftInBuffer = state.ConnectionBuffer.Length; while (bytesLeftInBuffer >= 8) { int bufferOffset = state.ConnectionBuffer.Length - bytesLeftInBuffer; int pduLength = ISCSIPDU.GetPDULength(state.ConnectionBuffer, bufferOffset); if (pduLength > bytesLeftInBuffer) { break; } else { byte[] pduBytes = ByteReader.ReadBytes(state.ConnectionBuffer, bufferOffset, pduLength); bytesLeftInBuffer -= pduLength; ISCSIPDU pdu = null; try { pdu = ISCSIPDU.GetPDU(pduBytes); } catch (UnsupportedSCSICommandException) { throw; } catch (Exception) { throw; } if (pdu.GetType() == typeof(ISCSIPDU)) { /* * Log("[{0}][ProcessCurrentBuffer] Unsupported PDU (0x{1})", state.Connection.Identifier, pdu.OpCode.ToString("X")); * // Unsupported PDU * RejectPDU reject = new RejectPDU(); * reject.Reason = RejectReason.CommandNotSupported; * reject.StatSN = state.Connection.StatSN; * reject.ExpCmdSN = state.Connection.ExpCmdSN; * reject.MaxCmdSN = state.Connection.ExpCmdSN + ISCSIServer.CommandQueueSize; * reject.Data = ByteReader.ReadBytes(pduBytes, 0, 48); * * // StatSN is advanced after a Reject * state.Connection.StatSN++; * * TrySendPDU(state, reject);*/ throw new Exception("Unsupported"); } else { ProcessPDU(pdu, state); } } } if (bytesLeftInBuffer > 0) { state.ConnectionBuffer = ByteReader.ReadBytes(state.ConnectionBuffer, state.ConnectionBuffer.Length - bytesLeftInBuffer, bytesLeftInBuffer); } else { state.ConnectionBuffer = new byte[0]; } }