Exemplo n.º 1
0
 def parse(self, data):
     """ unpacks iwparam"""
     
     size = struct_calcsize(self.fmt)
     m, e, i, pad = struct_unpack(self.fmt, data[:size])
     # XXX well, its not *the* frequency - we need a better name
     if e == 0:
         return m
     else:
         return float(m)*10**e
Exemplo n.º 2
0
    def parse(self, data):
        """ unpacks iwparam"""

        size = struct_calcsize(self.fmt)
        m, e, i, pad = struct_unpack(self.fmt, data[:size])
        # XXX well, its not *the* frequency - we need a better name
        if e == 0:
            return m
        else:
            return float(m) * 10**e
Exemplo n.º 3
0
    def parse_data(self, fmt, data):
        """ unpacks raw C data """
        size = struct_calcsize(fmt)
        idx = self.idx

        str = data[idx:idx + size]
        self.idx = idx+size
        value = struct_unpack(fmt, str)

        # take care of a tuple like (int, )
        if len(value) == 1:
            return value[0]
        else:
            return value
Exemplo n.º 4
0
    def parse_data(self, fmt, data):
        """ unpacks raw C data """
        size = struct_calcsize(fmt)
        idx = self.idx

        str = data[idx:idx + size]
        self.idx = idx + size
        value = struct_unpack(fmt, str)

        # take care of a tuple like (int, )
        if len(value) == 1:
            return value[0]
        else:
            return value
Exemplo n.º 5
0
    def import_bytes(cls, data: bytes, hash_function=None, base_offset: int = 0) -> BloomFilter:
        """
        Import an exported `BloomFilter` byte sequence and make an instance.

        :param data: A byte sequence from which to extract the data representing a `BloomFilter`.
        :param hash_function: A hash function to use in case the exported name cannot be looked up in the `hashlib`.
            module.
        :param base_offset: The offset from the start of the byte sequence from where to start extracting.
        :return: A `BloomFilter` instance as represented by the pointed-to byte sequence.
        """

        offset = base_offset

        capacity = struct_unpack_from('>Q', buffer=data, offset=offset)[0]
        offset += struct_calcsize('>Q')

        false_positive_probability = struct_unpack_from('f', buffer=data, offset=offset)[0]
        offset += struct_calcsize('f')

        hash_function_name_bytes_len: int = struct_unpack_from(cls._IMPORT_EXPORT_LENGTH_FORMAT, buffer=data, offset=offset)[0]
        offset += struct_calcsize(cls._IMPORT_EXPORT_LENGTH_FORMAT)
        hash_function_name: str = data[offset:offset+hash_function_name_bytes_len].decode()
        offset += hash_function_name_bytes_len

        bit_array_bytes_len: int = struct_unpack_from(cls._IMPORT_EXPORT_LENGTH_FORMAT, buffer=data, offset=offset)[0]
        offset += struct_calcsize(cls._IMPORT_EXPORT_LENGTH_FORMAT)

        bit_array = bitarray()
        bit_array.frombytes(data[offset:offset + bit_array_bytes_len])

        return cls(
            capacity=capacity,
            false_positive_probability=false_positive_probability,
            hash_function=hash_function or getattr(hashlib, hash_function_name),
            bit_array=bit_array
        )
Exemplo n.º 6
0
 def addEvent(self, cmd, data):
     """Attempts to add the data from an event to a scanresult
        Only certain data is accept, in which case the result is True
        If the event data is invalid, None is returned
        If the data is valid but unused, False is returned
     """
     if cmd <= SIOCIWLAST:
         if cmd < SIOCIWFIRST:
             return None
     elif cmd >= IWEVFIRST:
         if cmd > IWEVLAST:
             return None
     else:
         return None
         
     if cmd == SIOCGIWESSID:
         self.essid = data[4:]
     elif cmd == SIOCGIWMODE:
         self.mode = modes[self.iwstruct.unpack('i', data[:4])[0]]
     elif cmd == SIOCGIWRATE:
         # TODO, deal with multiple rates, or at least the highest rate
         freqsize = struct_calcsize("ihbb")
         while len(data) >= freqsize:
             iwfreq = Iwfreq(data)
             self.rate.append(iwfreq.getBitrate())
             data = data[freqsize:]
     elif cmd == IWEVQUAL:
         self.quality.parse(data)
     elif cmd == SIOCGIWFREQ:
         self.frequency = Iwfreq(data)
     elif cmd == SIOCGIWENCODE:
         self.encode = data
     elif cmd == IWEVCUSTOM:
         self.custom.append(data[1:])
     elif cmd == SIOCGIWNAME:
         self.protocol = data[:len(data)-2]
     else:
         #print "Cmd:", cmd
         return False
     return True
Exemplo n.º 7
0
    def addEvent(self, cmd, data):
        """Attempts to add the data from an event to a scanresult
           Only certain data is accept, in which case the result is True
           If the event data is invalid, None is returned
           If the data is valid but unused, False is returned
        """
        if cmd <= SIOCIWLAST:
            if cmd < SIOCIWFIRST:
                return None
        elif cmd >= IWEVFIRST:
            if cmd > IWEVLAST:
                return None
        else:
            return None

        if cmd == SIOCGIWESSID:
            self.essid = data[4:]
        elif cmd == SIOCGIWMODE:
            self.mode = modes[self.iwstruct.unpack('i', data[:4])[0]]
        elif cmd == SIOCGIWRATE:
            # TODO, deal with multiple rates, or at least the highest rate
            freqsize = struct_calcsize("ihbb")
            while len(data) >= freqsize:
                iwfreq = Iwfreq(data)
                self.rate.append(iwfreq.getBitrate())
                data = data[freqsize:]
        elif cmd == IWEVQUAL:
            self.quality.parse(data)
        elif cmd == SIOCGIWFREQ:
            self.frequency = Iwfreq(data)
        elif cmd == SIOCGIWENCODE:
            self.encode = data
        elif cmd == IWEVCUSTOM:
            self.custom.append(data[1:])
        elif cmd == SIOCGIWNAME:
            self.protocol = data[:len(data) - 2]
        else:
            #print "Cmd:", cmd
            return False
        return True
Exemplo n.º 8
0
    def start_C_prog(self):
        pid = os.fork()
        if pid == 0:
            #Child
            sleep(1)
            try:
                os.execl(self.prog_path, self.prog_path, *self.prog_params)
            except OSError:
                print("OSerror exception")
                raise
        else:
            #Parent
            self.child_pid = pid

            cycle_prev, cycle_curr = -1, -1
            exe_time_prev, exe_time_curr = -1, -1

            counts_prev = [-1, -1, -1, -1]
            counts_curr = [-1, -1, -1, -1]

            import zmq

            context = zmq.Context()
            zmq_subscriber = context.socket(zmq.SUB)
            zmq_subscriber.connect("tcp://*****:*****@7L", out_str)
                except struct_error:
                    #End of *DRAINER should be OR syncro problems
                    print("STRUCT UNPACK ERROR | out_str = {} (len={})".format(
                        out_str, len(out_str)))
                    print("msg size should be = {}".format(
                        struct_calcsize("@7L")))
                    cycles = 0
                    exe_time = 0
                    counts = [0, 0, 0, 0]
                    exe_m_time = 1
                    None

                #Now exe_time in miliseconds. It should be in seconds!
                if abs(exe_time) >= const.EPS:
                    exe_time /= 1000.0
                    self.exec_time = exe_time

                print("cycles = {}, execution_time = {} | type = {}".format(
                    cycles, self.exec_time, type(cycles)))

                if cycle_prev == -1:
                    cycle_prev = cycles
                    exe_time_prev = exe_time
                else:
                    if cycle_curr == -1:
                        cycle_curr = cycles
                        exe_time_curr = exe_time
                    else:
                        cycle_prev, cycle_curr = cycle_curr, cycles
                        exe_time_prev, exe_time_curr = exe_time_curr, exe_time

                    if (exe_time_curr - exe_time_prev == 0):
                        None
                    else:
                        self.cycles_per_time = (cycle_curr - cycle_prev) / (
                            exe_time_curr - exe_time_prev)
                        self.cycles_per_time *= self.coinc + 1

                        while Gtk.events_pending():
                            Gtk.main_iteration_do(False)

                        print("cycle_per_time = {:f}".format(
                            self.cycles_per_time))
                        '''
                        for i in range(4):
                            diff_per_s = round( diff_counts[i]/(exe_m_time/1000.0) )
                            self.det_counts[i] = diff_per_s
                            
                            print("det_counts[{}] = {}".format(i, self.det_counts[i]))
                        
                        print("exe_m_time = {:d}".format(exe_m_time))
                        '''

                        if counts != [0, 0, 0, 0]:
                            for i in range(const.DET_NUM):
                                self.det_counts[i] = 0

                                if counts_prev[i] == -1:
                                    counts_prev[i] = counts[i]
                                else:
                                    if counts_curr[i] == -1:
                                        counts_curr[i] = counts[i]
                                    else:
                                        counts_prev[i] = counts_curr[i]
                                        counts_curr[i] = counts[i]

                                        #diff_per_s = round( (counts_curr[i] - counts_prev[i])/(exe_m_time/1000) )
                                        diff_per_s = round(
                                            (counts_curr[i] - counts_prev[i]) /
                                            (exe_time_curr - exe_time_prev))
                                        self.det_counts[i] = diff_per_s

                        print("exe_m_time = {:d}".format(exe_m_time))

                sleep(const.SLEEP_S_SOCK_READ)

        return 0