File: AirportDiscoverer.java

package info (click to toggle)
airport-utils 2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,024 kB
  • ctags: 5,067
  • sloc: java: 30,844; xml: 571; sh: 553; makefile: 36
file content (294 lines) | stat: -rw-r--r-- 7,872 bytes parent folder | download | duplicates (5)
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
/*
 * Wireless Host Monitor
 *
 * Copyright (C) 2003, 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
 *
 */


package airporthostmon;



import java.util.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;




/**
*	Broadcasts discovery packet, waits for responses. Displays responding devices' IP addresses,
*	names, and device-type identifying strings.
*/

public class AirportDiscoverer extends JFrame
					 implements ActionListener,
								Runnable
{
	JButton stopDiscoveryButton, closeButton;
	JTextArea theArea;
	
	Thread discoveryThread;
		
	
	
	public AirportDiscoverer()
	{
		
		setUpDisplay();
		this.pack();
		this.show();
		
		discoveryThread = new Thread(this);
		discoveryThread.start();
		
	}
	
	
	
	private void setUpDisplay()
	{
		
		this.setTitle("Base Station Discovery");
		
		
		stopDiscoveryButton = new JButton("Stop Discovery");
		stopDiscoveryButton.setActionCommand("stop discovery");
		stopDiscoveryButton.addActionListener(this);
		
		closeButton = new JButton("Close");
		closeButton.setActionCommand("close");
		closeButton.addActionListener(this);
		
		theArea = new JTextArea(20,70);
		theArea.setLineWrap(true);
		theArea.setFont(new Font("Monospaced", Font.PLAIN, 10));
		JScrollPane messagesScroll = new JScrollPane(theArea);
		
		// 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;
		Insets theMargin = new Insets(2,2,2,2);
		c.insets = theMargin;
		c.anchor = GridBagConstraints.CENTER;
		c.weightx = .5;
		c.weighty = .5;
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(theLayout);
		c.gridx = 1;
		c.gridy = 1;
		theLayout.setConstraints(stopDiscoveryButton, c);
		buttonPanel.add(stopDiscoveryButton);
		
		c.gridx = 2;
		c.gridy = 1;
		theLayout.setConstraints(closeButton, c);
		buttonPanel.add(closeButton);
		
		this.getContentPane().setLayout(new BorderLayout(5,5));
		this.getContentPane().add("North", buttonPanel);
		this.getContentPane().add("Center", messagesScroll);
		
	}
	
	
	
	
	public void actionPerformed(ActionEvent theEvent)
	// respond to button pushes, menu selections
	{
		String command = theEvent.getActionCommand();
		
		try
		{
			if (command == "close")
			{
				discoveryThread.interrupt();
				this.hide();
				this.dispose();
			}
			
			if (command == "stop discovery")
			{
				discoveryThread.interrupt();
			}
			
		}
		catch (Exception e)
		{
			
		}
		
	}
	
	
	
	public void run() 
	{
		try
		{
			DatagramSocket dSocket = new DatagramSocket();
			dSocket.setSoTimeout(1000);	//1 second
			int AIRPORT_PORT = 192;
			InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255");
	
			byte[] bytes = new byte[116];	// from sniffs
			bytes[0] = (byte)0x01;
			
			
			DatagramPacket outPacket = new DatagramPacket(bytes, bytes.length, broadcastAddress, AIRPORT_PORT);
					
			dSocket.send(outPacket);
			
			while(! discoveryThread.interrupted())
			{
				//bytes = new byte[116];	// from sniffs
				
				DatagramPacket inPacket = new DatagramPacket(bytes, bytes.length);
					
				try
				{
					dSocket.receive(inPacket);
					
					bytes = inPacket.getData();
					
					String sourceAddress = inPacket.getAddress().getHostAddress();
					
					// parse info in response packet
					
					/*
					System.out.println("Returned Message bytes:");
					for (int i = 0; i < bytes.length; ++i)
						System.out.print(hexByte(bytes[i]) + " ");
					*/
				
					AirportDiscoveryInfo theInfo = new AirportDiscoveryInfo(bytes);
					
					long upTime = Long.parseLong(theInfo.get("Base station uptime").toString());
					upTime = upTime / 100;		// put into seconds
					
					// change to days:hours:minutes format
					long days = upTime / (3600 * 24);
					long hours = (upTime / 3600) % 24;
					long minutes = (upTime / 60) % 60;
					long seconds = upTime % 60;
					
					String dayString = new String();
					dayString += days;
					
					String hourString = new String();
					hourString += hours;
					if (hourString.length() < 2)
						hourString = "0" + hourString;
					
					// make hours, minutes and seconds have 2 digits
					String minuteString = new String();
					minuteString += minutes;
					if (minuteString.length() < 2)
						minuteString = "0" + minuteString;
					
					String secondString = new String();
					secondString += seconds;
					if (secondString.length() < 2)
						secondString = "0" + secondString;
					
					String timeString = dayString + ":" + hourString + ":" + minuteString;
					
					theArea.append("Access point found:\n");
					theArea.append("  Local LAN IP address:     " + theInfo.get("Base station IP address").toString() + "\n");
					theArea.append("  External IP address:      " + sourceAddress + "\n");
					theArea.append("  MAC address:              " + theInfo.get("Base station Mac address").toString() + "\n");
					theArea.append("  Device name:              " + theInfo.get("System name").toString() + "\n");
					theArea.append("  Device type:              " + theInfo.get("Device identifying string").toString() + "\n");
					theArea.append("  Uptime (days:hrs:mins):   " + timeString + "\n");
					
					/*
					// now send out some SNMP requests to retrieve additional info from
					// the responding station; would really like to ARP on the MAC address
					// in case the returned IP address is unhappy (e.g., 0.0.0.0)....
					try
					{
					
						String community = "public";
						int version = 0;	// SNMPv1
						InetAddress hostAddress = InetAddress.getByName(theInfo.get("waIP").toString());
						SNMPv1CommunicationInterface comInterface = new SNMPv1CommunicationInterface(version, hostAddress, community);
						
						String itemID = "";	
						SNMPVarBindList newVars = comInterface.getMIBEntry(itemID);
						SNMPSequence pair = (SNMPSequence)(newVars.getSNMPObjectAt(0));
						SNMPObject snmpValue = pair.getSNMPObjectAt(1);
						theArea.append("External IP adress:    " + snmpValue.toString() + "\n");
					
							
					}
					catch(Exception e)
					{
						// do nothing; problem getting SNMP stuff...
					}
					*/
					
					theArea.append("\n");
					
				
				
				}
				catch (InterruptedIOException e)
				{
					// will get here if nothing received in 1 second; used to check if interrupted
					// by user!
				}
				
			}
			
			
					
		}
		catch(Exception e)
		{
			theArea.append("Exception during discovery: " + e + "\n");
		}
		
		theArea.append("Discovery finished.\n");
	}
	
	
	
	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;
	}
	
	
}