public abstract class SnmpContextv3Basis extends AbstractSnmpContext implements SnmpContextv3Face, java.lang.Cloneable
This class will perform the v3 discovery of the SNMP engine ID and
time line if necessary. This is done with the classes
TimeWindow
and UsmDiscoveryBean
.
Now that the stack can send traps and receive requests, it needs to be able to act as an authoritative SNMP engine. This is done via the interface UsmAgent. The DefaultUsmAgent is not guaranteed to work; agents (or rather authoritative engines) should provide a better implementation.
This class will use the User Security Model (USM) as described in SNMP-USER-BASED-SM-MIB. See also RFC 3411.
It is advised to set all the properties of this class before any PDU,
using this class, is sent.
All properties are being used to encode the message. Some properties are
being used to decode the Response or Report PDU.
When any of these last properties were changed in between flight there
is a possibility the decoding fails, causing a
DecodingException
.
destroy()
should be called when the context is no longer
used. This is the only way the threads will be stopped and garbage
collected.
SnmpContextv3Face
,
SnmpContextv3Pool
,
TimeWindow
,
UsmAgent
,
DefaultUsmAgent
,
setUsmAgent(UsmAgent)
,
UsmDiscoveryBean
Modifier and Type | Field and Description |
---|---|
protected int |
authenticationProtocol |
protected byte[] |
contextEngineId |
protected java.lang.String |
contextName |
protected int |
privacyProtocol |
protected boolean |
useAuthentication |
protected boolean |
usePrivacy |
protected java.lang.String |
userAuthenticationPassword |
protected byte[] |
userAuthKeyMD5 |
protected byte[] |
userAuthKeySHA1 |
protected java.lang.String |
userName |
protected java.lang.String |
userPrivacyPassword |
protected byte[] |
userPrivKeyMD5 |
protected byte[] |
userPrivKeySHA1 |
protected UsmAgent |
usmAgent |
anyPduExpectingResponse, bindAddr, hostname, hostPort, isDestroyed, maxRecvSize, pduSupport, trapSupport, typeSocket
AES_ENCRYPT, Default_ContextName, Default_UserName, DES_ENCRYPT, MD5_PROTOCOL, ProtocolNames, SHA1_PROTOCOL, SNMPv1_Security_Model, SNMPv2c_Security_Model, USM_Security_Model
DEFAULT_PORT, MAXPDU, MSS, STANDARD_SOCKET, TCP_SOCKET
Constructor and Description |
---|
SnmpContextv3Basis(java.lang.String host,
int port)
Constructor.
|
SnmpContextv3Basis(java.lang.String host,
int port,
java.lang.String typeSocketA)
Constructor.
|
SnmpContextv3Basis(java.lang.String host,
int port,
java.lang.String bindAddress,
java.lang.String typeSocketA)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
protected byte[] |
actualEncodePacket(byte msg_type,
int rId,
int errstat,
int errind,
java.util.Enumeration ve,
uk.co.westhawk.snmp.stack.TimeWindowNode node,
java.lang.Object obj)
Does the actual encoding.
|
boolean |
addDiscoveryPdu(DiscoveryPdu pdu)
Adds a discovery pdu.
|
boolean |
addPdu(Pdu pdu)
Adds a PDU.
|
protected boolean |
addPdu(Pdu pdu,
boolean checkDiscovery)
Adds a PDU.
|
void |
addRequestPduListener(RequestPduListener l,
ListeningContextPool lcontext)
Adds the specified request pdu listener to receive PDUs on the
specified listening context that matches this context.
|
protected java.lang.String |
checkContextSanity()
Checks the sanity of the context and returns an error message when it
is not correct.
|
java.lang.Object |
cloneParameters(SnmpContextv3Face clContext)
Copies all parameters into another SnmpContextv3.
|
protected UsmAgent |
createUsmAgent()
Creates the USM agent.
|
byte[] |
encodeDiscoveryPacket(byte msg_type,
int rId,
int errstat,
int errind,
java.util.Enumeration ve,
java.lang.Object obj)
Encodes a discovery PDU packet.
|
byte[] |
encodePacket(byte msg_type,
int rId,
int errstat,
int errind,
java.util.Enumeration ve,
java.lang.Object obj)
Encodes a PDU.
|
int |
getAuthenticationProtocol()
Returns the protocol to be used for authentication.
|
byte[] |
getContextEngineId()
Returns the contextEngineID.
|
java.lang.String |
getContextName()
Returns the contextName.
|
java.lang.String |
getHashKey()
Returns the hash key.
|
int |
getPrivacyProtocol()
Returns the protocol to be used for privacy.
|
java.lang.String |
getUserAuthenticationPassword()
Returns the user authentication password.
|
java.lang.String |
getUserName()
Returns the username.
|
java.lang.String |
getUserPrivacyPassword()
Returns the user privacy password.
|
UsmAgent |
getUsmAgent()
Returns the UsmAgent.
|
int |
getVersion()
Returns the SNMP version of this context.
|
protected boolean |
isAuthoritative(byte msg_type)
Returns if we send this PDU in authoritative role or not.
|
boolean |
isUseAuthentication()
Returns if authentication is used or not.
|
boolean |
isUsePrivacy()
Returns if privacy is used or not.
|
protected void |
processIncomingResponse(java.io.ByteArrayInputStream in)
Processes an incoming SNMP v3 response.
|
boolean |
removePdu(int rid)
Removes a PDU.
|
void |
setAuthenticationProtocol(int protocol)
Sets the protocol to be used for authentication.
|
void |
setContextEngineId(byte[] newContextEngineId)
Sets the contextEngineID.
|
void |
setContextName(java.lang.String newContextName)
Sets the contextName.
|
void |
setPrivacyProtocol(int protocol)
Sets the protocol to be used for privacy.
|
void |
setUseAuthentication(boolean newUseAuthentication)
Sets whether authentication has to be used.
|
void |
setUsePrivacy(boolean newUsePrivacy)
Sets whether privacy has to be used.
|
void |
setUserAuthenticationPassword(java.lang.String newUserAuthPassword)
Sets the user authentication password.
|
void |
setUserName(java.lang.String newUserName)
Sets the username.
|
void |
setUserPrivacyPassword(java.lang.String newUserPrivacyPassword)
Sets the user privacy password.
|
void |
setUsmAgent(UsmAgent agent)
Sets the UsmAgent, needed when this stack is used as authoritative
SNMP engine.
|
java.lang.String |
toString()
Returns a string representation of the object.
|
activate, addRequestPduListener, addRequestPduListener, addTrapListener, addTrapListener, addTrapListener, clone, destroy, freeTransmitters, getBindAddress, getDebugString, getHost, getHostAddress, getMaxRecvSize, getPort, getReceivedFromHostAddress, getSendToHostAddress, getTypeSocket, isDestroyed, processIncomingPdu, rawPduReceived, removeRequestPduListener, removeRequestPduListener, removeRequestPduListener, removeTrapListener, removeTrapListener, removeTrapListener, run, sendPacket, setMaxRecvSize, stop
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
addRequestPduListener, addRequestPduListener, addTrapListener, addTrapListener, addTrapListener, clone, destroy, getBindAddress, getHost, getPort, getReceivedFromHostAddress, getSendToHostAddress, getTypeSocket, isDestroyed, processIncomingPdu, removeRequestPduListener, removeRequestPduListener, removeRequestPduListener, removeTrapListener, removeTrapListener, removeTrapListener, sendPacket
protected java.lang.String userName
protected boolean useAuthentication
protected java.lang.String userAuthenticationPassword
protected byte[] userAuthKeyMD5
protected byte[] userAuthKeySHA1
protected int authenticationProtocol
protected int privacyProtocol
protected boolean usePrivacy
protected java.lang.String userPrivacyPassword
protected byte[] userPrivKeyMD5
protected byte[] userPrivKeySHA1
protected byte[] contextEngineId
protected java.lang.String contextName
protected UsmAgent usmAgent
public SnmpContextv3Basis(java.lang.String host, int port) throws java.io.IOException
host
- The host to which the PDU will be sentport
- The port where the SNMP server will bejava.io.IOException
AbstractSnmpContext.AbstractSnmpContext(String, int)
public SnmpContextv3Basis(java.lang.String host, int port, java.lang.String typeSocketA) throws java.io.IOException
host
- The host to which the Pdu will be sentport
- The port where the SNMP server will betypeSocketA
- The local address the server will bind tojava.io.IOException
AbstractSnmpContext.AbstractSnmpContext(String, int, String)
public SnmpContextv3Basis(java.lang.String host, int port, java.lang.String bindAddress, java.lang.String typeSocketA) throws java.io.IOException
host
- The host to which the PDU will be sentport
- The port where the SNMP server will bebindAddress
- The local address the server will bind totypeSocketA
- The type of socket to use.java.io.IOException
AbstractSnmpContext.AbstractSnmpContext(String, int, String)
,
SnmpContextBasisFace.STANDARD_SOCKET
,
SnmpContextBasisFace.TCP_SOCKET
public int getVersion()
AbstractSnmpContext
getVersion
in interface SnmpContextBasisFace
getVersion
in class AbstractSnmpContext
SnmpConstants.SNMP_VERSION_1
,
SnmpConstants.SNMP_VERSION_2c
,
SnmpConstants.SNMP_VERSION_3
public java.lang.String getUserName()
getUserName
in interface SnmpContextv3Face
public void setUserName(java.lang.String newUserName)
setUserName
in interface SnmpContextv3Face
newUserName
- The new usernameSnmpContextv3Face.Default_UserName
public boolean isUseAuthentication()
isUseAuthentication
in interface SnmpContextv3Face
public void setUseAuthentication(boolean newUseAuthentication)
setUseAuthentication
in interface SnmpContextv3Face
newUseAuthentication
- The use of authenticationpublic java.lang.String getUserAuthenticationPassword()
getUserAuthenticationPassword
in interface SnmpContextv3Face
public void setUserAuthenticationPassword(java.lang.String newUserAuthPassword)
setUserAuthenticationPassword
in interface SnmpContextv3Face
newUserAuthPassword
- The user authentication passwordpublic void setAuthenticationProtocol(int protocol) throws java.lang.IllegalArgumentException
setAuthenticationProtocol
in interface SnmpContextv3Face
protocol
- The authentication protocol to be usedjava.lang.IllegalArgumentException
SnmpContextv3Face.MD5_PROTOCOL
,
SnmpContextv3Face.SHA1_PROTOCOL
public int getAuthenticationProtocol()
getAuthenticationProtocol
in interface SnmpContextv3Face
SnmpContextv3Face.MD5_PROTOCOL
,
SnmpContextv3Face.SHA1_PROTOCOL
public void setPrivacyProtocol(int protocol) throws java.lang.IllegalArgumentException
setPrivacyProtocol
in interface SnmpContextv3Face
protocol
- The privacy protocol to be usedjava.lang.IllegalArgumentException
SnmpContextv3Face.AES_ENCRYPT
,
SnmpContextv3Face.DES_ENCRYPT
public int getPrivacyProtocol()
getPrivacyProtocol
in interface SnmpContextv3Face
SnmpContextv3Face.AES_ENCRYPT
,
SnmpContextv3Face.DES_ENCRYPT
public boolean isUsePrivacy()
isUsePrivacy
in interface SnmpContextv3Face
public void setUsePrivacy(boolean newUsePrivacy)
setUsePrivacy
in interface SnmpContextv3Face
newUsePrivacy
- The use of privacypublic java.lang.String getUserPrivacyPassword()
getUserPrivacyPassword
in interface SnmpContextv3Face
public void setUserPrivacyPassword(java.lang.String newUserPrivacyPassword)
setUserPrivacyPassword
in interface SnmpContextv3Face
newUserPrivacyPassword
- The user privacy passwordpublic void setContextEngineId(byte[] newContextEngineId) throws java.lang.IllegalArgumentException
Note, when the stack is an authoritative engine, this parameter should equal the UsmAgent.getSnmpEngineId(). See the StackUsage documentation for an explanation.
If the contextEngineID is of length zero, the encoder will use the (discovered) snmpEngineId.
setContextEngineId
in interface SnmpContextv3Face
newContextEngineId
- The contextEngineIDjava.lang.IllegalArgumentException
UsmAgent.getSnmpEngineId()
public byte[] getContextEngineId()
getContextEngineId
in interface SnmpContextv3Face
public void setContextName(java.lang.String newContextName)
setContextName
in interface SnmpContextv3Face
newContextName
- The contextNameSnmpContextv3Face.Default_ContextName
public java.lang.String getContextName()
getContextName
in interface SnmpContextv3Face
public boolean addDiscoveryPdu(DiscoveryPdu pdu) throws java.io.IOException, PduException
addDiscoveryPdu
in interface SnmpContextv3Face
pdu
- the discovery pdujava.io.IOException
PduException
AbstractSnmpContext.addPdu(Pdu)
,
addPdu(Pdu)
public boolean addPdu(Pdu pdu) throws java.io.IOException, PduException
addPdu
in interface SnmpContextBasisFace
addPdu
in class AbstractSnmpContext
pdu
- the PDUjava.io.IOException
PduException
AbstractSnmpContext.addPdu(Pdu)
,
addDiscoveryPdu(DiscoveryPdu)
protected UsmAgent createUsmAgent()
DefaultUsmAgent
,
isAuthoritative(byte)
public void setUsmAgent(UsmAgent agent)
setUsmAgent
in interface SnmpContextv3Face
agent
- The USM authoritative interfaceDefaultUsmAgent
public UsmAgent getUsmAgent()
getUsmAgent
in interface SnmpContextv3Face
setUsmAgent(uk.co.westhawk.snmp.stack.UsmAgent)
protected boolean addPdu(Pdu pdu, boolean checkDiscovery) throws java.io.IOException, PduException
checkDiscovery
.
If discovery is needed this method will block until it has done so.
Discovery is only needed if the stack is non authoritative.
This method stores the SNMPv3 msgId and PDU request id in a Hashtable. Since the encoding only happens once and every retry sends the same encoded packet, only one msgId is used.
pdu
- the PDUcheckDiscovery
- check if discovery is neededjava.io.IOException
PduException
AbstractSnmpContext.addPdu(Pdu)
,
addDiscoveryPdu(DiscoveryPdu)
,
addPdu(Pdu)
public boolean removePdu(int rid)
removePdu
in interface SnmpContextBasisFace
removePdu
in class AbstractSnmpContext
rid
- the PDU request idAbstractSnmpContext.removePdu(int)
public byte[] encodeDiscoveryPacket(byte msg_type, int rId, int errstat, int errind, java.util.Enumeration ve, java.lang.Object obj) throws java.io.IOException, EncodingException
encodeDiscoveryPacket
in interface SnmpContextv3Face
java.io.IOException
EncodingException
public byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, java.util.Enumeration ve, java.lang.Object obj) throws java.io.IOException, EncodingException
When the stack is
encodePacket
in interface SnmpContextBasisFace
encodePacket
in class AbstractSnmpContext
msg_type
- The message typerId
- The message iderrstat
- The error statuserrind
- The error indexve
- The varbind listobj
- Additional object (only used in SNMPv3)java.io.IOException
EncodingException
isAuthoritative(byte)
protected java.lang.String checkContextSanity()
protected byte[] actualEncodePacket(byte msg_type, int rId, int errstat, int errind, java.util.Enumeration ve, uk.co.westhawk.snmp.stack.TimeWindowNode node, java.lang.Object obj) throws java.io.IOException, EncodingException
protected void processIncomingResponse(java.io.ByteArrayInputStream in) throws DecodingException, java.io.IOException
processIncomingResponse
in class AbstractSnmpContext
DecodingException
java.io.IOException
AbstractSnmpContext.run()
protected boolean isAuthoritative(byte msg_type)
public void addRequestPduListener(RequestPduListener l, ListeningContextPool lcontext) throws java.io.IOException
Don't use the TCP_SOCKET when listening for request PDUs. It doesn't provide functionality to send a response back.
addRequestPduListener
in interface SnmpContextBasisFace
addRequestPduListener
in class AbstractSnmpContext
l
- The request PDU listenerlcontext
- The listening contextjava.io.IOException
AbstractSnmpContext.addRequestPduListener(RequestPduListener, ListeningContextPool)
public java.lang.Object cloneParameters(SnmpContextv3Face clContext)
public java.lang.String getHashKey()
getHashKey
in interface SnmpContextBasisFace
getHashKey
in class AbstractSnmpContext
public java.lang.String toString()
toString
in class java.lang.Object