Package org.snmp4j.mp
Class MPv3
java.lang.Object
org.snmp4j.mp.MPv3
- All Implemented Interfaces:
 EngineIdCacheSize,MessageProcessingModel
The 
MPv3 is the message processing model for SNMPv3.- Version:
 - 1.9.2
 - Author:
 - Frank Fock
 
- 
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classTheCachestores state reference information for the MPv3.protected static classMPv3.CacheEntry<A extends Address>TheCacheEntryclass holds state reference information for the MPv3 message processing model for a single message.static interfaceTheEngineIdCacheFactorycreates an engine ID cache with upper limit.static classTheHeaderDatarepresents the message header information of SNMPv3 message. - 
Field Summary
FieldsModifier and TypeFieldDescriptionprotected MPv3.EngineIdCacheFactorystatic final intprotected PDUFactorystatic final OctetStringLocal engine ID constant for context engineID discovery as defined by RFC 5343.static final intstatic final intstatic final intstatic final intstatic final intFields inherited from interface org.snmp4j.mp.MessageProcessingModel
MPv1, MPv2c, MPv2u, MPv3 - 
Constructor Summary
ConstructorsConstructorDescriptionMPv3()Creates a MPv3 with a default local engine ID.MPv3(byte[] localEngineID) Creates a MPv3 with a supplied local engine ID.MPv3(byte[] localEngineID, PDUFactory incomingPDUFactory) Creates a MPv3 with a supplied local engine ID andPDUFactoryfor incoming messages.MPv3(byte[] localEngineID, PDUFactory incomingPDUFactory, SecurityProtocols secProtocols, SecurityModels secModels, CounterSupport counterSupport) Creates a fully qualified MPv3 instance with custom security protocols and models as well as a custom counter support.This is a convenience constructor which can be used to create a MPv3 which is bound to a specific USM instance. - 
Method Summary
Modifier and TypeMethodDescriptionbooleanaddEngineID(Address address, OctetString engineID) Adds an engine ID (other than the local engine ID) to the internal storage.protected OctetStringaddEngineIdToCache(Address address, OctetString engineID) Put the engine ID for the given address into the internal cache.voidAdds a SNMP engine listener that needs to be informed about changes to the engine ID cache.static byte[]Creates a local engine ID based on the local IP address and additional four random bytes.static byte[]Creates a local engine ID based on the ID string suppliedDeprecated.protected voidfireEngineChanged(SnmpEngineEvent engineEvent) Fires a SNMP engine event the registered listeners.protected voidFire a counter incrementation event.getAuthProtocol(OID id) Gets an authentication protocol for the supplied ID.Gets the counter support instance that can be used to register for counter incrementation events.getEngineID(Address address) Gets the engine ID associated with the supplied address from the local storage and fires the correspondingSnmpEngineEvent.Returns the enging ID factory that was used to create the current engine ID cache.intGet the number of cached engine IDs.intgetID()Gets the numerical ID of the message processing model as defined by the constants in this interface or by an appropriate constant in the class implementing this interface.byte[]Gets a copy of the local engine ID.intGets the maximum number of transport address to engineID mappings to be hold in the cache.intGets unique message ID.intGets the message ID that will be used for the next request to be sent by this message processing model.getPrivProtocol(OID id) Gets an privacy protocol for the supplied ID.getSecurityModel(int id) Gets the security model for the supplied ID.Gets the security models supported by this MPv3.Gets the security protocols supported by thisMPv3.voidCreates and initializes the default security protocols.booleanisProtocolVersionSupported(int version) Checks whether the supplied SNMP protocol version is supported by this message processing model.intprepareDataElements(MessageDispatcher messageDispatcher, Address transportAddress, BERInputStream wholeMsg, TransportStateReference tmStateReference, Integer32 messageProcessingModel, Integer32 securityModel, OctetString securityName, Integer32 securityLevel, MutablePDU pdu, PduHandle sendPduHandle, Integer32 maxSizeResponseScopedPDU, StatusInformation statusInformation, MutableStateReference mutableStateReference) Prepare data elements from an incoming SNMP message as described in RFC3412 §7.2.intprepareOutgoingMessage(Address transportAddress, int maxMessageSize, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse, PduHandle sendPduHandle, Address destTransportAddress, BEROutputStream outgoingMessage, TransportStateReference tmStateReference) Prepares an outgoing message as defined in RFC3412 §7.1.intprepareResponseMessage(int messageProcessingModel, int maxMessageSize, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference stateReference, StatusInformation statusInformation, BEROutputStream outgoingMessage) Prepares a response message as defined in RFC3412 §7.1.static intrandomMsgID(int engineBoots) Creates a random message ID according to the method proposed by RFC3412: "Values for msgID SHOULD be generated in a manner that avoids re-use of any outstanding values.voidreleaseStateReference(PduHandle pduHandle) Release the state reference associated with the suppliedPduHandle.removeEngineID(Address address) Removes an engine ID association from the local storage and fires the correspondingSnmpEngineEvent.voidRemoves a SNMP engine listener.intsendReport(MessageDispatcher messageDispatcher, ScopedPDU pdu, int securityLevel, int securityModel, OctetString securityName, int maxSizeResponseScopedPDU, StateReference<?> stateReference, VariableBinding payload) Sends a report message.voidsetCounterSupport(CounterSupport counterSupport) Sets the counter support instance.voidsetCurrentMsgID(int nextMsgID) Sets the next message ID.voidsetEngineIdCacheFactory(MPv3.EngineIdCacheFactory engineIdCacheFactory) Sets the engine ID cache factory and resets (clears) the current cache.voidsetLocalEngineID(byte[] engineID) Sets the local engine ID.voidsetMaxEngineIdCacheSize(int maxEngineIdCacheSize) Sets the upper limit for the engine ID cache.voidsetSecurityModels(SecurityModels securityModels) Sets the security models supported by this MPv3.voidsetSecurityProtocols(SecurityProtocols securityProtocols) Sets the security protocols for thisMPv3. 
- 
Field Details
- 
ID
public static final int ID- See Also:
 
 - 
