1 20 21 22 23 24 25 package org.snmp4j.mp; 26 27 import org.snmp4j.MessageDispatcher; 28 import org.snmp4j.smi.Address; 29 import org.snmp4j.asn1.BERInputStream; 30 import org.snmp4j.smi.Integer32; 31 import org.snmp4j.smi.OctetString; 32 import org.snmp4j.MutablePDU; 33 import java.io.IOException ; 34 import org.snmp4j.PDU; 35 import org.snmp4j.log.*; 36 import org.snmp4j.ScopedPDU; 37 import org.snmp4j.asn1.BER; 38 import org.snmp4j.security.SecurityModel; 39 import org.snmp4j.asn1.BER.MutableByte; 40 import org.snmp4j.PDUv1; 41 import org.snmp4j.security.SecurityModels; 42 import org.snmp4j.security.SecurityLevel; 43 import org.snmp4j.asn1.BEROutputStream; 44 import java.nio.ByteBuffer ; 45 46 51 public class MPv1 implements MessageProcessingModel { 52 53 public static final int ID = MessageProcessingModel.MPv1; 54 private static final LogAdapter logger = LogFactory.getLogger(MPv1.class); 55 56 public MPv1() { 57 } 58 59 public int getID() { 60 return ID; 61 } 62 63 public int prepareOutgoingMessage(Address transportAddress, 64 int maxMessageSize, 65 int messageProcessingModel, 66 int securityModel, 67 byte[] securityName, 68 int securityLevel, 69 PDU pdu, 70 boolean expectResponse, 71 PduHandle sendPduHandle, 72 Address destTransportAddress, 73 BEROutputStream outgoingMessage) 74 throws IOException 75 { 76 if ((securityLevel != SecurityLevel.NOAUTH_NOPRIV) || 77 (securityModel != SecurityModel.SECURITY_MODEL_SNMPv1)) { 78 logger.error("MPv1 used with unsupported security model"); 79 return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL; 80 } 81 if (pdu instanceof ScopedPDU) { 82 String txt = "ScopedPDU must not be used with MPv1"; 83 logger.error(txt); 84 throw new IllegalArgumentException (txt); 85 } 86 87 if (!isProtocolVersionSupported(messageProcessingModel)) { 88 logger.error("MPv1 used with unsupported SNMP version"); 89 return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL; 90 } 91 92 93 OctetString community = new OctetString(securityName); 94 Integer32 version = new Integer32(messageProcessingModel); 95 int length = pdu.getBERLength(); 97 length += community.getBERLength(); 98 length += version.getBERLength(); 99 100 ByteBuffer buf = ByteBuffer.allocate(length + 101 BER.getBERLengthOfLength(length) + 1); 102 outgoingMessage.setBuffer(buf); 104 105 BER.encodeHeader(outgoingMessage, BER.SEQUENCE, length); 107 version.encodeBER(outgoingMessage); 108 109 community.encodeBER(outgoingMessage); 110 pdu.encodeBER(outgoingMessage); 111 112 return SnmpConstants.SNMP_MP_OK; 113 } 114 115 public int prepareResponseMessage(int messageProcessingModel, 116 int maxMessageSize, 117 int securityModel, 118 byte[] securityName, 119 int securityLevel, 120 PDU pdu, 121 int maxSizeResponseScopedPDU, 122 StateReference stateReference, 123 StatusInformation statusInformation, 124 BEROutputStream outgoingMessage) 125 throws IOException 126 { 127 return prepareOutgoingMessage(stateReference.getAddress(), 128 maxMessageSize, 129 messageProcessingModel, 130 securityModel, 131 securityName, 132 securityLevel, 133 pdu, 134 false, 135 stateReference.getPduHandle(), 136 null, 137 outgoingMessage); 138 } 139 140 public int prepareDataElements(MessageDispatcher messageDispatcher, 141 Address transportAddress, 142 BERInputStream wholeMsg, 143 Integer32 messageProcessingModel, 144 Integer32 securityModel, 145 OctetString securityName, 146 Integer32 securityLevel, 147 MutablePDU pdu, 148 PduHandle sendPduHandle, 149 Integer32 maxSizeResponseScopedPDU, 150 StatusInformation statusInformation, 151 MutableStateReference stateReference) 152 throws IOException 153 { 154 MutableByte mutableByte = new MutableByte(); 155 int length = BER.decodeHeader(wholeMsg, mutableByte); 156 int startPos = (int)wholeMsg.getPosition(); 157 if (mutableByte.getValue() != BER.SEQUENCE) { 158 String txt = "SNMPv1 PDU must start with a SEQUENCE"; 159 logger.error(txt); 160 throw new IOException (txt); 161 } 162 Integer32 version = new Integer32(); 163 version.decodeBER(wholeMsg); 164 165 securityName.decodeBER(wholeMsg); 166 securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV); 167 securityModel.setValue(SecurityModel.SECURITY_MODEL_SNMPv1); 168 messageProcessingModel.setValue(ID); 169 170 PDUv1 v1PDU = new PDUv1(); 171 pdu.setPdu(v1PDU); 172 v1PDU.decodeBER(wholeMsg); 173 174 BER.checkSequenceLength(length, (int)wholeMsg.getPosition() - startPos, 175 v1PDU); 176 177 sendPduHandle.setTransactionID(v1PDU.getRequestID().getValue()); 178 179 StateReference stateRef = 181 new StateReference(sendPduHandle, 182 transportAddress, 183 null, 184 SecurityModels.getInstance().getSecurityModel(securityModel), 185 securityName.getValue(), 186 SnmpConstants.SNMP_ERROR_SUCCESS); 187 stateReference.setStateReference(stateRef); 188 189 return SnmpConstants.SNMP_MP_OK; 190 } 191 192 public boolean isProtocolVersionSupported(int snmpProtocolVersion) { 193 return (snmpProtocolVersion == SnmpConstants.version1); 194 } 195 196 public void releaseStateReference(PduHandle pduHandle) { 197 } 199 200 } 201 | Popular Tags |