Package org.jscsi.parser.scsi

Examples of org.jscsi.parser.scsi.SCSICommandParser


    public void execute (ProtocolDataUnit pdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException {

        LOGGER.debug("Initiator has sent FORMAT UNIT command.");

        final BasicHeaderSegment bhs = pdu.getBasicHeaderSegment();
        final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();

        ProtocolDataUnit responsePdu = null;// the response PDU

        // get command details in CDB
        final FormatUnitCDB cdb = new FormatUnitCDB(parser.getCDB());
        final FieldPointerSenseKeySpecificData[] illegalFieldPointers = cdb.getIllegalFieldPointers();

        if (illegalFieldPointers != null) {
            // an illegal request has been made

            FixedFormatSenseData senseData = new FixedFormatSenseData(false,// valid
            ErrorType.CURRENT,// error type
            false,// file mark
            false,// end of medium
            false,// incorrect length indicator
            SenseKey.ILLEGAL_REQUEST,// sense key
            new FourByteInformation(),// information
            new FourByteInformation(),// command specific information
            AdditionalSenseCodeAndQualifier.INVALID_FIELD_IN_CDB,// additional
                                                                 // sense
                                                                 // code
                                                                 // and
                                                                 // qualifier
            (byte) 0,// field replaceable unit code
            illegalFieldPointers[0],// sense key specific data, only
                                    // report first problem
            new AdditionalSenseBytes());// additional sense bytes

            responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                    false,// bidirectionalReadResidualUnderflow
                    false,// residualOverflow
                    false,// residualUnderflow,
                    SCSIResponseParser.ServiceResponse.COMMAND_COMPLETED_AT_TARGET,// response,
                    SCSIStatus.CHECK_CONDITION,// status,
                    bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                    0,// snackTag
                    0,// expectedDataSequenceNumber
                    0,// bidirectionalReadResidualCount
                    0,// residualCount
                    new ScsiResponseDataSegment(senseData, parser.getExpectedDataTransferLength()));// data
                                                                                                    // segment

        } else {
            // PDU is okay

            // carry out command
            /*
             * If we were nice, we would have to get (we would actually have to save it first) the number of blocks and
             * the block length requested by the initiator in the last MODE SENSE command and then change the logical
             * block layout accordingly. However, since the target is not required by the SCSI standard to make those
             * changes ("The degree that the medium is altered by this command is vendor specific."), doing nothing is
             * okay.
             */

            responsePdu = createScsiResponsePdu(SCSIStatus.GOOD,// status
                    bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                    parser.getExpectedDataTransferLength(),// expectedDataTransferLength,
                    0);// responseDataSize
        }

        // send response
        connection.sendPdu(responsePdu);
View Full Code Here


    /** {@inheritDoc} */
    public final void execute () throws InternetSCSIException {

        final ProtocolDataUnit protocolDataUnit = protocolDataUnitFactory.create(false, true, OperationCode.SCSI_COMMAND, connection.getSetting(OperationalTextKey.HEADER_DIGEST), connection.getSetting(OperationalTextKey.DATA_DIGEST));
        final SCSICommandParser scsi = (SCSICommandParser) protocolDataUnit.getBasicHeaderSegment().getParser();

        scsi.setReadExpectedFlag(false);
        scsi.setWriteExpectedFlag(true);
        scsi.setTaskAttributes(taskAttributes);

        scsi.setExpectedDataTransferLength(expectedDataTransferLength);

        final int maxRecvDataSegmentLength = connection.getSettingAsInt(OperationalTextKey.MAX_RECV_DATA_SEGMENT_LENGTH);
        scsi.setCommandDescriptorBlock(SCSICommandDescriptorBlockParser.createWriteMessage(logicalBlockAddress, transferLength));

        final IDataSegment dataSegment = DataSegmentFactory.create(buffer, bufferPosition, expectedDataTransferLength, DataSegmentFormat.BINARY, maxRecvDataSegmentLength);
        final IDataSegmentIterator iterator = dataSegment.iterator();
        int bufferOffset = 0;

View Full Code Here

    /** {@inheritDoc} */
    public final void execute () throws InternetSCSIException {

        final ProtocolDataUnit protocolDataUnit = protocolDataUnitFactory.create(false, true, OperationCode.SCSI_COMMAND, connection.getSetting(OperationalTextKey.HEADER_DIGEST), connection.getSetting(OperationalTextKey.DATA_DIGEST));
        final SCSICommandParser scsi = (SCSICommandParser) protocolDataUnit.getBasicHeaderSegment().getParser();

        scsi.setReadExpectedFlag(true);
        scsi.setWriteExpectedFlag(false);
        scsi.setTaskAttributes(taskAttributes);

        scsi.setExpectedDataTransferLength(EXPECTED_DATA_TRANSFER_LENGTH);

        scsi.setCommandDescriptorBlock(SCSICommandDescriptorBlockParser.createReadCapacityMessage());

        connection.send(protocolDataUnit);
        connection.nextState(new CapacityResponseState(connection, capacityInformation));
        super.stateFollowing = true;
        // return true;
View Full Code Here

    @Override
    public void execute (ProtocolDataUnit pdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException {

        final BasicHeaderSegment bhs = pdu.getBasicHeaderSegment();
        final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();

        ProtocolDataUnit responsePdu;// the response PDU

        // get command details in CDB
        final TestUnitReadyCdb cdb = new TestUnitReadyCdb(parser.getCDB());
        final FieldPointerSenseKeySpecificData[] illegalFieldPointers = cdb.getIllegalFieldPointers();

        if (illegalFieldPointers != null) {
            // an illegal request has been made

            responsePdu = createFixedFormatErrorPdu(illegalFieldPointers,// senseKeySpecificData,
                    bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                    parser.getExpectedDataTransferLength());// expDataTransferLength

        } else {
            // PDU is okay
            // carry out command
            // the logical unit is always ready
View Full Code Here

    @Override
    public void execute (ProtocolDataUnit pdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException {

        final BasicHeaderSegment bhs = pdu.getBasicHeaderSegment();
        final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();

        ProtocolDataUnit responsePdu = null;// the response PDU

        // get command details in CDB
        final ReportLunsCDB cdb = new ReportLunsCDB(parser.getCDB());
        final FieldPointerSenseKeySpecificData[] illegalFieldPointers = cdb.getIllegalFieldPointers();

        if (illegalFieldPointers != null) {
            // an illegal request has been made
            responsePdu = createFixedFormatErrorPdu(illegalFieldPointers,// senseKeySpecificData
                    bhs.getInitiatorTaskTag(),// initiatorTaskTag
                    parser.getExpectedDataTransferLength());// expectedDataTransferLength

            // send response
            connection.sendPdu(responsePdu);

        } else {
            // PDU is okay
            // carry out command
            final SelectReport selectReport = cdb.getSelectReport();
            LOGGER.debug("selectReport = " + selectReport);

            // there are only well known LUNs
            ReportLunsParameterData reportLunsParameterData;

            // TODO the switch isn't really needed right now, but maybe in
            // future implementations
            switch (selectReport) {
                case SELECTED_ADDRESSING_METHODS :
                case WELL_KNOWN_LUNS_ONLY :
                case ALL :
                    reportLunsParameterData = new ReportLunsParameterData(session.getTargetServer().getConfig().getLogicalUnitNumber());
                    break;
                default :
                    throw new InternetSCSIException();
                    /*
                     * Unreachable, this case has already been checked in the ReportLunsCDB constructor
                     */
            }

            // send response
            sendResponse(bhs.getInitiatorTaskTag(),// initiatorTaskTag
                    parser.getExpectedDataTransferLength(),// expectedDataTransferLength
                    reportLunsParameterData);// responseData

        }
    }
View Full Code Here

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = new ProtocolDataUnitFactory().create(false, true, OperationCode.SCSI_COMMAND, "None", "None").getBasicHeaderSegment();
                final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();
                final SendDiagnosticCdb cdb = new SendDiagnosticCdb(parser.getCDB());

                // create the whole sense data
                FixedFormatSenseData senseData = new FixedFormatSenseData(false,// valid
                ErrorType.CURRENT,// error type
                false,// file mark
                false,// end of medium
                false,// incorrect length indicator
                SenseKey.ILLEGAL_REQUEST,// sense key
                new FourByteInformation(),// information
                new FourByteInformation(),// command specific information
                AdditionalSenseCodeAndQualifier.INVALID_FIELD_IN_CDB,// additional sense
                                                                     // code and
                // qualifier
                (byte) 0,// field replaceable unit code
                cdb.getIllegalFieldPointers()[0],// sense key specific data, only report
                // first problem
                new AdditionalSenseBytes());// additional sense bytes

                // keep only the part of the sense data that will be sent
                final ScsiResponseDataSegment dataSegment = new ScsiResponseDataSegment(senseData, parser.getExpectedDataTransferLength());
                final int senseDataSize = senseData.size();

                // calculate residuals and flags
                final int residualCount = Math.abs(parser.getExpectedDataTransferLength() - senseDataSize);
                final boolean residualOverflow = parser.getExpectedDataTransferLength() < senseDataSize;
                final boolean residualUnderflow = parser.getExpectedDataTransferLength() > senseDataSize;

                // create and return PDU
                responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                        false,// bidirectionalReadResidualUnderflow
                        residualOverflow,// residualOverflow
                        residualUnderflow,// residualUnderflow,
                        SCSIResponseParser.ServiceResponse.COMMAND_COMPLETED_AT_TARGET,// response,
                        SCSIStatus.CHECK_CONDITION,// status,
                        bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                        0,// snackTag
                        0,// expectedDataSequenceNumber
                        0,// bidirectionalReadResidualCount
                        residualCount,// residualCount
                        dataSegment);// data segment
                return responsePdu;
            }
        }, // ReportLunsStage checker
        new Checker() {

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = new ProtocolDataUnitFactory().create(false, true, OperationCode.SCSI_COMMAND, "None", "None").getBasicHeaderSegment();
                final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();
                final ReportLunsCDB cdb = new ReportLunsCDB(parser.getCDB());
                final FieldPointerSenseKeySpecificData[] illegalFieldPointers = cdb.getIllegalFieldPointers();

                FixedFormatSenseData senseData = new FixedFormatSenseData(false,// valid
                ErrorType.CURRENT,// error type
                false,// file mark
                false,// end of medium
                false,// incorrect length indicator
                SenseKey.ILLEGAL_REQUEST,// sense key
                new FourByteInformation(),// information
                new FourByteInformation(),// command specific information
                AdditionalSenseCodeAndQualifier.INVALID_FIELD_IN_CDB,// additional sense
                                                                     // code and
                // qualifier
                (byte) 0,// field replaceable unit code
                illegalFieldPointers[0],// sense key specific data, only report
                                        // first problem
                new AdditionalSenseBytes());// additional sense bytes

                // keep only the part of the sense data that will be sent
                final ScsiResponseDataSegment dataSegment = new ScsiResponseDataSegment(senseData, parser.getExpectedDataTransferLength());
                final int senseDataSize = senseData.size();
                // calculate residuals and flags
                final int residualCount = Math.abs(parser.getExpectedDataTransferLength() - senseDataSize);
                final boolean residualOverflow = parser.getExpectedDataTransferLength() < senseDataSize;
                final boolean residualUnderflow = parser.getExpectedDataTransferLength() > senseDataSize;
                // create and return PDU
                responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                        false,// bidirectionalReadResidualUnderflow
                        residualOverflow,// residualOverflow
                        residualUnderflow,// residualUnderflow,
                        SCSIResponseParser.ServiceResponse.COMMAND_COMPLETED_AT_TARGET,// response,
                        SCSIStatus.CHECK_CONDITION,// status,
                        bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                        0,// snackTag
                        0,// expectedDataSequenceNumber
                        0,// bidirectionalReadResidualCount
                        residualCount,// residualCount
                        dataSegment);// data segment

                // send response
                return responsePdu;
            }
        }, // InquiryStage checker
        new Checker() {

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = new ProtocolDataUnitFactory().create(false, true, OperationCode.SCSI_COMMAND, "None", "None").getBasicHeaderSegment();
                final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();
                final InquiryCDB cdb = new InquiryCDB(parser.getCDB());
                IResponseData responseData = null;

                if (!cdb.getEnableVitalProductData()) {
                    responseData = StandardInquiryData.getInstance();
                } else {
                    final VitalProductDataPageName pageName = cdb.getPageCode().getVitalProductDataPageName();

                    switch (pageName) {// is never null
                        case SUPPORTED_VPD_PAGES :
                            responseData = SupportedVpdPages.getInstance();
                            break;
                        case DEVICE_IDENTIFICATION :
                            responseData = session.getTargetServer().getDeviceIdentificationVpdPage();
                            break;
                        default :
                            throw new InternetSCSIException();
                    }
                }

                // The part from the targetfullfeaturephase

                final ByteBuffer fullBuffer = ByteBuffer.allocate(responseData.size());
                responseData.serialize(fullBuffer, 0);

                ByteBuffer trimmedBuffer;
                if (fullBuffer.capacity() <= parser.getExpectedDataTransferLength()) {
                    trimmedBuffer = fullBuffer;
                } else {
                    trimmedBuffer = ByteBuffer.allocate(parser.getExpectedDataTransferLength());
                    trimmedBuffer.put(fullBuffer.array(),// source array
                            0,// offset in source
                            parser.getExpectedDataTransferLength());// length
                }

                final boolean residualOverflow = parser.getExpectedDataTransferLength() < fullBuffer.capacity();
                final boolean residualUnderflow = parser.getExpectedDataTransferLength() > fullBuffer.capacity();
                final int residualCount = Math.abs(parser.getExpectedDataTransferLength() - fullBuffer.capacity());

                responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                        false,// bidirectionalReadResidualUnderflow
                        residualOverflow,// residualOverflow
                        residualUnderflow,// residualUnderflow
                        ServiceResponse.COMMAND_COMPLETED_AT_TARGET,// response
                        SCSIStatus.GOOD,// status
                        bhs.getInitiatorTaskTag(), 0,// snackTag, reserved
                        0,// expectedDataSequenceNumber
                        0,// bidirectionalReadResidualCount
                        residualCount,// residualCount
                        ScsiResponseDataSegment.EMPTY_DATA_SEGMENT);// scsiResponseDataSegment

                return responsePdu;
            }
        }, // RequestSenseStage checker
        new Checker() {

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = new ProtocolDataUnitFactory().create(false, true, OperationCode.SCSI_COMMAND, "None", "None").getBasicHeaderSegment();
                final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();
                final RequestSenseCdb cdb = new RequestSenseCdb(parser.getCDB());
                SenseData senseData;
                final SenseKey senseKey = SenseKey.NO_SENSE;
                final AdditionalSenseCodeAndQualifier additionalSense = AdditionalSenseCodeAndQualifier.NO_ADDITIONAL_SENSE_INFORMATION;

                if (cdb.getDescriptorFormat()) {
                    senseData = new DescriptorFormatSenseData(ErrorType.CURRENT,// errorType
                    senseKey,// sense key
                    additionalSense,// additional sense code and qualifier
                    new SenseDataDescriptor[0]);// sense data descriptors
                } else {
                    senseData = new FixedFormatSenseData(false,// valid
                    ErrorType.CURRENT,// error type
                    false,// file mark
                    false,// end of medium
                    false,// incorrect length indicator
                    senseKey,// sense key
                    new FourByteInformation(),// information
                    new FourByteInformation(),// command specific
                                              // information
                    additionalSense,// additional sense code and qualifier
                    (byte) 0,// field replaceable unit code
                    null,// sense key specific data, only report first
                         // problem
                    null);// additional sense bytes
                }

                responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                        false,// bidirectionalReadResidualUnderflow
                        false,// residualOverflow
                        false,// residualUnderflow,
                        SCSIResponseParser.ServiceResponse.COMMAND_COMPLETED_AT_TARGET,// response,
                        SCSIStatus.GOOD,// status,
                        bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                        0,// snackTag
                        0,// expectedDataSequenceNumber
                        0,// bidirectionalReadResidualCount
                        0,// residualCount
                        new ScsiResponseDataSegment(senseData, parser.getExpectedDataTransferLength()));

                return responsePdu;
            }
        }, // TextNegotiationStage checker
        new Checker() {

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException , SettingsException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = textNegotationUnit.getBasicHeaderSegment();

                final int initiatorTaskTag = bhs.getInitiatorTaskTag();

                final String textRequest = new String(textNegotationUnit.getDataSegment().array());

                ByteBuffer replyDataSegment = null;// for later

                // tokenize key-value pairs
                final List<String> requestKeyValuePairs = TextParameter.tokenizeKeyValuePairs(textRequest);

                final List<String> responseKeyValuePairs = new Vector<String>();

                // process SendTargets command
                if (requestKeyValuePairs != null) {
                    String sendTargetsValue = null;

                    if (requestKeyValuePairs.size() == 1) sendTargetsValue = TextParameter.getSuffix(requestKeyValuePairs.get(0),// string
                            TextKeyword.SEND_TARGETS + TextKeyword.EQUALS);// prefix

                    if (sendTargetsValue != null) {
                        final boolean normal = session.isNormalSession();
                        final boolean sendTargetName = // see upper table
                        !normal && sendTargetsValue.equals(TextKeyword.ALL);
                        final boolean sendTargetAddress = // see upper table
                        (!normal && sendTargetsValue.equals(TextKeyword.ALL)) || (session.getTargetServer().isValidTargetName(sendTargetsValue)) || (normal && sendTargetsValue.length() == 0);

                        // add TargetName
                        if (sendTargetName) {
                            for (String curTargetName : session.getTargetServer().getTargetNames()) {
                                responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.TARGET_NAME, curTargetName));
                                // add TargetAddress
                                if (sendTargetAddress) responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.TARGET_ADDRESS, session.getTargetServer().getConfig().getTargetAddress() + // domain
                                TextKeyword.COLON + // :
                                session.getTargetServer().getConfig().getPort() + // port
                                TextKeyword.COMMA + // ,
                                session.getTargetServer().getConfig().getTargetPortalGroupTag())); // groupTag)
                            }
                        } else {
                            // We're here if they sent us a target name and are asking for the
                            // address (I think)
                            if (sendTargetAddress) responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.TARGET_ADDRESS, session.getTargetServer().getConfig().getTargetAddress() + // domain
                            TextKeyword.COLON + // :
                            session.getTargetServer().getConfig().getPort() + // port
                            TextKeyword.COMMA + // ,
                            session.getTargetServer().getConfig().getTargetPortalGroupTag())); // groupTag)
                        }

                    }
                    // concatenate and serialize reply
                    final String replyString = TextParameter.concatenateKeyValuePairs(responseKeyValuePairs);

                    replyDataSegment = ReadWrite.stringToTextDataSegments(replyString, connection.getSettings().getMaxRecvDataSegmentLength())[0];
                }

                responsePdu = TargetPduFactory.createTextResponsePdu(true,// finalFlag
                        false,// continueFlag
                        0,// logicalUnitNumber
                        initiatorTaskTag, 0xffffffff,// targetTransferTag
                        replyDataSegment);// dataSegment

                return responsePdu;
            }
        }, // UnsupportedOpCodeStage checker
        new Checker() {

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = new ProtocolDataUnitFactory().create(false, true, OperationCode.SCSI_TM_REQUEST, "None", "None").getBasicHeaderSegment();
                final InitiatorMessageParser parser = (InitiatorMessageParser) bhs.getParser();

                final FieldPointerSenseKeySpecificData fp = new FieldPointerSenseKeySpecificData(true,// senseKeySpecificDataValid
                true,// commandData (i.e. invalid field in CDB)
                false,// bitPointerValid
                0,// bitPointer, reserved since invalid
                0);// fieldPointer to the SCSI OpCode field

                final FieldPointerSenseKeySpecificData[] fpArray = new FieldPointerSenseKeySpecificData[] { fp };

                // create the whole sense data
                FixedFormatSenseData senseData = new FixedFormatSenseData(false,// valid
                ErrorType.CURRENT,// error type
                false,// file mark
                false,// end of medium
                false,// incorrect length indicator
                SenseKey.ILLEGAL_REQUEST,// sense key
                new FourByteInformation(),// information
                new FourByteInformation(),// command specific information
                AdditionalSenseCodeAndQualifier.INVALID_FIELD_IN_CDB,// additional sense
                                                                     // code and
                // qualifier
                (byte) 0,// field replaceable unit code
                fpArray[0],// sense key specific data, only report
                           // first problem
                new AdditionalSenseBytes());// additional sense bytes

                // keep only the part of the sense data that will be sent
                final ScsiResponseDataSegment dataSegment = new ScsiResponseDataSegment(senseData, parser.getExpectedStatusSequenceNumber());
                final int senseDataSize = senseData.size();

                // calculate residuals and flags
                final int residualCount = Math.abs(parser.getExpectedStatusSequenceNumber() - senseDataSize);
                final boolean residualOverflow = parser.getExpectedStatusSequenceNumber() < senseDataSize;
                final boolean residualUnderflow = parser.getExpectedStatusSequenceNumber() > senseDataSize;

                // create and return PDU
                responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                        false,// bidirectionalReadResidualUnderflow
                        residualOverflow,// residualOverflow
                        residualUnderflow,// residualUnderflow,
                        SCSIResponseParser.ServiceResponse.COMMAND_COMPLETED_AT_TARGET,// response,
                        SCSIStatus.CHECK_CONDITION,// status,
                        bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                        0,// snackTag
                        0,// expectedDataSequenceNumber
                        0,// bidirectionalReadResidualCount
                        residualCount,// residualCount
                        dataSegment);// data segment

                return responsePdu;
            }
        }, // FormatUnitStage checker
        new Checker() {

            @Override
            public ProtocolDataUnit check (final Connection pConnection) throws InterruptedException , IOException , InternetSCSIException {
                ProtocolDataUnit responsePdu;
                final BasicHeaderSegment bhs = new ProtocolDataUnitFactory().create(false, true, OperationCode.SCSI_COMMAND, "None", "None").getBasicHeaderSegment();
                final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();

                // Taken from the stage
                final int residualCount = Math.abs(parser.getExpectedDataTransferLength() - 0);
                final boolean residualOverflow = parser.getExpectedDataTransferLength() < 0;
                final boolean residualUnderflow = parser.getExpectedDataTransferLength() > 0;

                responsePdu = TargetPduFactory.createSCSIResponsePdu(false,// bidirectionalReadResidualOverflow
                        false,// bidirectionalReadResidualUnderflow
                        residualOverflow,// residualOverflow,
                        residualUnderflow,// residualUnderflow,
View Full Code Here

            case SCSI_TM_REQUEST :
                return new TaskManagementFunctionRequestParser(protocolDataUnit);
            case SCSI_TM_RESPONSE :
                return new TaskManagementFunctionResponseParser(protocolDataUnit);
            case SCSI_COMMAND :
                return new SCSICommandParser(protocolDataUnit);
            case SCSI_RESPONSE :
                return new SCSIResponseParser(protocolDataUnit);
            default :
                if (LOGGER.isErrorEnabled()) {
                    LOGGER.error("Parser not supported with this operation code " + operationCode);
View Full Code Here

         * RequestSenseStage
         */

        final ProtocolDataUnit pdu = new ProtocolDataUnitFactory().create(false, true, OperationCode.LOGIN_REQUEST, "None", "None");

        final SCSICommandParser parser = new SCSICommandParser(pdu);
        parser.setExpectedDataTransferLength(8);

        // get command details in CDB
        final RequestSenseCdb cdb = new RequestSenseCdb(parser.getCDB());

        SenseData senseData;

        if (cdb.getDescriptorFormat()) {
            // descriptor format sense data has been requested

            senseData = new DescriptorFormatSenseData(ErrorType.CURRENT,// errorType
            SenseKey.ILLEGAL_REQUEST,// sense key
            AdditionalSenseCodeAndQualifier.INVALID_FIELD_IN_CDB,// additional
                                                                 // sense
                                                                 // code
                                                                 // and
                                                                 // qualifier
            new SenseDataDescriptor[0]);// sense data descriptors

        } else {
            // fixed format sense data has been requested

            senseData = new FixedFormatSenseData(false,// valid
            ErrorType.CURRENT,// error type
            false,// file mark
            false,// end of medium
            false,// incorrect length indicator
            SenseKey.ILLEGAL_REQUEST,// sense key
            new FourByteInformation(),// information
            new FourByteInformation(),// command specific
                                      // information
            AdditionalSenseCodeAndQualifier.INVALID_FIELD_IN_CDB,// additional
                                                                 // sense
                                                                 // code
                                                                 // and
                                                                 // qualifier
            (byte) 0,// field replaceable unit code
            new FieldPointerSenseKeySpecificData(true,// senseKeySpecificDataValid
            true,// commandData (i.e. invalid field in CDB)
            true,// bitPointerValid
            3,// bitPointer
            8),// fieldPointer,// sense key specific data, only
               // report first problem
            new AdditionalSenseBytes());// additional sense bytes
        }

        nonEmpty = new ScsiResponseDataSegment(senseData, parser.getExpectedDataTransferLength());
    }
View Full Code Here

            // identify desired stage
            switch (bhs.getOpCode()) {

                case SCSI_COMMAND :
                    if (connection.getTargetSession().isNormalSession()) {
                        final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();
                        ScsiOperationCode scsiOpCode = ScsiOperationCode.valueOf(parser.getCDB().get(0));

                        LOGGER.debug("scsiOpCode = " + scsiOpCode);// log SCSI
                                                                   // Operation Code

                        if (scsiOpCode != null) {
                            switch (scsiOpCode) {
                                case TEST_UNIT_READY :
                                    stage = new TestUnitReadyStage(this);
                                    break;
                                case REQUEST_SENSE :
                                    stage = new RequestSenseStage(this);
                                    break;
                                case FORMAT_UNIT :
                                    stage = new FormatUnitStage(this);
                                    break;
                                case INQUIRY :
                                    stage = new InquiryStage(this);
                                    break;
                                case MODE_SELECT_6 :
                                    stage = null;
                                    scsiOpCode = null;
                                    break;
                                case MODE_SENSE_6 :
                                    stage = new ModeSenseStage(this);
                                    if (!((ModeSenseStage) stage).canHandle(pdu)) {
                                        stage = null;
                                        scsiOpCode = null;
                                    }
                                    break;
                                case SEND_DIAGNOSTIC :
                                    stage = new SendDiagnosticStage(this);
                                    break;
                                case READ_CAPACITY_10 :// use common read capacity stage
                                case READ_CAPACITY_16 :
                                    stage = new ReadCapacityStage(this);
                                    break;
                                case WRITE_6 :// use common write stage
                                case WRITE_10 :
                                    stage = new WriteStage(this);
                                    break;
                                case READ_6 :// use common read stage
                                case READ_10 :
                                    stage = new ReadStage(this);
                                    break;
                                case REPORT_LUNS :
                                    stage = new ReportLunsStage(this);
                                    break;
                                default :
                                    scsiOpCode = null;

                            }
                        }// else, or if default block was entered (programmer error)
                        if (scsiOpCode == null) {
                            LOGGER.error("Unsupported SCSI OpCode 0x" + Integer.toHexString(parser.getCDB().get(0) & 255) + " in SCSI Command PDU.");
                            stage = new UnsupportedOpCodeStage(this);
                        }

                    } else {// session is discovery session
                        throw new InternetSCSIException("received SCSI command in discovery session");
View Full Code Here

    @Override
    public void execute (ProtocolDataUnit pdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException {

        // find out the type of READ CAPACITY command ((10) or (16))
        final BasicHeaderSegment bhs = pdu.getBasicHeaderSegment();
        final SCSICommandParser parser = (SCSICommandParser) bhs.getParser();
        final ScsiOperationCode opCode = ScsiOperationCode.valueOf(parser.getCDB().get(0));
        ReadCapacityCdb cdb;
        if (opCode == ScsiOperationCode.READ_CAPACITY_10)
            cdb = new ReadCapacity10Cdb(parser.getCDB());
        else if (opCode == ScsiOperationCode.READ_CAPACITY_16)
            cdb = new ReadCapacity16Cdb(parser.getCDB());
        else {
            // programmer error, we should not be here, close the connection
            throw new InternetSCSIException("wrong SCSI Operation Code " + opCode + " in ReadCapacityStage");
        }

        /*
         * Everything is fine, carry on. The PMI bit of the command descriptor block is ignored, since there is no way
         * to know if "substantial vendor specific delay in data transfer may be encountered" after the address in the
         * LOGICAL BLOCK ADDRESS field. Therefore we always try to return the whole length of the storage medium.
         */

        // make sure that the LOGICAL BLOCK ADDRESS field is valid and send
        // appropriate response
        if (session.getStorageModule().checkBounds(cdb.getLogicalBlockAddress(), 0) != 0) {
            // invalid, log error, send error PDU, and return
            LOGGER.error("encountered " + cdb.getClass() + " in ReadCapacityStage with " + "LOGICAL BLOCK ADDRESS = " + cdb.getLogicalBlockAddress());

            final FieldPointerSenseKeySpecificData fp = new FieldPointerSenseKeySpecificData(true,// senseKeySpecificDataValid
            true,// commandData (i.e. invalid field in CDB)
            false,// bitPointerValid
            0,// bitPointer, reserved since invalid
            0);// fieldPointer to the SCSI OpCode field
            final FieldPointerSenseKeySpecificData[] fpArray = new FieldPointerSenseKeySpecificData[] { fp };
            final ProtocolDataUnit responsePdu = createFixedFormatErrorPdu(fpArray,// senseKeySpecificData
                    AdditionalSenseCodeAndQualifier.LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE,// additionalSenseCodeAndQualifier
                    bhs.getInitiatorTaskTag(),// initiatorTaskTag
                    parser.getExpectedDataTransferLength());// expectedDataTransferLength
            connection.sendPdu(responsePdu);
            return;
        } else {
            // send PDU with requested READ CAPACITY parameter data
            ReadCapacityParameterData parameterData;
            if (cdb instanceof ReadCapacity10Cdb)
                parameterData = new ReadCapacity10ParameterData(session.getStorageModule().getSizeInBlocks(),// returnedLogicalBlockAddress
                VIRTUAL_BLOCK_SIZE);// logicalBlockLengthInBytes
            else
                parameterData = new ReadCapacity16ParameterData(session.getStorageModule().getSizeInBlocks(),// returnedLogicalBlockAddress
                VIRTUAL_BLOCK_SIZE);// logicalBlockLengthInBytes

            sendResponse(bhs.getInitiatorTaskTag(),// initiatorTaskTag,
                    parser.getExpectedDataTransferLength(),// expectedDataTransferLength,
                    parameterData);// responseData
        }
    }
View Full Code Here

TOP

Related Classes of org.jscsi.parser.scsi.SCSICommandParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.