FileDocCategorySizeDatePackage
SnmpMessage.javaAPI DocJava SE 5 API12254Fri Aug 26 14:55:02 BST 2005com.sun.jmx.snmp

SnmpMessage

public class SnmpMessage extends SnmpMsg implements SnmpDefinitions
Is 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.

see
SnmpPduFactory
see
SnmpPduPacket
version
1.1 07/23/98
author
Sun Microsystems, Inc

Fields Summary
public byte[]
community
Community name.
String
dbgTag
Constructors Summary
Methods Summary
voiddebug(java.lang.String clz, java.lang.String func, java.lang.String info)

        Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info);
    
voiddebug(java.lang.String clz, java.lang.String func, java.lang.Throwable exception)

        Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, exception);
    
voiddebug(java.lang.String func, java.lang.String info)

        debug(dbgTag, func, info);
    
voiddebug(java.lang.String func, java.lang.Throwable exception)

        debug(dbgTag, func, exception);
    
public voiddecodeMessage(byte[] inputBytes, int byteCount)
Decodes the specified bytes and initializes this message. For internal use only.

param
inputBytes The bytes to be decoded.
exception
SnmpStatusException If the specified bytes are not a valid encoding.

        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 SnmpPdudecodeSnmpPdu()
Gets the PDU encoded in this message.

This method decodes the data field and returns the resulting PDU.

return
The resulting PDU.
exception
SnmpStatusException If the encoding is not valid.
since
1.5

	//
        // 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 intencodeMessage(byte[] outputBytes)
Encodes this message and puts the result in the specified byte array. For internal use only.

param
outputBytes An array to receive the resulting encoding.
exception
ArrayIndexOutOfBoundsException If the result does not fit into the specified array.

        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 voidencodeSnmpPdu(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.

param
pdu The PDU to be encoded.
param
maxDataLength The maximum length permitted for the data field.
exception
SnmpStatusException If the specified pdu is not valid.
exception
SnmpTooBigException If the resulting encoding does not fit into maxDataLength bytes.
exception
ArrayIndexOutOfBoundsException If the encoding exceeds maxDataLength.
since
1.5

	//
        // 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 intgetRequestId(byte[] inputBytes)
Returns the associated request ID.

param
inputBytes The flat message.
return
The request ID.
since
1.5

	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;
    
booleanisDebugOn()

        return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP);
    
booleanisTraceOn()

        return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP);
    
public java.lang.StringprintMessage()
Dumps this message in a string.

return
The string containing the dump.

	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();	
    
voidtrace(java.lang.String clz, java.lang.String func, java.lang.String info)

        Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info);
    
voidtrace(java.lang.String func, java.lang.String info)

        trace(dbgTag, func, info);