📄 snmpv1agentinterface.java
字号:
/*
* SNMP Package
*
* Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
*
* This is free software. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package snmp;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* The class SNMPv1AgentInterface implements an interface for responding to requests sent from a remote SNMP
* manager. The agent simply listens for requests for information, and passes requested OIDs on to concrete
* subclasses of SNMPRequestListener. These are expected to retrieve requested information from the system,
* and return this to the agent interface for inclusion in a response to the manager.
* The approach is that from version 1 of SNMP, using no encryption of data. Communication occurs
* via UDP, using port 162, the standard SNMP trap port, as the destination port.
*/
public class SNMPv1AgentInterface
implements Runnable
{
public static final int SNMP_PORT = 161;
// largest size for datagram packet payload; based on
// RFC 1157, need to handle messages of at least 484 bytes
public int receiveBufferSize = 512;
int version = 0;
private DatagramSocket dSocket;
private Thread receiveThread;
private Vector listenerVector;
private PrintWriter errorLog;
/**
* Construct a new agent object to listen for requests from remote SNMP managers. The agent listens
* on the standard SNMP UDP port 161.
*/
public SNMPv1AgentInterface(int version)
throws SocketException
{
this(version, SNMP_PORT, new PrintWriter(System.out));
}
/**
* Construct a new agent object to listen for requests from remote SNMP managers. The agent listens
* on the supplied port.
*/
public SNMPv1AgentInterface(int version, int localPort)
throws SocketException
{
this(version, localPort, new PrintWriter(System.out));
}
/**
* Construct a new agent object to listen for requests from remote SNMP managers. The agent listens
* on the supplied port, and sends error messages to the specified PrintWriter.
*/
public SNMPv1AgentInterface(int version, PrintWriter errorReceiver)
throws SocketException
{
this(version, SNMP_PORT, errorReceiver);
}
/**
* Construct a new agent object to listen for requests from remote SNMP managers. The agent listens
* on the supplied port, and sends error messages to the specified PrintWriter.
*/
public SNMPv1AgentInterface(int version, int localPort, PrintWriter errorReceiver)
throws SocketException
{
this.version = version;
dSocket = new DatagramSocket(localPort);
listenerVector = new Vector();
receiveThread = new Thread(this);
errorLog = errorReceiver;
}
/**
* Set the specified PrintWriter to receive error messages.
*/
public void setErrorReceiver(PrintWriter errorReceiver)
{
errorLog = errorReceiver;
}
public void addRequestListener(SNMPRequestListener listener)
{
// see if listener already added; if so, ignore
for (int i = 0; i < listenerVector.size(); i++)
{
if (listener == listenerVector.elementAt(i))
{
return;
}
}
// if got here, it's not in the list; add it
listenerVector.add(listener);
}
public void removeRequestListener(SNMPRequestListener listener)
{
// see if listener in list; if so, remove, if not, ignore
for (int i = 0; i < listenerVector.size(); i++)
{
if (listener == listenerVector.elementAt(i))
{
listenerVector.removeElementAt(i);
break;
}
}
}
/**
* Start listening for requests from remote managers.
*/
public void startReceiving()
{
// if receiveThread not already running, start it
if (!receiveThread.isAlive())
{
receiveThread = new Thread(this);
receiveThread.start();
}
}
/**
* Stop listening for requests from remote managers.
*/
public void stopReceiving()
throws SocketException
{
// interrupt receive thread so it will die a natural death
receiveThread.interrupt();
}
/**
* The run() method for the agent interface's listener. Just waits for SNMP request messages to
* come in on port 161 (or the port supplied in the constructor), then dispatches the retrieved
* SNMPPDU and community name to each of the registered SNMPRequestListeners by calling their
* processRequest() methods.
*/
public void run()
{
while (!receiveThread.isInterrupted())
{
try
{
DatagramPacket inPacket = new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);
dSocket.receive(inPacket);
InetAddress requesterAddress = inPacket.getAddress();
int requesterPort = inPacket.getPort();
byte[] encodedMessage = inPacket.getData();
/*
System.out.println("Message bytes length (in): " + inPacket.getLength());
System.out.println("Message bytes (in):");
for (int i = 0; i < encodedMessage.length; ++i)
{
System.out.print(hexByte(encodedMessage[i]) + " ");
}
System.out.println("\n");
*/
SNMPMessage receivedMessage = new SNMPMessage(SNMPBERCodec.extractNextTLV(encodedMessage,0).value);
String communityName = receivedMessage.getCommunityName();
SNMPPDU receivedPDU = receivedMessage.getPDU();
byte requestPDUType = receivedPDU.getPDUType();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -