2659 lines · 89.1 KB · Java
RawDownload
/* * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */  package com.sun.jmx.snmp.daemon;  // java imports//import java.util.Vector;import java.util.Enumeration;import java.util.logging.Level;import java.net.DatagramSocket;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.SocketException;import java.net.UnknownHostException;import java.io.ObjectInputStream;import java.io.IOException;import java.io.InterruptedIOException;  // jmx imports//import javax.management.MBeanServer;import javax.management.MBeanRegistration;import javax.management.ObjectName;import javax.management.InstanceAlreadyExistsException;import static com.sun.jmx.defaults.JmxProperties.SNMP_ADAPTOR_LOGGER;import com.sun.jmx.snmp.SnmpIpAddress;import com.sun.jmx.snmp.SnmpMessage;import com.sun.jmx.snmp.SnmpOid;import com.sun.jmx.snmp.SnmpPduFactory;import com.sun.jmx.snmp.SnmpPduPacket;import com.sun.jmx.snmp.SnmpPduRequest;import com.sun.jmx.snmp.SnmpPduTrap;import com.sun.jmx.snmp.SnmpTimeticks;import com.sun.jmx.snmp.SnmpVarBind;import com.sun.jmx.snmp.SnmpVarBindList;import com.sun.jmx.snmp.SnmpDefinitions;import com.sun.jmx.snmp.SnmpStatusException;import com.sun.jmx.snmp.SnmpTooBigException;import com.sun.jmx.snmp.InetAddressAcl;import com.sun.jmx.snmp.SnmpPeer;import com.sun.jmx.snmp.SnmpParameters;// SNMP Runtime imports//import com.sun.jmx.snmp.SnmpPduFactoryBER;import com.sun.jmx.snmp.agent.SnmpMibAgent;import com.sun.jmx.snmp.agent.SnmpMibHandler;import com.sun.jmx.snmp.agent.SnmpUserDataFactory;import com.sun.jmx.snmp.agent.SnmpErrorHandlerAgent; import com.sun.jmx.snmp.IPAcl.SnmpAcl; import com.sun.jmx.snmp.tasks.ThreadService; /** * Implements an adaptor on top of the SNMP protocol. * <P> * When this SNMP protocol adaptor is started it creates a datagram socket * and is able to receive requests and send traps or inform requests. * When it is stopped, the socket is closed and neither requests * and nor traps/inform request are processed. * <P> * The default port number of the socket is 161. This default value can be * changed by specifying a port number: * <UL> * <LI>in the object constructor</LI> * <LI>using the {@link com.sun.jmx.snmp.daemon.CommunicatorServer#setPort *     setPort} method before starting the adaptor</LI> * </UL> * The default object name is defined by {@link * com.sun.jmx.snmp.ServiceName#DOMAIN com.sun.jmx.snmp.ServiceName.DOMAIN} * and {@link com.sun.jmx.snmp.ServiceName#SNMP_ADAPTOR_SERVER * com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_SERVER}. * <P> * The SNMP protocol adaptor supports versions 1 and 2 of the SNMP protocol * in a stateless way: when it receives a v1 request, it replies with a v1 * response, when it receives a v2 request it replies with a v2 response. * <BR>The method {@link #snmpV1Trap snmpV1Trap} sends traps using SNMP v1 * format. * The method {@link #snmpV2Trap snmpV2Trap} sends traps using SNMP v2 format. * The method {@link #snmpInformRequest snmpInformRequest} sends inform * requests using SNMP v2 format. * <P> * To receive data packets, the SNMP protocol adaptor uses a buffer * which size can be configured using the property <CODE>bufferSize</CODE> * (default value is 1024). * Packets which do not fit into the buffer are rejected. * Increasing <CODE>bufferSize</CODE> allows the exchange of bigger packets. * However, the underlying networking system may impose a limit on the size * of UDP packets. * Packets which size exceed this limit will be rejected, no matter what * the value of <CODE>bufferSize</CODE> actually is. * <P> * An SNMP protocol adaptor may serve several managers concurrently. The * number of concurrent managers can be limited using the property * <CODE>maxActiveClientCount</CODE>. * <p> * The SNMP protocol adaptor specifies a default value (10) for the * <CODE>maxActiveClientCount</CODE> property. When the adaptor is stopped, * the active requests are interrupted and an error result is sent to * the managers. * <p><b>This API is a Sun Microsystems internal API  and is subject * to change without notice.</b></p> */ public class SnmpAdaptorServer extends CommunicatorServer    implements SnmpAdaptorServerMBean, MBeanRegistration, SnmpDefinitions,               SnmpMibHandler {     // PRIVATE VARIABLES    //------------------     /**     * Port number for sending SNMP traps.     * <BR>The default value is 162.     */    private int                 trapPort = 162;     /**     * Port number for sending SNMP inform requests.     * <BR>The default value is 162.     */    private int                 informPort = 162;     /**     * The <CODE>InetAddress</CODE> used when creating the datagram socket.     * <BR>It is specified when creating the SNMP protocol adaptor.     * If not specified, the local host machine is used.     */    InetAddress address = null;     /**     * The IP address based ACL used by this SNMP protocol adaptor.     */    private Object ipacl = null;     /**     * The factory object.     */    private SnmpPduFactory pduFactory = null;     /**     * The user-data factory object.     */    private SnmpUserDataFactory userDataFactory = null;     /**     * Indicates if the SNMP protocol adaptor sends a response in case     * of authentication failure     */    private boolean authRespEnabled = true;     /**     * Indicates if authentication traps are enabled.     */    private boolean authTrapEnabled = true;     /**     * The enterprise OID.     * <BR>The default value is "1.3.6.1.4.1.42".     */    private SnmpOid enterpriseOid = new SnmpOid("1.3.6.1.4.1.42");     /**     * The buffer size of the SNMP protocol adaptor.     * This buffer size is used for both incoming request and outgoing     * inform requests.     * <BR>The default value is 1024.     */    int bufferSize = 1024;     private transient long            startUpTime     = 0;    private transient DatagramSocket  socket          = null;    transient DatagramSocket          trapSocket      = null;    private transient SnmpSession     informSession   = null;    private transient DatagramPacket  packet          = null;    transient Vector<SnmpMibAgent>    mibs            = new Vector<SnmpMibAgent>();    private transient SnmpMibTree     root;     /**     * Whether ACL must be used.     */    private transient boolean         useAcl = true;      // SENDING SNMP INFORMS STUFF    //---------------------------     /**     * Number of times to try an inform request before giving up.     * The default number is 3.     */    private int maxTries = 3 ;     /**     * The amount of time to wait for an inform response from the manager.     * The default amount of time is 3000 millisec.     */    private int timeout = 3 * 1000 ;     // VARIABLES REQUIRED FOR IMPLEMENTING SNMP GROUP (MIBII)    //-------------------------------------------------------     /**     * The <CODE>snmpOutTraps</CODE> value defined in MIB-II.     */    int snmpOutTraps=0;     /**     * The <CODE>snmpOutGetResponses</CODE> value defined in MIB-II.     */    private int snmpOutGetResponses=0;     /**     * The <CODE>snmpOutGenErrs</CODE> value defined in MIB-II.     */    private int snmpOutGenErrs=0;     /**     * The <CODE>snmpOutBadValues</CODE> value defined in MIB-II.     */    private int snmpOutBadValues=0;     /**     * The <CODE>snmpOutNoSuchNames</CODE> value defined in MIB-II.     */    private int snmpOutNoSuchNames=0;     /**     * The <CODE>snmpOutTooBigs</CODE> value defined in MIB-II.     */    private int snmpOutTooBigs=0;     /**     * The <CODE>snmpOutPkts</CODE> value defined in MIB-II.     */    int snmpOutPkts=0;     /**     * The <CODE>snmpInASNParseErrs</CODE> value defined in MIB-II.     */    private int snmpInASNParseErrs=0;     /**     * The <CODE>snmpInBadCommunityUses</CODE> value defined in MIB-II.     */    private int snmpInBadCommunityUses=0;     /**     * The <CODE>snmpInBadCommunityNames</CODE> value defined in MIB-II.     */    private int snmpInBadCommunityNames=0;     /**     * The <CODE>snmpInBadVersions</CODE> value defined in MIB-II.     */    private int snmpInBadVersions=0;     /**     * The <CODE>snmpInGetRequests</CODE> value defined in MIB-II.     */    private int snmpInGetRequests=0;     /**     * The <CODE>snmpInGetNexts</CODE> value defined in MIB-II.     */    private int snmpInGetNexts=0;     /**     * The <CODE>snmpInSetRequests</CODE> value defined in MIB-II.     */    private int snmpInSetRequests=0;     /**     * The <CODE>snmpInPkts</CODE> value defined in MIB-II.     */    private int snmpInPkts=0;     /**     * The <CODE>snmpInTotalReqVars</CODE> value defined in MIB-II.     */    private int snmpInTotalReqVars=0;     /**     * The <CODE>snmpInTotalSetVars</CODE> value defined in MIB-II.     */    private int snmpInTotalSetVars=0;     /**     * The <CODE>snmpInTotalSetVars</CODE> value defined in rfc 1907 MIB-II.     */    private int snmpSilentDrops=0;     private static final String InterruptSysCallMsg =        "Interrupted system call";    static final SnmpOid sysUpTimeOid = new SnmpOid("1.3.6.1.2.1.1.3.0") ;    static final SnmpOid snmpTrapOidOid = new SnmpOid("1.3.6.1.6.3.1.1.4.1.0");     private ThreadService threadService;     private static int threadNumber = 6;     static {        String s = System.getProperty("com.sun.jmx.snmp.threadnumber");         if (s != null) {            try {                threadNumber = Integer.parseInt(System.getProperty(s));            } catch (Exception e) {                SNMP_ADAPTOR_LOGGER.logp(Level.FINER,                        SnmpAdaptorServer.class.getName(),                        "<static init>",                        "Got wrong value for com.sun.jmx.snmp.threadnumber: " +                        s + ". Use the default value: " + threadNumber);            }        }    }     // PUBLIC CONSTRUCTORS    //--------------------     /**     * Initializes this SNMP protocol adaptor using the default port (161).     * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default     * implementation of the <CODE>InetAddressAcl</CODE> interface.     */    public SnmpAdaptorServer() {        this(true, null, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT,             null) ;    }     /**     * Initializes this SNMP protocol adaptor using the specified port.     * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default     * implementation of the <CODE>InetAddressAcl</CODE> interface.     *     * @param port The port number for sending SNMP responses.     */    public SnmpAdaptorServer(int port) {        this(true, null, port, null) ;    }     /**     * Initializes this SNMP protocol adaptor using the default port (161)     * and the specified IP address based ACL implementation.     *     * @param acl The <CODE>InetAddressAcl</CODE> implementation.     *        <code>null</code> means no ACL - everybody is authorized.     *     * @since 1.5     */    public SnmpAdaptorServer(InetAddressAcl acl) {        this(false, acl, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT,             null) ;    }     /**     * Initializes this SNMP protocol adaptor using the default port (161)     * and the     * specified <CODE>InetAddress</CODE>.     * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default     * implementation of the <CODE>InetAddressAcl</CODE> interface.     *     * @param addr The IP address to bind.     */    public SnmpAdaptorServer(InetAddress addr) {        this(true, null, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT,             addr) ;    }     /**     * Initializes this SNMP protocol adaptor using the specified port and the     * specified IP address based ACL implementation.     *     * @param acl The <CODE>InetAddressAcl</CODE> implementation.     *        <code>null</code> means no ACL - everybody is authorized.     * @param port The port number for sending SNMP responses.     *     * @since 1.5     */    public SnmpAdaptorServer(InetAddressAcl acl, int port) {        this(false, acl, port, null) ;    }     /**     * Initializes this SNMP protocol adaptor using the specified port and the     * specified <CODE>InetAddress</CODE>.     * Use the {@link com.sun.jmx.snmp.IPAcl.SnmpAcl} default     * implementation of the <CODE>InetAddressAcl</CODE> interface.     *     * @param port The port number for sending SNMP responses.     * @param addr The IP address to bind.     */    public SnmpAdaptorServer(int port, InetAddress addr) {        this(true, null, port, addr) ;    }     /**     * Initializes this SNMP protocol adaptor using the specified IP     * address based ACL implementation and the specified     * <CODE>InetAddress</CODE>.     *     * @param acl The <CODE>InetAddressAcl</CODE> implementation.     * @param addr The IP address to bind.     *     * @since 1.5     */    public SnmpAdaptorServer(InetAddressAcl acl, InetAddress addr) {        this(false, acl, com.sun.jmx.snmp.ServiceName.SNMP_ADAPTOR_PORT,             addr) ;    }     /**     * Initializes this SNMP protocol adaptor using the specified port, the     * specified  address based ACL implementation and the specified     * <CODE>InetAddress</CODE>.     *     * @param acl The <CODE>InetAddressAcl</CODE> implementation.     * @param port The port number for sending SNMP responses.     * @param addr The IP address to bind.     *     * @since 1.5     */    public SnmpAdaptorServer(InetAddressAcl acl, int port, InetAddress addr) {        this(false, acl, port, addr);    }     /**     * Initializes this SNMP protocol adaptor using the specified port and the     * specified <CODE>InetAddress</CODE>.     * This constructor allows to initialize an SNMP adaptor without using     * the ACL mechanism (by setting the <CODE>useAcl</CODE> parameter to     * false).     * <br>This constructor must be used in particular with a platform that     * does not support the <CODE>java.security.acl</CODE> package like pJava.     *     * @param useAcl Specifies if this new SNMP adaptor uses the ACL mechanism.     * If the specified parameter is set to <CODE>true</CODE>, this     * constructor is equivalent to     * <CODE>SnmpAdaptorServer((int)port,(InetAddress)addr)</CODE>.     * @param port The port number for sending SNMP responses.     * @param addr The IP address to bind.     */    public SnmpAdaptorServer(boolean useAcl, int port, InetAddress addr) {        this(useAcl,null,port,addr);    }     // If forceAcl is `true' and InetAddressAcl is null, then a default    // SnmpAcl object is created.    //    private SnmpAdaptorServer(boolean forceAcl, InetAddressAcl acl,                              int port, InetAddress addr) {        super(CommunicatorServer.SNMP_TYPE) ;          // Initialize the ACL implementation.        //        if (acl == null && forceAcl) {            try {                acl = (InetAddressAcl)                    new SnmpAcl("SNMP protocol adaptor IP ACL");            } catch (UnknownHostException e) {                if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINEST)) {                    SNMP_ADAPTOR_LOGGER.logp(Level.FINEST, dbgTag,                        "constructor", "UnknowHostException when creating ACL",e);                }            }        } else {            this.useAcl = (acl!=null) || forceAcl;        }         init(acl, port, addr) ;    }     // GETTERS AND SETTERS

Lines 1–500 of 2659.