com.wjholden.nmap
Class NmapSubnet

java.lang.Object
  extended by Activity
      extended by com.wjholden.nmap.NmapSubnet
All Implemented Interfaces:
NmapConstants

public class NmapSubnet
extends Activity
implements NmapConstants

Shows information about the subnet the device is currently connected to via 802.11. This class includes a number of methods handling the confusing world of byte ordering with IP's in Java. There is no 'unsigned byte' in Java so portions of this were (very) frustrating to write, but it's been fun and educational.
TODO:
- save output to XML
- enable/disable debugging from here

Since:
30
Version:
0.1
Author:
William John Holden (wjholden@gmail.com)

Nested Class Summary
private  class NmapSubnet.PollWifiChanges
           
 
Field Summary
private  int base
           
private  int[] broadcast
           
private  java.lang.String classfulness
           
private  int[] dns1
           
private  int[] dns2
           
private  EditText editBC
           
private  EditText editClass
           
private  EditText editDG
           
private  EditText editDns1
           
private  EditText editDns2
           
private  EditText editHosts
           
private  EditText editIP
           
private  EditText editNet
           
private  EditText editSM
           
private  EditText editSSID
           
private  int[] gateway
           
(package private) static Handler handler
           
private  int[] ip
           
private  int[] mask
           
private  int[] network
           
(package private) static NmapSubnet.PollWifiChanges poller
           
private  java.lang.String ssid
           
(package private) static boolean subnetActivityClosing
           
 
Fields inherited from interface com.wjholden.nmap.NmapConstants
BUFFER_SIZE, FORCE_RESTART, INSTALL_ERROR, INSTALL_FILENAMES, INSTALL_GOOD, INSTALL_NO_ROOT, INSTALL_RESOURCES, MG_DEBUG, MG_DEFAULT, MG_SUBNET, MI_BIN, MI_DEBUG_EMAIL, MI_DEBUG_ENABLE, MI_DEC, MI_EMAIL_SUBNET, MI_EXIT, MI_HEX, MI_MORE_HELP, MI_REINSTALL, MI_SUBNET, MI_SURVEY, RUN_COMPLETE, RUN_ERROR, RUN_SUCCESS_NO_FILE, SCANTYPE_NCAT, SCANTYPE_NDIFF, SCANTYPE_NMAP_5_30, SCANTYPE_NMAP_5_50, SCANTYPE_NPING, SET_TITLE, SUBNET_CONNECTED, SUBNET_DISCONNECTED, SUBNET_POLL_INTERVAL, TAG, THREAD_ERROR
 
Constructor Summary
NmapSubnet()
           
 
Method Summary
private  int[] broadcast(int[] network, int subnet)
          Calculates the broadcast address for given network using given subnet.
private  int cidr(int subnet)
          Calculates the number of bits used for the network+subnet based off of given subnet mask.
private  void clearView()
          Clears all fields in the viewport.
private  java.lang.String findNetworkClass(int[] ip, int subnet)
          Finds the network class of a network given network address and subnet mask.
private  java.lang.String getWifiInfo()
          This method calls other methods, as appropriate, to gather and calculate information on the subnet the handset it currently connected to via 802.11.
private  int ipArrayToInt(int[] input)
          Converts an integer-array IPv4 address back to an integer representation.
private  int[] ipToArray(int ip)
          This is some good stuff I borrowed from this guy, who in turn says it came from Limewire.
private  java.lang.String ipToString(int[] ip, java.nio.ByteOrder endianness, int base)
          Converts an IP address in integer-array form to a String.
private  java.lang.String networkClassfulness(char classChar, int cidr)
          Basic logic to determine, given class and CIDR, whether a network is classful or classless.
protected  void onCreate(Bundle savedInstanceState)
           
protected  void onDestroy()
           
 boolean onMenuItemSelected(int featureId, MenuItem item)
           
 boolean onPrepareOptionsMenu(Menu menu)
           
private  int subnetNumberOfHosts(int subnet)
          Uses the CIDR method to calculate the number of hosts available based off of the provided subnet mask.
private  int subnetNumberOfOnes(int octet)
          Counts the number of ones in the binary representation of a single octet of your subnet mask.
Note that this was not designed for wildcard masks (i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

base

private transient int base

broadcast

private transient int[] broadcast

classfulness

private transient java.lang.String classfulness

dns1

private transient int[] dns1

dns2

private transient int[] dns2

editBC

private transient EditText editBC

editClass

private transient EditText editClass

editDG

private transient EditText editDG

editDns1

private transient EditText editDns1

editDns2

private transient EditText editDns2

editHosts

private transient EditText editHosts

editIP

private transient EditText editIP

editNet

private transient EditText editNet

editSM

private transient EditText editSM

editSSID

private transient EditText editSSID

gateway

private transient int[] gateway

handler

static Handler handler

ip

private transient int[] ip

mask

private transient int[] mask

network

private transient int[] network

poller

static NmapSubnet.PollWifiChanges poller

ssid

private transient java.lang.String ssid

subnetActivityClosing

static boolean subnetActivityClosing
Constructor Detail

NmapSubnet

public NmapSubnet()
Method Detail

broadcast

private int[] broadcast(int[] network,
                        int subnet)
Calculates the broadcast address for given network using given subnet. Since Android gives us the subnet as an integer we can just use what's already given by the API and not reinvent the wheel.

Parameters:
network - Integer-array formed network address.
subnet - Subnet provided by DhcpInfo.netmask.
Returns:
Integer-array broadcast address for subnet.

cidr

private int cidr(int subnet)
Calculates the number of bits used for the network+subnet based off of given subnet mask.

Parameters:
subnet - Host byte representation of the subnet mask.
Returns:
CIDR-style number of network bites (i.e. 192.168.1.0/24).

clearView

private void clearView()
Clears all fields in the viewport.


findNetworkClass

private java.lang.String findNetworkClass(int[] ip,
                                          int subnet)
Finds the network class of a network given network address and subnet mask.

Parameters:
ip - Network-order IP address of the network IP.
subnet - Host-order IP in integer format.
Returns:
String indicating class letter and whether classful or classless.

getWifiInfo

private java.lang.String getWifiInfo()
This method calls other methods, as appropriate, to gather and calculate information on the subnet the handset it currently connected to via 802.11. Also updates UI directly.

Returns:
String summary of all information gathered, for ease of use when e-mailing output.

ipArrayToInt

private int ipArrayToInt(int[] input)
Converts an integer-array IPv4 address back to an integer representation.

Parameters:
input - Integer-array IPv4 address in network byte order.
Returns:
Integer IPv4 address in host byte order.

ipToArray

private int[] ipToArray(int ip)
This is some good stuff I borrowed from this guy, who in turn says it came from Limewire. Go figure.
Automatically detects native byte order and (hopefully) makes the correct byte assignments. So you can use this on big or little endian platforms. I realize that Android IA/32 (ARM) will probably always be little endian but I like robust, reusable code.
Caveats:

Parameters:
ip - Integer representation in host byte order to be converted.
Returns:
Integer array in network byte order representing an IPv4 address.

ipToString

private java.lang.String ipToString(int[] ip,
                                    java.nio.ByteOrder endianness,
                                    int base)
Converts an IP address in integer-array form to a String. Because different methods in this program handle IP's in big- and little-endian format this method requires the developer to specify which is desired. If unknown, call java.nio.ByteOrder.nativeOrder().
IPv4 only.

Parameters:
ip - Integer-array IPv4 address.
endianness - ByteOrder.LITTLE_ENDIAN or ByteOrder.BIG_ENDIAN.
base - Indicates what base to output in (options are MI_DEC, MI_HEX, and MI_BIN, as defined in NmapConstants interface).
Returns:
String dot-decimal representation of IPv4 address.

networkClassfulness

private java.lang.String networkClassfulness(char classChar,
                                             int cidr)
Basic logic to determine, given class and CIDR, whether a network is classful or classless.

Parameters:
classChar - Character representation of class ('A', 'B', or 'C').
cidr - Number of bits used for subnet/network address (i.e. 192.168.1.1/xx <- xx is the CIDR).
Returns:
String telling you whether it's classful or classless.

onCreate

protected void onCreate(Bundle savedInstanceState)

onDestroy

protected void onDestroy()

onMenuItemSelected

public boolean onMenuItemSelected(int featureId,
                                  MenuItem item)

onPrepareOptionsMenu

public boolean onPrepareOptionsMenu(Menu menu)

subnetNumberOfHosts

private int subnetNumberOfHosts(int subnet)
Uses the CIDR method to calculate the number of hosts available based off of the provided subnet mask. Remember, the number of hosts is two fewer than two to the power of host bits because the first IP address is the network and last is the broadcast.

Parameters:
subnet - Host byte representation of the subnet mask.
Returns:
Maximum possible number of hosts in this network.

subnetNumberOfOnes

private int subnetNumberOfOnes(int octet)
Counts the number of ones in the binary representation of a single octet of your subnet mask.
Note that this was not designed for wildcard masks (i.e. 0.0.3.255); invert in that case (i.e. subnetNumberOfZeros(octet & 0xFF)).

Parameters:
octet - An IPv4-style octet values 0 to 255.
Returns:
Number of ones found in given subnet mask byte, Integer.MIN_VALUE on error. Returning Integer.MIN_VALUE makes it obvious to the developer and end user that a serious problem has occured that needs to be tracked down.