/** For SNMP Runtime private use only. */ public void encodeVarBindList(BerEncoder benc, SnmpVarBind[] varBindList) throws SnmpStatusException, SnmpTooBigException { // // Remember: the encoder does backward encoding // int encodedVarBindCount = 0; try { benc.openSequence(); if (varBindList != null) { for (int i = varBindList.length - 1; i >= 0; i--) { SnmpVarBind bind = varBindList[i]; if (bind != null) { benc.openSequence(); encodeVarBindValue(benc, bind.value); benc.putOid(bind.oid.longValue()); benc.closeSequence(); encodedVarBindCount++; } } } benc.closeSequence(); } catch (ArrayIndexOutOfBoundsException x) { throw new SnmpTooBigException(encodedVarBindCount); } }
/** * 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. */ public int encodeMessage(byte[] outputBytes) throws SnmpTooBigException { 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; }
/** * Initializes this message with the specified <CODE>pdu</CODE>. * * <p>This method initializes the data field with an array of <CODE>maxDataLength</CODE> bytes. It * encodes the <CODE>pdu</CODE>. The resulting encoding is stored in the data field and the length * of the encoding is stored in <CODE>dataLength</CODE>. * * <p>If the encoding length exceeds <CODE>maxDataLength</CODE>, 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 <CODE>pdu</CODE> is not valid. * @exception SnmpTooBigException If the resulting encoding does not fit into <CODE>maxDataLength * </CODE> bytes. * @exception ArrayIndexOutOfBoundsException If the encoding exceeds <CODE>maxDataLength</CODE>. * @since 1.5 */ public void encodeSnmpPdu(SnmpPdu pdu, int maxDataLength) throws SnmpStatusException, SnmpTooBigException { // // 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(); } }