com.wjholden.nmap
Class NmapUtilities

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

public final class NmapUtilities
extends java.lang.Object
implements NmapConstants

Class created to centralize and more easily manage certain pieces of boilerplate code, often code groups that are required more than once. PMD recommended I make this a "singleton" so here goes.

Author:
William John Holden (wjholden@gmail.com)

Field Summary
static boolean canGetRoot
          Tells us if "su" command is available
private static Context context
          Experimental: save the context as a private static variable, this way you need an NmapUtilities.init(Context) method, but don't need to pass context every time you run one of these variables, thereby simplifying the Scan and Install classes.
private static NmapUtilities FINAL_INSTANCE
          Not used, but makes sure that only one instance of this class runs at once.
 
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
private NmapUtilities()
          Private constructor can only be called in the above FINAL_INSTANCE.
 
Method Summary
static java.lang.String checkRootPermissions()
          Mimics 'which' function to find 'su' command.
static boolean deleteOutputFile(java.lang.String outputFileName)
          A consolidated method to delete the output file after reading it in at the end of RUN_COMPLETE in NmapMain.handler.
static java.lang.String findBinDir()
          Based on whether 'su' is available or not this method sets the location to write compiled binaries for later execution.
static java.lang.String getDataDirectory()
          Find the data directory to store applications in.
static java.lang.String hashFile(java.lang.String filename, java.lang.String algorithm)
          Code slightly adapted from my previous work on MD5 Sums in Java.
Does nothing if NmapMain.debug = false.
static void init(Context myContext)
           
static java.lang.String readOutputFile(java.lang.String command, java.lang.String type)
          This used to be in the RUN_COMPLETE area of NmapMain.handler.
static java.lang.String verifyTemporaryFolder()
          Removed from verifyInstallation for version 21.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

canGetRoot

public static boolean canGetRoot
Tells us if "su" command is available

Since:
25 Must be set by NmapUtilities.checkRootPermissions().

context

private static Context context
Experimental: save the context as a private static variable, this way you need an NmapUtilities.init(Context) method, but don't need to pass context every time you run one of these variables, thereby simplifying the Scan and Install classes.

Since:
26

FINAL_INSTANCE

private static final NmapUtilities FINAL_INSTANCE
Not used, but makes sure that only one instance of this class runs at once.

Constructor Detail

NmapUtilities

private NmapUtilities()
Private constructor can only be called in the above FINAL_INSTANCE. This ensures that only one instance of this "static" class exists, hence it's a singleton.

Method Detail

checkRootPermissions

public static java.lang.String checkRootPermissions()
Mimics 'which' function to find 'su' command. Sets Nmap.sh and Nmap.canGetRoot. Originally, the program just looked for a hard-coded 'su' path, but CM6 has 'su' in a different location. Logic robusted to look everywhere in $PATH. Also sets shell, using Nmap.setSh(String).

Returns:
true if su found in $PATH.
Since:
0.4.1

deleteOutputFile

public static boolean deleteOutputFile(java.lang.String outputFileName)
A consolidated method to delete the output file after reading it in at the end of RUN_COMPLETE in NmapMain.handler.

Parameters:
outputFileName - String representation of the absolute path to the file.
Returns:
Whether the file was deleted or not.
Since:
Added version 27.

findBinDir

public static java.lang.String findBinDir()
Based on whether 'su' is available or not this method sets the location to write compiled binaries for later execution.

Returns:
Absolute location to write binaries as String.
Since:
22 - UPDATE - now ALWAYS uses the {dataDir}/bin/ folder, 26 Removed param, calls getDataDirectory() above., 27 Now also create the bindir if it does not already exist here.

getDataDirectory

public static java.lang.String getDataDirectory()
Find the data directory to store applications in.

Returns:
Absolute path of data directory as String.
Since:
26 No params, NmapUtilities.init(Context) must be called beforehand.

hashFile

public static java.lang.String hashFile(java.lang.String filename,
                                        java.lang.String algorithm)
                                 throws java.io.IOException
Code slightly adapted from my previous work on MD5 Sums in Java.
Does nothing if NmapMain.debug = false.
Presently disabled - this algorithm is painfully slow and I need to research methods for hurrying it up.

Parameters:
filename - Filename to be hashed.
algorithm - In this program, always use "MD5".
Returns:
String representation of the MD5 sum.
Throws:
java.io.IOException - Should be handled in the 'ordinary' logic of the binary write process anyways.
Since:
Version 0.4.12 (28), where Vlatko announced Nmap 5.50 on Android.

init

public static void init(Context myContext)

readOutputFile

public static java.lang.String readOutputFile(java.lang.String command,
                                              java.lang.String type)
This used to be in the RUN_COMPLETE area of NmapMain.handler. Moved here for consolidation/reusability purposes. Should help maintainability significantly.

Parameters:
command - Until nping and ncat are fixed this will always be 'nmap'.
type - Valid values are 'nmap', 'xml', and 'gnmap'.
Returns:
String contents of the output file (might be very large).
Since:
Moved to NmapUtilties version 27.

verifyTemporaryFolder

public static java.lang.String verifyTemporaryFolder()
                                              throws java.io.IOException,
                                                     NameNotFoundException
Removed from verifyInstallation for version 21. If temporary folder does not already exist, create it.

Returns:
Absolute path of the temporary folder as String.
Throws:
java.io.IOException
NameNotFoundException
Since:
26 No longer takes Context as param. EU must init(Context) first.