001: /*_############################################################################
002: _##
003: _## SNMP4J - MPv1.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.mp;
022:
023: import org.snmp4j.MessageDispatcher;
024: import org.snmp4j.smi.Address;
025: import org.snmp4j.asn1.BERInputStream;
026: import org.snmp4j.smi.Integer32;
027: import org.snmp4j.smi.OctetString;
028: import org.snmp4j.MutablePDU;
029: import java.io.IOException;
030: import org.snmp4j.PDU;
031: import org.snmp4j.log.*;
032: import org.snmp4j.ScopedPDU;
033: import org.snmp4j.asn1.BER;
034: import org.snmp4j.security.SecurityModel;
035: import org.snmp4j.asn1.BER.MutableByte;
036: import org.snmp4j.PDUv1;
037: import org.snmp4j.security.SecurityModels;
038: import org.snmp4j.security.SecurityLevel;
039: import org.snmp4j.asn1.BEROutputStream;
040: import java.nio.ByteBuffer;
041: import org.snmp4j.util.PDUFactory;
042: import org.snmp4j.Target;
043:
044: /**
045: * The <code>MPv1</code> is the message processing model for SNMPv1.
046: * @author Frank Fock
047: * @version 1.9.1
048: */
049: public class MPv1 implements MessageProcessingModel {
050:
051: public static final int ID = MessageProcessingModel.MPv1;
052: private static final LogAdapter logger = LogFactory
053: .getLogger(MPv1.class);
054:
055: protected PDUFactory incomingPDUFactory = new PDUFactory() {
056: public PDU createPDU(Target target) {
057: return new PDUv1();
058: }
059: };
060:
061: /**
062: * Creates a SNMPv1 message processing model with a PDU factory for incoming
063: * messages that uses {@link PDUv1}.
064: */
065: public MPv1() {
066: }
067:
068: /**
069: * Creates a SNMPv1 message processing model with a custom PDU factory that
070: * must ignore the target parameter when creating a PDU for parsing incoming
071: * messages.
072: * @param incomingPDUFactory
073: * a {@link PDUFactory}. If <code>null</code> the default factory will be
074: * used which creates {@link ScopedPDU} instances.
075: */
076: public MPv1(PDUFactory incomingPDUFactory) {
077: if (incomingPDUFactory != null) {
078: this .incomingPDUFactory = incomingPDUFactory;
079: }
080: }
081:
082: public int getID() {
083: return ID;
084: }
085:
086: public int prepareOutgoingMessage(Address transportAddress,
087: int maxMessageSize, int messageProcessingModel,
088: int securityModel, byte[] securityName, int securityLevel,
089: PDU pdu, boolean expectResponse, PduHandle sendPduHandle,
090: Address destTransportAddress,
091: BEROutputStream outgoingMessage) throws IOException {
092: if ((securityLevel != SecurityLevel.NOAUTH_NOPRIV)
093: || (securityModel != SecurityModel.SECURITY_MODEL_SNMPv1)) {
094: logger.error("MPv1 used with unsupported security model");
095: return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL;
096: }
097: if (pdu instanceof ScopedPDU) {
098: String txt = "ScopedPDU must not be used with MPv1";
099: logger.error(txt);
100: throw new IllegalArgumentException(txt);
101: }
102:
103: if (!isProtocolVersionSupported(messageProcessingModel)) {
104: logger.error("MPv1 used with unsupported SNMP version");
105: return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL;
106: }
107:
108: OctetString community = new OctetString(securityName);
109: Integer32 version = new Integer32(messageProcessingModel);
110: // compute total length
111: int length = pdu.getBERLength();
112: length += community.getBERLength();
113: length += version.getBERLength();
114:
115: ByteBuffer buf = ByteBuffer.allocate(length
116: + BER.getBERLengthOfLength(length) + 1);
117: // set the buffer of the outgoing message
118: outgoingMessage.setBuffer(buf);
119:
120: // encode the message
121: BER.encodeHeader(outgoingMessage, BER.SEQUENCE, length);
122: version.encodeBER(outgoingMessage);
123:
124: community.encodeBER(outgoingMessage);
125: pdu.encodeBER(outgoingMessage);
126:
127: return SnmpConstants.SNMP_MP_OK;
128: }
129:
130: public int prepareResponseMessage(int messageProcessingModel,
131: int maxMessageSize, int securityModel, byte[] securityName,
132: int securityLevel, PDU pdu, int maxSizeResponseScopedPDU,
133: StateReference stateReference,
134: StatusInformation statusInformation,
135: BEROutputStream outgoingMessage) throws IOException {
136: return prepareOutgoingMessage(stateReference.getAddress(),
137: maxMessageSize, messageProcessingModel, securityModel,
138: securityName, securityLevel, pdu, false, stateReference
139: .getPduHandle(), null, outgoingMessage);
140: }
141:
142: public int prepareDataElements(MessageDispatcher messageDispatcher,
143: Address transportAddress, BERInputStream wholeMsg,
144: Integer32 messageProcessingModel, Integer32 securityModel,
145: OctetString securityName, Integer32 securityLevel,
146: MutablePDU pdu, PduHandle sendPduHandle,
147: Integer32 maxSizeResponseScopedPDU,
148: StatusInformation statusInformation,
149: MutableStateReference stateReference) throws IOException {
150: MutableByte mutableByte = new MutableByte();
151: int length = BER.decodeHeader(wholeMsg, mutableByte);
152: int startPos = (int) wholeMsg.getPosition();
153: if (mutableByte.getValue() != BER.SEQUENCE) {
154: String txt = "SNMPv1 PDU must start with a SEQUENCE";
155: logger.error(txt);
156: throw new IOException(txt);
157: }
158: Integer32 version = new Integer32();
159: version.decodeBER(wholeMsg);
160:
161: securityName.decodeBER(wholeMsg);
162: securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV);
163: securityModel.setValue(SecurityModel.SECURITY_MODEL_SNMPv1);
164: messageProcessingModel.setValue(ID);
165:
166: PDU v1PDU = incomingPDUFactory.createPDU(null);
167: pdu.setPdu(v1PDU);
168: v1PDU.decodeBER(wholeMsg);
169:
170: BER.checkSequenceLength(length, (int) wholeMsg.getPosition()
171: - startPos, v1PDU);
172:
173: sendPduHandle.setTransactionID(v1PDU.getRequestID().getValue());
174:
175: // create state reference
176: StateReference stateRef = new StateReference(sendPduHandle,
177: transportAddress, null, SecurityModels.getInstance()
178: .getSecurityModel(securityModel), securityName
179: .getValue(), SnmpConstants.SNMP_ERROR_SUCCESS);
180: stateReference.setStateReference(stateRef);
181:
182: return SnmpConstants.SNMP_MP_OK;
183: }
184:
185: public boolean isProtocolVersionSupported(int snmpProtocolVersion) {
186: return (snmpProtocolVersion == SnmpConstants.version1);
187: }
188:
189: public void releaseStateReference(PduHandle pduHandle) {
190: // we do not cache state information -> do nothing
191: }
192:
193: }
|