Esempio n. 1
0
 /** For SNMP Runtime private use only. */
 void encodeVarBindValue(BerEncoder benc, SnmpValue v) throws SnmpStatusException {
   if (v == null) {
     benc.putNull();
   } else if (v instanceof SnmpIpAddress) {
     benc.putOctetString(((SnmpIpAddress) v).byteValue(), SnmpValue.IpAddressTag);
   } else if (v instanceof SnmpCounter) {
     benc.putInteger(((SnmpCounter) v).longValue(), SnmpValue.CounterTag);
   } else if (v instanceof SnmpGauge) {
     benc.putInteger(((SnmpGauge) v).longValue(), SnmpValue.GaugeTag);
   } else if (v instanceof SnmpTimeticks) {
     benc.putInteger(((SnmpTimeticks) v).longValue(), SnmpValue.TimeticksTag);
   } else if (v instanceof SnmpOpaque) {
     benc.putOctetString(((SnmpOpaque) v).byteValue(), SnmpValue.OpaqueTag);
   } else if (v instanceof SnmpInt) {
     benc.putInteger(((SnmpInt) v).intValue());
   } else if (v instanceof SnmpString) {
     benc.putOctetString(((SnmpString) v).byteValue());
   } else if (v instanceof SnmpOid) {
     benc.putOid(((SnmpOid) v).longValue());
   } else if (v instanceof SnmpCounter64) {
     if (version == snmpVersionOne) {
       throw new SnmpStatusException("Invalid value for SNMP v1 : " + v);
     }
     benc.putInteger(((SnmpCounter64) v).longValue(), SnmpValue.Counter64Tag);
   } else if (v instanceof SnmpNull) {
     int tag = ((SnmpNull) v).getTag();
     if ((version == snmpVersionOne) && (tag != SnmpValue.NullTag)) {
       throw new SnmpStatusException("Invalid value for SNMP v1 : " + v);
     }
     if ((version == snmpVersionTwo)
         && (tag != SnmpValue.NullTag)
         && (tag != SnmpVarBind.errNoSuchObjectTag)
         && (tag != SnmpVarBind.errNoSuchInstanceTag)
         && (tag != SnmpVarBind.errEndOfMibViewTag)) {
       throw new SnmpStatusException("Invalid value " + v);
     }
     benc.putNull(tag);
   } else {
     throw new SnmpStatusException("Invalid value " + v);
   }
 }
Esempio n. 2
0
  /**
   * 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;
  }
Esempio n. 3
0
  /**
   * 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();
    }
  }