Search in sources :

Example 1 with SnmpTooBigException

use of com.sun.jmx.snmp.SnmpTooBigException in project jdk8u_jdk by JetBrains.

the class SnmpRequestHandler method makeResponseMessage.

/**
     * Here we make a response message from a request message.
     * We return null if there is no message to reply.
     */
private SnmpMessage makeResponseMessage(SnmpMessage reqMsg) {
    SnmpMessage respMsg = null;
    // Transform the request message into a request pdu
    //
    SnmpPduPacket reqPdu;
    Object userData = null;
    try {
        reqPdu = (SnmpPduPacket) pduFactory.decodeSnmpPdu(reqMsg);
        if (reqPdu != null && userDataFactory != null)
            userData = userDataFactory.allocateUserData(reqPdu);
    } catch (SnmpStatusException x) {
        reqPdu = null;
        SnmpAdaptorServer snmpServer = (SnmpAdaptorServer) adaptorServer;
        snmpServer.incSnmpInASNParseErrs(1);
        if (x.getStatus() == SnmpDefinitions.snmpWrongSnmpVersion)
            snmpServer.incSnmpInBadVersions(1);
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "message decoding failed", x);
        }
    }
    // Make the response pdu if any
    //
    SnmpPduPacket respPdu = null;
    if (reqPdu != null) {
        respPdu = makeResponsePdu(reqPdu, userData);
        try {
            if (userDataFactory != null)
                userDataFactory.releaseUserData(userData, respPdu);
        } catch (SnmpStatusException x) {
            respPdu = null;
        }
    }
    //
    if (respPdu != null) {
        try {
            respMsg = (SnmpMessage) pduFactory.encodeSnmpPdu(respPdu, packet.getData().length);
        } catch (SnmpStatusException x) {
            respMsg = null;
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "failure when encoding the response message", x);
            }
        } catch (SnmpTooBigException x) {
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "response message is too big");
            }
            try {
                //
                if (packet.getData().length <= 32)
                    throw x;
                int pos = x.getVarBindCount();
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "fail on element" + pos);
                }
                int old;
                while (true) {
                    try {
                        respPdu = reduceResponsePdu(reqPdu, respPdu, pos);
                        respMsg = (SnmpMessage) pduFactory.encodeSnmpPdu(respPdu, packet.getData().length - 32);
                        break;
                    } catch (SnmpTooBigException xx) {
                        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "response message is still too big");
                        }
                        old = pos;
                        pos = xx.getVarBindCount();
                        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "fail on element" + pos);
                        }
                        if (pos == old) {
                            //
                            throw xx;
                        }
                    }
                }
            // end of loop
            } catch (SnmpStatusException xx) {
                respMsg = null;
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "failure when encoding the response message", xx);
                }
            } catch (SnmpTooBigException xx) {
                try {
                    respPdu = newTooBigPdu(reqPdu);
                    respMsg = (SnmpMessage) pduFactory.encodeSnmpPdu(respPdu, packet.getData().length);
                } catch (SnmpTooBigException xxx) {
                    respMsg = null;
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "'too big' is 'too big' !!!");
                    }
                    adaptor.incSnmpSilentDrops(1);
                } catch (Exception xxx) {
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "Got unexpected exception", xxx);
                    }
                    respMsg = null;
                }
            } catch (Exception xx) {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponseMessage", "Got unexpected exception", xx);
                }
                respMsg = null;
            }
        }
    }
    return respMsg;
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) SnmpMessage(com.sun.jmx.snmp.SnmpMessage) SnmpPduPacket(com.sun.jmx.snmp.SnmpPduPacket) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException)

Example 2 with SnmpTooBigException

use of com.sun.jmx.snmp.SnmpTooBigException in project jdk8u_jdk by JetBrains.

the class SnmpV3Message method encodeSnmpPdu.

/**
     * 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 p 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>.
     */
