示例#1
0
    def on_message(self, client, userdata, msg):
        """Callback for when a PUBLISH message is received from the server.
        """
        if _debug:
            MQTTClient._debug("on_message %r, %s", msg.topic, btox(msg.payload, "."))

        # wrap it up and decode it
        pdu = PDU(msg.payload)
        bvlpdu = BVLPDU()
        bvlpdu.decode(pdu)
        if _debug:
            MQTTClient._debug("    - bvlpdu: %r", bvlpdu)

        # decode the next layer
        xpdu = bvl_pdu_types[bvlpdu.bvlciFunction]()
        xpdu.decode(bvlpdu)
        if _debug:
            MQTTClient._debug("    - xpdu: %r", xpdu)

        if isinstance(xpdu, OriginalUnicastNPDU):
            # from ourselves?
            if xpdu.bvlciAddress == self.client:
                if _debug:
                    MQTTClient._debug("    - from ourselves")
                return

            # build a PDU with the client address
            ypdu = PDU(
                xpdu.pduData,
                source=xpdu.bvlciAddress,
                destination=self.client,
                user_data=xpdu.pduUserData,
            )
            if _debug:
                MQTTClient._debug("    - upstream ypdu: %r", ypdu)

            deferred(self.response, ypdu)

        elif isinstance(xpdu, OriginalBroadcastNPDU):
            # from ourselves?
            if xpdu.bvlciAddress == self.client:
                if _debug:
                    MQTTClient._debug("    - from ourselves")
                return

            # build a PDU with a local broadcast address
            ypdu = PDU(
                xpdu.pduData,
                source=xpdu.bvlciAddress,
                destination=LocalBroadcast(),
                user_data=xpdu.pduUserData,
            )
            if _debug:
                MQTTClient._debug("    - upstream ypdu: %r", ypdu)

            deferred(self.response, ypdu)
示例#2
0
    def on_message(self, client, userdata, msg):
        """Callback for when a PUBLISH message is received from the server.
        """
        if _debug: MQTTSniffer._debug("on_message ...")
        if _debug: MQTTSniffer._debug("    - msg.topic: %r", msg.topic)
        if _debug: MQTTSniffer._debug("    - payload: %r", btox(msg.payload))

        topic_address = Address(xtob(msg.topic.split('/')[-1]))
        if _debug: MQTTSniffer._debug("    - topic_address: %r", topic_address)

        packet_data = decode_packet(msg.payload, topic_address)
        print(packet_data)
        packet_data.debug_contents(file=sys.stdout)
示例#3
0
    def on_message(self, client, userdata, msg):
        """Callback for when a PUBLISH message is received from the server.
        """
        if _debug:
            MQTTSniffer._debug("on_message ...")
        if _debug:
            MQTTSniffer._debug("    - msg.topic: %r", msg.topic)
        if _debug:
            MQTTSniffer._debug("    - payload: %r", btox(msg.payload))

        topic_address = msg.topic.split("/")[-1]
        if _debug:
            MQTTSniffer._debug("    - topic_address: %r", topic_address)

        # make destination a little more abstact
        if topic_address == BROADCAST_TOPIC:
            topic_address = LocalBroadcast()
        else:
            topic_address = topic2addr(topic_address)

        packet_data = decode_packet(msg.payload, topic_address)
        print(packet_data)
        packet_data.debug_contents(file=sys.stdout)
