Browse Source

Added current sync device;
Current sync device uuid will be updated on database lose;
All local attacks refer to the current device;

Julien Clauter 9 years ago
parent
commit
57fdf26345

+ 3 - 1
src/de/tudarmstadt/informatik/hostage/Handler.java

@@ -20,6 +20,7 @@ import de.tudarmstadt.informatik.hostage.logging.Logger;
 import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
 import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
 import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
+import de.tudarmstadt.informatik.hostage.logging.SyncDevice;
 import de.tudarmstadt.informatik.hostage.nio.Reader;
 import de.tudarmstadt.informatik.hostage.nio.Writer;
 import de.tudarmstadt.informatik.hostage.protocol.GHOST;
@@ -210,7 +211,8 @@ public class Handler implements Runnable {
 		AttackRecord record = new AttackRecord();
 		record.setAttack_id(attack_id);
         record.setSync_id(attack_id);
-        //TODO SET CURRENT DEVICE UUID
+        record.setDevice(SyncDevice.currentDevice().getDeviceID());
+
 		record.setProtocol(protocol.toString());
 		record.setExternalIP(externalIP);
 		record.setLocalIP(client.getLocalAddress().getHostAddress());

+ 2 - 9
src/de/tudarmstadt/informatik/hostage/Listener.java

@@ -263,16 +263,9 @@ public class Listener implements Runnable {
 	private void logPortscan(Socket client, long timestamp){
 		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
 		SharedPreferences connInfo = service.getSharedPreferences(service.getString(R.string.connection_info), Context.MODE_PRIVATE);
-
-		Editor editor = pref.edit();
-		int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
-		editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
-		editor.commit();
 		
-		AttackRecord attackRecord = new AttackRecord();
-		attackRecord.setAttack_id(attack_id);
-        attackRecord.setSync_id(attack_id);
-        //TODO SET DEVICE UUID
+		AttackRecord attackRecord = new AttackRecord(true);
+
 		attackRecord.setProtocol("PORTSCAN");
 		attackRecord.setExternalIP(connInfo.getString(service.getString(R.string.connection_info_external_ip), null));
 		attackRecord.setLocalIP(client.getLocalAddress().getHostAddress());

+ 22 - 0
src/de/tudarmstadt/informatik/hostage/logging/AttackRecord.java

@@ -2,8 +2,13 @@ package de.tudarmstadt.informatik.hostage.logging;
 
 import java.io.Serializable;
 
+import android.content.SharedPreferences;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.preference.PreferenceManager;
+
+import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
 
 /**
  * Holds all necessary information about a single attack.
@@ -54,6 +59,23 @@ public class AttackRecord implements Parcelable, Serializable {
         this.sync_id = source.readLong();
 	}
 
+    public AttackRecord(boolean autoincrement){
+        if (autoincrement){
+            SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());
+
+            SharedPreferences.Editor editor = pref.edit();
+            int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
+            editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
+            editor.commit();
+            this.attack_id = attack_id;
+            this.sync_id = attack_id;
+
+            SyncDevice currentDevice = SyncDevice.currentDevice();
+            this.setDevice(currentDevice.getDeviceID());
+        }
+
+    }
+
 	@Override
 	public int describeContents() {
 		return 0;

+ 68 - 0
src/de/tudarmstadt/informatik/hostage/logging/SyncDevice.java

@@ -1,5 +1,18 @@
 package de.tudarmstadt.informatik.hostage.logging;
 
+import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.preference.PreferenceManager;
+
+import java.util.ArrayList;
+import java.util.UUID;
+
+import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract;
+import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
+
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
+
 /**
  * Created by Julien on 04.12.2014.
  */
@@ -29,4 +42,59 @@ public class SyncDevice {
 
     public void setLast_sync_timestamp(long t){this.last_sync_timestamp = t;}
     public long getLast_sync_timestamp(){return this.last_sync_timestamp;}
+
+
+    public static  SyncDevice thisDevice = null;
+    /**
+     * Returns a SyncDevice Object representing the current device.
+     * @return {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice}
+     */
+    public static SyncDevice currentDevice()
+    {
+        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());
+        int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
+
+        // IF THE SHARED INSTANCE IS NOT AVAILABLE GET IT
+        if (thisDevice == null){
+            String deviceUUID = pref.getString("CURRENT_DEVICE_IDENTIFIER", UUID.randomUUID().toString());
+
+            String selectQuery = "SELECT * FROM " + HostageDBContract.SyncDeviceEntry.TABLE_NAME + " D "
+                    + " WHERE " + " d." + HostageDBContract.SyncDeviceEntry.COLUMN_NAME_DEVICE_ID + " = " + deviceUUID;
+            HostageDBOpenHelper dbh = new HostageDBOpenHelper(MainActivity.context);
+
+            SQLiteDatabase db = dbh.getReadableDatabase();
+            Cursor cursor = db.rawQuery(selectQuery, null);
+
+            // IF WE ALREADY HAVE A SYNC DEVICE FOR THE GIVEN DEVICE UUID
+            if (cursor.moveToFirst()) {
+                SyncDevice record = new SyncDevice();
+                record.setDeviceID(cursor.getString(0));
+                record.setLast_sync_timestamp(cursor.getLong(1));
+                record.setHighest_attack_id(cursor.getLong(2));
+                thisDevice = record;
+
+            } else {
+            // CREATE A NEW SYNC DEVICE
+                thisDevice = new SyncDevice();
+                // ITS IMPORTANT TO CREATE A COMPLETE NEW DEVICE UUID
+                deviceUUID = UUID.randomUUID().toString();
+                thisDevice.setDeviceID(deviceUUID);
+                SharedPreferences.Editor editor = pref.edit();
+                editor.putString(thisDevice.getDeviceID(), thisDevice.getDeviceID());
+                editor.commit();
+                thisDevice.setLast_sync_timestamp(0);
+                thisDevice.setHighest_attack_id(-1);
+                ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
+                devices.add(thisDevice);
+                dbh.insertSyncDevices(devices);
+            }
+            cursor.close();
+
+            // return record list
+            db.close();
+        }
+
+        thisDevice.setHighest_attack_id(attack_id - 1);
+        return thisDevice;
+    }
 }

+ 44 - 15
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -7,12 +7,15 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.UUID;
 
 import android.content.ContentValues;
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
+import android.preference.PreferenceManager;
 import android.util.Log;
 import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
 import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
@@ -29,6 +32,7 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEnt
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.ProfileEntry;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry;
 import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncInfoEntry;
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
 import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;
 import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
@@ -191,7 +195,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	 *            The added {@link AttackRecord} .
 	 */
 	public void addAttackRecord(AttackRecord record) {
-		Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
+		//Log.i("DBHelper", "Add Attack Record with id: " + record.getAttack_id());
 		SQLiteDatabase db = this.getWritableDatabase();
 
 		ContentValues attackValues = new ContentValues();
@@ -214,7 +218,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
     /**
      * Adds a given {@link AttackRecord}s to the database.
      *
-     * @param List<AttackRecord>
+     * @param {@link List}<AttackRecord>
      *            The added {@link AttackRecord}s .
      */
     public void insertAttackRecords(List<AttackRecord> records) {
@@ -865,7 +869,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	
 	/**
 	 * Updates the sync_info table with the information contained in the parameter.
-	 * @param syncInfo ArrayList of {@link SyncInfoRecord SyncInfoRecords}
+	 * @param syncInfos ArrayList of {@link SyncInfoRecord SyncInfoRecords}
 	 * @see  {@link HostageDBOpenHelper#updateSyncInfo(SyncInfoRecord syncInfo)}
 	 */
 	public synchronized void updateSyncInfo(ArrayList<SyncInfoRecord> syncInfos){
@@ -907,7 +911,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	}
 	
 	/**
-	 * Deletes a device with given id from the device {@link SyncDeviceEntry.TABLE_NAME} and also all data captured by this device in  {@link SyncInfoEntry.TABLE_NAME}
+	 * Deletes a device with given id from the device {@link de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.SyncDeviceEntry} and also all data captured by this device in  {@link SyncInfoEntry}
 	 */
 	public void clearSyncInfos(){
 		SQLiteDatabase db = this.getReadableDatabase();
@@ -918,7 +922,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	
 	
 	/**
-	 * Deletes all records from {@link #PacketEntry.TABLE_NAME}.
+	 * Deletes all records from {@link PacketEntry}s and {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord}.
 	 */
 	public void clearData() {
 		SQLiteDatabase db = this.getReadableDatabase();
@@ -928,7 +932,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	}
 
 	/**
-	 * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a specific BSSID.
+	 * Deletes all records from {@link PacketEntry}s with a specific BSSID.
 	 * 
 	 * @param bssid
 	 *            The BSSID to match against.
@@ -941,7 +945,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	}
 	
 	/**
-	 * Deletes all records from {@link #PacketEntry.TABLE_NAME} with a time stamp smaller
+	 * Deletes all records from {@link de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry}s with a time stamp smaller
 	 * then the given
 	 * 
 	 * @param date
@@ -955,7 +959,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 	}
 	
 	/**
-	 * Deletes all records from {@link #TABLE_RECORDS} with a specific Attack ID.
+	 * Deletes all {@link de.tudarmstadt.informatik.hostage.logging.AttackRecord} with a specific Attack ID.
 	 *
 	 * @param attackID
 	 *            The Attack ID to match against.
@@ -1502,7 +1506,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
      * Creates a {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} from a Cursor. If the cursor does not show to a
      * valid data structure a runtime exception is thrown.
      *
-     * @param cursor
+     * @param cursor the cursor
      * @return Returns the created {@link de.tudarmstadt.informatik.hostage.logging.SyncDevice} .
      */
     private synchronized SyncDevice createSyncDevice(Cursor cursor) {
@@ -1583,13 +1587,15 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
             Cursor cursor = db.rawQuery(selectQuery, null);
 
             // looping through all rows and adding to list
-            if (cursor != null &&  cursor.moveToFirst()) {
-                do {
-                    AttackRecord record = createAttackRecord(cursor);
-                    recordList.add(record);
-                } while (cursor.moveToNext());
+            if (cursor != null){
+                if (cursor.moveToFirst()) {
+                    do {
+                        AttackRecord record = createAttackRecord(cursor);
+                        recordList.add(record);
+                    } while (cursor.moveToNext());
+                }
+                cursor.close();
             }
-            cursor.close();
         }
 
         // return record list
@@ -1637,6 +1643,29 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
 
     }
 
+    public void insertSyncDevices(List<SyncDevice> devices){
+        SQLiteDatabase db = this.getWritableDatabase();
+
+        db.beginTransaction();
+
+        try {
+            for (SyncDevice device : devices){
+                ContentValues recordValues = new ContentValues();
+                recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, device.getDeviceID());
+                recordValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, device.getLast_sync_timestamp());
+                recordValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, device.getHighest_attack_id());
+
+                // Inserting Rows
+                db.insert(SyncDeviceEntry.TABLE_NAME, null, recordValues);
+            }
+            db.setTransactionSuccessful();
+        } finally {
+            db.endTransaction();
+        }
+
+        db.close(); // Closing database connection
+    }
+
 
     /** Returns the color for the given index
      * @return int color*/

+ 1 - 9
src/de/tudarmstadt/informatik/hostage/ui/fragment/RecordOverviewFragment.java

@@ -1381,15 +1381,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
                 if (maxMessagePerAttack <= 0) numRecordsPerAttack = 0;
 
                 /* ADD A ATTACK*/
-                AttackRecord attack = new AttackRecord();
-
-                SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());
-
-                SharedPreferences.Editor editor = pref.edit();
-                int attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
-                editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
-                editor.commit();
-                //attack.setAttack_id(attackId);
+                AttackRecord attack = new AttackRecord(true);
 
                 attack.setBssid(bssidName);