Browse Source

refactor of realistic timestamps function

the code for assigning realistic timestamps has been put into a separate function to make the main function "create_messages" more readable
Marcel Juschak 6 years ago
parent
commit
c2f0cd3b45
1 changed files with 50 additions and 39 deletions
  1. 50 39
      code/Attack/MembersMgmtCommAttack.py

+ 50 - 39
code/Attack/MembersMgmtCommAttack.py

@@ -312,6 +312,54 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
                     else:
                     else:
                          bot_configs[bot]["TTL"] = self.statistics.process_db_query("most_used(ttlValue)")
                          bot_configs[bot]["TTL"] = self.statistics.process_db_query("most_used(ttlValue)")
 
 
+        def assign_realistic_timestamps(messages: list, external_ids: set, local_ids: set, avg_delay_local:float, avg_delay_external: float, zero_reference:float):
+            """
+            Assigns realistic timestamps to a set of messages
+            
+            :param messages: the set of messages to be updated
+            :param external_ids: the set of bot ids, that are outside the network, i.e. external
+            :param local_ids: the set of bot ids, that are inside the network, i.e. local
+            :avg_delay_local: the avg_delay between the dispatch and the reception of a packet between local computers
+            :avg_delay_external: the avg_delay between the dispatch and the reception of a packet between a local and an external computer
+            :zero_reference: the timestamp which is regarded as the beginning of the pcap_file and therefore handled like a timestamp that resembles 0
+            """
+            updated_msgs = []
+            last_response = {}      # Dict, takes a tuple of 2 Bot_IDs as a key (requester, responder), returns the time of the last response, the requester received
+                                    # necessary in order to make sure, that additional requests are sent only after the response to the last one was received
+            for msg in messages:    # init
+                last_response[(msg.src, msg.dst)] = -1  
+
+            # update all timestamps
+            for req_msg in messages:
+
+                if(req_msg in updated_msgs):
+                    # message already updated
+                    continue
+
+                # if req_msg.timestamp would be before the timestamp of the response to the last request, req_msg needs to be sent later (else branch)
+                if last_response[(req_msg.src, req_msg.dst)] == -1 or last_response[(req_msg.src, req_msg.dst)] < (zero_reference + req_msg.time - 0.05):
+                    ## update req_msg timestamp with a variation of up to 50ms
+                    req_msg.time = zero_reference + req_msg.time + uniform(-0.05, 0.05)
+                    updated_msgs.append(req_msg)
+
+                else:
+                    req_msg.time = last_response[(req_msg.src, req_msg.dst)] + 0.06 + uniform(-0.05, 0.05)
+
+                # update response if necessary
+                if req_msg.refer_msg_id != -1:
+                    respns_msg = messages[req_msg.refer_msg_id]
+
+                    # check for local or external communication and update response timestamp with the respective avg delay
+                    if req_msg.src in external_ids or req_msg.dst in external_ids:
+                        #external communication
+                        respns_msg.time = req_msg.time + avg_delay_external + uniform(-0.1*avg_delay_external, 0.1*avg_delay_external)
+                    
+                    else:
+                        #local communication
+                        respns_msg.time = req_msg.time + avg_delay_local + uniform(-0.1*avg_delay_local, 0.1*avg_delay_local)
+
+                    updated_msgs.append(respns_msg)
+                    last_response[(req_msg.src, req_msg.dst)] = respns_msg.time
 
 
         def assign_ttls_from_caida(bot_configs):
         def assign_ttls_from_caida(bot_configs):
             """
             """
@@ -471,52 +519,15 @@ class MembersMgmtCommAttack(BaseAttack.BaseAttack):
             new_external_ips = sorted([ipgen.random_ip() for _ in range(remaining)])
             new_external_ips = sorted([ipgen.random_ip() for _ in range(remaining)])
             add_ids_to_config(sorted(external_ids), existing_external_ips, new_external_ips, bot_configs, idtype="external", router_mac=router_mac)
             add_ids_to_config(sorted(external_ids), existing_external_ips, new_external_ips, bot_configs, idtype="external", router_mac=router_mac)
 
 
-        #### Set realistic timestamps for messages ####
-
         # this is the timestamp at which the first packet should be injected, the packets have to be shifted to the beginning of the
         # this is the timestamp at which the first packet should be injected, the packets have to be shifted to the beginning of the
         # pcap file (INJECT_AT_TIMESTAMP) and then the offset of the packets have to be compensated to start at the given point in time
         # pcap file (INJECT_AT_TIMESTAMP) and then the offset of the packets have to be compensated to start at the given point in time
         zero_reference = self.get_param_value(Param.INJECT_AT_TIMESTAMP) - messages[0].time
         zero_reference = self.get_param_value(Param.INJECT_AT_TIMESTAMP) - messages[0].time
 
 
-        updated_msgs = []
-        last_response = {}      # Dict, takes a tuple of 2 Bot_IDs as a key (requester, responder), returns the time of the last response, the requester received
-                                # necessary in order to make sure, that additional requests are sent only after the response to the last one was received
-        for msg in messages:    # init
-            last_response[(msg.src, msg.dst)] = -1
-
         # calculate the average delay values for local and external responses
         # calculate the average delay values for local and external responses
         avg_delay_local, avg_delay_external = self.statistics.get_avg_delay_local_ext()
         avg_delay_local, avg_delay_external = self.statistics.get_avg_delay_local_ext()
 
 
-        # update all timestamps
-        for req_msg in messages:
-
-            if(req_msg in updated_msgs):
-                # message already updated
-                continue
-
-            # if req_msg.timestamp would be before the timestamp of the response to the last request, req_msg needs to be sent later (else branch)
-            if last_response[(req_msg.src, req_msg.dst)] == -1 or last_response[(req_msg.src, req_msg.dst)] < (zero_reference + req_msg.time - 0.05):
-                ## update req_msg timestamp with a variation of up to 50ms
-                req_msg.time = zero_reference + req_msg.time + uniform(-0.05, 0.05)
-                updated_msgs.append(req_msg)
-
-            else:
-                req_msg.time = last_response[(req_msg.src, req_msg.dst)] + 0.06 + uniform(-0.05, 0.05)
-
-            # update response if necessary
-            if req_msg.refer_msg_id != -1:
-                respns_msg = messages[req_msg.refer_msg_id]
-
-                # check for local or external communication and update response timestamp with the respective avg delay
-                if req_msg.src in external_ids or req_msg.dst in external_ids:
-                    #external communication
-                    respns_msg.time = req_msg.time + avg_delay_external + uniform(-0.1*avg_delay_external, 0.1*avg_delay_external)
-                
-                else:
-                    #local communication
-                    respns_msg.time = req_msg.time + avg_delay_local + uniform(-0.1*avg_delay_local, 0.1*avg_delay_local)
-
-                updated_msgs.append(respns_msg)
-                last_response[(req_msg.src, req_msg.dst)] = respns_msg.time
+        #set timestamps
+        assign_realistic_timestamps(messages, external_ids, local_ids, avg_delay_local, avg_delay_external, zero_reference)
 
 
         portSelector = PortSelectors.LINUX
         portSelector = PortSelectors.LINUX
         # create port configurations for the bots
         # create port configurations for the bots