public void encodeSnmpPdu(SnmpPdu p, int maxDataLength) throws SnmpStatusException, SnmpTooBigException {
    SnmpScopedPduPacket pdu = (SnmpScopedPduPacket) p;
    if (SNMP_LOGGER.isLoggable(Level.FINER)) {
        final StringBuilder strb = new StringBuilder().append("PDU to marshall: \n").append("security parameters : ").append(pdu.securityParameters).append("\n").append("type : ").append(pdu.type).append("\n").append("version : ").append(pdu.version).append("\n").append("requestId : ").append(pdu.requestId).append("\n").append("msgId : ").append(pdu.msgId).append("\n").append("msgMaxSize : ").append(pdu.msgMaxSize).append("\n").append("msgFlags : ").append(pdu.msgFlags).append("\n").append("msgSecurityModel : ").append(pdu.msgSecurityModel).append("\n").append("contextEngineId : ").append(pdu.contextEngineId).append("\n").append("contextName : ").append(pdu.contextName).append("\n");
        SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(), "encodeSnmpPdu", strb.toString());
    }
    version = pdu.version;
    address = pdu.address;
    port = pdu.port;
    msgId = pdu.msgId;
    msgMaxSize = pdu.msgMaxSize;
    msgFlags = pdu.msgFlags;
    msgSecurityModel = pdu.msgSecurityModel;
    contextEngineId = pdu.contextEngineId;
    contextName = pdu.contextName;
    securityParameters = pdu.securityParameters;
    //
    // Allocate the array to receive the encoding.
    //
    data = new byte[maxDataLength];
    try {
        BerEncoder benc = new BerEncoder(data);
        benc.openSequence();
        encodeVarBindList(benc, pdu.varBindList);
        switch(pdu.type) {
            case pduGetRequestPdu:
            case pduGetNextRequestPdu:
            case pduInformRequestPdu:
            case pduGetResponsePdu:
            case pduSetRequestPdu:
            case pduV2TrapPdu:
            case pduReportPdu:
                SnmpPduRequestType reqPdu = (SnmpPduRequestType) pdu;
                benc.putInteger(reqPdu.getErrorIndex());
                benc.putInteger(reqPdu.getErrorStatus());
                benc.putInteger(pdu.requestId);
                break;
            case pduGetBulkRequestPdu:
                SnmpPduBulkType bulkPdu = (SnmpPduBulkType) pdu;
                benc.putInteger(bulkPdu.getMaxRepetitions());
                benc.putInteger(bulkPdu.getNonRepeaters());
                benc.putInteger(pdu.requestId);
                break;
            default:
                throw new SnmpStatusException("Invalid pdu type " + String.valueOf(pdu.type));
        }
        benc.closeSequence(pdu.type);
        dataLength = benc.trim();
    } catch (ArrayIndexOutOfBoundsException x) {
        throw new SnmpTooBigException();
    }
}
Also used : SnmpPduBulkType(com.sun.jmx.snmp.SnmpPduBulkType) SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) BerEncoder(com.sun.jmx.snmp.BerEncoder) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException) SnmpScopedPduPacket(com.sun.jmx.snmp.SnmpScopedPduPacket) SnmpPduRequestType(com.sun.jmx.snmp.SnmpPduRequestType)

Example 3 with SnmpTooBigException

use of com.sun.jmx.snmp.SnmpTooBigException in project jdk8u_jdk by JetBrains.

the class SnmpAdaptorServer method sendTrapPdu.

/**
     * Send the specified trap PDU to the specified destination.
     */
private void sendTrapPdu(InetAddress addr, SnmpPduPacket pdu) throws SnmpStatusException, IOException {
    // Make an SNMP message from the pdu
    //
    SnmpMessage msg = null;
    try {
        msg = (SnmpMessage) pduFactory.encodeSnmpPdu(pdu, bufferSize);
        if (msg == null) {
            throw new SnmpStatusException(SnmpDefinitions.snmpRspAuthorizationError);
        }
    } catch (SnmpTooBigException x) {
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendTrapPdu", "Trap pdu is too big. " + "Trap hasn't been sent to the specified host.");
        }
        throw new SnmpStatusException(SnmpDefinitions.snmpRspTooBig);
    // FIXME: is the right exception to throw ?
    // We could simply forward SnmpTooBigException ?
    }
    // Now send the SNMP message to specified destination
    //
    openTrapSocketIfNeeded();
    if (addr != null) {
        msg.address = addr;
        try {
            sendTrapMessage(msg);
        } catch (SnmpTooBigException x) {
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendTrapPdu", "Trap pdu is too big. " + "Trap hasn't been sent to " + msg.address);
            }
        }
    }
    closeTrapSocketIfNeeded();
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) SnmpMessage(com.sun.jmx.snmp.SnmpMessage) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException)

Example 4 with SnmpTooBigException

use of com.sun.jmx.snmp.SnmpTooBigException in project jdk8u_jdk by JetBrains.

the class SnmpRequestHandler method makeResponsePacket.

/**
     * Here we make a response packet from a request packet.
     * We return null if there no response packet to sent.
     */
