Beispiel #1
0
    def _decode_next_layer(self, dict_, length=None):
        """Decode next layer protocol.

        Positional arguments:
            dict_ -- dict, info buffer
            proto -- str, next layer protocol name
            length -- int, valid (not padding) length

        Returns:
            * dict -- current protocol with packet extracted

        """
        seek_cur = self._file.tell()
        try:
            next_ = self._import_next_layer(self._prot, length)
        except Exception as error:
            dict_['error'] = str(error)
            self._file.seek(seek_cur, os.SEEK_SET)
            next_ = beholder(self._import_next_layer)(self, self._prot, length, error=True)
        info, chain = next_.info, next_.protochain

        # make next layer protocol name
        layer = next_.alias.lower()
        # proto = next_.__class__.__name__

        # write info and protocol chain into dict
        self._next = next_
        self._protos = chain
        dict_[layer] = info
        dict_['protocols'] = self._protos.chain
        return dict_
Beispiel #2
0
    def _decode_next_layer(self, data, length=None):  # pylint: disable=arguments-differ
        """Decode next layer protocol.

        Positional arguments:
            data (dict): info buffer
            length (int): valid (*non-padding*) length

        Returns:
            dict: current protocol with packet extracted

        """
        seek_cur = self._file.tell()
        try:
            next_ = self._import_next_layer(self._prot, length)
        except Exception:
            data['error'] = traceback.format_exc(limit=1).strip().split(
                os.linesep)[-1]
            self._file.seek(seek_cur, os.SEEK_SET)
            next_ = beholder(self._import_next_layer)(self,
                                                      self._prot,
                                                      length,
                                                      error=True)
        info, chain = next_.info, next_.protochain

        # make next layer protocol name
        layer = next_.alias.lower()
        # proto = next_.__class__.__name__

        # write info and protocol chain into dict
        self._next = next_  # pylint: disable=attribute-defined-outside-init
        self._protos = chain  # pylint: disable=attribute-defined-outside-init
        data[layer] = info
        data['protocols'] = self._protos.chain
        return data
Beispiel #3
0
    def _decode_next_layer(self, dict_, proto=None, length=None):
        """Decode next layer protocol.

        Positional arguments:
            * dict_ -- dict, info buffer
            * proto -- str, next layer protocol name
            * length -- int, valid (not padding) length

        Returns:
            * dict -- current protocol with next layer extracted

        """
        if self._onerror:
            next_ = beholder(self._import_next_layer)(self, proto, length)
        else:
            next_ = self._import_next_layer(proto, length)
        info, chain = next_.info, next_.protochain

        # make next layer protocol name
        layer = next_.alias.lower()
        # proto = next_.__class__.__name__

        # write info and protocol chain into dict
        dict_[layer] = info
        self._next = next_
        self._protos = ProtoChain(self.__class__, self.alias, basis=chain)
        return dict_
Beispiel #4
0
    def _decode_next_layer(self, dict_, proto=None, length=None):
        """Decode next layer protocol.

        Arguments:
            dict_ (dict): info buffer
            proto (int): next layer protocol index
            length (int): valid (*non-padding*) length

        Returns:
            dict: current protocol with next layer extracted

        """
        if self._onerror:
            next_ = beholder(self._import_next_layer)(self, proto, length)
        else:
            next_ = self._import_next_layer(proto, length)
        info, chain = next_.info, next_.protochain

        # make next layer protocol name
        layer = next_.alias.lower()
        # proto = next_.__class__.__name__

        # write info and protocol chain into dict
        dict_[layer] = info
        self._next = next_  # pylint: disable=attribute-defined-outside-init
        self._protos = ProtoChain(self.__class__, self.alias, basis=chain)  # pylint: disable=attribute-defined-outside-init
        return dict_
Beispiel #5
0
    def _decode_next_layer(self,
                           dict_,
                           proto=None,
                           length=None,
                           *,
                           version=4,
                           ipv6_exthdr=None):  # pylint: disable=arguments-differ
        """Decode next layer extractor.

        Arguments:
            dict_ (dict): info buffer
            proto (int): next layer protocol index
            length (int): valid (*non-padding*) length

        Keyword Arguments:
            version (Literal[4, 6]): IP version
            ipv6_exthdr (pcapkit.corekit.protochain.ProtoChain): protocol chain of IPv6 extension headers

        Returns:
            dict: current protocol with next layer extracted

        """
        if self._onerror:
            next_ = beholder(self._import_next_layer)(self,
                                                      proto,
                                                      length,
                                                      version=version)
        else:
            next_ = self._import_next_layer(proto, length, version=version)
        info, chain = next_.info, next_.protochain

        # make next layer protocol name
        layer = next_.alias.lower()
        # proto = next_.__class__.__name__

        # write info and protocol chain into dict
        dict_[layer] = info
        self._next = next_  # pylint: disable=attribute-defined-outside-init
        if ipv6_exthdr is not None:
            for proto_cls in reversed(ipv6_exthdr):
                chain = ProtoChain(proto_cls.__class__,
                                   proto_cls.alias,
                                   basis=chain)
        self._protos = ProtoChain(self.__class__, self.alias, basis=chain)  # pylint: disable=attribute-defined-outside-init
        return dict_
Beispiel #6
0
    def _decode_next_layer(self,
                           dict_,
                           proto=None,
                           length=None,
                           *,
                           version=4,
                           ipv6_exthdr=None):
        """Decode next layer extractor.

        Positional arguments:
            * dict_ -- dict, info buffer
            * proto -- str, next layer protocol name
            * length -- int, valid (not padding) length

        Keyword Arguments:
            * version -- int, IP version (4 in default)
                            <keyword> 4 / 6
            * ext_proto -- ProtoChain, ProtoChain of IPv6 extension headers

        Returns:
            * dict -- current protocol with next layer extracted

        """
        if self._onerror:
            next_ = beholder(self._import_next_layer)(self,
                                                      proto,
                                                      length,
                                                      version=version)
        else:
            next_ = self._import_next_layer(proto, length, version=version)
        info, chain = next_.info, next_.protochain

        # make next layer protocol name
        layer = next_.alias.lower()
        # proto = next_.__class__.__name__

        # write info and protocol chain into dict
        dict_[layer] = info
        self._next = next_
        if ipv6_exthdr is not None:
            for proto in reversed(ipv6_exthdr):
                chain = ProtoChain(proto.__class__, proto.alias, basis=chain)
        self._protos = ProtoChain(self.__class__, self.alias, basis=chain)
        return dict_