MPv3_REPORTABLE_FLAG
public static final int MPv3_REPORTABLE_FLAG- See Also:
 
 - 
MAX_MESSAGE_ID
public static final int MAX_MESSAGE_ID- See Also:
 
 - 
LOCAL_ENGINE_ID
Local engine ID constant for context engineID discovery as defined by RFC 5343. - 
MAXLEN_ENGINE_ID
public static final int MAXLEN_ENGINE_ID- See Also:
 
 - 
MINLEN_ENGINE_ID
public static final int MINLEN_ENGINE_ID- See Also:
 
 - 
MAX_HEADER_LENGTH
public static final int MAX_HEADER_LENGTH - 
engineIdCacheFactory
 - 
incomingPDUFactory
 
 - 
 - 
Constructor Details
- 
MPv3
public MPv3()Creates a MPv3 with a default local engine ID. - 
MPv3
public MPv3(byte[] localEngineID) Creates a MPv3 with a supplied local engine ID.- Parameters:
 localEngineID- the local engine ID. Its length must be greater or equal than 5 and less or equal than 32.
 - 
MPv3
Creates a MPv3 with a supplied local engine ID andPDUFactoryfor incoming messages.- Parameters:
 localEngineID- the local engine ID. Its length must be greater or equal than 5 and less or equal than 32.incomingPDUFactory- aPDUFactory. Ifnullthe default factory will be used which createsScopedPDUinstances.- Since:
 - 1.9.1
 
 - 
MPv3
This is a convenience constructor which can be used to create a MPv3 which is bound to a specific USM instance. A dedicated USM instance per MPv3 is necessary if multipleSnmpinstances are used within a VM.- Parameters:
 usm- an USM instance.- Since:
 - 1.10
 
 - 
