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
Modifier and TypeClassDescriptionprotected static class
TheCache
stores state reference information for the MPv3.protected static class
MPv3.CacheEntry<A extends Address>
TheCacheEntry
class holds state reference information for the MPv3 message processing model for a single message.static interface
TheEngineIdCacheFactory
creates an engine ID cache with upper limit.static class
TheHeaderData
represents the message header information of SNMPv3 message. -
Field Summary
Modifier and TypeFieldDescriptionprotected MPv3.EngineIdCacheFactory
static final int
protected PDUFactory
static final OctetString
Local engine ID constant for context engineID discovery as defined by RFC 5343.static final int
static final int
static final int
static final int
static final int
Fields inherited from interface org.snmp4j.mp.MessageProcessingModel
MPv1, MPv2c, MPv2u, MPv3
-
Constructor Summary
ConstructorDescriptionMPv3()
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 andPDUFactory
for 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 TypeMethodDescriptionboolean
addEngineID
(Address address, OctetString engineID) Adds an engine ID (other than the local engine ID) to the internal storage.protected OctetString
addEngineIdToCache
(Address address, OctetString engineID) Put the engine ID for the given address into the internal cache.void
Adds 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 void
fireEngineChanged
(SnmpEngineEvent engineEvent) Fires a SNMP engine event the registered listeners.protected void
Fire 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.int
Get the number of cached engine IDs.int
getID()
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.int
Gets the maximum number of transport address to engineID mappings to be hold in the cache.int
Gets unique message ID.int
Gets 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
.void
Creates and initializes the default security protocols.boolean
isProtocolVersionSupported
(int version) Checks whether the supplied SNMP protocol version is supported by this message processing model.<A extends Address>
intprepareDataElements
(MessageDispatcher messageDispatcher, A transportAddress, BERInputStream wholeMsg, TransportStateReference tmStateReference, Integer32 messageProcessingModel, Integer32 securityModel, OctetString securityName, Integer32 securityLevel, MutablePDU pdu, PduHandle sendPduHandle, Integer32 maxSizeResponseScopedPDU, StatusInformation statusInformation, MutableStateReference<A> mutableStateReference) Prepare data elements from an incoming SNMP message as described in RFC3412 §7.2.<A extends Address>
intprepareOutgoingMessage
(A transportAddress, int maxMessageSize, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse, PduHandle sendPduHandle, BEROutputStream outgoingMessage, TransportStateReference tmStateReference) Prepares an outgoing message as defined in RFC3412 §7.1.<A extends Address>
intprepareResponseMessage
(int messageProcessingModel, int maxMessageSize, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference<A> stateReference, StatusInformation statusInformation, BEROutputStream outgoingMessage) Prepares a response message as defined in RFC3412 §7.1.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.void
releaseStateReference
(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
.void
Removes a SNMP engine listener.int
sendReport
(MessageDispatcher messageDispatcher, ScopedPDU pdu, int securityLevel, int securityModel, OctetString securityName, int maxSizeResponseScopedPDU, StateReference<?> stateReference, VariableBinding payload) Sends a report message.void
setCounterSupport
(CounterSupport counterSupport) Sets the counter support instance.void
setCurrentMsgID
(int nextMsgID) Sets the next message ID.void
setEngineIdCacheFactory
(MPv3.EngineIdCacheFactory engineIdCacheFactory) Sets the engine ID cache factory and resets (clears) the current cache.void
setLocalEngineID
(byte[] engineID) Sets the local engine ID.void
setMaxEngineIdCacheSize
(int maxEngineIdCacheSize) Sets the upper limit for the engine ID cache.void
setSecurityModels
(SecurityModels securityModels) Sets the security models supported by this MPv3.void
setSecurityProtocols
(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 andPDUFactory
for 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
. Ifnull
the default factory will be used which createsScopedPDU
instances.- 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 multipleSnmp
instances 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
. Ifnull
the default factory will be used which createsScopedPDU
instances.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.EngineIdCacheFactory
implementation. - 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 theEngineIdCacheSize
interface. By default the maximum cache sizeSNMP4JSettings.getMaxEngineIdCacheSize()
is used.- Parameters:
engineIdCacheFactory
- aMPv3.EngineIdCacheFactory
implementation that is used to create a new cache.- Since:
- 2.3.4
-
getMaxEngineIdCacheSize
public int getMaxEngineIdCacheSize()Description copied from interface:EngineIdCacheSize
Gets the maximum number of transport address to engineID mappings to be hold in the cache.- Specified by:
getMaxEngineIdCacheSize
in 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.EngineIdCacheFactory
and 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
AuthenticationProtocol
instance if the supplied ID is supported, otherwisenull
is returned.
-
getPrivProtocol
Gets an privacy protocol for the supplied ID.- Parameters:
id
- an privacy protocol OID.- Returns:
- an
PrivacyProtocol
instance if the supplied ID is supported, otherwisenull
is returned.
-
getSecurityModel
Gets the security model for the supplied ID.- Parameters:
id
- a security model ID.- Returns:
- a
SecurityModel
instance if the supplied ID is supported, otherwisenull
is returned.
-
getID
public int getID()Description copied from interface:MessageProcessingModel
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.- Specified by:
getID
in interfaceMessageProcessingModel
- Returns:
- a positive integer value.
-
isProtocolVersionSupported
public boolean isProtocolVersionSupported(int version) Description copied from interface:MessageProcessingModel
Checks whether the supplied SNMP protocol version is supported by this message processing model.- Specified by:
isProtocolVersionSupported
in interfaceMessageProcessingModel
- Parameters:
version
- the SNMP protocol version.- Returns:
true
if the supplied SNMP protocol is supported,false
otherwise.
-
addEngineID
Adds an engine ID (other than the local engine ID) to the internal storage.- Parameters:
address
- theAddress
of the remote SNMP engine.engineID
- the engine ID of the remote SNMP engine.- Returns:
true
if the engine ID has been added,false
otherwise (if the suppliedengineID
equals 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
null
if 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
- theAddress
of the remote SNMP engine.- Returns:
- the engine ID of the remote SNMP engine or
null
if there is no entry foraddress
in the local storage.
-
removeEngineID
Removes an engine ID association from the local storage and fires the correspondingSnmpEngineEvent
.- Parameters:
address
- theAddress
of the remote SNMP engine for whose engine ID is to be removed.- Returns:
- the removed engine ID of the remote SNMP engine or
null
if there is no entry foraddress
in 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:MessageProcessingModel
Release the state reference associated with the suppliedPduHandle
.- Specified by:
releaseStateReference
in interfaceMessageProcessingModel
- Parameters:
pduHandle
- aPduHandle
.
-
prepareOutgoingMessage
public <A extends Address> int prepareOutgoingMessage(A transportAddress, int maxMessageSize, int messageProcessingModel, int securityModel, byte[] securityName, int securityLevel, PDU pdu, boolean expectResponse, PduHandle sendPduHandle, BEROutputStream outgoingMessage, TransportStateReference tmStateReference) throws IOException Description copied from interface:MessageProcessingModel
Prepares an outgoing message as defined in RFC3412 §7.1.- Specified by:
prepareOutgoingMessage
in interfaceMessageProcessingModel
- Type Parameters:
A
- the targetAddress
type.- Parameters:
transportAddress
- the destination transportAddress
.maxMessageSize
- the maximum message size the transport mapping for the destination address is capable of.messageProcessingModel
- theMessageProcessingModel
ID (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
- thePDU
to send. For a SNMPv1 trappdu
has to be aPDUv1
instance, for SNMPv3 messages it has to be aScopedPDU
instance.expectResponse
- indicates if a message expects a response. This has to betrue
for confirmed class PDUs andfalse
otherwise.sendPduHandle
- thePduHandle
that uniquely identifies the sent message.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_OK
is 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 <A extends Address> int prepareResponseMessage(int messageProcessingModel, int maxMessageSize, int securityModel, byte[] securityName, int securityLevel, PDU pdu, int maxSizeResponseScopedPDU, StateReference<A> stateReference, StatusInformation statusInformation, BEROutputStream outgoingMessage) throws IOException Description copied from interface:MessageProcessingModel
Prepares a response message as defined in RFC3412 §7.1.- Specified by:
prepareResponseMessage
in interfaceMessageProcessingModel
- Type Parameters:
A
- the targetAddress
type.- Parameters:
messageProcessingModel
- int theMessageProcessingModel
ID (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
- thePDU
to send. For a SNMPv1 trappdu
has to be aPDUv1
instance, for SNMPv3 messages it has to be aScopedPDU
instance.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_OK
is 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 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 <A extends Address> int prepareDataElements(MessageDispatcher messageDispatcher, A transportAddress, BERInputStream wholeMsg, TransportStateReference tmStateReference, Integer32 messageProcessingModel, Integer32 securityModel, OctetString securityName, Integer32 securityLevel, MutablePDU pdu, PduHandle sendPduHandle, Integer32 maxSizeResponseScopedPDU, StatusInformation statusInformation, MutableStateReference<A> mutableStateReference) Description copied from interface:MessageProcessingModel
Prepare data elements from an incoming SNMP message as described in RFC3412 §7.2.- Specified by:
prepareDataElements
in interfaceMessageProcessingModel
- Type Parameters:
A
- the targetAddress
type.- Parameters:
messageDispatcher
- theMessageDispatcher
instance to be used to send reports. Thus,messageDispatcher
is 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 occured, 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 theMessageProcessingModel
to send reports over the same transport as it received them.- Returns:
- int the status of the message preparation.
SnmpConstants.SNMP_MP_OK
is returned on success, otherwise any of theSnmpConstants.SNMP_MP_*
values may be returned.
-
setSecurityModels
Sets the security models supported by this MPv3.- Parameters:
securityModels
- aSecurityModels
instance.
-
getSecurityModels
Gets the security models supported by this MPv3.- Returns:
- a
SecurityModels
instance.
-
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
CounterSupport
instance that is used to fireCounterEvent
.
-
setCounterSupport
Sets the counter support instance. By default, the singleton instance provided by theCounterSupport
instance is used.- Parameters:
counterSupport
- aCounterSupport
subclass instance.
-
addSnmpEngineListener
Adds a SNMP engine listener that needs to be informed about changes to the engine ID cache.- Parameters:
l
- aSnmpEngineListener
instance.- Since:
- 1.6
-
removeSnmpEngineListener
Removes a SNMP engine listener.- Parameters:
l
- aSnmpEngineListener
instance.- 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
- thetarget
parameter must be ignored.- Returns:
- a
ScopedPDU
instance 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
- theSnmpEngineEvent
instance to fire.- Since:
- 1.6
-
DefaultPDUFactory.createPDU(MessageProcessingModel, int)
instead.