Search in sources :

Example 1 with SnmpPduPacket

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

the class SnmpRequestHandler method makeResponsePdu.

/**
     * Here we make a response pdu from a request pdu.
     * We return null if there is no pdu to reply.
     */
private SnmpPduPacket makeResponsePdu(SnmpPduPacket reqPdu, Object userData) {
    SnmpAdaptorServer snmpServer = (SnmpAdaptorServer) adaptorServer;
    SnmpPduPacket respPdu = null;
    snmpServer.updateRequestCounters(reqPdu.type);
    if (reqPdu.varBindList != null)
        snmpServer.updateVarCounters(reqPdu.type, reqPdu.varBindList.length);
    if (checkPduType(reqPdu)) {
        respPdu = checkAcl(reqPdu);
        if (respPdu == null) {
            // reqPdu is accepted by ACLs
            if (mibs.size() < 1) {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag, "makeResponsePdu", "Request " + reqPdu.requestId + " received but no MIB registered.");
                }
                return makeNoMibErrorPdu((SnmpPduRequest) reqPdu, userData);
            }
            switch(reqPdu.type) {
                case SnmpPduPacket.pduGetRequestPdu:
                case SnmpPduPacket.pduGetNextRequestPdu:
                case SnmpPduPacket.pduSetRequestPdu:
                    respPdu = makeGetSetResponsePdu((SnmpPduRequest) reqPdu, userData);
                    break;
                case SnmpPduPacket.pduGetBulkRequestPdu:
                    respPdu = makeGetBulkResponsePdu((SnmpPduBulk) reqPdu, userData);
                    break;
            }
        } else {
            // We send this response only if authResEnabled is true.
            if (!snmpServer.getAuthRespEnabled()) {
                // No response should be sent
                respPdu = null;
            }
            if (snmpServer.getAuthTrapEnabled()) {
                // A trap must be sent
                try {
                    snmpServer.snmpV1Trap(SnmpPduTrap.trapAuthenticationFailure, 0, new SnmpVarBindList());
                } catch (Exception x) {
                    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {
                        SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag, "makeResponsePdu", "Failure when sending authentication trap", x);
                    }
                }
            }
        }
    }
    return respPdu;
}
Also used : SnmpPduBulk(com.sun.jmx.snmp.SnmpPduBulk) SnmpPduPacket(com.sun.jmx.snmp.SnmpPduPacket) SnmpPduRequest(com.sun.jmx.snmp.SnmpPduRequest) SnmpVarBindList(com.sun.jmx.snmp.SnmpVarBindList) SnmpTooBigException(com.sun.jmx.snmp.SnmpTooBigException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException)

Example 2 with SnmpPduPacket

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

the class SnmpRequestHandler method makeGetSetResponsePdu.

/**
     * Here we make the response pdu from a get/set request pdu.
     * At this level, the result is never null.
     */
private SnmpPduPacket makeGetSetResponsePdu(SnmpPduRequest req, Object userData) {
    if (req.varBindList == null) {
        //
        return newValidResponsePdu(req, null);
    }
    // First we need to split the request into subrequests
    //
    splitRequest(req);
    int nbSubRequest = subs.size();
    if (nbSubRequest == 1)
        return turboProcessingGetSet(req, userData);
    // Execute all the subrequests resulting from the split of the
    // varbind list.
    //
    SnmpPduPacket result = executeSubRequest(req, userData);
    if (result != null)
        // method.
        return result;
    //
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag, "makeGetSetResponsePdu", "Build the unified response for request " + req.requestId);
    }
    return mergeResponses(req);
}
Also used : SnmpPduPacket(com.sun.jmx.snmp.SnmpPduPacket)

Example 3 with SnmpPduPacket

use of com.sun.jmx.snmp.SnmpPduPacket 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 4 with SnmpPduPacket

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

the class SnmpRequestHandler method makeGetBulkResponsePdu.

/**
     * Here we make the response pdu for a bulk request.
     * At this level, the result is never null.
     */