MPv3
public MPv3(byte[] localEngineID, PDUFactory incomingPDUFactory, SecurityProtocols secProtocols, SecurityModels secModels, CounterSupport counterSupport) Creates a fully qualified MPv3 instance with custom security protocols and models as well as a custom counter support. The current message ID is set using the USM engine boots counter (if available) according to the RFC3412 §6.2.- Parameters:
 localEngineID- the local engine ID. Its length must be greater or equal than 5 and less or equal than 32.incomingPDUFactory- aPDUFactory. Ifnullthe default factory will be used which createsScopedPDUinstances.secProtocols- the SecurityProtocols instance to use when looking up a security protocol. To get a default instance, useSecurityProtocols.getInstance().secModels- the SecurityModels instance to use when looking up a security model. If you use more than one USM instance, you need to create a SecurityProtocols instance (container) for each such USM instance (and MPv3 combination). To get a default instance, useSecurityProtocols.getInstance().counterSupport- The CounterSupport instance to be used to count events created by this MPv3 instance. To get a default instance, useCounterSupport.getInstance().- Since:
 - 1.10
 
 
 - 
 - 
Method Details
- 
getEngineIdCacheFactory
Returns the enging ID factory that was used to create the current engine ID cache.- Returns:
 - a 
MPv3.EngineIdCacheFactoryimplementation. - Since:
 - 2.3.4
 
 - 
setEngineIdCacheFactory
Sets the engine ID cache factory and resets (clears) the current cache. The maximum size of the cache is determined usinggetMaxEngineIdCacheSize()as this implements theEngineIdCacheSizeinterface. By default the maximum cache sizeSNMP4JSettings.getMaxEngineIdCacheSize()is used.- Parameters:
 engineIdCacheFactory- aMPv3.EngineIdCacheFactoryimplementation that is used to create a new cache.- Since:
 - 2.3.4
 
 - 
getMaxEngineIdCacheSize
public int getMaxEngineIdCacheSize()Description copied from interface:EngineIdCacheSizeGets the maximum number of transport address to engineID mappings to be hold in the cache.- Specified by:
 getMaxEngineIdCacheSizein interfaceEngineIdCacheSize- Returns:
 - the maximum size of the engine ID cache.
 
 - 
setMaxEngineIdCacheSize
public void setMaxEngineIdCacheSize(int maxEngineIdCacheSize) Sets the upper limit for the engine ID cache. Modifying this value will not immediately take effect on the cache size.- Parameters:
 maxEngineIdCacheSize- the maximum number of engine IDs hold in the internal cache. If more than those engine IDs are used by the MPv3, the eldest engine ID is removed from the cache. Eldest means the eldest initial use. A different cache can be implemented by using a customMPv3.EngineIdCacheFactoryand setting it after calling this constructor.
 - 
createLocalEngineID
public static byte[] createLocalEngineID()Creates a local engine ID based on the local IP address and additional four random bytes. WARNING: Do not use this engine ID generator for a command responder (agent) if you DO NOT persistently save the one time generated engine ID for subsequent use when the agent is restarted.- Returns:
 - a new local engine ID with a random part to avoid engine ID clashes for multiple command generators on the same system.
 
 - 
createLocalEngineID
Creates a local engine ID based on the ID string supplied- Parameters:
 id- an ID string.- Returns:
 - a new local engine ID.
 
 - 
setLocalEngineID
public void setLocalEngineID(byte[] engineID) Sets the local engine ID. This value must not be changed after message processing has been started. Note: When setting the local engine ID, the engine boots counter should be known at the same time. Thus, please also callsetCurrentMsgID(randomMsgID(engineBoots));
before starting the message processing.- Parameters:
 engineID- the local engine ID. Its length must be greater or equal than 5 and less or equal than 32.
 - 
randomMsgID
public static int randomMsgID(int engineBoots) Creates a random message ID according to the method proposed by RFC3412: "Values for msgID SHOULD be generated in a manner that avoids re-use of any outstanding values. Doing so provides protection against some replay attacks. One possible implementation strategy would be to use the low-order bits of snmpEngineBoots [RFC3411] as the high-order portion of the msgID value and a monotonically increasing integer for the low-order portion of msgID."- Parameters:
 engineBoots- the number of boot operations already occurred for this SNMP entity.- Returns:
 - the new random message ID.
 
 - 
