Beispiel #1
0
    def load(self, stream, endianness, seen=None):
        record_type = read_int(stream, 16, False, endianness)
        record_length = read_int(stream, 16, False, endianness)

        if record_type == NRB_RECORD_END:
            raise StreamEmpty("End marker reached")

        data = read_bytes_padded(stream, record_length)

        if record_type == NRB_RECORD_IPv4:
            return {
                "type": record_type,
                "address": unpack_ipv4(data[:4]),
                "names":
                [x.decode() for x in data[4:].split(b"\x00") if x != b""],
            }

        if record_type == NRB_RECORD_IPv6:
            return {
                "type":
                record_type,
                "address":
                unpack_ipv6(data[:16]),
                "names":
                [x.decode() for x in data[16:].split(b"\x00") if x != b""],
            }

        return {"type": record_type, "raw": data}
Beispiel #2
0
def read_bytes(stream, size):
    """
    Read the given amount of raw bytes from a stream.

    :param stream: the stream from which to read data
    :param size: the size to read, in bytes
    :returns: the read data
    :raises: :py:exc:`~pcapng.exceptions.StreamEmpty` if zero bytes were read
    :raises: :py:exc:`~pcapng.exceptions.TruncatedFile` if 0 < bytes < size
        were read
    """

    if size == 0:
        return b""

    data = stream.read(size)
    if len(data) == 0:
        raise StreamEmpty("Zero bytes read from stream")
    if len(data) < size:
        raise TruncatedFile("Trying to read {0} bytes, only got {1}".format(
            size, len(data)))
    return data
Beispiel #3
0
    def load(self, stream, endianness):
        record_type = read_int(stream, 16, False, endianness)
        record_length = read_int(stream, 16, False, endianness)

        if record_type == 0:
            raise StreamEmpty('End marker reached')

        data = read_bytes_padded(stream, record_length)

        if record_type == 1:  # IPv4
            return {
                'type': record_type,
                'address': data[:4],
                'name': data[4:],
            }

        if record_type == 2:  # IPv6
            return {
                'type': record_type,
                'address': data[:16],
                'name': data[16:],
            }

        return {'type': record_type, 'raw': data}
Beispiel #4
0
    def load(self, stream, endianness, seen=None):
        record_type = read_int(stream, 16, False, endianness)
        record_length = read_int(stream, 16, False, endianness)

        if record_type == NRB_RECORD_END:
            raise StreamEmpty('End marker reached')

        data = read_bytes_padded(stream, record_length)

        if record_type == NRB_RECORD_IPv4:
            return {
                'type': record_type,
                'address': unpack_ipv4(data[:4]),
                'names': [x.decode() for x in data[4:].split(b"\x00")],
            }

        if record_type == NRB_RECORD_IPv6:
            return {
                'type': record_type,
                'address': unpack_ipv6(data[:16]),
                'names': [x.decode() for x in data[16:].split(b"\x00")],
            }

        return {'type': record_type, 'raw': data}