private SnmpPduPacket makeGetBulkResponsePdu(SnmpPduBulk req, Object userData) {
    SnmpVarBind[] respVarBindList;
    // RFC 1905, Section 4.2.3, p14
    int L = req.varBindList.length;
    int N = Math.max(Math.min(req.nonRepeaters, L), 0);
    int M = Math.max(req.maxRepetitions, 0);
    int R = L - N;
    if (req.varBindList == null) {
        //
        return newValidResponsePdu(req, null);
    }
    // Split the request into subrequests.
    //
    splitBulkRequest(req, N, M, R);
    SnmpPduPacket result = executeSubRequest(req, userData);
    if (result != null)
        return result;
    respVarBindList = mergeBulkResponses(N + (M * R));
    // Now we remove useless trailing endOfMibView.
    //
    // respVarBindList[m2] item and next are going to be removed
    int m2;
    int t = respVarBindList.length;
    while ((t > N) && (respVarBindList[t - 1].value.equals(SnmpVarBind.endOfMibView))) {
        t--;
    }
    if (t == N)
        m2 = N + R;
    else
        // Trivial, of course...
        m2 = N + ((t - 1 - N) / R + 2) * R;
    if (m2 < respVarBindList.length) {
        SnmpVarBind[] truncatedList = new SnmpVarBind[m2];
        for (int i = 0; i < m2; i++) {
            truncatedList[i] = respVarBindList[i];
        }
        respVarBindList = truncatedList;
    }
    //
    return newValidResponsePdu(req, respVarBindList);
}
Also used : SnmpPduPacket(com.sun.jmx.snmp.SnmpPduPacket) SnmpVarBind(com.sun.jmx.snmp.SnmpVarBind)

Example 5 with SnmpPduPacket

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

the class SnmpRequestHandler method checkAcl.

/**
     * Check if the specified pdu is conform to the ACL.
     * This method returns null if the pdu is ok. If not, it returns
     * the response pdu to be replied.
     */
private SnmpPduPacket checkAcl(SnmpPduPacket pdu) {
    SnmpPduPacket response = null;
    String community = new String(pdu.community);
    //
    if (ipacl != null) {
        if (pdu.type == SnmpDefinitions.pduSetRequestPdu) {
            if (!ipacl.checkWritePermission(pdu.address, community)) {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag, "checkAcl", "sender is " + pdu.address + " with " + community + ". Sender has no write permission");
                }
                int err = SnmpSubRequestHandler.mapErrorStatus(SnmpDefinitions.snmpRspAuthorizationError, pdu.version, pdu.type);
                response = newErrorResponsePdu(pdu, err, 0);
            } else {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag, "checkAcl", "sender is " + pdu.address + " with " + community + ". Sender has write permission");
                }
            }
        } else {
            if (!ipacl.checkReadPermission(pdu.address, community)) {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag, "checkAcl", "sender is " + pdu.address + " with " + community + ". Sender has no read permission");
                }
                int err = SnmpSubRequestHandler.mapErrorStatus(SnmpDefinitions.snmpRspAuthorizationError, pdu.version, pdu.type);
                response = newErrorResponsePdu(pdu, err, 0);
                SnmpAdaptorServer snmpServer = (SnmpAdaptorServer) adaptorServer;
                snmpServer.updateErrorCounters(SnmpDefinitions.snmpRspNoSuchName);
            } else {
                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
                    SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag, "checkAcl", "sender is " + pdu.address + " with " + community + ". Sender has read permission");
                }
            }
        }
    }
    //
    if (response != null) {
        SnmpAdaptorServer snmpServer = (SnmpAdaptorServer) adaptorServer;
        snmpServer.incSnmpInBadCommunityUses(1);
        if (ipacl.checkCommunity(community) == false)
            snmpServer.incSnmpInBadCommunityNames(1);
    }
    return response;
}
Also used : SnmpPduPacket(com.sun.jmx.snmp.SnmpPduPacket)

Aggregations

SnmpPduPacket (com.sun.jmx.snmp.SnmpPduPacket)6 SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)3 SnmpMessage (com.sun.jmx.snmp.SnmpMessage)2 SnmpTooBigException (com.sun.jmx.snmp.SnmpTooBigException)2 InterruptedIOException (java.io.InterruptedIOException)2 SocketException (java.net.SocketException)2 SnmpPduBulk (com.sun.jmx.snmp.SnmpPduBulk)1 SnmpPduRequest (com.sun.jmx.snmp.SnmpPduRequest)1 SnmpVarBind (com.sun.jmx.snmp.SnmpVarBind)1 SnmpVarBindList (com.sun.jmx.snmp.SnmpVarBindList)1