|
Java example source code file (SnmpV3Message.java)
The SnmpV3Message.java Java example source code
/*
* Copyright (c) 2001, 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;
// java imports
//
import java.util.Vector;
import java.util.logging.Level;
import java.net.InetAddress;
// import debug stuff
//
import static com.sun.jmx.defaults.JmxProperties.SNMP_LOGGER;
import com.sun.jmx.snmp.internal.SnmpMsgProcessingSubSystem;
import com.sun.jmx.snmp.internal.SnmpSecurityModel;
import com.sun.jmx.snmp.internal.SnmpDecryptedPdu;
import com.sun.jmx.snmp.internal.SnmpSecurityCache;
import com.sun.jmx.snmp.SnmpMsg;
import com.sun.jmx.snmp.SnmpPdu;
import com.sun.jmx.snmp.SnmpStatusException;
import com.sun.jmx.snmp.SnmpTooBigException;
import com.sun.jmx.snmp.SnmpScopedPduBulk;
import com.sun.jmx.snmp.BerException;
import com.sun.jmx.snmp.SnmpScopedPduRequest;
import com.sun.jmx.snmp.BerDecoder;
import com.sun.jmx.snmp.SnmpDefinitions;
import com.sun.jmx.snmp.SnmpEngineId;
import com.sun.jmx.snmp.SnmpScopedPduPacket;
import com.sun.jmx.snmp.BerEncoder;
import com.sun.jmx.snmp.SnmpPduRequestType;
import com.sun.jmx.snmp.SnmpPduBulkType;
/**
* Is a partially decoded representation of an SNMP V3 packet.
* <P>
* This class can be used when developing customized manager or agent.
* <P>
* The <CODE>SnmpV3Message class is directly mapped onto the
* message syntax defined in RFC 2572.
* <BLOCKQUOTE>
* <PRE>
* SNMPv3Message ::= SEQUENCE {
* msgVersion INTEGER ( 0 .. 2147483647 ),
* -- administrative parameters
* msgGlobalData HeaderData,
* -- security model-specific parameters
* -- format defined by Security Model
* msgSecurityParameters OCTET STRING,
* msgData ScopedPduData
* }
* HeaderData ::= SEQUENCE {
* msgID INTEGER (0..2147483647),
* msgMaxSize INTEGER (484..2147483647),
*
* msgFlags OCTET STRING (SIZE(1)),
* -- .... ...1 authFlag
* -- .... ..1. privFlag
* -- .... .1.. reportableFlag
* -- Please observe:
* -- .... ..00 is OK, means noAuthNoPriv
* -- .... ..01 is OK, means authNoPriv
* -- .... ..10 reserved, must NOT be used.
* -- .... ..11 is OK, means authPriv
*
* msgSecurityModel INTEGER (1..2147483647)
* }
* </BLOCKQUOTE>
* </PRE>
* <p>This API is a Sun Microsystems internal API and is subject
* to change without notice.</b>
* @since 1.5
*/
public class SnmpV3Message extends SnmpMsg {
/**
* Message identifier.
*/
public int msgId = 0;
/**
* Message max size the pdu sender can deal with.
*/
public int msgMaxSize = 0;
/**
* Message flags. Reportable flag and security level.</P>
*<PRE>
* -- .... ...1 authFlag
* -- .... ..1. privFlag
* -- .... .1.. reportableFlag
* -- Please observe:
* -- .... ..00 is OK, means noAuthNoPriv
* -- .... ..01 is OK, means authNoPriv
* -- .... ..10 reserved, must NOT be used.
* -- .... ..11 is OK, means authPriv
*</PRE>
*/
public byte msgFlags = 0;
/**
* The security model the security sub system MUST use in order to deal with this pdu (eg: User based Security Model Id = 3).
*/
public int msgSecurityModel = 0;
/**
* The unmarshalled security parameters.
*/
public byte[] msgSecurityParameters = null;
/**
* The context engine Id in which the pdu must be handled (Generaly the local engine Id).
*/
public byte[] contextEngineId = null;
/**
* The context name in which the OID has to be interpreted.
*/
public byte[] contextName = null;
/** The encrypted form of the scoped pdu (Only relevant when dealing with privacy).
*/
public byte[] encryptedPdu = null;
/**
* Constructor.
*
*/
public SnmpV3Message() {
}
/**
* 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 (SNMP_LOGGER.isLoggable(Level.FINER)) {
SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(),
"encodeMessage",
"Can't encode directly V3Message! Need a SecuritySubSystem");
}
throw new IllegalArgumentException("Can't encode");
}
/**
* Decodes the specified bytes and initializes this message.
* For internal use only.
*
* @param inputBytes The bytes to be decoded.
*
* @exception SnmpStatusException If the specified bytes are not a valid encoding.
*/
public void decodeMessage(byte[] inputBytes, int byteCount)
throws SnmpStatusException {
try {
BerDecoder bdec = new BerDecoder(inputBytes);
bdec.openSequence();
version = bdec.fetchInteger();
bdec.openSequence();
msgId = bdec.fetchInteger();
msgMaxSize = bdec.fetchInteger();
msgFlags = bdec.fetchOctetString()[0];
msgSecurityModel =bdec.fetchInteger();
bdec.closeSequence();
msgSecurityParameters = bdec.fetchOctetString();
if( (msgFlags & SnmpDefinitions.privMask) == 0 ) {
bdec.openSequence();
contextEngineId = bdec.fetchOctetString();
contextName = bdec.fetchOctetString();
data = bdec.fetchAny();
dataLength = data.length;
bdec.closeSequence();
}
else {
encryptedPdu = bdec.fetchOctetString();
}
bdec.closeSequence() ;
}
catch(BerException x) {
x.printStackTrace();
throw new SnmpStatusException("Invalid encoding") ;
}
if (SNMP_LOGGER.isLoggable(Level.FINER)) {
final StringBuilder strb = new StringBuilder()
.append("Unmarshalled message : \n")
.append("version : ").append(version)
.append("\n")
.append("msgId : ").append(msgId)
.append("\n")
.append("msgMaxSize : ").append(msgMaxSize)
.append("\n")
.append("msgFlags : ").append(msgFlags)
.append("\n")
.append("msgSecurityModel : ").append(msgSecurityModel)
.append("\n")
.append("contextEngineId : ").append(contextEngineId == null ? null :
SnmpEngineId.createEngineId(contextEngineId))
.append("\n")
.append("contextName : ").append(contextName)
.append("\n")
.append("data : ").append(data)
.append("\n")
.append("dat len : ").append((data == null) ? 0 : data.length)
.append("\n")
.append("encryptedPdu : ").append(encryptedPdu)
.append("\n");
SNMP_LOGGER.logp(Level.FINER, SnmpV3Message.class.getName(),
"decodeMessage", strb.toString());
}
}
/**
* Returns the associated request Id.
* @param data The flat message.
* @return The request Id.
*/
public int getRequestId(byte[] data) throws SnmpStatusException {
BerDecoder bdec = null;
int msgId = 0;
try {
bdec = new BerDecoder(data);
bdec.openSequence();
bdec.fetchInteger();
bdec.openSequence();
msgId = bdec.fetchInteger();
}catch(BerException x) {
throw new SnmpStatusException("Invalid encoding") ;
}
try {
bdec.closeSequence();
}
catch(BerException x) {
}
return msgId;
}
/**
* Initializes this message with the specified <CODE>pdu.
* <P>
* This method initializes the data field with an array of
* <CODE>maxDataLength bytes. It encodes the
Other Java examples (source code examples)Here is a short list of links related to this Java SnmpV3Message.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.