1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
|
/*
* SNMP Agent Test
*
* 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.
*
*/
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import java.awt.event.*;
import java.io.*;
import snmp.*;
public class SNMPAgentTest extends JFrame
implements ActionListener, SNMPRequestListener, Runnable
{
JButton clearButton;
JTextArea messagesArea;
JScrollPane messagesScroll;
JLabel authorLabel;
MenuBar theMenubar;
Menu fileMenu;
MenuItem aboutItem, quitItem, setReportFileItem;
SNMPv1AgentInterface agentInterface;
String communityName = "public";
SNMPOctetString storedSNMPValue;
PipedReader errorReader;
PipedWriter errorWriter;
Thread readerThread;
boolean haveReportFile = false;
FileWriter reportFileWriter;
private class WindowCloseAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
readerThread.interrupt();
System.exit(0);
}
}
public SNMPAgentTest()
{
setUpDisplay();
storedSNMPValue = new SNMPOctetString("Original value");
try
{
errorReader = new PipedReader();
errorWriter = new PipedWriter(errorReader);
readerThread = new Thread(this);
readerThread.start();
int version = 0; // SNMPv1
agentInterface = new SNMPv1AgentInterface(version, new PrintWriter(errorWriter));
agentInterface.addRequestListener(this);
agentInterface.setReceiveBufferSize(5120);
agentInterface.startReceiving();
}
catch(Exception e)
{
messagesArea.append("Problem starting Agent Test: " + e.toString() + "\n");
}
}
private void setUpDisplay()
{
this.setTitle("SNMP Agent Test");
this.getRootPane().setBorder(new BevelBorder(BevelBorder.RAISED));
// set fonts to smaller-than-normal size, for compaction!
FontUIResource appFont = new FontUIResource("SansSerif", Font.PLAIN, 10);
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
Enumeration keys = defaults.keys();
while (keys.hasMoreElements())
{
String nextKey = (String)(keys.nextElement());
if ((nextKey.indexOf("font") > -1) || (nextKey.indexOf("Font") > -1))
{
UIManager.put(nextKey, appFont);
}
}
// add WindowCloseAdapter to catch window close-box closings
addWindowListener(new WindowCloseAdapter());
theMenubar = new MenuBar();
this.setMenuBar(theMenubar);
fileMenu = new Menu("File");
aboutItem = new MenuItem("About...");
aboutItem.setActionCommand("about");
aboutItem.addActionListener(this);
fileMenu.add(aboutItem);
setReportFileItem = new MenuItem("Set report file...");
setReportFileItem.setActionCommand("set report file");
setReportFileItem.addActionListener(this);
fileMenu.add(setReportFileItem);
fileMenu.addSeparator();
quitItem = new MenuItem("Quit");
quitItem.setActionCommand("quit");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
theMenubar.add(fileMenu);
clearButton = new JButton("Clear messages");
clearButton.setActionCommand("clear messages");
clearButton.addActionListener(this);
authorLabel = new JLabel(" Version 1.0 J. Sevy, August 2003 ");
authorLabel.setFont(new Font("SansSerif", Font.ITALIC, 8));
messagesArea = new JTextArea(10,60);
messagesScroll = new JScrollPane(messagesArea);
// now set up display
// set params for layout manager
GridBagLayout theLayout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.NONE;
c.ipadx = 0;
c.ipady = 0;
c.insets = new Insets(2,2,2,2);
c.anchor = GridBagConstraints.CENTER;
c.weightx = 0;
c.weighty = 0;
JPanel messagesPanel = new JPanel();
messagesPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.WEST;
JLabel messagesLabel = new JLabel("Received requests:");
theLayout.setConstraints(messagesLabel, c);
messagesPanel.add(messagesLabel);
c.gridx = 2;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
theLayout.setConstraints(clearButton, c);
messagesPanel.add(clearButton);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 2;
c.weightx = .5;
c.weighty = .5;
c.anchor = GridBagConstraints.CENTER;
theLayout.setConstraints(messagesScroll, c);
messagesPanel.add(messagesScroll);
c.gridwidth = 1;
c.weightx = 0;
c.weighty = 0;
this.getContentPane().setLayout(theLayout);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 1;
c.weightx = .5;
c.weighty = .5;
theLayout.setConstraints(messagesPanel, c);
this.getContentPane().add(messagesPanel);
c.fill = GridBagConstraints.NONE;
c.gridx = 1;
c.gridy = 2;
c.weightx = 0;
c.weighty = 0;
theLayout.setConstraints(authorLabel, c);
this.getContentPane().add(authorLabel);
}
public void actionPerformed(ActionEvent theEvent)
// respond to button pushes, menu selections
{
String command = theEvent.getActionCommand();
if (command == "quit")
{
readerThread.interrupt();
System.exit(0);
}
if (command == "clear messages")
{
messagesArea.setText("");
}
if (command == "about")
{
//AboutDialog aboutDialog = new AboutDialog(this);
}
if (command == "set report file")
{
try
{
FileDialog fd = new FileDialog(this, "Select report file...", FileDialog.LOAD);
fd.show();
if (fd.getFile() != null)
{
File newFile = new File(fd.getDirectory(), fd.getFile());
reportFileWriter = new FileWriter(newFile);
try
{
reportFileWriter.write("SNMP Agent Report File\n");
reportFileWriter.write("Date: " + (new Date()).toString() + "\n\n");
reportFileWriter.flush();
}
catch (IOException e)
{
messagesArea.append("Unable to write message to report file\n\n");
}
haveReportFile = true;
}
}
catch (Exception e)
{
messagesArea.append("Error opening report file: " + e.getMessage() + "\n");
}
}
}
// Tried making it synchronized so error and "normal" messages won't be interleaved,
// but this led to hangs during testing; so guess I'll live with possible message interleaving.
private void writeMessage(String message)
{
messagesArea.append(message);
// also write to report file, if any
if (haveReportFile)
{
try
{
reportFileWriter.write(message);
reportFileWriter.flush();
}
catch (IOException e)
{
messagesArea.append("Unable to write message to report file\n\n");
}
}
}
public SNMPSequence processRequest(SNMPPDU pdu, String communityName)
throws SNMPGetException, SNMPSetException
{
writeMessage("Got pdu:\n");
writeMessage(" community name: " + communityName + "\n");
writeMessage(" request ID: " + pdu.getRequestID() + "\n");
writeMessage(" pdu type: ");
byte pduType = pdu.getPDUType();
switch (pduType)
{
case SNMPBERCodec.SNMPGETREQUEST:
{
writeMessage("SNMPGETREQUEST\n");
break;
}
case SNMPBERCodec.SNMPGETNEXTREQUEST:
{
writeMessage("SNMPGETNEXTREQUEST\n");
break;
}
case SNMPBERCodec.SNMPSETREQUEST:
{
writeMessage("SNMPSETREQUEST\n");
break;
}
case SNMPBERCodec.SNMPGETRESPONSE:
{
writeMessage("SNMPGETRESPONSE\n");
break;
}
case SNMPBERCodec.SNMPTRAP:
{
writeMessage("SNMPTRAP\n");
break;
}
default:
{
writeMessage("unknown\n");
break;
}
}
SNMPSequence varBindList = pdu.getVarBindList();
SNMPSequence responseList = new SNMPSequence();
for (int i = 0; i < varBindList.size(); i++)
{
SNMPSequence variablePair = (SNMPSequence)varBindList.getSNMPObjectAt(i);
SNMPObjectIdentifier snmpOID = (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(0);
SNMPObject snmpValue = (SNMPObject)variablePair.getSNMPObjectAt(1);
writeMessage(" OID: " + snmpOID + "\n");
writeMessage(" value: " + snmpValue + "\n");
// check to see if supplied community name is ours; if not, we'll just silently
// ignore the request by not returning anything
if (!communityName.equals(this.communityName))
{
continue;
}
// we'll only respond to requests for OIDs 1.3.6.1.2.1.99.0 and 1.3.6.1.2.1.100.0
// OID 1.3.6.1.2.1.99.0: it's read-only
if (snmpOID.toString().equals("1.3.6.1.2.1.99.0"))
{
if (pduType == SNMPBERCodec.SNMPSETREQUEST)
{
// got a set-request for our variable; throw an exception to indicate the
// value is read-only - the SNMPv1AgentInterface will create the appropriate
// error message using our supplied error index and status
// note that error index starts at 1, not 0, so it's i+1
int errorIndex = i+1;
int errorStatus = SNMPRequestException.VALUE_READ_ONLY;
throw new SNMPSetException("Trying to set a read-only variable!", errorIndex, errorStatus);
}
else if (pduType == SNMPBERCodec.SNMPGETREQUEST)
{
// got a get-request for our variable; send back a value - just a string
try
{
SNMPVariablePair newPair = new SNMPVariablePair(new SNMPObjectIdentifier(snmpOID.toString()), new SNMPOctetString("Boo"));
//SNMPVariablePair newPair = new SNMPVariablePair(snmpOID, new SNMPOctetString("Boo"));
responseList.addSNMPObject(newPair);
}
catch (SNMPBadValueException e)
{
// won't happen...
}
}
}
if (snmpOID.toString().equals("1.3.6.1.2.1.100.0"))
{
if (pduType == SNMPBERCodec.SNMPSETREQUEST)
{
// got a set-request for our variable; supplied value must be a string
if (snmpValue instanceof SNMPOctetString)
{
// assign new value
storedSNMPValue = (SNMPOctetString)snmpValue;
// return SNMPVariablePair to indicate we've handled this OID
try
{
SNMPVariablePair newPair = new SNMPVariablePair(snmpOID, storedSNMPValue);
responseList.addSNMPObject(newPair);
}
catch (SNMPBadValueException e)
{
// won't happen...
}
}
else
{
int errorIndex = i+1;
int errorStatus = SNMPRequestException.BAD_VALUE;
throw new SNMPSetException("Supplied value must be SNMPOctetString", errorIndex, errorStatus);
}
}
else if (pduType == SNMPBERCodec.SNMPGETREQUEST)
{
// got a get-request for our variable; send back a value - just a string
try
{
SNMPVariablePair newPair = new SNMPVariablePair(snmpOID, storedSNMPValue);
responseList.addSNMPObject(newPair);
}
catch (SNMPBadValueException e)
{
// won't happen...
}
}
}
}
writeMessage("\n");
// return the created list of variable pairs
return responseList;
}
public SNMPSequence processGetNextRequest(SNMPPDU pdu, String communityName)
throws SNMPGetException
{
writeMessage("Got pdu:\n");
writeMessage(" community name: " + communityName + "\n");
writeMessage(" request ID: " + pdu.getRequestID() + "\n");
writeMessage(" pdu type: ");
byte pduType = pdu.getPDUType();
switch (pduType)
{
case SNMPBERCodec.SNMPGETREQUEST:
{
writeMessage("SNMPGETREQUEST\n");
break;
}
case SNMPBERCodec.SNMPGETNEXTREQUEST:
{
writeMessage("SNMPGETNEXTREQUEST\n");
break;
}
case SNMPBERCodec.SNMPSETREQUEST:
{
writeMessage("SNMPSETREQUEST\n");
break;
}
case SNMPBERCodec.SNMPGETRESPONSE:
{
writeMessage("SNMPGETRESPONSE\n");
break;
}
case SNMPBERCodec.SNMPTRAP:
{
writeMessage("SNMPTRAP\n");
break;
}
default:
{
writeMessage("unknown\n");
break;
}
}
SNMPSequence varBindList = pdu.getVarBindList();
SNMPSequence responseList = new SNMPSequence();
for (int i = 0; i < varBindList.size(); i++)
{
SNMPSequence variablePair = (SNMPSequence)varBindList.getSNMPObjectAt(i);
SNMPObjectIdentifier suppliedOID = (SNMPObjectIdentifier)variablePair.getSNMPObjectAt(0);
SNMPObject suppliedObject = (SNMPObject)variablePair.getSNMPObjectAt(1);
writeMessage(" OID: " + suppliedOID + "\n");
writeMessage(" value: " + suppliedObject + "\n");
// check to see if supplied community name is ours; if not, we'll just silently
// ignore the request by not returning anything
if (!communityName.equals(this.communityName))
{
continue;
}
// we'll only respond to requests for OID 1.3.6.1.2.1.99.0, and it's read-only;
// for get-next request, we'll return the value for 1.3.6.1.2.1.100.0
if (suppliedOID.toString().equals("1.3.6.1.2.1.99.0"))
{
if (pduType == SNMPBERCodec.SNMPGETNEXTREQUEST)
{
// got a get-next-request for our variable; send back a value for OID 1.3.6.1.2.1.100.0
try
{
// create SNMPVariablePair for the next OID and its value
SNMPObjectIdentifier nextOID = new SNMPObjectIdentifier("1.3.6.1.2.1.100.0");
SNMPVariablePair innerPair = new SNMPVariablePair(nextOID, storedSNMPValue);
// now create a pair containing the supplied OID and the variable pair containing the following
// OID and its value; this allows the ANMPv1AgentInterface to know which of the supplied OIDs
// the new OID corresponds to (follows).
SNMPVariablePair outerPair = new SNMPVariablePair(suppliedOID, innerPair);
// add the "compound" SNMPVariablePair to the response list
responseList.addSNMPObject(outerPair);
}
catch (SNMPBadValueException e)
{
// won't happen...
}
}
}
}
writeMessage("\n");
// return the created list of variable pairs
return responseList;
}
public void run()
{
int numChars;
char[] charArray = new char[256];
try
{
while (!readerThread.isInterrupted() && ((numChars = errorReader.read(charArray, 0, charArray.length)) != -1))
{
StringBuffer errorMessage = new StringBuffer();
errorMessage.append("Problem receiving request:\n");
errorMessage.append(new String(charArray, 0, numChars));
errorMessage.append("\n");
writeMessage(errorMessage.toString());
}
}
catch(IOException e)
{
messagesArea.append("Problem receiving errors; error reporter exiting!");
}
}
public static void main(String args[])
{
try
{
SNMPAgentTest theApp = new SNMPAgentTest();
theApp.pack();
theApp.setSize(600,500);
theApp.show();
}
catch (Exception e)
{}
}
}
|