getLocalEngineID
public byte[] getLocalEngineID()Gets a copy of the local engine ID.- Returns:
 - a byte array containing the local engine ID.
 
 - 
initDefaults
public void initDefaults()Creates and initializes the default security protocols.- See Also:
 
 - 
getAuthProtocol
Gets an authentication protocol for the supplied ID.- Parameters:
 id- an authentication protocol OID.- Returns:
 - an 
AuthenticationProtocolinstance if the supplied ID is supported, otherwisenullis returned. 
 - 
getPrivProtocol
Gets an privacy protocol for the supplied ID.- Parameters:
 id- an privacy protocol OID.- Returns:
 - an 
PrivacyProtocolinstance if the supplied ID is supported, otherwisenullis returned. 
 - 
getSecurityModel
Gets the security model for the supplied ID.- Parameters:
 id- a security model ID.- Returns:
 - a 
SecurityModelinstance if the supplied ID is supported, otherwisenullis returned. 
 - 
getID
public int getID()Description copied from interface:MessageProcessingModelGets the numerical ID of the message processing model as defined by the constants in this interface or by an appropriate constant in the class implementing this interface.- Specified by:
 getIDin interfaceMessageProcessingModel- Returns:
 - a positive integer value.
 
 - 
isProtocolVersionSupported
public boolean isProtocolVersionSupported(int version) Description copied from interface:MessageProcessingModelChecks whether the supplied SNMP protocol version is supported by this message processing model.- Specified by:
 isProtocolVersionSupportedin interfaceMessageProcessingModel- Parameters:
 version- the SNMP protocol version.- Returns:
 trueif the supplied SNMP protocol is supported,falseotherwise.
 - 
addEngineID
Adds an engine ID (other than the local engine ID) to the internal storage.- Parameters:
 address- theAddressof the remote SNMP engine.engineID- the engine ID of the remote SNMP engine.- Returns:
 trueif the engine ID has been added,falseotherwise (if the suppliedengineIDequals the local one).
 - 
addEngineIdToCache
Put the engine ID for the given address into the internal cache. If the cache reached its limit,- Parameters:
 address- the address of the engine IDengineID- the engine ID to cache.- Returns:
 - the previous engine ID or 
nullif there was no engine ID cached for the given address. - Throws:
 IllegalArgumentException- when the local maximum cache size is exceeded.- Since:
 - 2.3.4
 
 - 
getEngineID
Gets the engine ID associated with the supplied address from the local storage and fires the correspondingSnmpEngineEvent.- Parameters:
 address- theAddressof the remote SNMP engine.- Returns:
 - the engine ID of the remote SNMP engine or 
nullif there is no entry foraddressin the local storage. 
 - 
removeEngineID
Removes an engine ID association from the local storage and fires the correspondingSnmpEngineEvent.- Parameters:
 address- theAddressof the remote SNMP engine for whose engine ID is to be removed.- Returns:
 - the removed engine ID of the remote SNMP engine or 
nullif there is no entry foraddressin the local storage. 
 - 
getNextMessageID
public int getNextMessageID()Gets unique message ID.- Returns:
 - a message ID greater or equal to one and less or equal 
MAX_MESSAGE_ID. 
 - 
getSecurityProtocols
Gets the security protocols supported by thisMPv3.- Returns:
 - return a 
SecurityProtocols. 
 - 
setSecurityProtocols
Sets the security protocols for thisMPv3.- Parameters:
 securityProtocols- SecurityProtocols
 - 
releaseStateReference
Description copied from interface:MessageProcessingModelRelease the state reference associated with the suppliedPduHandle.- Specified by:
 releaseStateReferencein interfaceMessageProcessingModel- Parameters:
 pduHandle- aPduHandle.
 - 
