Browse Source

Fixed smaller issues.
More source code documentation.

lp-tu 10 years ago
parent
commit
b361a2a246

+ 0 - 7
src/de/tudarmstadt/informatik/hostage/HoneyListener.java

@@ -106,7 +106,6 @@ public class HoneyListener implements Runnable {
 	public void stop() {
 		try {
 			server.close();
-			stopHandler();
 			thread.interrupt();
 			editor.putBoolean(protocol + MainActivity.LISTENER, false);
 			editor.commit();
@@ -116,12 +115,6 @@ public class HoneyListener implements Runnable {
 			e.printStackTrace();
 		}
 	}
-	
-	private void stopHandler(){
-		for(AbstractHandler handler : handlers){
-			handler.kill();
-		}
-	}
 
 	/**
 	 * Determine the name of the protocol the listener is running on.

+ 41 - 1
src/de/tudarmstadt/informatik/hostage/handler/AbstractHandler.java

@@ -18,6 +18,9 @@ import de.tudarmstadt.informatik.hostage.protocol.Protocol;
 
 public abstract class AbstractHandler implements Runnable {
 
+	/**
+	 * Time until the socket throws a time out. The time is in milliseconds.
+	 */
 	protected static final int TIMEOUT = 30 * 1000;
 
 	protected Protocol protocol;
@@ -31,6 +34,15 @@ public abstract class AbstractHandler implements Runnable {
 	private HoneyListener listener;
 	protected Logger log;
 
+	/**
+	 * Constructor of the class.
+	 * Initializes class variables for communication and logging.
+	 * Then starts itself in a new Thread.
+	 * @param service The background service.
+	 * @param listener The Listener that called the service.
+	 * @param protocol The protocol on which the handler is running.
+	 * @param client A Socket for the communication with a remote client.
+	 */
 	public AbstractHandler(HoneyService service, HoneyListener listener,
 			Protocol protocol, Socket client) {
 		this.listener = listener;
@@ -38,7 +50,6 @@ public abstract class AbstractHandler implements Runnable {
 		this.protocol = protocol;
 		this.client = client;
 		this.thread = new Thread(this);
-		// TODO attack_id für jeden Logger
 		SharedPreferences pref = PreferenceManager
 				.getDefaultSharedPreferences(service);
 		Editor editor = pref.edit();
@@ -51,6 +62,11 @@ public abstract class AbstractHandler implements Runnable {
 		thread.start();
 	}
 
+	/**
+	 * Set the timeout of the socket to the hard coded time out variable.
+	 * @param client The socket
+	 * @see #TIMEOUT
+	 */
 	private void setSoTimeout(Socket client) {
 		try {
 			client.setSoTimeout(TIMEOUT);
@@ -59,6 +75,11 @@ public abstract class AbstractHandler implements Runnable {
 		}
 	}
 
+	/**
+	 * Creates InputStream and OutputStream for the socket.
+	 * Starts communication with client.
+	 * When the client closes the connection or a time out occurs the handler is finished.
+	 */
 	@Override
 	public void run() {
 		InputStream in;
@@ -73,6 +94,9 @@ public abstract class AbstractHandler implements Runnable {
 		kill();
 	}
 
+	/**
+	 * Sets the interrupt flag of the thread and tries to close the socket.
+	 */
 	public void kill() {
 		thread.interrupt();
 		try {
@@ -83,13 +107,29 @@ public abstract class AbstractHandler implements Runnable {
 		listener.refreshHandlers();
 	}
 
+	/**
+	 * Determines if the interrupt flag of the thread is set.
+	 * @return True when the flag is set, else false.
+	 */
 	public boolean isTerminated() {
 		return thread.isInterrupted();
 	}
 
+	/**
+	 * Communicates with a client using the corresponding protocol implementation.
+	 * @param in InputStream of the socket.
+	 * @param out OutputStream of the socket.
+	 * @throws IOException
+	 */
 	abstract protected void talkToClient(InputStream in, OutputStream out)
 			throws IOException;
 
+	/**
+	 * Creates a Record for a message exchanged with a client.
+	 * @param type The type of the message.
+	 * @param packet The content of the message.
+	 * @return The Record representing the communication message.
+	 */
 	protected Record createRecord(TYPE type, String packet) {
 		Record record = new Record();
 		record.setAttack_id(attack_id);

+ 17 - 2
src/de/tudarmstadt/informatik/hostage/io/ReaderWriter.java

@@ -2,11 +2,26 @@ package de.tudarmstadt.informatik.hostage.io;
 
 import java.io.IOException;
 import java.util.List;
-
+/**
+ * Interface for a generic class that offers methods for read and write.
+ * Is used to abstract the implementation details for String and Byte protocols.
+ * @author Mihai Plasoianu 
+ *
+ * @param <T>
+ */
 public interface ReaderWriter<T> {
-
+	/**
+	 * Method to read from a medium.
+	 * @return Returns the output.
+	 * @throws IOException
+	 */
 	T read() throws IOException;
 
+	/**
+	 * Method to write to a medium.
+	 * @param outputLine The input to write.
+	 * @throws IOException
+	 */
 	void write(List<T> outputLine) throws IOException;
 
 }

+ 90 - 18
src/de/tudarmstadt/informatik/hostage/logging/DatabaseHandler.java

@@ -10,7 +10,15 @@ import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;
-
+/**
+ * This class creates SQL tables and handles all access to the database.<br>
+ * It contains several methods with predefined queries to extract different kinds of information from the database.<br>
+ * The database contains two tables: {@link #TABLE_RECORDS} and {@link #TABLE_BSSIDS}:<br>
+ * {@link #TABLE_RECORDS} contains all logging information of a single message record except the SSID.<br>
+ * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the corresponding SSID.<br>
+ * @author Lars Pandikow
+ *
+ */
 public class DatabaseHandler extends SQLiteOpenHelper {
 
 	// All Static variables
@@ -74,7 +82,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		onCreate(db);
 	}
 
-	// Adding new record
+	/**
+	 * Adds a given record to the database.
+	 * @param record The added record.
+	 */
 	public void addRecord(Record record) {
 		SQLiteDatabase db = this.getWritableDatabase();
 
@@ -95,8 +106,6 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		recordValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
 		recordValues.put(KEY_BSSID, record.getBSSID());
 		recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
-		
-
 
 		// Inserting Rows
 		db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
@@ -104,6 +113,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		db.close(); // Closing database connection
 	}
 	
+	/**
+	 * Creates a {@link Record} from a {@link Cursor}. If the cursor does not show to a valid data structure a runtime exception is thrown.
+	 * @param cursor
+	 * @return Returns the created Record.
+	 */
 	private Record createRecord(Cursor cursor){
 		Record record = new Record();
 		try {
@@ -126,7 +140,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		return record;
 	}
 
-	// Getting single record
+	/**
+	 * Gets a single {@link Record} with the given ID from the database.
+	 * @param id The ID of the {@link Record};
+	 * @return The {@link Record}.
+	 */
 	public Record getRecord(int id) {
 		String selectQuery = "SELECT  * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_ID + " = " + id;
 		SQLiteDatabase db = this.getReadableDatabase();
@@ -135,7 +153,6 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		Record record = null;
 		if (cursor.moveToFirst()){
 			record = createRecord(cursor);
-			Log.i("DatabaseHandler", record == null ? "null" : "not null");
 		} 
 		
         cursor.close();
@@ -144,7 +161,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		return record;
 	}
 
-	// Getting All Records
+	/**
+	 * Gets all {@link Record Records} saved in the database. 
+	 * @return A ArrayList of all the {@link Record Records} in the Database.
+	 */
 	public ArrayList<Record> getAllRecords() {
 		ArrayList<Record> recordList = new ArrayList<Record>();
 		// Select All Query
@@ -167,7 +187,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		return recordList;
 	}
     
-	// Getting first Record for each AttackId greater than a given id
+	/**
+	 * Gets a single {@link Record} with the given attack id from the database.
+	 * @param attack_id The attack id of the {@link Record};
+	 * @return The {@link Record}.
+	 */
     public Record getRecordOfAttackId(long attack_id) {
         String selectQuery = "SELECT  * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -179,12 +203,15 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		}
         cursor.close();
  
-        // return count
+        // return record list
         db.close();
         return record;
     }
     
-	// Getting first Record for each AttackId
+	/**
+	 * Gets a representative {@link Record} for every attack identified by its attack id.
+	 * @return A ArrayList with one {@link Record Records} for each attack id in the Database.
+	 */
     public ArrayList<Record> getRecordOfEachAttack() {
     	ArrayList<Record> recordList = new ArrayList<Record>();
         String selectQuery = "SELECT  * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " GROUP BY " + KEY_ATTACK_ID;
@@ -201,12 +228,16 @@ public class DatabaseHandler extends SQLiteOpenHelper {
 		}       
         cursor.close();
  
-        // return count
+        // return record list
         db.close();
         return recordList;
     }
     
-	// Getting first Record for each AttackId greater than a given id
+	/**
+	 * Gets a representative {@link Record} for every attack with a higher attack id than the specified.
+	 * @param attack_id The attack id to match the query against.
+	 * @return A ArrayList with one {@link Record Records} for each attack id higher than the given.
+	 */
     public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
     	ArrayList<Record> recordList = new ArrayList<Record>();
         String selectQuery = "SELECT  * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_BSSIDS + " WHERE " + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
@@ -228,7 +259,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return recordList;
     }
 	
-	// Getting record Count
+	/**
+	 * Determines the number of {@link Record Records} in the database.
+	 * @return The number of {@link Record Records} in the database.
+	 */
     public int getRecordCount() {
         String countQuery = "SELECT  * FROM " + TABLE_RECORDS;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -241,7 +275,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return result;
     }
     
-	// Getting record Count
+	/**
+	 * Determines the number of different attack_ids in the database.
+	 * @return The number of different attack_ids in the database.
+	 */
     public int getAttackCount() {
         String countQuery = "SELECT  * FROM " + TABLE_RECORDS + " GROUP BY " + KEY_ATTACK_ID;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -254,7 +291,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return result;
     }
     
-	// Getting record Count
+	/**
+	 * Determines the number of different attack_ids for a specific protocol in the database.
+	 * @param protocol The String representation of the {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol}
+	 * @return The number of different attack_ids in the database.
+	 */
     public int getAttackPerProtokolCount(String protocol) {
         String countQuery = "SELECT  * FROM " + TABLE_RECORDS + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " GROUP BY " + KEY_ATTACK_ID;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -267,7 +308,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return result;
     }
     
-    //Get the smallest AttackId
+   /**
+    * Determines the smallest attack id stored in the database.
+    * @return The smallest attack id stored in the database.
+    */
     public long getSmallestAttackId(){
     	String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID +") FROM " + TABLE_RECORDS;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -284,7 +328,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
     	return result;
     }
     
-    //Get the smallest AttackId
+    /**
+     * Determines the highest attack id stored in the database.
+     * @return The highest attack id stored in the database.
+     */
     public long getHighestAttackId(){
     	String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID +") FROM " + TABLE_RECORDS;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -301,6 +348,12 @@ public class DatabaseHandler extends SQLiteOpenHelper {
     	return result;
     }
    
+    /**
+     * Determines if an attack has been recorded on a specific protocol in a network with a given BSSID.
+     * @param protocol The {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol} to inspect.
+     * @param BSSID The BSSID of the network.
+     * @return True if an attack on the given protocol has been recorded in a network with the given BSSID, else false.
+     */
     public boolean bssidSeen(String protocol, String BSSID){
         String countQuery = "SELECT  * FROM " + TABLE_RECORDS + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = " + "'" + BSSID + "'";
         SQLiteDatabase db = this.getReadableDatabase();
@@ -311,6 +364,10 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return result > 0;        
     }
     
+    /**
+     * Returns a String array with all BSSIDs stored in the database.
+     * @return String[] of all recorded BSSIDs.
+     */
     public String[] getAllBSSIDS(){
         String selectQuery = "SELECT  * FROM " + TABLE_BSSIDS;
         SQLiteDatabase db = this.getReadableDatabase();
@@ -329,6 +386,11 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return bssidList;
     }
     
+    /**
+     * Gets the last recorded SSID to a given BSSID.
+     * @param bssid The BSSID to match against.
+     * @return A String of the last SSID or null if the BSSID is not in the database.
+     */
     public String getSSID(String bssid){
     	String selectQuery = "SELECT "+ KEY_SSID +" FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
     	SQLiteDatabase db = this.getReadableDatabase();
@@ -342,12 +404,20 @@ public class DatabaseHandler extends SQLiteOpenHelper {
         return ssid;
     }
     
+    /**
+     * Deletes all records from {@link TABLE_RECORDS} with a specific BSSID.
+     * @param bssid The BSSID to match against.
+     */
     public void deleteByBSSID(String bssid){
     	SQLiteDatabase db = this.getReadableDatabase();
     	db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[]{bssid});
     	db.close();
     }
     
+    /**
+     * Deletes all records from {@link TABLE_RECORDS} with a time stamp smaller then the given
+     * @param date A Date represented in milliseconds.
+     */
     public void deleteByDate(long date){
     	SQLiteDatabase db = this.getReadableDatabase();
     	String deleteQuery = "DELETE  FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
@@ -355,7 +425,9 @@ public class DatabaseHandler extends SQLiteOpenHelper {
     	db.close();
     }
     
-    //Delete all Data from Database
+    /**
+     *  Deletes all records from {@link TABLE_RECORDS}.
+     */
     public void clearData(){
     	SQLiteDatabase db = this.getReadableDatabase();
         db.delete(TABLE_RECORDS, null, null);

+ 55 - 32
src/de/tudarmstadt/informatik/hostage/wrapper/ByteArray.java

@@ -1,32 +1,55 @@
-package de.tudarmstadt.informatik.hostage.wrapper;
-
-public class ByteArray {
-
-	private final byte[] array;
-
-	public ByteArray() {
-		this.array = null;
-	}
-
-	public ByteArray(byte[] array) {
-		this.array = array;
-	}
-	
-	public ByteArray(String string) {
-		this.array = string.getBytes();
-	}
-
-	public byte[] get() {
-		return array;
-	}
-
-	public int size() {
-		return array.length;
-	}
-
-	@Override
-	public String toString() {
-		return new String(array);
-	}
-
-}
+package de.tudarmstadt.informatik.hostage.wrapper;
+/**
+ * Wrapper class to use a byte array as an Object.
+ * @author Mihai Plasoianu 
+ *
+ */
+public class ByteArray {
+
+	private final byte[] array;
+
+	/**
+	 * Constructor without parameters. Sets the internal array to null.
+	 */
+	public ByteArray() {
+		this.array = null;
+	}
+
+	/**
+	 * Constructor with a byte arrays as parameter. Sets the internal array to the given.
+	 * @param array The byte array that should be wrapped.
+	 */
+	public ByteArray(byte[] array) {
+		this.array = array;
+	}
+	
+	/**
+	 * Constructor with a String as parameter. Sets the internal array to a byte array representation of the string.
+	 * @param string
+	 */
+	public ByteArray(String string) {
+		this.array = string.getBytes();
+	}
+
+	/**
+	 * Returns the byte array.
+	 * @return The byte array
+	 */
+	public byte[] get() {
+		return array;
+	}
+
+	/**
+	 * Determines size of the array.
+	 * @return Size of the array
+	 */
+	public int size() {
+		return array.length;
+	}
+
+	@Override
+	public String toString() {
+		return new String(array);
+	}
+
+}