📄 snmpv1agentinterface.java
字号:
//System.out.println("Received message; community = " + communityName + ", pdu type = " + Byte.toString(requestPDUType));
//System.out.println(" read community = " + readCommunityName + ", write community = " + writeCommunityName);
SNMPSequence requestedVarList = receivedPDU.getVarBindList();
Hashtable variablePairHashtable = new Hashtable();
SNMPSequence responseVarList = new SNMPSequence();
int errorIndex = 0;
int errorStatus = SNMPRequestException.NO_ERROR;
int requestID = receivedPDU.getRequestID();
try
{
// pass the received PDU and community name to the processRequest method of any listeners;
// handle differently depending on whether the request is a get-next, or a get or set
if ((requestPDUType == SNMPBERCodec.SNMPGETREQUEST) || (requestPDUType == SNMPBERCodec.SNMPSETREQUEST))
{
// pass the received PDU and community name to any registered listeners
for (int i = 0; i < listenerVector.size(); i++)
{
SNMPRequestListener listener = (SNMPRequestListener)listenerVector.elementAt(i);
// return value is sequence of variable pairs for those OIDs handled by the listener
SNMPSequence handledVarList = listener.processRequest(receivedPDU, communityName);
// add to Hashtable of handled OIDs, if not already there
for (int j = 0; j < handledVarList.size(); j++)
{
SNMPSequence handledPair = (SNMPSequence)handledVarList.getSNMPObjectAt(j);
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)handledPair.getSNMPObjectAt(0);
SNMPObject snmpObject = (SNMPObject)handledPair.getSNMPObjectAt(1);
if (!variablePairHashtable.containsKey(snmpOID))
{
variablePairHashtable.put(snmpOID, snmpObject);
}
}
}
// construct response containing the handled OIDs; if any OID not handled, throw exception
for (int j = 0; j < requestedVarList.size(); j++)
{
SNMPSequence requestPair = (SNMPSequence)requestedVarList.getSNMPObjectAt(j);
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)requestPair.getSNMPObjectAt(0);
// find corresponding SNMP object in hashtable
if (!variablePairHashtable.containsKey(snmpOID))
{
errorIndex = j + 1;
errorStatus = SNMPRequestException.VALUE_NOT_AVAILABLE;
if (requestPDUType == SNMPBERCodec.SNMPGETREQUEST)
throw new SNMPGetException("OID " + snmpOID + " not handled", errorIndex, errorStatus);
else
throw new SNMPSetException("OID " + snmpOID + " not handled", errorIndex, errorStatus);
}
SNMPObject snmpObject = (SNMPObject)variablePairHashtable.get(snmpOID);
SNMPVariablePair responsePair = new SNMPVariablePair(snmpOID, snmpObject);
responseVarList.addSNMPObject(responsePair);
}
}
else if (requestPDUType == SNMPBERCodec.SNMPGETNEXTREQUEST)
{
// pass the received PDU and community name to any registered listeners
for (int i = 0; i < listenerVector.size(); i++)
{
SNMPRequestListener listener = (SNMPRequestListener)listenerVector.elementAt(i);
// return value is sequence of nested variable pairs for those OIDs handled by the listener:
// consists of (supplied OID, (following OID, value)) nested variable pairs
SNMPSequence handledVarList = listener.processGetNextRequest(receivedPDU, communityName);
// add variable pair to Hashtable of handled OIDs, if not already there
for (int j = 0; j < handledVarList.size(); j++)
{
SNMPSequence handledPair = (SNMPSequence)handledVarList.getSNMPObjectAt(j);
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)handledPair.getSNMPObjectAt(0);
SNMPObject snmpObject = (SNMPObject)handledPair.getSNMPObjectAt(1);
if (!variablePairHashtable.containsKey(snmpOID))
{
variablePairHashtable.put(snmpOID, snmpObject);
}
}
}
// construct response containing the handled OIDs; if any OID not handled, throw exception
for (int j = 0; j < requestedVarList.size(); j++)
{
SNMPSequence requestPair = (SNMPSequence)requestedVarList.getSNMPObjectAt(j);
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)requestPair.getSNMPObjectAt(0);
// find corresponding SNMP object in hashtable
if (!variablePairHashtable.containsKey(snmpOID))
{
errorIndex = j + 1;
errorStatus = SNMPRequestException.VALUE_NOT_AVAILABLE;
throw new SNMPGetException("OID " + snmpOID + " not handled", errorIndex, errorStatus);
}
// value in hashtable is complete variable pair
SNMPVariablePair responsePair = (SNMPVariablePair)variablePairHashtable.get(snmpOID);
responseVarList.addSNMPObject(responsePair);
}
}
else
{
// some other PDU type; silently ignore
continue;
}
}
catch (SNMPRequestException e)
{
// exception should contain the index and cause of error; return this in message
errorIndex = e.errorIndex;
errorStatus = e.errorStatus;
// just return request variable list as response variable list
responseVarList = requestedVarList;
}
catch (Exception e)
{
// don't have a specific index and cause of error; return message as general error, index 0
errorIndex = 0;
errorStatus = SNMPRequestException.FAILED;
// just return request variable list as response variable list
responseVarList = requestedVarList;
// also report the exception locally
errorLog.println("Exception while processing request: " + e.toString());
errorLog.flush();
}
SNMPPDU pdu = new SNMPPDU(SNMPBERCodec.SNMPGETRESPONSE, requestID, errorStatus, errorIndex, responseVarList);
SNMPMessage message = new SNMPMessage(version, communityName, pdu);
byte[] messageEncoding = message.getBEREncoding();
DatagramPacket outPacket = new DatagramPacket(messageEncoding, messageEncoding.length, requesterAddress, requesterPort);
dSocket.send(outPacket);
}
catch (IOException e)
{
// just report the problem
errorLog.println("IOException during request processing: " + e.getMessage());
errorLog.flush();
}
catch (SNMPBadValueException e)
{
// just report the problem
errorLog.println("SNMPBadValueException during request processing: " + e.getMessage());
errorLog.flush();
}
catch (Exception e)
{
// just report the problem
errorLog.println("Exception during request processing: " + e.toString());
errorLog.flush();
}
}
}
private String hexByte(byte b)
{
int pos = b;
if (pos < 0)
pos += 256;
String returnString = new String();
returnString += Integer.toHexString(pos/16);
returnString += Integer.toHexString(pos%16);
return returnString;
}
/**
* Set the size of the buffer used to receive response packets. RFC 1157 stipulates that an SNMP
* implementation must be able to receive packets of at least 484 bytes, so if you try to set the
* size to a value less than this, the receive buffer size will be set to 484 bytes. In addition,
* the maximum size of a UDP packet payload is 65535 bytes, so setting the buffer to a larger size
* will just waste memory. The default value is 512 bytes. The value may need to be increased if
* get-requests are issued for multiple OIDs.
*/
public void setReceiveBufferSize(int receiveBufferSize)
{
if (receiveBufferSize >= 484)
{
this.receiveBufferSize = receiveBufferSize;
}
else
{
this.receiveBufferSize = 484;
}
}
/**
* Returns the current size of the buffer used to receive response packets.
*/
public int getReceiveBufferSize()
{
return this.receiveBufferSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -