AbstractHandler.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package de.tudarmstadt.informatik.hostage.handler;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import android.content.SharedPreferences;
  7. import android.content.SharedPreferences.Editor;
  8. import android.preference.PreferenceManager;
  9. import de.tudarmstadt.informatik.hostage.ConnectionRegister;
  10. import de.tudarmstadt.informatik.hostage.HoneyListener;
  11. import de.tudarmstadt.informatik.hostage.HoneyService;
  12. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  13. import de.tudarmstadt.informatik.hostage.logging.Logger;
  14. import de.tudarmstadt.informatik.hostage.logging.Record;
  15. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  16. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  17. public abstract class AbstractHandler implements Runnable {
  18. /**
  19. * Time until the socket throws a time out. The time is in milliseconds.
  20. */
  21. protected static final int TIMEOUT = 30 * 1000;
  22. protected Protocol protocol;
  23. private Socket client;
  24. protected Thread thread;
  25. private int attack_id;
  26. private String BSSID;
  27. private String SSID;
  28. private HoneyListener listener;
  29. protected Logger log;
  30. /**
  31. * Constructor of the class.
  32. * Initializes class variables for communication and logging.
  33. * Then starts itself in a new Thread.
  34. * @param service The background service.
  35. * @param listener The Listener that called the service.
  36. * @param protocol The protocol on which the handler is running.
  37. * @param client A Socket for the communication with a remote client.
  38. */
  39. public AbstractHandler(HoneyService service, HoneyListener listener,
  40. Protocol protocol, Socket client) {
  41. this.listener = listener;
  42. this.log = service.getLog();
  43. this.protocol = protocol;
  44. this.client = client;
  45. this.thread = new Thread(this);
  46. SharedPreferences pref = PreferenceManager
  47. .getDefaultSharedPreferences(service);
  48. Editor editor = pref.edit();
  49. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  50. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  51. editor.commit();
  52. BSSID = HelperUtils.getBSSID(service.getApplicationContext());
  53. SSID = HelperUtils.getSSID(service.getApplicationContext());
  54. setSoTimeout(client);
  55. thread.start();
  56. }
  57. /**
  58. * Set the timeout of the socket to the hard coded time out variable.
  59. * @param client The socket
  60. * @see #TIMEOUT
  61. */
  62. private void setSoTimeout(Socket client) {
  63. try {
  64. client.setSoTimeout(TIMEOUT);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * Creates InputStream and OutputStream for the socket.
  71. * Starts communication with client.
  72. * When the client closes the connection or a time out occurs the handler is finished.
  73. */
  74. @Override
  75. public void run() {
  76. InputStream in;
  77. OutputStream out;
  78. try {
  79. in = client.getInputStream();
  80. out = client.getOutputStream();
  81. talkToClient(in, out);
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. ConnectionRegister.closeConnection();
  85. }
  86. kill();
  87. }
  88. /**
  89. * Sets the interrupt flag of the thread and tries to close the socket.
  90. */
  91. public void kill() {
  92. thread.interrupt();
  93. try {
  94. client.close();
  95. ConnectionRegister.closeConnection();
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. listener.refreshHandlers();
  100. }
  101. /**
  102. * Determines if the interrupt flag of the thread is set.
  103. * @return True when the flag is set, else false.
  104. */
  105. public boolean isTerminated() {
  106. return thread.isInterrupted();
  107. }
  108. /**
  109. * Communicates with a client using the corresponding protocol implementation.
  110. * @param in InputStream of the socket.
  111. * @param out OutputStream of the socket.
  112. * @throws IOException
  113. */
  114. abstract protected void talkToClient(InputStream in, OutputStream out)
  115. throws IOException;
  116. /**
  117. * Creates a Record for a message exchanged with a client.
  118. * @param type The type of the message.
  119. * @param packet The content of the message.
  120. * @return The Record representing the communication message.
  121. */
  122. protected Record createRecord(TYPE type, String packet) {
  123. Record record = new Record();
  124. record.setAttack_id(attack_id);
  125. record.setProtocol(protocol.toString());
  126. record.setType(type);
  127. record.setTimestamp(System.currentTimeMillis());
  128. record.setLocalIP(client.getLocalAddress());
  129. record.setLocalPort(protocol.getPort());
  130. record.setRemoteIP(client.getInetAddress());
  131. record.setRemotePort(client.getPort());
  132. record.setBSSID(BSSID);
  133. record.setSSID(SSID);
  134. record.setPacket(packet);
  135. return record;
  136. }
  137. }