Prechádzať zdrojové kódy

added message records to syncrecord

Julien Clauter 10 rokov pred
rodič
commit
b16c07915e

+ 41 - 77
src/de/tudarmstadt/informatik/hostage/logging/SyncRecord.java

@@ -4,6 +4,7 @@ package de.tudarmstadt.informatik.hostage.logging;
  * Created by Julien on 08.12.2014.
  */
 
+import android.os.Message;
 import android.os.Parcel;
 import android.os.Parcelable;
 
@@ -13,8 +14,12 @@ import android.os.Parcel;
 import android.os.Parcelable;
 
 import java.io.Serializable;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.zip.Deflater;
 
+import de.tudarmstadt.informatik.hostage.wrapper.Packet;
+
 public class SyncRecord implements Parcelable, Serializable {
 
     private static final long serialVersionUID = 7106818788090434192L;
@@ -33,10 +38,12 @@ public class SyncRecord implements Parcelable, Serializable {
     private int wasInternalAttack; // 1 if attacker ip and local ip were in same subnet, else 0
 
     // attack
-    private int id;
-    private long timestamp;
-    private MessageRecord.TYPE type;
-    private String packet;
+    //private int id;
+    //private long timestamp;
+    //private MessageRecord.TYPE type;
+    //private String packet;
+
+    private ArrayList<MessageRecord> messageRecords;
 
     public static final Parcelable.Creator<SyncRecord> CREATOR = new Parcelable.Creator<SyncRecord>() {
         @Override
@@ -54,6 +61,19 @@ public class SyncRecord implements Parcelable, Serializable {
 
     }
 
+    public SyncRecord(AttackRecord attackRecord){
+        this.setAttack_id(attackRecord.getAttack_id());
+        this.setProtocol(attackRecord.getProtocol());
+        this.setExternalIP(attackRecord.getExternalIP());
+        this.setLocalPort(attackRecord.getLocalPort());
+        this.setRemoteIP(attackRecord.getRemoteIP());
+        this.setRemotePort(attackRecord.getRemotePort());
+        this.setWasInternalAttack(attackRecord.getWasInternalAttack());
+        this.setBssid(attackRecord.getBssid());
+        this.setSync_id(attackRecord.getSync_id());
+        this.setDevice(attackRecord.getDevice());
+    }
+
     public SyncRecord(Parcel source) {
         super();
         this.attack_id = source.readLong();
@@ -68,10 +88,12 @@ public class SyncRecord implements Parcelable, Serializable {
         this.device = source.readString();
         this.sync_id = source.readLong();
 
-        this.id = source.readInt();
-        this.timestamp = source.readLong();
-        this.type = MessageRecord.TYPE.valueOf(source.readString());
-        this.packet = source.readString();
+        this.messageRecords = source.readArrayList(MessageRecord.class.getClassLoader());
+
+        //this.id = source.readInt();
+        //this.timestamp = source.readLong();
+        //this.type = MessageRecord.TYPE.valueOf(source.readString());
+        //this.packet = source.readString();
     }
 
     @Override
@@ -93,10 +115,11 @@ public class SyncRecord implements Parcelable, Serializable {
         dest.writeString(device);
         dest.writeLong(sync_id);
 
-        dest.writeInt(id);
-        dest.writeLong(timestamp);
-        dest.writeString(type.name());
-        dest.writeString(packet);
+        dest.writeList(this.messageRecords);
+        //dest.writeInt(id);
+        //dest.writeLong(timestamp);
+        //dest.writeString(type.name());
+        //dest.writeString(packet);
     }
 
 
@@ -122,13 +145,12 @@ public class SyncRecord implements Parcelable, Serializable {
         return record;
     }
 
-    public MessageRecord getMessageRecord(){
-        MessageRecord record = new MessageRecord(true);
-        record.setAttack_id(this.attack_id);
-        record.setPacket(this.packet);
-        record.setTimestamp(this.timestamp);
-        record.setType(this.type);
-        return record;
+    public ArrayList<MessageRecord> getMessageRecords(){
+        return this.messageRecords;
+    }
+
+    public void setMessageRecords(ArrayList<MessageRecord> mr){
+        this.messageRecords = mr;
     }
 
     /**
@@ -259,64 +281,6 @@ public class SyncRecord implements Parcelable, Serializable {
     public void setWasInternalAttack(boolean b) {wasInternalAttack = b ? 1 : 0;}
 
 
-
-    /*
-    *
-    *
-    *  MESSAGE RECORD
-    *
-    *
-    */
-    /**
-     * @return the id
-     */
-    public int getId() {
-        return id;
-    }
-    /**
-     * @param id the id to set
-     */
-    public void setId(int id) {
-        this.id = id;
-    }
-
-    /**
-     * @return the timestamp
-     */
-    public long getTimestamp() {
-        return timestamp;
-    }
-    /**
-     * @param timestamp the timestamp to set
-     */
-    public void setTimestamp(long timestamp) {
-        this.timestamp = timestamp;
-    }
-    /**
-     * @return the type
-     */
-    public MessageRecord.TYPE getType() {
-        return type;
-    }
-    /**
-     * @param type the type to set
-     */
-    public void setType(MessageRecord.TYPE type) {
-        this.type = type;
-    }
-    /**
-     * @return the packet
-     */
-    public String getPacket() {
-        return packet;
-    }
-    /**
-     * @param packet the packet to set
-     */
-    public void setPacket(String packet) {
-        this.packet = packet;
-    }
-
     /**
      * @return the number_of_attacks
      */

+ 49 - 28
src/de/tudarmstadt/informatik/hostage/persistence/HostageDBOpenHelper.java

@@ -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());
                 }