Example #1
0
    def abstract(self, symbolList):
        """This method abstract each message of the current session
        into symbols according to a list of symbols given as
        parameter.

        >>> from netzob.all import *
        >>> symbolSYN = Symbol([Field(ASCII("SYN"))], name="Symbol_SYN")
        >>> symbolSYNACK = Symbol([Field(ASCII("SYN/ACK"))], name="Symbol_SYNACK")
        >>> symbolACK = Symbol([Field(ASCII("ACK"))], name="Symbol_ACK")
        >>> symbolList = [symbolSYN, symbolSYNACK, symbolACK]

        >>> msg1 = RawMessage("SYN", source="A", destination="B")
        >>> msg2 = RawMessage("SYN/ACK", source="B", destination="A")
        >>> msg3 = RawMessage("ACK", source="A", destination="B")

        >>> session = Session([msg1, msg2, msg3])
        >>> if session.isTrueSession():
        ...    for src, dst, sym in session.abstract(symbolList):
        ...        print str(src) + " - " + str(dst) + " : " + str(sym.name)
        A - B : Symbol_SYN
        B - A : Symbol_SYNACK
        A - B : Symbol_ACK

        :return: a list of tuples containing the following elements : (source, destination, symbol).
        :rtype: a :class:`list`

        """

        abstractSession = []
        if not self.isTrueSession():
            self._logger.warn(
                "The current session cannot be abstracted as it not a true session (i.e. it may contain inner true sessions)."
            )
            return abstractSession
        for message in self.messages.values():
            symbol = AbstractField.abstract(message.data, symbolList)
            abstractSession.append(
                (message.source, message.destination, symbol))
        return abstractSession
    def readSymbol(self, timeout=EmptySymbol.defaultReceptionTimeout()):
        """Read from the abstraction layer a message and abstract it
        into a message.
        The timeout parameter represents the amount of time (in millisecond) above which
        no reception of a message triggers the reception of an  :class:`netzob.Common.Models.Vocabulary.EmptySymbol.EmptySymbol`. If timeout set to None
        or to a negative value means it always wait for the reception of a message.

        :keyword timeout: the time above which no reception of message triggers the reception of an :class:`netzob.Common.Models.Vocabulary.EmptySymbol.EmptySymbol`
        :type timeout: :class:`int`
        :raise TypeError if the parameter is not valid and Exception if an error occurs.
        """
        self._logger.info("Going to read from communication channel...")
        data = self.channel.read()
        self._logger.info("Received data: '{0}'".format(repr(data)))

        symbol = AbstractField.abstract(data, self.symbols)
        if symbol is not None:
            self._logger.info("Received symbol on communication channel: '{0}'".format(symbol.name))
        else:
            self._logger.info("Received symbol on communication channel: '{0}'".format(symbol))

        return (symbol, data)
Example #3
0
    def abstract(self, symbolList):
        """This method abstract each message of the current session
        into symbols according to a list of symbols given as
        parameter.

        >>> from netzob.all import *
        >>> symbolSYN = Symbol([Field(ASCII("SYN"))], name="Symbol_SYN")
        >>> symbolSYNACK = Symbol([Field(ASCII("SYN/ACK"))], name="Symbol_SYNACK")
        >>> symbolACK = Symbol([Field(ASCII("ACK"))], name="Symbol_ACK")
        >>> symbolList = [symbolSYN, symbolSYNACK, symbolACK]

        >>> msg1 = RawMessage("SYN", source="A", destination="B")
        >>> msg2 = RawMessage("SYN/ACK", source="B", destination="A")
        >>> msg3 = RawMessage("ACK", source="A", destination="B")

        >>> session = Session([msg1, msg2, msg3])
        >>> if session.isTrueSession():
        ...    for src, dst, sym in session.abstract(symbolList):
        ...        print str(src) + " - " + str(dst) + " : " + str(sym.name)
        A - B : Symbol_SYN
        B - A : Symbol_SYNACK
        A - B : Symbol_ACK

        :return: a list of tuples containing the following elements : (source, destination, symbol).
        :rtype: a :class:`list`

        """

        abstractSession = []
        if not self.isTrueSession():
            self._logger.warn("The current session cannot be abstracted as it not a true session (i.e. it may contain inner true sessions).")
            return abstractSession
        for message in self.messages.values():
            symbol = AbstractField.abstract(message.data, symbolList)
            abstractSession.append((message.source, message.destination, symbol))
        return abstractSession