|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.adventnet.snmp.snmp2.SnmpPDU
The SnmpPDU class represents the SNMP PDU used in protocol operations.
Any SNMP operation can be performed using this class.
The following should be set on it before performing any SNMP operation:
1. The SNMP command. It may be SnmpAPI.GET_REQ_MSG or
SnmpAPI.SET_REQ_MSG or SnmpAPI.GETNEXT_REQ_MSG
2. The SnmpVarBinds that should be present in this PDU.
3. The network communication variables such as remoteHost and remotePort
( in case of UDP ). This is not needed if it is already present in
SnmpSession. But it is always good to set it on pdu rather than on
SnmpSession.
The following code snippet will give a clear idea of how to do a
SNMP operation using this class:
This code snippet does a simple SNMP GET request for the oid
".1.3.6.1.2.1.1.1.0", to the host "remoteHost" and port "remotePort".
SnmpPDU pdu = new SnmpPDU();
pdu.setCommand(SnmpAPI.GET_REQ_MSG);
pdu.addNull(new SnmpOID(".1.3.6.1.2.1.1.1.0"));
If the under lying protocol that is used is UDP, then
an instance of UDPProtocolOptions should be instantiated
to set the remotePort and remoteHost variables.
Note: By default AdventNet SNMP Stack uses UDP as the
underlying protocol.
If SAS is used for communications, then an instance of
SASProtocolOptions should be instantiated to set the
remotePort and remoteHost.
UDPProtocolOptions opt = new UDPProtocolOptions();
opt.setRemoteHost(remoteHost);
opt.setRemotePort(remotePort);
pdu.setProtocolOptions(opt);
SnmpPDU response_pdu = session.syncSend(pdu);
When a PDU is re-used, ensure the reqid field is set properly. If reqid is set non-zero, the API leaves it as it is. If it is 0, then the API assigns a unique reqid, to ensure no conflict with other requests. So, if you do not want to manage the reqids yourself, set it to 0 before each request. With SnmpSession.syncSend() you don't need to store the value for later reference. With SnmpSession.send(), the reqid assigned is returned, and you will need it later to check up on the response.
The SnmpPDU is encapsulated in a message, where the message could be SNMPv1, SNMPv2c or an SNMPv3 message. Applications do not have to be aware of the message classes Snmp3Message and SnmpMessage and they can work with the SnmpPDU alone, to be able to interact with peers. The SnmpPDU provides a getMsg( ) method to access the SnmpMessage associated with it. Similarly when a SnmpPDU is instantiated, it instantiates the appropriate message object along with it. Applications must not instantiate Snmp3Message and SnmpMessage objects and should always use SnmpPDU.
The SnmpPDU provides most of the communication parameters' related methods that are available with the SnmpSession. Wherever the value of the parameter is set in the pdu, it overrides the value in the session.
Constructor Summary | |
SnmpPDU()
Creates a new SnmpPDU instance. |
Method Summary | |
void |
addNull(SnmpOID oid)
This method will add a vairable binding which contains this SnmpOID and a placeholder ( NULL ) as the value. |
void |
addVariableBinding(int index,
SnmpVarBind varbind)
Adds SNMP variable at specified index in PDUs list of variable bindings. |
void |
addVariableBinding(SnmpVarBind varbind)
Adds SNMP variable at the end of PDU's list of variable bindings. |
SnmpPDU |
copy()
Makes a copy of entire SnmpPDU including the SnmpMessage it encapsulates. |
SnmpPDU |
copyWithoutVarbinds()
|
boolean |
decode()
Decodes the remainder of the PDU. |
void |
fix()
Fixes PDU to eliminate erred variable binding, determined by errindex |
java.net.InetAddress |
getAddress()
Deprecated. use the following instead.
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
|
java.lang.String |
getAgentAddr()
Gets the address of object generating trap in String foramt. |
java.net.InetAddress |
getAgentAddress()
Gets address of object generating trap. |
int |
getClientID()
This method will return the id for the source which sent this pdu |
byte |
getCommand()
Gets command type of this PDU. |
java.lang.String |
getCommunity()
Gets community string received/sent. |
byte[] |
getContextID()
Gets context ID sent/received with this PDU. |
byte[] |
getContextName()
Gets the context name sent/received with this PDU. |
byte[] |
getData()
Gets encoded data to be sent, or received. |
int |
getEncodedLength(SnmpSession session)
Returns the length of the encoded pdu. |
SnmpOID |
getEnterprise()
Gets the enterprise OID. |
int |
getErrindex()
Gets error index of this PDU. |
java.lang.String |
getError()
Gets error information as a String, with offending varbind if any. |
int |
getErrstat()
Gets error status of this PDU. |
int |
getMaxRepetitions()
GetsMax-Repetitions value of this PDU. |
SnmpMessage |
getMsg()
Returns the SnmpMessage instance for this pdu. |
int |
getNonRepeaters()
Gets Non-Repeaters value of this PDU. |
SnmpOID |
getObjectID(int index)
Gets the specified SNMP ObjectID from PDUs list of variable bindings. |
ProtocolOptions |
getProtocolOptions()
This method will return the transport mechanism's protocol options |
java.lang.String |
getRemoteHost()
Deprecated. use the following instead.
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
|
int |
getRemotePort()
Deprecated. use the following instead.
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
|
int |
getReqid()
Gets request id of this PDU. |
int |
getRetries()
Gets number of retries for this PDU before timeout. |
long |
getRoundTripDelay()
Returns round-trip delay if received response PDU |
int |
getSecurityModel()
Gets the securityModel associated with this PDU. |
int |
getSpecificType()
Gets specific trap type. |
int |
getTimeout()
Gets the timeout value. |
SnmpOID |
getTrapOID()
This method returns the trap-oid for this SNMPv2 trap-pdu. |
int |
getTrapType()
Gets the generic type of the trap. |
long |
getUpTime()
Gets the timeStamp of the object which has generated the trap. |
byte[] |
getUserName()
Gets the principal on whose behalf SNMPv3 requests are made. |
SnmpVar |
getVariable(int index)
Gets specified SNMP variable from PDUs list of variables. |
SnmpVarBind |
getVariableBinding(int index)
Gets specified variable binding from PDUs list of variable bindings. |
java.util.Vector |
getVariableBindings()
Gets list of SnmpVarBind objects as a vector. |
int |
getVersion()
Gets snmp Version number in PDU. |
java.lang.String |
getWriteCommunity()
Gets writeCommunity string received/sent. |
boolean |
isBroadCastEnabled()
This method says whether this pdu is a broadCastpdu or not. |
java.lang.String |
printVarBinds()
Returns String of all variable bindings with Tags, e.g. |
void |
removeVariableBinding(int index)
Removes SNMP variable binding at specified index in PDUs list of variable bindings. |
void |
removeVariableBinding(SnmpVarBind varbind)
Removes specified SNMP variable binding from PDUs list of variable bindings. |
void |
setAddress(java.net.InetAddress address)
Deprecated. use the following instead.
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
|
void |
setAgentAddr(java.lang.String agentaddr)
sets the agent address as a string. |
void |
setAgentAddress(java.net.InetAddress addr)
Sets Address of object generating trap. |
void |
setBroadCastEnable(boolean bool)
Calling this method with a "true" value will indicate that this pdu is a broadcast pdu. |
void |
setClientID(int id)
This method will indicate the id for source which sent this pdu |
void |
setCommand(byte type)
Sets command type of this PDU. |
void |
setCommunity(java.lang.String t)
Sets community string received/sent. |
void |
setCommunityEncoding(java.lang.String enc)
Sets the encoding to use for community string. |
void |
setContextID(byte[] id)
Sets context ID associated with the PDU. |
void |
setContextName(byte[] name)
Sets the context name associated with the PDU |
void |
setData(byte[] b)
Sets encoded data to be sent, or received. |
void |
setDNSLookup(boolean lookup)
Deprecated. since no lookup will be done unnecessarily. |
void |
setEnterprise(SnmpOID oid)
Sets the enterprise OID. |
void |
setErrindex(int index)
Sets Error index for this PDU. |
void |
setErrstat(int stat)
Set Error status for this PDU. |
void |
setMaxRepetitions(int max_rep)
Sets Max-Repetitions value for this PDU. |
void |
setNonRepeaters(int non_rep)
Sets Non-Repeaters value for this PDU. |
void |
setProtocolOptions(ProtocolOptions tParam)
This sets the transport mechanism's protocol options. |
void |
setReEncode(boolean reEncode)
Controls ASN.1 encoding (default true). |
void |
setRemoteHost(java.lang.String host)
Deprecated. use the following instead.
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
|
void |
setRemotePort(int port)
Deprecated. use the following instead.
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
|
void |
setReqid(int id)
Sets Request id for this PDU. |
void |
setRetries(int N)
Sets number of retries for this PDU before timeout. |
void |
setSecurityModel(int model)
Sets the securityModel to be associated with the PDU. |
void |
setSpecificType(int type)
Sets Specific trap type. |
void |
setTimeout(int t)
Sets the timeout value. |
void |
setTrapType(int type)
Sets the generic type of the Trap. |
void |
setUpTime(long uptime)
Sets the timeStamp of the object which has to generate the trap. |
void |
setUserName(byte[] name)
Sets the principal on whose behalf SNMPv3 requests are to be made. |
void |
setVariable(int index,
SnmpVar var)
Sets SNMP variable at specified index in PDUs list of variables, to value var. |
void |
setVersion(int v)
Sets snmp Version number in PDU. |
void |
setWriteCommunity(java.lang.String t)
Sets writeCommunity string received/sent. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public SnmpPDU()
Method Detail |
public boolean isBroadCastEnabled()
public void setBroadCastEnable(boolean bool)
bool
- the boolean value which says, whether this pdu
is a broadCast pdu or not.public SnmpMessage getMsg()
public void setAddress(java.net.InetAddress address)
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
opt.setRemoteAddress(address);
address
- the inetaddress to which this pdu should be
sent.public java.net.InetAddress getAddress()
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
InetAddress address = opt.getRemoteAddress();
public java.lang.String getRemoteHost()
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
String remoteHost = opt.getRemoteHost();
public void setRemoteHost(java.lang.String host)
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
opt.setRemoteHost(host);
host
- The name of the SNMP peer to which requests should
be sent.public int getRemotePort()
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
int remotePort = opt.getRemotePort();
public void setRemotePort(int port)
UDPProtocolOptions opt = (UDPProtocolOptions)SnmpPDU.getProtocolOptions();
opt.setRemotePort(port);
port
- The port of the SNMP peer to which requests should
be sent.
This method accepts port values such that * port >= 0 and < 65536
public byte getCommand()
public void setCommand(byte type)
type
- The command type to be set for this PDU.
The various command types that can be passed as argument
to this method are:
SnmpAPI.GET_REQ_MSG, SnmpAPI.GETNEXT_REQ_MSG,
SnmpAPI.SET_REQ_MSG, SnmpAPI.TRP_REQ_MSG,
SnmpAPI.GET_RSP_MSG,
SnmpAPI.TRP2_REQ_MSG, SnmpAPI.GETBULK_REQ_MSG,
SnmpAPI.INFORM_REQ_MSG and SnmpAPI.REPORT_MSG
This method will not accept values other than the one mentioned above.
public int getReqid()
public void setReqid(int id)
id
- The requestID for this PDU.
The valid values ranges from -2147483648 to 2147483647(both inclusive).public int getErrstat()
public java.lang.String getError()
public void setErrstat(int stat)
stat
- The error status to be set for this PDU.
The error status values that can be set using this method are
This method will not accept values other than the one mentioned above.
public int getErrindex()
public void setErrindex(int index)
index
- The error index to be set for this PDU.
Only values >= zero will be accepted by this method.public int getNonRepeaters()
public void setNonRepeaters(int non_rep)
non_rep
- The Non-Repeaters value for this PDU.public int getMaxRepetitions()
public void setMaxRepetitions(int max_rep)
max_rep
- The Max-Repetitions value for this PDU.
The valid values ranges from 0 to 65535(both inclusive).public SnmpOID getEnterprise()
public void setEnterprise(SnmpOID oid)
oid
- The SnmpOID representing the entriprise field of PDU to
be set.public java.net.InetAddress getAgentAddress()
public void setAgentAddress(java.net.InetAddress addr)
addr
- The address of object generating trap.public java.lang.String getAgentAddr()
public void setAgentAddr(java.lang.String agentaddr)
agentaddr
- the agent address as a string. This can be
hostname or an address in the dotted form.agentaddr
- the agent address.public int getTrapType()
public void setTrapType(int type)
type
- The generic type of the trap for this PDU.
This method will accept values only from 0 to
6(both inclusive).public int getSpecificType()
public void setSpecificType(int type)
type
- The Specific trap type for this PDU.public long getUpTime()
public SnmpOID getTrapOID()
public void setUpTime(long uptime)
uptime
- The timeStamp for the object which has to
generate the trap.
This method will accept values such that uptime >= 0 and uptime < 4294967296L
public java.util.Vector getVariableBindings()
public SnmpVarBind getVariableBinding(int index)
index
- The index of the SnmpVarBind which should be returned
from the PDU's list of variable bindings.public SnmpVar getVariable(int index)
index
- The index of the SnmpVarBind from the PDU's list
of variable bindings.public void setVariable(int index, SnmpVar var)
index
- The index for the PDU's list of varaible bindings
for which the SnmpVar has to be set.var
- The SnmpVar which has to be set in the PDU's list
of variable binding into the index specified by the index parameter.public void addVariableBinding(int index, SnmpVarBind varbind)
index
- The index for the PDU's list of variable bindings
for which the SnmpVarBind has to be added.varbind
- The SnmpVarBind which has to be added in the
specified position specified by the index in the PDU's list
of variable bindings.
The varbind will not be added to the list of variable bindings
if any one of the following condition is true:
1. The varbind object is null
2. The index specified is invalid, that is, if the index is
less than 0 or greater than the variable bindings size.
3. The SnmpOID in the SnmpVarBind is invalid or null.
public void addVariableBinding(SnmpVarBind varbind)
varbind
- The SnmpVarBind to be added at the end of PDU's
list of variable bindings.
The varbind will not be added to the list of variable bindings
if any one of the following condition is true:
1. The varbind object is null
2. The SnmpOID in the SnmpVarBind is invalid or null.
public void removeVariableBinding(int index)
index
- The index value in the PDU's list of variable
bindings for which the SnmpVarBind has to be removed.public void removeVariableBinding(SnmpVarBind varbind)
varbind
- The SnmpVarbind which has to be removed from the
PDU's list of variable bindings.public SnmpOID getObjectID(int index)
index
- The index value in the PDU's list of variable
bindings for which the SnmpOID is to be retrived.public java.lang.String getCommunity()
public void setCommunity(java.lang.String t)
t
- The community to be set for this PDU.public java.lang.String getWriteCommunity()
public void setWriteCommunity(java.lang.String t)
t
- The writeCommunity to be set for this PDU.public int getTimeout()
public void setTimeout(int t)
t
- The timeout value for this PDU.
This method accepts values which are greater than zero.public int getRetries()
public void setRetries(int N)
N
- The retries value for this PDU.
This method accepts value such that N >= 0.public byte[] getData()
public void setData(byte[] b)
b
- The encoded data to be sent, or received.public int getVersion()
It is to be noted that when an application sends an SNMPv1 pdu using a session whose version is set to SNMP_VERSION_3, an SNMPv3 message is sent to the peer. This problem arises because the API uses SNMP_VERSION_1 as the default pdu version and it could not distinguish between applications leaving the version in pdu to default and setting it explicitly to SNMP_VERSION_1. To circumvent this problem, applications should set session version to SNMP_VERSION_1 and set the pdu version explicitly to SNMP_VERSION_2C or SNMP_VERSION_3 while communicating with v2c and v3 peers.
public void setVersion(int v)
It is to be noted that when an application sends an SNMPv1 pdu using a session whose version is set to SNMP_VERSION_3, an SNMPv3 message is sent to the peer. This problem arises because the API uses SNMP_VERSION_1 as the default pdu version and it could not distinguish between applications leaving the version in pdu to default and setting it explicitly to SNMP_VERSION_1. To circumvent this problem, applications should set session version to SNMP_VERSION_1 and set the pdu version explicitly to SNMP_VERSION_2C or SNMP_VERSION_3 while communicating with v2c and v3 peers.
v
- The version for outgoing SNMP requests.
This method will accept the value only when the value is
SnmpAPI.SNMP_VERSION_1 or SnmpAPI.SNMP_VERSION2C or
SnmpAPI.SNMP_VERSION_3public void setUserName(byte[] name)
name
- The principal on whose behalf, SNMPv3 requests
are to be sent.public byte[] getUserName()
public void setContextID(byte[] id)
id
- The context ID associated with the PDU.public byte[] getContextID()
public void setContextName(byte[] name)
name
- The contextName to be sent with the PDU.public byte[] getContextName()
public void setSecurityModel(int model)
model
- The securityModel to be sent with the PDU.
The valid values ranges from 1 to 2147483647(both inclusive).public int getSecurityModel()
public void setReEncode(boolean reEncode)
reEncode
- The boolean value which specifies if the PDU is
to be encoded each time for a send request.public void setCommunityEncoding(java.lang.String enc)
enc
- A character-encoding name
This method will set the string only when enc is a
non-null valid encoding string.public SnmpPDU copy()
public SnmpPDU copyWithoutVarbinds()
public void addNull(SnmpOID oid)
oid
- the SnmpOID object to add to the VarBind listgetVariableBindings()
public void fix()
public long getRoundTripDelay()
public java.lang.String printVarBinds()
public int getEncodedLength(SnmpSession session)
public boolean decode() throws SnmpException
SnmpException
- is thrown upon decode errors.public void setProtocolOptions(ProtocolOptions tParam)
tParam
- protocol options to be set.public ProtocolOptions getProtocolOptions()
public void setClientID(int id)
public int getClientID()
public void setDNSLookup(boolean lookup)
lookup
- The DNS lookup flag to be set.
WARNING : This flag can be set to false while doing non-SNMP operations if
the user wants to just set the remoteHost field with no DNS lookup done.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |