Ejemplo n.º 1
0
    def __process_packets(self, packets, out_writer, drop_writer, validation_file):
        """
        :type packets: list[Packet]
        :return A tuple with the number of packets anonymized and the number of packets dropped
        :rtype (int, int)
        """

        for index, packet in enumerate(packets):

            if index and (index % 10000) == 0:
                self.app.log.info("pcap:{}: Process packet id = '{}'".format(self.file, index))

            packet_id = index + 1  # packet id start with 1

            # packet_backup = Packet(str(packet))
            packet_backup = packet.original
            packet_backup_time = packet.time

            try:
                try:
                    if self.app.phase is Phase.phase_1:
                        self.app.packet.discover(packet)
                    elif self.app.phase is Phase.phase_3:
                        self.app.packet.anonymize(packet)
                    elif self.app.phase is Phase.phase_4:
                        validation = self.app.packet.validate(packet)
                        if validation is not None:
                            validation_file.write("\n\nPacket id {}:\n  ".format(packet_id))
                            validation = validation.replace('\n', '\n  ')  # Indent
                            validation_file.write(validation)

                except Exception as e:
                    if isinstance(e, ExplicitDropException):
                        self.app.log.debug("file:pcap:{}: Packet explicitly dropped: id = '{}', {}, {}".format(
                            self.file, packet_id, e.message, repr(packet.summary())))
                    elif isinstance(e, ImplicitDropException):
                        self.app.log.warning("file:pcap:{}: Packet implicitly dropped: id = '{}', {}, {}".format(
                            self.file, packet_id, e.message, repr(packet.summary())))
                    elif isinstance(e, ErrorDropException):
                        self.app.log.error("file:pcap:{}: Error packet dropped: id = '{}', {}, {}".format(
                            self.file, packet_id, e.message, repr(packet.summary())))
                    else:
                        self.app.log.critical("file:pcap:{}: Unexpected error packet dropped: id = '{}', {}, {}".format(
                            self.file, packet_id, e.message, repr(packet.summary())))

                    if self.app.phase is Phase.phase_3:
                        packet_backup = Packet(packet_backup)
                        packet_backup.time = packet_backup_time
                        drop_writer.write(packet_backup)

                else:
                    if self.app.phase is Phase.phase_3:
                        out_writer.write(packet)

            except Exception as e:
                self.app.log.critical(
                    "sirano:file:pcap:{}: Unexpected error: id = '{}', exception = '{}', message = '{}', {}".format(
                        self.file, packet_id, type(e), e.message, repr(packet.summary())))