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
|
/*
* AirportBaseStationHangup Utility
*
* Copyright (C) 2000, Jonathan Sevy <jsevy@mcs.drexel.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
import java.util.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import java.awt.event.*;
//import snmp.*;
public class AirportBaseTester extends JFrame
implements ActionListener, Runnable, WindowListener
{
JButton sendPayloadButton, newHostButton, setPasswordButton;
JTextArea messagesArea;
JScrollPane messagesScroll;
JTextField hostIDField, payloadField;
JLabel authorLabel, hostIDLabel, payloadLabel;
MenuBar theMenubar;
Menu fileMenu;
MenuItem quitItem;
//SNMPv1CommunicationInterface comInterface;
String community;
InetAddress hostAddress;
int version;
boolean minimized;
long startTime; // used to hold approximate modem connection start time
Thread statusThread;
public AirportBaseTester()
{
setUpDisplay();
try
{
community = "public";
version = 0; // SNMPv1
hostAddress = InetAddress.getByName(hostIDField.getText());
//comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
// set socket timeout to 5 seconds
//comInterface.setSocketTimeout(5000);
/*
statusThread = new Thread(this);
statusThread.start();
*/
}
catch(Exception e)
{
messagesArea.setText("Exception during startup: " + e + "\n");
}
}
private void setUpDisplay()
{
try
{
// set look-and-feel to native platform l&f, if possible
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
// or not...
}
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);
}
}
// register self as WindowListener, to catch minimization events
addWindowListener(this);
/*
theMenubar = new MenuBar();
this.setMenuBar(theMenubar);
fileMenu = new Menu("File");
quitItem = new MenuItem("Quit");
quitItem.setActionCommand("quit");
quitItem.addActionListener(this);
fileMenu.add(quitItem);
theMenubar.add(fileMenu);
*/
hostIDLabel = new JLabel("Address:");
hostIDField = new JTextField(10);
hostIDField.setText("10.0.1.1");
hostIDField.setEditable(false);
payloadLabel = new JLabel("Payload:");
payloadField = new JTextField(10);
payloadField.setEditable(true);
authorLabel = new JLabel(" Version 1.0 J. Sevy, March 2002");
authorLabel.setFont(new Font("SansSerif", Font.ITALIC, 8));
sendPayloadButton = new JButton("Send payload");
sendPayloadButton.setActionCommand("send payload");
sendPayloadButton.addActionListener(this);
newHostButton = new JButton("Set address");
newHostButton.setActionCommand("new host");
newHostButton.addActionListener(this);
setPasswordButton = new JButton("Set password");
setPasswordButton.setActionCommand("set password");
setPasswordButton.addActionListener(this);
messagesArea = new JTextArea(20,30);
messagesScroll = new JScrollPane(messagesArea);
/*
URL url = AirportBaseStationHangup.class.getResource("iconImage.gif");
this.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
*/
// 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;
this.setTitle("OSU-NMS Tester");
Panel buttonPanel = new Panel();
buttonPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
theLayout.setConstraints(sendPayloadButton, c);
buttonPanel.add(sendPayloadButton);
c.gridx = 2;
c.gridy = 1;
theLayout.setConstraints(payloadField, c);
buttonPanel.add(payloadField);
Panel hostPanel = new Panel();
hostPanel.setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
theLayout.setConstraints(hostIDLabel, c);
hostPanel.add(hostIDLabel);
c.gridx = 2;
c.gridy = 1;
theLayout.setConstraints(hostIDField, c);
hostPanel.add(hostIDField);
c.gridx = 1;
c.gridy = 2;
theLayout.setConstraints(newHostButton, c);
hostPanel.add(newHostButton);
c.gridx = 2;
c.gridy = 2;
theLayout.setConstraints(setPasswordButton, c);
hostPanel.add(setPasswordButton);
c.gridwidth = 1;
c.anchor = GridBagConstraints.CENTER;
this.getContentPane().setLayout(theLayout);
c.gridx = 1;
c.gridy = 1;
theLayout.setConstraints(hostPanel, c);
this.getContentPane().add(hostPanel);
c.gridx = 1;
c.gridy = 2;
theLayout.setConstraints(buttonPanel, c);
this.getContentPane().add(buttonPanel);
c.gridx = 1;
c.gridy = 3;
JLabel messagesLabel = new JLabel("Messages:");
theLayout.setConstraints(messagesLabel, c);
this.getContentPane().add(messagesLabel);
c.weightx = .5;
c.weighty = .5;
c.gridx = 1;
c.gridy = 4;
c.fill = GridBagConstraints.BOTH;
theLayout.setConstraints(messagesScroll, c);
this.getContentPane().add(messagesScroll);
c.weightx = 0;
c.weighty = 0;
c.fill = GridBagConstraints.NONE;
c.gridx = 1;
c.gridy = 5;
theLayout.setConstraints(authorLabel, c);
this.getContentPane().add(authorLabel);
}
// WindowListener methods
public void windowActivated(WindowEvent e)
{
// do nothing
}
public void windowClosing(WindowEvent e)
{
// exit
System.exit(0);
}
public void windowClosed(WindowEvent e)
{
// do nothing
}
public void windowDeactivated(WindowEvent e)
{
// do nothing
}
public void windowOpened(WindowEvent e)
{
// do nothing
}
public void windowIconified(WindowEvent e)
{
// display minimized stuff; will print just connect time (or "Unconnected") as title text
minimized = true;
}
public void windowDeiconified(WindowEvent e)
{
// display full title; connect time will be placed in
minimized = false;
}
public void actionPerformed(ActionEvent theEvent)
// respond to button pushes, menu selections
{
String command = theEvent.getActionCommand();
if (command == "quit")
{
System.exit(0);
}
if (command == "new host")
{
String newHost = JOptionPane.showInputDialog("Input new host:");
if (newHost != null)
{
try
{
hostAddress = InetAddress.getByName(newHost);
//comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
hostIDField.setText(newHost);
startTime = -1; // -1 indicates start time not yet set
}
catch(UnknownHostException e)
{
JOptionPane.showMessageDialog(this, "Unknown host name supplied.");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, "Error setting new host.");
}
}
}
if (command == "set password")
{
String newPassword = JOptionPane.showInputDialog("Base station password:");
if (newPassword != null)
{
try
{
community = newPassword;
//comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
startTime = -1; // -1 indicates start time not yet set
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, "Error entering new password.");
}
}
}
if (command == "send payload")
{
sendPayload();
}
}
private void sendPayload()
{
try
{
DatagramSocket dSocket = new DatagramSocket();
dSocket.setSoTimeout(1000); // 1 second
int OSU_NMS_PORT = 192;
InetAddress hostAddress = InetAddress.getByName(hostIDField.getText());
try
{
messagesArea.append("Sending payload " + payloadField.getText() + "\n");
// get the payload field, convert to bytes
byte[] payloadBytes = new byte[116];
byte[] textBytes = convertFromHexString(payloadField.getText());
int length = payloadBytes.length < textBytes.length? payloadBytes.length : textBytes.length;
for(int i = 0; i < length; i++)
payloadBytes[i] = textBytes[i];
DatagramPacket outPacket = new DatagramPacket(payloadBytes, payloadBytes.length, hostAddress, OSU_NMS_PORT);
dSocket.send(outPacket);
// wait for a reply
byte[] bytes = new byte[116]; // from sniffs
DatagramPacket inPacket = new DatagramPacket(bytes, bytes.length);
dSocket.receive(inPacket);
bytes = inPacket.getData();
messagesArea.append("Returned Message bytes:\n");
for (int i = 0; i < bytes.length; ++i)
{
messagesArea.append(hexByte(bytes[i]) + " ");
if ((i+1)%16 == 0)
messagesArea.append("\n");
}
messagesArea.append("\n");
for (int i = 0; i < bytes.length; ++i)
{
messagesArea.append((char)bytes[i] + "");
if ((i+1)%16 == 0)
messagesArea.append("\n");
}
messagesArea.append("\n");
}
catch(Exception e)
{
System.out.println("Exception during payload send: " + e.toString() + "\n");
}
}
catch(Exception e)
{
//messagesArea.append("Exception during payload send: " + e.toString() + "\n");
}
}
private byte[] convertFromHexString(String hexString)
throws NumberFormatException
{
// eliminate spaces in string
hexString.trim();
int index;
while((index = hexString.indexOf(' ')) != -1)
{
hexString = hexString.substring(0,index) + hexString.substring(index+1);
}
// make sure have even number of hex digits
if (2 * (hexString.length()/2) != hexString.length())
throw new NumberFormatException("Must have an even number of hexadecimal digits.");
byte[] bytes = new byte[hexString.length()/2];
for (int i = 0; i < bytes.length; i++)
{
// get next pair of digits
String digitString = hexString.substring(2*i,2*i+2);
try
{
int value = Integer.parseInt(digitString, 16);
bytes[i] = (byte)value;
}
catch (NumberFormatException e)
{
throw new NumberFormatException("Entries must be hexadecimal digits (0 through 9 and a through f or A through F) or spaces.");
}
}
return bytes;
}
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;
}
public void run()
{
statusPollingThreadRoutine();
}
private void statusPollingThreadRoutine()
{
}
public static void main(String args[])
{
try
{
AirportBaseTester theApp = new AirportBaseTester();
theApp.pack();
theApp.show();
}
catch (Exception e)
{}
}
}
|