示例#4
0
def decode_packet(data, destination):
    """decode the data, return some kind of PDU."""
    if _debug:
        decode_packet._debug("decode_packet %r %r", data, destination)

    # build a PDU
    pdu = PDU(data, destination=destination)

    # check for a BVLL header
    if pdu.pduData[0] == 0x84:
        if _debug:
            decode_packet._debug("    - BVLL header found")

        xpdu = BVLPDU()
        xpdu.decode(pdu)
        pdu = xpdu

        # make a more focused interpretation
        atype = bvl_pdu_types.get(pdu.bvlciFunction)
        if not atype:
            if _debug:
                decode_packet._debug("    - unknown BVLL type: %r", pdu.bvlciFunction)
            return pdu

        # decode it as one of the basic types
        try:
            xpdu = pdu
            bpdu = atype()
            bpdu.decode(pdu)
            if _debug:
                decode_packet._debug("    - bpdu: %r", bpdu)

            pdu = bpdu

            # source address in the packet
            pdu.pduSource = pdu.bvlciAddress

            # no deeper decoding for some
            if atype not in (OriginalUnicastNPDU, OriginalBroadcastNPDU):
                return pdu

        except Exception as err:
            if _debug:
                decode_packet._debug("    - decoding Error: %r", err)
            return xpdu

    # check for version number
    if pdu.pduData[0] != 0x01:
        if _debug:
            decode_packet._debug(
                "    - not a version 1 packet: %s...", btox(pdu.pduData[:30], ".")
            )
        return None

    # it's an NPDU
    try:
        npdu = NPDU()
        npdu.decode(pdu)
    except Exception as err:
        if _debug:
            decode_packet._debug("    - decoding Error: %r", err)
        return None

    # application or network layer message
    if npdu.npduNetMessage is None:
        if _debug:
            decode_packet._debug("    - not a network layer message, try as an APDU")

        # decode as a generic APDU
        try:
            xpdu = APDU()
            xpdu.decode(npdu)
            apdu = xpdu
        except Exception as err:
            if _debug:
                decode_packet._debug("    - decoding Error: %r", err)
            return npdu

        # "lift" the source and destination address
        if npdu.npduSADR:
            apdu.pduSource = npdu.npduSADR
        else:
            apdu.pduSource = npdu.pduSource
        if npdu.npduDADR:
            apdu.pduDestination = npdu.npduDADR
        else:
            apdu.pduDestination = npdu.pduDestination

        # make a more focused interpretation
        atype = apdu_types.get(apdu.apduType)
        if not atype:
            if _debug:
                decode_packet._debug("    - unknown APDU type: %r", apdu.apduType)
            return apdu

        # decode it as one of the basic types
        try:
            xpdu = apdu
            apdu = atype()
            apdu.decode(xpdu)
        except Exception as err:
            if _debug:
                decode_packet._debug("    - decoding Error: %r", err)
            return xpdu

        # decode it at the next level
        if isinstance(apdu, ConfirmedRequestPDU):
            atype = confirmed_request_types.get(apdu.apduService)
            if not atype:
                if _debug:
                    decode_packet._debug(
                        "    - no confirmed request decoder: %r", apdu.apduService
                    )
                return apdu

        elif isinstance(apdu, UnconfirmedRequestPDU):
            atype = unconfirmed_request_types.get(apdu.apduService)
            if not atype:
                if _debug:
                    decode_packet._debug(
                        "    - no unconfirmed request decoder: %r", apdu.apduService
                    )
                return apdu

        elif isinstance(apdu, SimpleAckPDU):
            atype = None

        elif isinstance(apdu, ComplexAckPDU):
            atype = complex_ack_types.get(apdu.apduService)
            if not atype:
                if _debug:
                    decode_packet._debug(
                        "    - no complex ack decoder: %r", apdu.apduService
                    )
                return apdu

        elif isinstance(apdu, SegmentAckPDU):
            atype = None

        elif isinstance(apdu, ErrorPDU):
            atype = error_types.get(apdu.apduService)
            if not atype:
                if _debug:
                    decode_packet._debug("    - no error decoder: %r", apdu.apduService)
                return apdu

        elif isinstance(apdu, RejectPDU):
            atype = None

        elif isinstance(apdu, AbortPDU):
            atype = None
        if _debug:
            decode_packet._debug("    - atype: %r", atype)

        # deeper decoding
        try:
            if atype:
                xpdu = apdu
                apdu = atype()
                apdu.decode(xpdu)
        except Exception as err:
            if _debug:
                decode_packet._debug("    - decoding error: %r", err)
            return xpdu

        # success
        return apdu

    else:
        # make a more focused interpretation
        ntype = npdu_types.get(npdu.npduNetMessage)
        if not ntype:
            if _debug:
                decode_packet._debug(
                    "    - no network layer decoder: %r", npdu.npduNetMessage
                )
            return npdu
        if _debug:
            decode_packet._debug("    - ntype: %r", ntype)

        # deeper decoding
        try:
            xpdu = npdu
            npdu = ntype()
            npdu.decode(xpdu)
        except Exception as err:
            if _debug:
                decode_packet._debug("    - decoding error: %r", err)
            return xpdu

        # success
        return npdu