Example #1
0
    def _get_psml_struct(self, fd):
        """
        Gets the current PSML (packet summary xml) structure in a tuple ((None, leftover_data)),
        only if the capture is configured to return it, else returns (None, leftover_data).

        A coroutine.
        """
        data = b''
        psml_struct = None

        if self.only_summaries:
            # If summaries are read, we need the psdml structure which appears on top of the file.
            while not psml_struct:
                data += yield From(fd.read(self.SUMMARIES_BATCH_SIZE))
                psml_struct, data = self._extract_tag_from_data(data, b'structure')
                if psml_struct:
                    psml_struct = psml_structure_from_xml(psml_struct)
            raise Return(psml_struct, data)
        else:
            raise Return(None, data)
Example #2
0
    def _packets_from_fd(self, fd, previous_data=b'', packet_count=None, 
                         wait_for_more_data=True, batch_size=4096):
        """
        Reads packets from a file-like object containing a TShark XML.
        Returns a generator.

        :param fd: A file-like object containing a TShark XML
        :param previous_data: Any data to put before the file.
        :param packet_count: A maximum amount of packets to stop after.
        :param wait_for_more_data: Whether to wait for more data or stop when
            none is available (i.e. when the fd is a standard file)
        """
        data = previous_data
        packets_captured = 0
        psml_struct = None

        if self.only_summaries:
            # If summaries are read, we need the psdml structure which appears 
            # on top of the file.
            while not psml_struct:
                data += fd.read(batch_size)
                psml_struct, data = self._extract_tag_from_data(data, 
                                                                'structure')
                psml_struct = psml_structure_from_xml(psml_struct)

        while True:
            # Read data until we get a packet, and yield it.
            new_data = fd.read(batch_size)
            data += new_data
            packet, data = self._extract_tag_from_data(data)

            if packet:
                packets_captured += 1
                yield packet_from_xml_packet(packet, psml_structure=psml_struct)

            if packet is None and not wait_for_more_data and \
                                    len(new_data) < batch_size:
                break

            if packet_count and packets_captured >= packet_count:
                break
Example #3
0
    def _get_psml_struct(self, fd):
        """
        Gets the current PSML (packet summary xml) structure in a tuple ((None, leftover_data)),
        only if the capture is configured to return it, else returns (None, leftover_data).

        A coroutine.
        """
        data = b''
        psml_struct = None

        if self.only_summaries:
            # If summaries are read, we need the psdml structure which appears on top of the file.
            while not psml_struct:
                data += yield From(fd.read(self.SUMMARIES_BATCH_SIZE))
                psml_struct, data = self._extract_tag_from_data(
                    data, b'structure')
                if psml_struct:
                    psml_struct = psml_structure_from_xml(psml_struct)
            raise Return(psml_struct, data)
        else:
            raise Return(None, data)
Example #4
0
    async def _get_psml_struct(self, fd):
        """Gets the current PSML (packet summary xml) structure in a tuple ((None, leftover_data)),
        only if the capture is configured to return it, else returns (None, leftover_data).

        A coroutine.
        """
        data = b""
        psml_struct = None

        if self._only_summaries:
            # If summaries are read, we need the psdml structure which appears on top of the file.
            while not psml_struct:
                new_data = await fd.read(self.SUMMARIES_BATCH_SIZE)
                data += new_data
                psml_struct, data = self._extract_tag_from_data(data, b"structure")
                if psml_struct:
                    psml_struct = psml_structure_from_xml(psml_struct)
                elif not new_data:
                    return None, data
            return psml_struct, data
        else:
            return None, data