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.security.SecurityModels; 41 import org.snmp4j.security.SecurityLevel; 42 import org.snmp4j.asn1.BEROutputStream; 43 import java.nio.ByteBuffer ; 44 45 52 public class MPv2c implements MessageProcessingModel { 53 54 public static final int ID = MessageProcessingModel.MPv2c; 55 private static final LogAdapter logger = LogFactory.getLogger(MPv2c.class); 56 57 public MPv2c() { 58 } 59 60 public int getID() { 61 return ID; 62 } 63 64 public int prepareOutgoingMessage(Address transportAddress, 65 int maxMessageSize, 66 int messageProcessingModel, 67 int securityModel, 68 byte[] securityName, 69 int securityLevel, 70 PDU pdu, 71 boolean expectResponse, 72 PduHandle sendPduHandle, 73 Address destTransportAddress, 74 BEROutputStream outgoingMessage) 75 throws IOException 76 { 77 if ((securityLevel != SecurityLevel.NOAUTH_NOPRIV) || 78 (securityModel != SecurityModel.SECURITY_MODEL_SNMPv2c)) { 79 logger.error("MPv2c used with unsupported security model"); 80 return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL; 81 } 82 if (pdu instanceof ScopedPDU) { 83 String txt = "ScopedPDU must not be used with MPv2c"; 84 logger.error(txt); 85 throw new IllegalArgumentException (txt); 86 } 87 88 if (!isProtocolVersionSupported(messageProcessingModel)) { 89 logger.error("MPv2c used with unsupported SNMP version"); 90 return SnmpConstants.SNMP_MP_UNSUPPORTED_SECURITY_MODEL; 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 155 MutableByte mutableByte = new MutableByte(); 156 int length = BER.decodeHeader(wholeMsg, mutableByte); 157 int startPos = (int)wholeMsg.getPosition(); 158 159 if (mutableByte.getValue() != BER.SEQUENCE) { 160 String txt = "SNMPv2c PDU must start with a SEQUENCE"; 161 logger.error(txt); 162 throw new IOException (txt); 163 } 164 Integer32 version = new Integer32(); 165 version.decodeBER(wholeMsg); 166 167 securityName.decodeBER(wholeMsg); 168 securityLevel.setValue(SecurityLevel.NOAUTH_NOPRIV); 169 securityModel.setValue(SecurityModel.SECURITY_MODEL_SNMPv2c); 170 messageProcessingModel.setValue(ID); 171 172 PDU v2cPDU = new PDU(); 173 pdu.setPdu(v2cPDU); 174 v2cPDU.decodeBER(wholeMsg); 175 176 BER.checkSequenceLength(length, 177 (int)wholeMsg.getPosition() - startPos, 178 v2cPDU); 179 180 sendPduHandle.setTransactionID(v2cPDU.getRequestID().getValue()); 181 182 StateReference stateRef = 184 new StateReference(sendPduHandle, 185 transportAddress, 186 null, 187 SecurityModels.getInstance().getSecurityModel(securityModel), 188 securityName.getValue(), 189 SnmpConstants.SNMP_ERROR_SUCCESS); 190 stateReference.setStateReference(stateRef); 191 192 return SnmpConstants.SNMP_MP_OK; 193 } 194 195 public boolean isProtocolVersionSupported(int snmpProtocolVersion) { 196 return (snmpProtocolVersion == SnmpConstants.version2c); 197 } 198 199 public void releaseStateReference(PduHandle pduHandle) { 200 } 202 } 203 | Popular Tags |