|
@@ -304,6 +304,56 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
this.updateSyncDevices(devices);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates the own devices and connects attack records without a device to the own device
|
|
|
+ */
|
|
|
+ synchronized public void updateUntrackedAttacks(){
|
|
|
+ SQLiteDatabase db = this.getWritableDatabase();
|
|
|
+ String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A WHERE " + AttackEntry.COLUMN_NAME_DEVICE + " IS NULL ORDER BY " + AttackEntry.COLUMN_NAME_ATTACK_ID + " DESC";
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
+
|
|
|
+ SyncDevice ownDevice = currentDevice();
|
|
|
+
|
|
|
+ long highestID = ownDevice.getHighest_attack_id();
|
|
|
+
|
|
|
+ ArrayList<AttackRecord> records = new ArrayList<AttackRecord>();
|
|
|
+
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ AttackRecord record = this.createAttackRecord(cursor);
|
|
|
+ record.setDevice(ownDevice.getDeviceID());
|
|
|
+ highestID = (highestID > record.getAttack_id())? highestID : record.getAttack_id();
|
|
|
+ records.add(record);
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+ ownDevice.setHighest_attack_id(highestID);
|
|
|
+
|
|
|
+
|
|
|
+ // UPDATE RECORDS
|
|
|
+ if (records.size() > 0){
|
|
|
+ db.beginTransaction();
|
|
|
+ try {
|
|
|
+ for (AttackRecord record : records){
|
|
|
+ this.insertAttackRecordWithOnConflict(record,db);
|
|
|
+ }
|
|
|
+ db.setTransactionSuccessful();
|
|
|
+ } finally {
|
|
|
+ db.endTransaction();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (highestID != ownDevice.getHighest_attack_id()){
|
|
|
+ // THERE WERE ATTACKS WITHOUT A DEVICE ID
|
|
|
+ ArrayList<SyncDevice> devices = new ArrayList<SyncDevice>();
|
|
|
+ devices.add(ownDevice);
|
|
|
+ updateSyncDevices(devices);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ db.close();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Adds a given {@link AttackRecord}s to the database.
|
|
|
*
|
|
@@ -421,31 +471,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_ID, currentDevice.getDeviceID());
|
|
|
syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_DEVICE_TIMESTAMP, System.currentTimeMillis());
|
|
|
syncDeviceValues.put(SyncDeviceEntry.COLUMN_NAME_HIGHEST_ATTACK_ID, record.getAttack_id());
|
|
|
- //String query = "SELECT * FROM " + SyncInfoEntry.TABLE_NAME +
|
|
|
- // " WHERE " + SyncInfoEntry.COLUMN_NAME_DEVICE_ID + " = ? " +
|
|
|
- // "AND " + SyncInfoEntry.COLUMN_NAME_BSSID + " = ?";
|
|
|
- //Cursor cursor = db.rawQuery(query, new String[] {mac, record.getBssid()});
|
|
|
- //long attackCount = 0;
|
|
|
- //long portscanCount = 0;
|
|
|
- //if (cursor.moveToFirst()){
|
|
|
- // attackCount = cursor.getLong(2);
|
|
|
- // portscanCount = cursor.getLong(3);
|
|
|
- //}
|
|
|
-
|
|
|
- //if("PORTSCAN".equals(record.getProtocol())){
|
|
|
- // portscanCount++;
|
|
|
- //}else { attackCount++; }
|
|
|
-
|
|
|
- //Log.i("DBHelper", "Update number of attack: " + attackCount);
|
|
|
-
|
|
|
- //ContentValues synInfoValues = new ContentValues();
|
|
|
- //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_BSSID, record.getBssid());
|
|
|
- //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_DEVICE_ID, mac);
|
|
|
- //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_ATTACKS, attackCount);
|
|
|
- //synInfoValues.put(SyncInfoEntry.COLUMN_NAME_NUMBER_PORTSCANS, portscanCount);
|
|
|
-
|
|
|
- // Inserting Rows
|
|
|
- //db.insertWithOnConflict(SyncInfoEntry.TABLE_NAME, null, synInfoValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
+
|
|
|
db.insertWithOnConflict(SyncDeviceEntry.TABLE_NAME, null, syncDeviceValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
|
db.close(); // Closing database connection
|
|
|
}
|
|
@@ -1064,6 +1090,8 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @return {@link de.tudarmstadt.informatik.hostage.logging.SyncInfo}
|
|
|
*/
|
|
|
public synchronized SyncInfo getOwnState(){
|
|
|
+ updateUntrackedAttacks();
|
|
|
+
|
|
|
ArrayList<SyncDevice> devices = this.getSyncDevices();
|
|
|
|
|
|
HashMap<String, Long> deviceMap = new HashMap<String, Long>();
|
|
@@ -2004,6 +2032,9 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
|
|
|
SQLiteDatabase db = this.getReadableDatabase();
|
|
|
|
|
|
+ SyncDevice currentDevice = currentDevice();
|
|
|
+ String own_device_id = currentDevice.getDeviceID();
|
|
|
+
|
|
|
for (SyncDevice sDevice : updatedDevices){
|
|
|
String deviceID = sDevice.getDeviceID();
|
|
|
Long maxID = deviceMap.get(deviceID);
|