UglyDbHelper.java 23 KB

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