SnmpMessagepublic class SnmpMessage extends SnmpMsg implements SnmpDefinitionsIs a partially decoded representation of an SNMP packet.
You will not normally need to use this class unless you decide to
implement your own {@link com.sun.jmx.snmp.SnmpPduFactory SnmpPduFactory} object.
The SnmpMessage class is directly mapped onto the
Message syntax defined in RFC1157 and RFC1902.
Message ::= SEQUENCE {
version INTEGER { version(1) }, -- for SNMPv2
community OCTET STRING, -- community name
data ANY -- an SNMPv2 PDU
}
This API is a Sun Microsystems internal API and is subject
to change without notice. |
Fields Summary |
---|
public byte[] | communityCommunity name. | String | dbgTag |
Methods Summary |
---|
void | debug(java.lang.String clz, java.lang.String func, java.lang.String info)
Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
| void | debug(java.lang.String clz, java.lang.String func, java.lang.Throwable exception)
Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception);
| void | debug(java.lang.String func, java.lang.String info)
debug(dbgTag, func, info);
| void | debug(java.lang.String func, java.lang.Throwable exception)
debug(dbgTag, func, exception);
| public void | decodeMessage(byte[] inputBytes, int byteCount)Decodes the specified bytes and initializes this message.
For internal use only.
try {
BerDecoder bdec = new BerDecoder(inputBytes/*, byteCount */) ; // FIXME
bdec.openSequence() ;
version = bdec.fetchInteger() ;
community = bdec.fetchOctetString() ;
data = bdec.fetchAny() ;
dataLength = data.length ;
bdec.closeSequence() ;
}
catch(BerException x) {
throw new SnmpStatusException("Invalid encoding") ;
}
| public SnmpPdu | decodeSnmpPdu()Gets the PDU encoded in this message.
This method decodes the data field and returns the resulting PDU.
//
// Decode the pdu
//
SnmpPduPacket pdu = null ;
BerDecoder bdec = new BerDecoder(data) ;
try {
int type = bdec.getTag() ;
bdec.openSequence(type) ;
switch(type) {
case pduGetRequestPdu :
case pduGetNextRequestPdu :
case pduInformRequestPdu :
case pduGetResponsePdu :
case pduSetRequestPdu :
case pduV2TrapPdu :
case pduReportPdu :
SnmpPduRequest reqPdu = new SnmpPduRequest() ;
reqPdu.requestId = bdec.fetchInteger() ;
reqPdu.errorStatus = bdec.fetchInteger() ;
reqPdu.errorIndex = bdec.fetchInteger() ;
pdu = reqPdu ;
break ;
case pduGetBulkRequestPdu :
SnmpPduBulk bulkPdu = new SnmpPduBulk() ;
bulkPdu.requestId = bdec.fetchInteger() ;
bulkPdu.nonRepeaters = bdec.fetchInteger() ;
bulkPdu.maxRepetitions = bdec.fetchInteger() ;
pdu = bulkPdu ;
break ;
case pduV1TrapPdu :
SnmpPduTrap trapPdu = new SnmpPduTrap() ;
trapPdu.enterprise = new SnmpOid(bdec.fetchOid()) ;
byte []b = bdec.fetchOctetString(SnmpValue.IpAddressTag);
if(b.length != 0)
trapPdu.agentAddr = new SnmpIpAddress(b) ;
else
trapPdu.agentAddr = null;
trapPdu.genericTrap = bdec.fetchInteger() ;
trapPdu.specificTrap = bdec.fetchInteger() ;
trapPdu.timeStamp = bdec.fetchInteger(SnmpValue.TimeticksTag) ;
pdu = trapPdu ;
break ;
default:
throw new SnmpStatusException(snmpRspWrongEncoding) ;
}
pdu.type = type ;
pdu.varBindList = decodeVarBindList(bdec) ;
bdec.closeSequence() ;
} catch(BerException e) {
if (isDebugOn()) {
debug("decodeSnmpPdu", e);
}
throw new SnmpStatusException(snmpRspWrongEncoding);
} catch(IllegalArgumentException e) {
// bug id 4654066
if (isDebugOn()) {
debug("decodeSnmpPdu", e);
}
throw new SnmpStatusException(snmpRspWrongEncoding);
}
//
// The easy work
//
pdu.version = version ;
pdu.community = community ;
pdu.address = address ;
pdu.port = port ;
return pdu;
| public int | encodeMessage(byte[] outputBytes)Encodes this message and puts the result in the specified byte array.
For internal use only.
int encodingLength = 0 ;
if (data == null)
throw new IllegalArgumentException("Data field is null") ;
//
// Reminder: BerEncoder does backward encoding !
//
try {
BerEncoder benc = new BerEncoder(outputBytes) ;
benc.openSequence() ;
benc.putAny(data, dataLength) ;
benc.putOctetString((community != null) ? community : new byte[0]) ;
benc.putInteger(version) ;
benc.closeSequence() ;
encodingLength = benc.trim() ;
}
catch(ArrayIndexOutOfBoundsException x) {
throw new SnmpTooBigException() ;
}
return encodingLength ;
| public void | encodeSnmpPdu(SnmpPdu pdu, int maxDataLength)Initializes this message with the specified pdu .
This method initializes the data field with an array of
maxDataLength bytes. It encodes the pdu .
The resulting encoding is stored in the data field
and the length of the encoding is stored in dataLength .
If the encoding length exceeds maxDataLength ,
the method throws an exception.
//
// The easy work
//
SnmpPduPacket pdupacket = (SnmpPduPacket) pdu;
version = pdupacket.version ;
community = pdupacket.community ;
address = pdupacket.address ;
port = pdupacket.port ;
//
// Allocate the array to receive the encoding.
//
data = new byte[maxDataLength] ;
//
// Encode the pdupacket
// Reminder: BerEncoder does backward encoding !
//
try {
BerEncoder benc = new BerEncoder(data) ;
benc.openSequence() ;
encodeVarBindList(benc, pdupacket.varBindList) ;
switch(pdupacket.type) {
case pduGetRequestPdu :
case pduGetNextRequestPdu :
case pduInformRequestPdu :
case pduGetResponsePdu :
case pduSetRequestPdu :
case pduV2TrapPdu :
case pduReportPdu :
SnmpPduRequest reqPdu = (SnmpPduRequest)pdupacket ;
benc.putInteger(reqPdu.errorIndex) ;
benc.putInteger(reqPdu.errorStatus) ;
benc.putInteger(reqPdu.requestId) ;
break ;
case pduGetBulkRequestPdu :
SnmpPduBulk bulkPdu = (SnmpPduBulk)pdupacket ;
benc.putInteger(bulkPdu.maxRepetitions) ;
benc.putInteger(bulkPdu.nonRepeaters) ;
benc.putInteger(bulkPdu.requestId) ;
break ;
case pduV1TrapPdu :
SnmpPduTrap trapPdu = (SnmpPduTrap)pdupacket ;
benc.putInteger(trapPdu.timeStamp, SnmpValue.TimeticksTag) ;
benc.putInteger(trapPdu.specificTrap) ;
benc.putInteger(trapPdu.genericTrap) ;
if(trapPdu.agentAddr != null)
benc.putOctetString(trapPdu.agentAddr.byteValue(), SnmpValue.IpAddressTag) ;
else
benc.putOctetString(new byte[0], SnmpValue.IpAddressTag);
benc.putOid(trapPdu.enterprise.longValue()) ;
break ;
default:
throw new SnmpStatusException("Invalid pdu type " + String.valueOf(pdupacket.type)) ;
}
benc.closeSequence(pdupacket.type) ;
dataLength = benc.trim() ;
}
catch(ArrayIndexOutOfBoundsException x) {
throw new SnmpTooBigException() ;
}
| public int | getRequestId(byte[] inputBytes)Returns the associated request ID.
int requestId = 0;
BerDecoder bdec = null;
BerDecoder bdec2 = null;
byte[] any = null;
try {
bdec = new BerDecoder(inputBytes);
bdec.openSequence();
bdec.fetchInteger();
bdec.fetchOctetString();
any = bdec.fetchAny();
bdec2 = new BerDecoder(any);
int type = bdec2.getTag();
bdec2.openSequence(type);
requestId = bdec2.fetchInteger();
}
catch(BerException x) {
throw new SnmpStatusException("Invalid encoding") ;
}
try {
bdec.closeSequence();
}
catch(BerException x) {
}
try {
bdec2.closeSequence();
}
catch(BerException x) {
}
return requestId;
| boolean | isDebugOn()
return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
| boolean | isTraceOn()
return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
| public java.lang.String | printMessage()Dumps this message in a string.
StringBuffer sb = new StringBuffer();
if (community == null) {
sb.append("Community: null") ;
}
else {
sb.append("Community: {\n") ;
sb.append(dumpHexBuffer(community, 0, community.length)) ;
sb.append("\n}\n") ;
}
return sb.append(super.printMessage()).toString();
| void | trace(java.lang.String clz, java.lang.String func, java.lang.String info)
Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
| void | trace(java.lang.String func, java.lang.String info)
trace(dbgTag, func, info);
|
|