private DatagramPacket makeResponsePacket(DatagramPacket reqPacket) {
    DatagramPacket respPacket = null;
    // Transform the request packet into a request SnmpMessage
    //
    SnmpMessage reqMsg = new SnmpMessage();
    try {
        reqMsg.decodeMessage(reqPacket.getData(), reqPacket.getLength());
        reqMsg.address = reqPacket.getAddress();
        reqMsg.port = reqPacket.getPort();
    } catch (SnmpStatusException x) {
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponsePacket", "packet decoding failed", x);
        }
        reqMsg = null;
        ((SnmpAdaptorServer) adaptorServer).incSnmpInASNParseErrs(1);
    }
    // Make the response SnmpMessage if any
    //
    SnmpMessage respMsg = null;
    if (reqMsg != null) {
        respMsg = makeResponseMessage(reqMsg);
    }
    //
    if (respMsg != null) {
        try {
            reqPacket.setLength(respMsg.encodeMessage(reqPacket.getData()));
            respPacket = reqPacket;
        } catch (SnmpTooBigException x) {
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponsePacket", "response message is too big");
            }
            try {
                respMsg = newTooBigMessage(reqMsg);
                reqPacket.setLength(respMsg.encodeMessage(reqPacket.getData()));
                respPacket = reqPacket;
            } catch (SnmpTooBigException xx) {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponsePacket", "'too big' is 'too big' !!!");
                }
                adaptor.incSnmpSilentDrops(1);
            }
        }
    }
    return respPacket;
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) SnmpMessage(com.sun.jmx.snmp.SnmpMessage) DatagramPacket(java.net.DatagramPacket) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException)

Example 5 with SnmpTooBigException

use of com.sun.jmx.snmp.SnmpTooBigException in project jdk8u_jdk by JetBrains.

the class SnmpAdaptorServer method sendTrapPdu.

/**
     * Send the specified trap PDU to every destinations from the ACL file.
     */
private void sendTrapPdu(SnmpPduPacket pdu) throws SnmpStatusException, IOException {
    // Make an SNMP message from the pdu
    //
    SnmpMessage msg = null;
    try {
        msg = (SnmpMessage) pduFactory.encodeSnmpPdu(pdu, bufferSize);
        if (msg == null) {
            throw new SnmpStatusException(SnmpDefinitions.snmpRspAuthorizationError);
        }
    } catch (SnmpTooBigException x) {
        if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
            SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendTrapPdu", "Trap pdu is too big. " + "Trap hasn't been sent to anyone");
        }
        throw new SnmpStatusException(SnmpDefinitions.snmpRspTooBig);
    // FIXME: is the right exception to throw ?
    // We could simply forward SnmpTooBigException ?
    }
    // Now send the SNMP message to each destination
    //
    int sendingCount = 0;
    openTrapSocketIfNeeded();
    if (ipacl != null) {
        Enumeration<InetAddress> ed = ipacl.getTrapDestinations();
        while (ed.hasMoreElements()) {
            msg.address = ed.nextElement();
            Enumeration<String> ec = ipacl.getTrapCommunities(msg.address);
            while (ec.hasMoreElements()) {
                msg.community = ec.nextElement().getBytes();
                try {
                    sendTrapMessage(msg);
                    sendingCount++;
                } catch (SnmpTooBigException x) {
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendTrapPdu", "Trap pdu is too big. " + "Trap hasn't been sent to " + msg.address);
                    }
                }
            }
        }
    }
    //
    if (sendingCount == 0) {
        try {
            msg.address = InetAddress.getLocalHost();
            sendTrapMessage(msg);
        } catch (SnmpTooBigException x) {
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendTrapPdu", "Trap pdu is too big. " + "Trap hasn't been sent.");
            }
        } catch (UnknownHostException e) {
            if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "sendTrapPdu", "Trap pdu is too big. " + "Trap hasn't been sent.");
            }
        }
    }
    closeTrapSocketIfNeeded();
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) UnknownHostException(java.net.UnknownHostException) SnmpMessage(com.sun.jmx.snmp.SnmpMessage) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException) InetAddress(java.net.InetAddress)

Aggregations

SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)5 SnmpTooBigException (com.sun.jmx.snmp.SnmpTooBigException)5 SnmpMessage (com.sun.jmx.snmp.SnmpMessage)4 BerEncoder (com.sun.jmx.snmp.BerEncoder)1 SnmpPduBulkType (com.sun.jmx.snmp.SnmpPduBulkType)1 SnmpPduPacket (com.sun.jmx.snmp.SnmpPduPacket)1 SnmpPduRequestType (com.sun.jmx.snmp.SnmpPduRequestType)1 SnmpScopedPduPacket (com.sun.jmx.snmp.SnmpScopedPduPacket)1 InterruptedIOException (java.io.InterruptedIOException)1 DatagramPacket (java.net.DatagramPacket)1 InetAddress (java.net.InetAddress)1 SocketException (java.net.SocketException)1 UnknownHostException (java.net.UnknownHostException)1