| 1 | |
package org.jscsi.target.connection.stage.fullfeature; |
| 2 | |
|
| 3 | |
|
| 4 | |
import java.io.IOException; |
| 5 | |
import java.nio.ByteBuffer; |
| 6 | |
import java.security.DigestException; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Vector; |
| 9 | |
|
| 10 | |
import org.jscsi.exception.InternetSCSIException; |
| 11 | |
import org.jscsi.parser.BasicHeaderSegment; |
| 12 | |
import org.jscsi.parser.ProtocolDataUnit; |
| 13 | |
import org.jscsi.target.connection.TargetPduFactory; |
| 14 | |
import org.jscsi.target.connection.phase.TargetFullFeaturePhase; |
| 15 | |
import org.jscsi.target.settings.SettingsException; |
| 16 | |
import org.jscsi.target.settings.TextKeyword; |
| 17 | |
import org.jscsi.target.settings.TextParameter; |
| 18 | |
import org.jscsi.target.util.ReadWrite; |
| 19 | |
import org.slf4j.Logger; |
| 20 | |
import org.slf4j.LoggerFactory; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public final class TextNegotiationStage extends TargetFullFeatureStage { |
| 32 | |
|
| 33 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(TextNegotiationStage.class); |
| 34 | |
|
| 35 | |
public TextNegotiationStage (TargetFullFeaturePhase targetFullFeaturePhase) { |
| 36 | 0 | super(targetFullFeaturePhase); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
@Override |
| 40 | |
public void execute (ProtocolDataUnit pdu) throws IOException , InterruptedException , InternetSCSIException , DigestException , SettingsException { |
| 41 | |
|
| 42 | 0 | final BasicHeaderSegment bhs = pdu.getBasicHeaderSegment(); |
| 43 | |
|
| 44 | 0 | final int initiatorTaskTag = bhs.getInitiatorTaskTag(); |
| 45 | |
|
| 46 | 0 | final String textRequest = new String(pdu.getDataSegment().array()); |
| 47 | |
|
| 48 | 0 | LOGGER.debug("text request: " + textRequest); |
| 49 | |
|
| 50 | 0 | ByteBuffer replyDataSegment = null; |
| 51 | |
|
| 52 | |
|
| 53 | 0 | final List<String> requestKeyValuePairs = TextParameter.tokenizeKeyValuePairs(textRequest); |
| 54 | |
|
| 55 | 0 | final List<String> responseKeyValuePairs = new Vector<String>(); |
| 56 | |
|
| 57 | |
|
| 58 | 0 | if (requestKeyValuePairs != null) { |
| 59 | |
|
| 60 | |
|
| 61 | 0 | String sendTargetsValue = null; |
| 62 | |
|
| 63 | 0 | if (requestKeyValuePairs.size() == 1) sendTargetsValue = TextParameter.getSuffix(requestKeyValuePairs.get(0), |
| 64 | |
TextKeyword.SEND_TARGETS + TextKeyword.EQUALS); |
| 65 | |
|
| 66 | 0 | if (sendTargetsValue != null) { |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | 0 | final boolean normal = session.isNormalSession(); |
| 80 | 0 | final boolean sendTargetName = |
| 81 | |
!normal && sendTargetsValue.equals(TextKeyword.ALL); |
| 82 | 0 | final boolean sendTargetAddress = |
| 83 | |
(!normal && sendTargetsValue.equals(TextKeyword.ALL)) || (session.getTargetServer().isValidTargetName(sendTargetsValue)) || (normal && sendTargetsValue.length() == 0); |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | 0 | if (sendTargetName) { |
| 92 | 0 | for (String curTargetName : session.getTargetServer().getTargetNames()) { |
| 93 | 0 | responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.TARGET_NAME, curTargetName)); |
| 94 | |
|
| 95 | 0 | if (sendTargetAddress) responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.TARGET_ADDRESS, session.getTargetServer().getConfig().getTargetAddress() + |
| 96 | |
TextKeyword.COLON + |
| 97 | |
session.getTargetServer().getConfig().getPort() + |
| 98 | |
TextKeyword.COMMA + |
| 99 | |
session.getTargetServer().getConfig().getTargetPortalGroupTag())); |
| 100 | |
} |
| 101 | |
} else { |
| 102 | |
|
| 103 | 0 | if (sendTargetAddress) responseKeyValuePairs.add(TextParameter.toKeyValuePair(TextKeyword.TARGET_ADDRESS, session.getTargetServer().getConfig().getTargetAddress() + |
| 104 | |
TextKeyword.COLON + |
| 105 | |
session.getTargetServer().getConfig().getPort() + |
| 106 | |
TextKeyword.COMMA + |
| 107 | |
session.getTargetServer().getConfig().getTargetPortalGroupTag())); |
| 108 | |
} |
| 109 | |
|
| 110 | |
} else { |
| 111 | |
|
| 112 | |
|
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | 0 | final String replyString = TextParameter.concatenateKeyValuePairs(responseKeyValuePairs); |
| 117 | |
|
| 118 | 0 | LOGGER.debug("text negotiation stage reply: " + replyString); |
| 119 | |
|
| 120 | 0 | replyDataSegment = ReadWrite.stringToTextDataSegments(replyString, settings.getMaxRecvDataSegmentLength())[0]; |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | 0 | final ProtocolDataUnit responsePdu = TargetPduFactory.createTextResponsePdu(true, |
| 130 | |
false, |
| 131 | |
0, |
| 132 | |
initiatorTaskTag, 0xffffffff, |
| 133 | |
replyDataSegment); |
| 134 | |
|
| 135 | 0 | connection.sendPdu(responsePdu); |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
} |