|
@@ -1,5 +1,7 @@
|
|
|
package de.tudarmstadt.informatik.hostage.persistence;
|
|
|
|
|
|
+import java.lang.reflect.Array;
|
|
|
+import java.sql.SQLInput;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedList;
|
|
@@ -262,6 +264,11 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
}
|
|
|
db.close();
|
|
|
}
|
|
|
+ public void insertMessageRecords(List<MessageRecord> records, SQLiteDatabase db){
|
|
|
+ for (MessageRecord record : records){
|
|
|
+ this.insertMessageRecordWithOnConflict(record,db);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
private void insertMessageRecordWithOnConflict(MessageRecord record, SQLiteDatabase db){
|
|
|
ContentValues recordValues = new ContentValues();
|
|
@@ -350,7 +357,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
try {
|
|
|
for (SyncRecord record : records){
|
|
|
this.insertAttackRecordWithOnConflict(record.getAttackRecord(), db);
|
|
|
- this.insertMessageRecordWithOnConflict(record.getMessageRecord(), db);
|
|
|
+ this.insertMessageRecords(record.getMessageRecords(), db);
|
|
|
}
|
|
|
db.setTransactionSuccessful();
|
|
|
} finally {
|
|
@@ -1012,12 +1019,21 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates sync devices
|
|
|
+ * @param devices array list of sync devices
|
|
|
+ */
|
|
|
public synchronized void updateSyncDevices(ArrayList<SyncDevice> devices){
|
|
|
SQLiteDatabase db = this.getWritableDatabase();
|
|
|
this.updateSyncDevices(devices, db);
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates sync devices.
|
|
|
+ * @param devices array list of sync devices
|
|
|
+ * @param db sqlite database
|
|
|
+ */
|
|
|
public synchronized void updateSyncDevices(ArrayList<SyncDevice> devices, SQLiteDatabase db){
|
|
|
db.beginTransaction();
|
|
|
|
|
@@ -1232,6 +1248,31 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
db.close();
|
|
|
}
|
|
|
|
|
|
+ public ArrayList<MessageRecord> getMessageRecords(AttackRecord attackRecord , SQLiteDatabase db){
|
|
|
+ ArrayList<MessageRecord> mr = new ArrayList<MessageRecord>();
|
|
|
+
|
|
|
+ String selectQuery = "SELECT * FROM " + PacketEntry.TABLE_NAME + " WHERE " + PacketEntry.COLUMN_NAME_ATTACK_ID + " = " + attackRecord.getAttack_id();
|
|
|
+
|
|
|
+ boolean createdDB = false;
|
|
|
+ if (db == null){
|
|
|
+ db = this.getReadableDatabase();
|
|
|
+ createdDB = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ Cursor cursor = db.rawQuery(selectQuery, null);
|
|
|
+
|
|
|
+ if (cursor.moveToFirst()) {
|
|
|
+ do {
|
|
|
+ MessageRecord record = createMessageRecord(cursor);
|
|
|
+ mr.add(record);
|
|
|
+ } while (cursor.moveToNext());
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ if (createdDB) db.close();
|
|
|
+ return mr;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Creates a {@link MessageRecord} from a Cursor. If the cursor does not show to a
|
|
|
* valid data structure a runtime exception is thrown.
|
|
@@ -1280,31 +1321,12 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
* @param cursor
|
|
|
* @return Returns the created {@link SyncRecord} .
|
|
|
*/
|
|
|
- private synchronized SyncRecord createSyncRecord(Cursor cursor){
|
|
|
- SyncRecord record = new SyncRecord();
|
|
|
- record.setAttack_id(cursor.getLong(0));
|
|
|
- record.setProtocol(cursor.getString(1));
|
|
|
- record.setExternalIP(cursor.getString(2));
|
|
|
- record.setLocalIP(cursor.getString(3));
|
|
|
- record.setLocalPort(Integer.parseInt(cursor.getString(4)));
|
|
|
- record.setRemoteIP(cursor.getString(5));
|
|
|
- record.setRemotePort(Integer.parseInt(cursor.getString(6)));
|
|
|
- record.setWasInternalAttack(cursor.getInt(7) == 1);
|
|
|
- record.setBssid(cursor.getString(8));
|
|
|
- record.setSync_id(cursor.getLong(9));
|
|
|
- record.setDevice(cursor.getString(10));
|
|
|
+ private synchronized SyncRecord createSyncRecord(Cursor cursor, SQLiteDatabase db){
|
|
|
+ AttackRecord attackRecord = this.createAttackRecord(cursor);
|
|
|
+ SyncRecord record = new SyncRecord(attackRecord);
|
|
|
|
|
|
- record.setId(Integer.parseInt(cursor.getString(11)));
|
|
|
- //record.setAttack_id(cursor.getLong(12));
|
|
|
- String v = cursor.getString(13);
|
|
|
- if (v != null && !v.equals("RECEIVE")) {
|
|
|
- record.setType(TYPE.SEND);
|
|
|
- //record.setType(MessageRecord.TYPE.valueOf(v));
|
|
|
- } else {
|
|
|
- record.setType(TYPE.RECEIVE);
|
|
|
- }
|
|
|
- record.setTimestamp(cursor.getLong(14));
|
|
|
- record.setPacket(cursor.getString(15));
|
|
|
+ ArrayList<MessageRecord> mr = this.getMessageRecords(attackRecord,db);
|
|
|
+ record.setMessageRecords(mr);
|
|
|
|
|
|
return record;
|
|
|
}
|
|
@@ -1965,10 +1987,9 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
for (SyncDevice sDevice : updatedDevices){
|
|
|
String deviceID = sDevice.getDeviceID();
|
|
|
Long maxID = deviceMap.get(deviceID);
|
|
|
- String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A " + " , " + PacketEntry.TABLE_NAME + " P "
|
|
|
+ String selectQuery = "SELECT * FROM " + AttackEntry.TABLE_NAME + " A "
|
|
|
+ " WHERE "
|
|
|
+" ( "
|
|
|
- + "A." + AttackEntry.COLUMN_NAME_ATTACK_ID + " = " + " P."+ PacketEntry.COLUMN_NAME_ATTACK_ID
|
|
|
+ " AND " + " A." + AttackEntry.COLUMN_NAME_DEVICE + " = " + "'" + deviceID + "'"
|
|
|
+ " AND " + " A." + AttackEntry.COLUMN_NAME_SYNC_ID + " > " + maxID
|
|
|
+ " ) "
|
|
@@ -1980,7 +2001,7 @@ public class HostageDBOpenHelper extends SQLiteOpenHelper {
|
|
|
if (cursor != null){
|
|
|
if (cursor.moveToFirst()) {
|
|
|
do {
|
|
|
- SyncRecord record = createSyncRecord(cursor);
|
|
|
+ SyncRecord record = createSyncRecord(cursor , db);
|
|
|
recordList.add(record);
|
|
|
} while (cursor.moveToNext());
|
|
|
}
|