DatabaseHandler.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import de.tudarmstadt.informatik.hostage.R;
  7. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  8. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  9. import android.content.ContentValues;
  10. import android.content.Context;
  11. import android.database.Cursor;
  12. import android.database.sqlite.SQLiteDatabase;
  13. import android.database.sqlite.SQLiteOpenHelper;
  14. import android.util.Log;
  15. /**
  16. * This class creates SQL tables and handles all access to the database.<br>
  17. * It contains several methods with predefined queries to extract different kinds of information from the database.<br>
  18. * The database contains two tables: {@link #TABLE_RECORDS} and {@link #TABLE_BSSIDS}:<br>
  19. * {@link #TABLE_RECORDS} contains all logging information of a single message record except the SSID.<br>
  20. * {@link #TABLE_BSSIDS} contains the BSSID of all recorded Networks and the corresponding SSID.<br>
  21. * @author Lars Pandikow
  22. */
  23. public class DatabaseHandler extends SQLiteOpenHelper {
  24. // All Static variables
  25. // Database Version
  26. private static final int DATABASE_VERSION = 1;
  27. // Database Name
  28. private static final String DATABASE_NAME = "recordManager";
  29. // Contacts table names
  30. private static final String TABLE_ATTACK_INFO = "attack_info";
  31. private static final String TABLE_RECORDS = "records";
  32. private static final String TABLE_BSSIDS = "bssids";
  33. private static final String TABLE_PORTS = "ports";
  34. // Contacts Table Columns names
  35. public static final String KEY_ID = "_id";
  36. public static final String KEY_ATTACK_ID = "_attack_id";
  37. public static final String KEY_TYPE = "type";
  38. public static final String KEY_TIME = "timestamp";
  39. public static final String KEY_PACKET = "packet";
  40. public static final String KEY_PROTOCOL = "protocol";
  41. public static final String KEY_EXTERNAL_IP ="externalIP";
  42. public static final String KEY_LOCAL_IP = "localIP";
  43. public static final String KEY_LOCAL_HOSTNAME = "localHostName";
  44. public static final String KEY_LOCAL_PORT = "localPort";
  45. public static final String KEY_REMOTE_IP = "remoteIP";
  46. public static final String KEY_REMOTE_HOSTNAME = "remoteHostName";
  47. public static final String KEY_REMOTE_PORT = "remotePort";
  48. public static final String KEY_BSSID = "_bssid";
  49. public static final String KEY_SSID = "ssid";
  50. public static final String KEY_LATITUDE = "latitude";
  51. public static final String KEY_LONGITUDE = "longitude";
  52. public static final String KEY_ACCURACY = "accuracy";
  53. // Database sql create statements
  54. private static final String CREATE_RECORD_TABLE = "CREATE TABLE " + TABLE_RECORDS + "("
  55. + KEY_ID + " INTEGER NOT NULL," + KEY_ATTACK_ID + " INTEGER NOT NULL,"
  56. + KEY_TYPE + " TEXT," + KEY_TIME + " INTEGER," + KEY_PACKET + " TEXT,"
  57. + "FOREIGN KEY("+ KEY_ATTACK_ID +") REFERENCES " + TABLE_ATTACK_INFO + "("+KEY_ATTACK_ID+")"
  58. + "PRIMARY KEY("+ KEY_ID + ", " + KEY_ATTACK_ID + ")"
  59. + ")";
  60. private static final String CREATE_ATTACK_INFO_TABLE = "CREATE TABLE " + TABLE_ATTACK_INFO + "("
  61. + KEY_ATTACK_ID + " INTEGER PRIMARY KEY," + KEY_PROTOCOL + " TEXT,"
  62. + KEY_EXTERNAL_IP + " TEXT," + KEY_LOCAL_IP + " BLOB," + KEY_LOCAL_HOSTNAME + " TEXT,"
  63. + KEY_REMOTE_IP + " BLOB," + KEY_REMOTE_HOSTNAME + " TEXT," + KEY_REMOTE_PORT + " INTEGER," + KEY_BSSID + " TEXT,"
  64. + "FOREIGN KEY("+ KEY_BSSID +") REFERENCES " + TABLE_BSSIDS + "("+KEY_BSSID+")"
  65. + "FOREIGN KEY("+ KEY_PROTOCOL +") REFERENCES " + TABLE_PORTS + "("+KEY_PROTOCOL+")"
  66. + ")";
  67. private static final String CREATE_BSSID_TABLE = "CREATE TABLE " + TABLE_BSSIDS + "("
  68. + KEY_BSSID + " TEXT PRIMARY KEY," + KEY_SSID + " TEXT," + KEY_LATITUDE + " INTEGER,"
  69. + KEY_LONGITUDE + " INTEGER," + KEY_ACCURACY + " INTEGER," + KEY_TIME + " INTEGER"
  70. + ")";
  71. private static final String CREATE_PORT_TABLE = "CREATE TABLE " + TABLE_PORTS + "("
  72. + KEY_PROTOCOL + " TEXT PRIMARY KEY," + KEY_LOCAL_PORT + " INTEGER"
  73. + ")";
  74. private Context context;
  75. public DatabaseHandler(Context context) {
  76. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  77. this.context = context;
  78. }
  79. // Creating Tables
  80. @Override
  81. public void onCreate(SQLiteDatabase db) {
  82. db.execSQL(CREATE_PORT_TABLE);
  83. db.execSQL(CREATE_BSSID_TABLE);
  84. db.execSQL(CREATE_ATTACK_INFO_TABLE);
  85. db.execSQL(CREATE_RECORD_TABLE);
  86. String[] protocols = context.getResources().getStringArray(R.array.protocols);
  87. String packageName = Protocol.class.getPackage().getName();
  88. //Initialize Port Table
  89. for (String protocol : protocols) {
  90. try {
  91. int port = ((Protocol) Class.forName(String.format("%s.%s", packageName, protocol)).newInstance()).getPort();
  92. db.execSQL("INSERT INTO " + TABLE_PORTS + " VALUES ( '" + protocol + "'," + port + ")");
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. // Upgrading database
  99. @Override
  100. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  101. // Drop older table if existed
  102. db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS);
  103. db.execSQL("DROP TABLE IF EXISTS " + TABLE_ATTACK_INFO);
  104. db.execSQL("DROP TABLE IF EXISTS " + TABLE_BSSIDS);
  105. db.execSQL("DROP TABLE IF EXISTS " + TABLE_PORTS);
  106. // Create tables again
  107. onCreate(db);
  108. }
  109. /**
  110. * Adds a given {@link Record} to the database.
  111. * @param record The added {@link Record} .
  112. */
  113. public void addRecord(Record record) {
  114. SQLiteDatabase db = this.getWritableDatabase();
  115. HashMap<String, Object> bssidValues = new HashMap<String, Object>();
  116. bssidValues.put(KEY_BSSID, record.getBSSID());
  117. bssidValues.put(KEY_SSID, record.getSSID());
  118. bssidValues.put(KEY_LATITUDE, record.getLatitude());
  119. bssidValues.put(KEY_LONGITUDE, record.getLongitude());
  120. bssidValues.put(KEY_ACCURACY, record.getAccuracy());
  121. bssidValues.put(KEY_TIME, record.getTimestampLocation());
  122. ContentValues attackValues = new ContentValues();
  123. attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  124. attackValues.put(KEY_PROTOCOL, record.getProtocol().toString());
  125. attackValues.put(KEY_EXTERNAL_IP, record.getExternalIP());
  126. attackValues.put(KEY_LOCAL_IP, record.getLocalIP().getAddress()); // Log Local IP
  127. attackValues.put(KEY_LOCAL_HOSTNAME, record.getLocalIP().getHostName());
  128. attackValues.put(KEY_REMOTE_IP, record.getRemoteIP().getAddress()); // Log Remote IP
  129. attackValues.put(KEY_REMOTE_HOSTNAME, record.getRemoteIP().getHostName());
  130. attackValues.put(KEY_REMOTE_PORT, record.getRemotePort()); // Log Remote Port
  131. attackValues.put(KEY_BSSID, record.getBSSID());
  132. ContentValues recordValues = new ContentValues();
  133. recordValues.put(KEY_ID, record.getId()); // Log Message Number
  134. recordValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
  135. recordValues.put(KEY_TYPE, record.getType().name()); // Log Type
  136. recordValues.put(KEY_TIME, record.getTimestamp()); // Log Timestamp
  137. recordValues.put(KEY_PACKET, record.getPacket()); // Log Packet
  138. // Inserting Rows
  139. db.insertWithOnConflict(TABLE_ATTACK_INFO, null, attackValues, SQLiteDatabase.CONFLICT_REPLACE);
  140. db.insert(TABLE_RECORDS, null, recordValues);
  141. db.close(); // Closing database connection
  142. // Update Network Information
  143. updateNetworkInformation(bssidValues);
  144. }
  145. /**
  146. * Creates a {@link Record} from a Cursor. If the cursor does not show to a valid data structure a runtime exception is thrown.
  147. * @param cursor
  148. * @return Returns the created {@link Record} .
  149. */
  150. private Record createRecord(Cursor cursor){
  151. Record record = new Record();
  152. try {
  153. record.setId(Integer.parseInt(cursor.getString(0)));
  154. record.setAttack_id(cursor.getLong(1));
  155. record.setType(cursor.getString(2).equals("SEND") ? TYPE.SEND : TYPE.RECEIVE);
  156. record.setTimestamp(cursor.getLong(3));
  157. record.setPacket(cursor.getString(4));
  158. record.setProtocol(cursor.getString(5));
  159. record.setExternalIP(cursor.getString(6));
  160. record.setLocalIP(InetAddress.getByAddress(cursor.getString(8), cursor.getBlob(7)));
  161. record.setRemoteIP(InetAddress.getByAddress(cursor.getString(10), cursor.getBlob(9)));
  162. record.setRemotePort(Integer.parseInt(cursor.getString(11)));
  163. record.setBSSID(cursor.getString(12));
  164. record.setSSID(cursor.getString(13));
  165. record.setLatitude(Double.parseDouble(cursor.getString(14)));
  166. record.setLongitude(Double.parseDouble(cursor.getString(15)));
  167. record.setAccuracy(Float.parseFloat(cursor.getString(16)));
  168. record.setTimestampLocation(cursor.getLong(17));
  169. record.setLocalPort(Integer.parseInt(cursor.getString(18)));
  170. } catch (UnknownHostException e) {
  171. e.printStackTrace();
  172. }
  173. return record;
  174. }
  175. /**
  176. * Gets a single {@link Record} with the given ID from the database.
  177. * @param id The ID of the {@link Record};
  178. * @return The {@link Record}.
  179. */
  180. public Record getRecord(int id) {
  181. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ID + " = " + id;
  182. SQLiteDatabase db = this.getReadableDatabase();
  183. Cursor cursor = db.rawQuery(selectQuery, null);
  184. Record record = null;
  185. if (cursor.moveToFirst()){
  186. record = createRecord(cursor);
  187. }
  188. cursor.close();
  189. db.close();
  190. // return contact
  191. return record;
  192. }
  193. /**
  194. * Gets all {@link Record Records} saved in the database.
  195. * @return A ArrayList of all the {@link Record Records} in the Database.
  196. */
  197. public ArrayList<Record> getAllRecords() {
  198. ArrayList<Record> recordList = new ArrayList<Record>();
  199. // Select All Query
  200. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS;
  201. SQLiteDatabase db = this.getWritableDatabase();
  202. Cursor cursor = db.rawQuery(selectQuery, null);
  203. Log.i("Database", "Start loop");
  204. // looping through all rows and adding to list
  205. if (cursor.moveToFirst()) {
  206. do {
  207. Log.i("Database", "Add Record");
  208. Record record = createRecord(cursor);
  209. // Adding record to list
  210. recordList.add(record);
  211. } while (cursor.moveToNext());
  212. }
  213. cursor.close();
  214. db.close();
  215. // return record list
  216. return recordList;
  217. }
  218. /**
  219. * Gets a single {@link Record} with the given attack id from the database.
  220. * @param attack_id The attack id of the {@link Record};
  221. * @return The {@link Record}.
  222. */
  223. public Record getRecordOfAttackId(long attack_id) {
  224. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ATTACK_ID + " = " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
  225. SQLiteDatabase db = this.getReadableDatabase();
  226. Cursor cursor = db.rawQuery(selectQuery, null);
  227. Record record = null;
  228. if (cursor.moveToFirst()) {
  229. record = createRecord(cursor);
  230. }
  231. cursor.close();
  232. // return record list
  233. db.close();
  234. return record;
  235. }
  236. /**
  237. * Gets all received {@link Record Records} for every attack identified by its attack id and ordered by date.
  238. * @return A ArrayList with all received {@link Record Records} for each attack id in the Database.
  239. */
  240. public ArrayList<Record> getAllReceivedRecordsOfEachAttack() {
  241. ArrayList<Record> recordList = new ArrayList<Record>();
  242. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_TYPE + "='RECEIVE'" + " ORDER BY " + KEY_TIME;
  243. SQLiteDatabase db = this.getReadableDatabase();
  244. Cursor cursor = db.rawQuery(selectQuery, null);
  245. // looping through all rows and adding to list
  246. if (cursor.moveToFirst()) {
  247. do {
  248. Record record = createRecord(cursor);
  249. // Adding record to list
  250. recordList.add(record);
  251. } while (cursor.moveToNext());
  252. }
  253. cursor.close();
  254. // return record list
  255. db.close();
  256. return recordList;
  257. }
  258. /**
  259. * Gets a representative {@link Record} for every attack identified by its attack id.
  260. * @return A ArrayList with one {@link Record Records} for each attack id in the Database.
  261. */
  262. public ArrayList<Record> getRecordOfEachAttack() {
  263. ArrayList<Record> recordList = new ArrayList<Record>();
  264. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " GROUP BY " + KEY_ATTACK_ID;
  265. SQLiteDatabase db = this.getReadableDatabase();
  266. Cursor cursor = db.rawQuery(selectQuery, null);
  267. // looping through all rows and adding to list
  268. if (cursor.moveToFirst()) {
  269. do {
  270. Record record = createRecord(cursor);
  271. // Adding record to list
  272. recordList.add(record);
  273. } while (cursor.moveToNext());
  274. }
  275. cursor.close();
  276. // return record list
  277. db.close();
  278. return recordList;
  279. }
  280. /**
  281. * Gets a representative {@link Record} for every attack with a higher attack id than the specified.
  282. * @param attack_id The attack id to match the query against.
  283. * @return A ArrayList with one {@link Record Records} for each attack id higher than the given.
  284. */
  285. public ArrayList<Record> getRecordOfEachAttack(long attack_id) {
  286. ArrayList<Record> recordList = new ArrayList<Record>();
  287. String selectQuery = "SELECT * FROM " + TABLE_RECORDS + " NATURAL JOIN " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS + " NATURAL JOIN " + TABLE_PORTS + " WHERE " + KEY_ATTACK_ID + " > " + attack_id + " GROUP BY " + KEY_ATTACK_ID;
  288. SQLiteDatabase db = this.getReadableDatabase();
  289. Cursor cursor = db.rawQuery(selectQuery, null);
  290. // looping through all rows and adding to list
  291. if (cursor.moveToFirst()) {
  292. do {
  293. Record record = createRecord(cursor);
  294. // Adding record to list
  295. recordList.add(record);
  296. } while (cursor.moveToNext());
  297. }
  298. cursor.close();
  299. // return count
  300. db.close();
  301. return recordList;
  302. }
  303. /**
  304. * Determines the number of {@link Record Records} in the database.
  305. * @return The number of {@link Record Records} in the database.
  306. */
  307. public int getRecordCount() {
  308. String countQuery = "SELECT * FROM " + TABLE_RECORDS;
  309. SQLiteDatabase db = this.getReadableDatabase();
  310. Cursor cursor = db.rawQuery(countQuery, null);
  311. int result = cursor.getCount();
  312. cursor.close();
  313. // return count
  314. db.close();
  315. return result;
  316. }
  317. /**
  318. * Determines the number of different attack_ids in the database.
  319. * @return The number of different attack_ids in the database.
  320. */
  321. public int getAttackCount() {
  322. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO;
  323. SQLiteDatabase db = this.getReadableDatabase();
  324. Cursor cursor = db.rawQuery(countQuery, null);
  325. int result = cursor.getCount();
  326. cursor.close();
  327. // return count
  328. db.close();
  329. return result;
  330. }
  331. /**
  332. * Determines the number of different attack_ids for a specific protocol in the database.
  333. * @param protocol The String representation of the {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol}
  334. * @return The number of different attack_ids in the database.
  335. */
  336. public int getAttackPerProtokolCount(String protocol) {
  337. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'";
  338. SQLiteDatabase db = this.getReadableDatabase();
  339. Cursor cursor = db.rawQuery(countQuery, null);
  340. int result = cursor.getCount();
  341. cursor.close();
  342. // return count
  343. db.close();
  344. return result;
  345. }
  346. /**
  347. * Determines the smallest attack id stored in the database.
  348. * @return The smallest attack id stored in the database.
  349. */
  350. public long getSmallestAttackId(){
  351. String selectQuery = "SELECT MIN(" + KEY_ATTACK_ID +") FROM " + TABLE_ATTACK_INFO;
  352. SQLiteDatabase db = this.getReadableDatabase();
  353. Cursor cursor = db.rawQuery(selectQuery, null);
  354. int result;
  355. if (cursor.moveToFirst()) {
  356. result = cursor.getInt(0);
  357. } else{
  358. result = -1;
  359. }
  360. cursor.close();
  361. db.close();
  362. return result;
  363. }
  364. /**
  365. * Determines the highest attack id stored in the database.
  366. * @return The highest attack id stored in the database.
  367. */
  368. public long getHighestAttackId(){
  369. String selectQuery = "SELECT MAX(" + KEY_ATTACK_ID +") FROM " + TABLE_ATTACK_INFO;
  370. SQLiteDatabase db = this.getReadableDatabase();
  371. Cursor cursor = db.rawQuery(selectQuery, null);
  372. int result;
  373. if (cursor.moveToFirst()) {
  374. result = cursor.getInt(0);
  375. } else{
  376. result = -1;
  377. }
  378. cursor.close();
  379. db.close();
  380. return result;
  381. }
  382. /**
  383. * Determines if a network with given BSSID has already been recorded as malicious.
  384. * @param BSSID The BSSID of the network.
  385. * @return True if an attack has been recorded in a network with the given BSSID, else false.
  386. */
  387. public boolean bssidSeen(String BSSID){
  388. String countQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + BSSID + "'";
  389. SQLiteDatabase db = this.getReadableDatabase();
  390. Cursor cursor = db.rawQuery(countQuery, null);
  391. int result = cursor.getCount();
  392. cursor.close();
  393. db.close();
  394. return result > 0;
  395. }
  396. /**
  397. * Determines if an attack has been recorded on a specific protocol in a network with a given BSSID.
  398. * @param protocol The {@link de.tudarmstadt.informatik.hostage.protocol.Protocol Protocol} to inspect.
  399. * @param BSSID The BSSID of the network.
  400. * @return True if an attack on the given protocol has been recorded in a network with the given BSSID, else false.
  401. */
  402. public boolean bssidSeen(String protocol, String BSSID){
  403. String countQuery = "SELECT * FROM " + TABLE_ATTACK_INFO + " NATURAL JOIN " + TABLE_BSSIDS+ " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'" + " AND " + KEY_BSSID + " = " + "'" + BSSID + "'";
  404. SQLiteDatabase db = this.getReadableDatabase();
  405. Cursor cursor = db.rawQuery(countQuery, null);
  406. int result = cursor.getCount();
  407. cursor.close();
  408. db.close();
  409. return result > 0;
  410. }
  411. /**
  412. * Returns a String array with all BSSIDs stored in the database.
  413. * @return String[] of all recorded BSSIDs.
  414. */
  415. public String[] getAllBSSIDS(){
  416. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  417. SQLiteDatabase db = this.getReadableDatabase();
  418. Cursor cursor = db.rawQuery(selectQuery, null);
  419. String[] bssidList = new String[cursor.getCount()];
  420. int counter = 0;
  421. // looping through all rows and adding to list
  422. if (cursor.moveToFirst()) {
  423. do {
  424. bssidList[counter] = cursor.getString(0);
  425. counter++;
  426. } while (cursor.moveToNext());
  427. }
  428. cursor.close();
  429. db.close();
  430. return bssidList;
  431. }
  432. /**
  433. * Gets the last recorded SSID to a given BSSID.
  434. * @param bssid The BSSID to match against.
  435. * @return A String of the last SSID or null if the BSSID is not in the database.
  436. */
  437. public String getSSID(String bssid){
  438. String selectQuery = "SELECT "+ KEY_SSID +" FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  439. SQLiteDatabase db = this.getReadableDatabase();
  440. Cursor cursor = db.rawQuery(selectQuery, null);
  441. String ssid = null;
  442. if(cursor.moveToFirst()){
  443. ssid = cursor.getString(0);
  444. }
  445. cursor.close();
  446. db.close();
  447. return ssid;
  448. }
  449. /**
  450. * Deletes all records from {@link #TABLE_RECORDS} with a specific BSSID.
  451. * @param bssid The BSSID to match against.
  452. */
  453. public void deleteByBSSID(String bssid){
  454. SQLiteDatabase db = this.getReadableDatabase();
  455. db.delete(TABLE_RECORDS, KEY_BSSID + " = ?", new String[]{bssid});
  456. db.delete(TABLE_ATTACK_INFO, KEY_BSSID + " = ?", new String[]{bssid});
  457. db.close();
  458. }
  459. //TODO Delete statement �berarbeiten
  460. /**
  461. * Deletes all records from {@link #TABLE_RECORDS} with a time stamp smaller then the given
  462. * @param date A Date represented in milliseconds.
  463. */
  464. public void deleteByDate(long date){
  465. SQLiteDatabase db = this.getReadableDatabase();
  466. String deleteQuery = "DELETE FROM " + TABLE_RECORDS + " WHERE " + KEY_TIME + " < " + date;
  467. //TODO Delete statement �berarbeiten
  468. // String deleteQuery2 = "DELETE "
  469. db.execSQL(deleteQuery);
  470. db.close();
  471. }
  472. /**
  473. * Deletes all records from {@link #TABLE_RECORDS}.
  474. */
  475. public void clearData(){
  476. SQLiteDatabase db = this.getReadableDatabase();
  477. db.delete(TABLE_RECORDS, null, null);
  478. db.delete(TABLE_ATTACK_INFO, null, null);
  479. db.close();
  480. }
  481. public ArrayList<HashMap<String, Object>> getNetworkInformation(){
  482. String selectQuery = "SELECT * FROM " + TABLE_BSSIDS;
  483. SQLiteDatabase db = this.getReadableDatabase();
  484. Cursor cursor = db.rawQuery(selectQuery, null);
  485. ArrayList<HashMap<String, Object>> networkInformation = new ArrayList<HashMap<String, Object>>();
  486. // looping through all rows and adding to list
  487. if (cursor.moveToFirst()) {
  488. do {
  489. HashMap<String, Object> values = new HashMap<String, Object>();
  490. values.put(KEY_BSSID, cursor.getString(0));
  491. values.put(KEY_SSID, cursor.getString(1));
  492. values.put(KEY_LATITUDE, Double.parseDouble(cursor.getString(2)));
  493. values.put(KEY_LONGITUDE, Double.parseDouble(cursor.getString(3)));
  494. values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
  495. values.put(KEY_TIME, cursor.getLong(5));
  496. networkInformation.add(values);
  497. } while (cursor.moveToNext());
  498. }
  499. cursor.close();
  500. db.close();
  501. return networkInformation;
  502. }
  503. public void updateNetworkInformation(HashMap<String, Object> networkInformation){
  504. SQLiteDatabase db = this.getReadableDatabase();
  505. String bssid = (String) networkInformation.get(KEY_BSSID);
  506. String bssidQuery = "SELECT * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
  507. Cursor cursor = db.rawQuery(bssidQuery, null);
  508. int result = cursor.getCount();
  509. if( cursor != null && cursor.moveToFirst() && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(KEY_TIME)));{
  510. ContentValues bssidValues = new ContentValues();
  511. bssidValues.put(KEY_BSSID, bssid);
  512. bssidValues.put(KEY_SSID, (String) networkInformation.get(KEY_SSID));
  513. bssidValues.put(KEY_LATITUDE, (double)(Double) networkInformation.get(KEY_LATITUDE));
  514. bssidValues.put(KEY_LONGITUDE, (double)(Double) networkInformation.get(KEY_LONGITUDE));
  515. bssidValues.put(KEY_ACCURACY, (float)(Float) networkInformation.get(KEY_ACCURACY));
  516. bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
  517. db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
  518. }
  519. cursor.close();
  520. db.close();
  521. }
  522. public void updateNetworkInformation(ArrayList<HashMap<String, Object>> networkInformation){
  523. Log.i("DatabaseHandler", "Starte updating");
  524. for(HashMap<String, Object> values : networkInformation){
  525. updateNetworkInformation(values);
  526. }
  527. }
  528. }