prepareOutgoingMessage
public int prepareOutgoingMessage(Address transportAddress, int maxMessageSize, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse, PduHandle sendPduHandle, Address destTransportAddress, BEROutputStream outgoingMessage, TransportStateReference tmStateReference) throws IOException Description copied from interface:MessageProcessingModelPrepares an outgoing message as defined in RFC3412 §7.1.- Specified by:
 prepareOutgoingMessagein interfaceMessageProcessingModel- Parameters:
 transportAddress- the destination transportAddress.maxMessageSize- the maximum message size the transport mapping for the destination address is capable of.messageProcessingModel- theMessageProcessingModelID (typically, the SNMP version).securityModel- the security model ID (seeSecurityModel) to use.securityName- the principal on behalf the message is to be sent.securityLevel- the level of security requested (seeSecurityLevel).pdu- thePDUto send. For a SNMPv1 trappduhas to be aPDUv1instance, for SNMPv3 messages it has to be aScopedPDUinstance.expectResponse- indicates if a message expects a response. This has to betruefor confirmed class PDUs andfalseotherwise.sendPduHandle- thePduHandlethat uniquely identifies the sent message.destTransportAddress- returns the destination transport address (currently set always set totransportAddress.outgoingMessage- returns the message to send.tmStateReference- the transport model state reference as defined by RFC 5590.- Returns:
 - the status of the message preparation. 
SnmpConstants.SNMP_MP_OKis returned if on success, otherwise any of theSnmpConstants.SNMP_MP_*values may be returned. - Throws:
 IOException- if the supplied PDU could not be encoded to theoutgoingMessage
 - 
prepareResponseMessage
public int prepareResponseMessage(int messageProcessingModel, int maxMessageSize, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference stateReference, StatusInformation statusInformation, BEROutputStream outgoingMessage) throws IOException Description copied from interface:MessageProcessingModelPrepares a response message as defined in RFC3412 §7.1.- Specified by:
 prepareResponseMessagein interfaceMessageProcessingModel- Parameters:
 messageProcessingModel- int theMessageProcessingModelID (typically, the SNMP version).maxMessageSize- the maximum message size the transport mapping for the destination address is capable of.securityModel- the security model ID (seeSecurityModel) to use.securityName- the principal on behalf the message is to be sent.securityLevel- the level of security requested (seeSecurityLevel).pdu- thePDUto send. For a SNMPv1 trappduhas to be aPDUv1instance, for SNMPv3 messages it has to be aScopedPDUinstance.maxSizeResponseScopedPDU- the maximum size of the scoped PDU the sender (of the request) can accept.stateReference- reference to state information presented with the request.statusInformation- returns success or error indication. When an error occured, the error counter OID and value are included.outgoingMessage- returns the message to send.- Returns:
 - the status of the message preparation. 
SnmpConstants.SNMP_MP_OKis returned if on success, otherwise any of theSnmpConstants.SNMP_MP_*values may be returned. - Throws:
 IOException- if an internal error or a resource exception occured.
 - 
sendReport
public int sendReport(MessageDispatcher messageDispatcher, ScopedPDU pdu, int securityLevel, int securityModel, OctetString securityName, int maxSizeResponseScopedPDU, StateReference<?> stateReference, VariableBinding payload) Sends a report message.- Parameters:
 messageDispatcher- Send the message on behalf the supplied MessageDispatcher instance.pdu- ScopedPDU Ifnull, then contextEngineID, contextName, and requestID of the report generated will be zero length and zero respective. Otherwise these values are extracted from the PDU.securityLevel- The security level to use when sending this report.securityModel- The security model to use when sending this report.securityName- The security name to use when sending this report.maxSizeResponseScopedPDU- the maximum size of the report message (will be most likely ignored because a report should always fit in 484 bytes).stateReference- the state reference associated with the original message.payload- the variable binding to include in the report message.- Returns:
 - an SNMP MPv3 error code or 0 if the report has been send successfully.
 
 - 
prepareDataElements
public int prepareDataElements(MessageDispatcher messageDispatcher, Address transportAddress, BERInputStream wholeMsg, TransportStateReference tmStateReference, Integer32 messageProcessingModel, Integer32 securityModel, OctetString securityName, Integer32 securityLevel, MutablePDU pdu, PduHandle sendPduHandle, Integer32 maxSizeResponseScopedPDU, StatusInformation statusInformation, MutableStateReference mutableStateReference) Description copied from interface:MessageProcessingModelPrepare data elements from an incoming SNMP message as described in RFC3412 §7.2.- Specified by:
 prepareDataElementsin interfaceMessageProcessingModel- Parameters:
 messageDispatcher- theMessageDispatcherinstance to be used to send reports. Thus,messageDispatcheris typically the calling module.transportAddress- the origin transport address.wholeMsg- the whole message as received from the network.tmStateReference- the transport model state reference as defined by RFC 5590.messageProcessingModel- returns the message processing model (typically the SNMP version).securityModel- returns the security model ID (seeSecurityModel.securityName- returns the principal.securityLevel- returns the requested security level (seeSecurityLevel).pdu- returns SNMP protocol data unit (the payload of the received message).sendPduHandle- returns the handle to match request.maxSizeResponseScopedPDU- returns the maximum size of the scoped PDU the sender can accept.statusInformation- returns success or error indication. When an error occurred, the error counter OID and value are included.mutableStateReference- returns the state reference to be used for a possible response. On input the stateReference may contain information about the transport mapping of the incoming request. This allows theMessageProcessingModelto send reports over the same transport as it received them.- Returns:
 - int
   the status of the message preparation. 
SnmpConstants.SNMP_MP_OKis returned on success, otherwise any of theSnmpConstants.SNMP_MP_*values may be returned. 
 - 
setSecurityModels
Sets the security models supported by this MPv3.- Parameters:
 securityModels- aSecurityModelsinstance.
 - 
getSecurityModels
Gets the security models supported by this MPv3.- Returns:
 - a 
SecurityModelsinstance. 
 - 
fireIncrementCounter
Fire a counter incrementation event.- Parameters:
 e- CounterEvent
 - 
getCounterSupport
Gets the counter support instance that can be used to register for counter incrementation events.- Returns:
 - a 
CounterSupportinstance that is used to fireCounterEvent. 
 - 
setCounterSupport
Sets the counter support instance. By default, the singleton instance provided by theCounterSupportinstance is used.- Parameters:
 counterSupport- aCounterSupportsubclass instance.
 - 
addSnmpEngineListener
Adds a SNMP engine listener that needs to be informed about changes to the engine ID cache.- Parameters:
 l- aSnmpEngineListenerinstance.- Since:
 - 1.6
 
 - 
removeSnmpEngineListener
Removes a SNMP engine listener.- Parameters:
 l- aSnmpEngineListenerinstance.- Since:
 - 1.6
 
 - 
getEngineIdCacheSize
public int getEngineIdCacheSize()Get the number of cached engine IDs.- Returns:
 - size of the internal engine ID cache.
 - Since:
 - 2.3.4
 
 - 
createPDU
Deprecated.Creates a PDU class that is used to parse incoming SNMP messages.- Parameters:
 target- thetargetparameter must be ignored.- Returns:
 - a 
ScopedPDUinstance by default. - Since:
 - 1.9.1
 
 - 
getNextMsgID
public int getNextMsgID()Gets the message ID that will be used for the next request to be sent by this message processing model.- Returns:
 - the next message ID used by the MPv3.
 - Since:
 - 2.4.3
 
 - 
setCurrentMsgID
public void setCurrentMsgID(int nextMsgID) Sets the next message ID. According to RFC3412, the message ID should be unique across reboots: "Values for msgID SHOULD be generated in a manner that avoids re-use of any outstanding values. Doing so provides protection against some replay attacks. One possible implementation strategy would be to use the low-order bits of snmpEngineBoots [RFC3411] as the high-order portion of the msgID value and a monotonically increasing integer for the low-order portion of msgID."- Parameters:
 nextMsgID- a message ID that has not been used by this SNMP entity yet (preferably also not used during previous runs).- Since:
 - 2.4.3
 
 - 
fireEngineChanged
Fires a SNMP engine event the registered listeners.- Parameters:
 engineEvent- theSnmpEngineEventinstance to fire.- Since:
 - 1.6
 
 
 - 
 
DefaultPDUFactory.createPDU(MessageProcessingModel, int)instead.