Пример #1
0
    def create_ack_schc_ko(self, schc_frag):
        """Create schc_ack packet in case of wrong RCS (C=0)
            return schc_ack packet
        """
        bit_list = find_missing_tiles_mic_ko_yes_all_1(
            self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
            frag_msg.get_fcn_all_1(self.rule))
        for tile in self.tile_list:
            dprint("w-num: {} t-num: {} nb_tiles:{}".format(
                tile['w-num'], tile['t-num'], tile['nb_tiles']))
            dprint("raw_tiles:{}".format(tile['raw_tiles']))
        dprint('send ack before done {}'.format(bit_list))
        assert bit_list is not None

        if bit_list:
            # Some tiles are actually missing, send ACK for first window with missing tiles
            dprint("missing wn={} bitmap={}".format(bit_list[0][0],
                                                    bit_list[0][1]))
            # XXX compress bitmap if needed.
            # ACK failure message
            schc_ack = frag_msg.frag_receiver_tx_all1_ack(
                schc_frag.rule,
                schc_frag.dtag,
                win=bit_list[0][0],
                cbit=0,
                bitmap=bit_list[0][1])
        elif not self.tile_list:
            dprint("No tile received before All-1, sending empty bitmap")
            # ACK failure message
            schc_ack = frag_msg.frag_receiver_tx_all1_ack(schc_frag.rule,
                                                          schc_frag.dtag,
                                                          win=0,
                                                          cbit=0,
                                                          bitmap=BitBuffer([]))
        else:
            window_list = make_bit_list_mic_ko(
                self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                frag_msg.get_fcn_all_1(self.rule))
            last_window = max(window_list.keys())

            # No tiles are detected missing, send ACK for last window
            dprint("No missing tiles, sending last window: wn={} bitmap={}".
                   format(last_window, BitBuffer(window_list[last_window])))
            # XXX compress bitmap if needed.
            # ACK failure message
            schc_ack = frag_msg.frag_receiver_tx_all1_ack(
                schc_frag.rule,
                schc_frag.dtag,
                win=last_window,
                cbit=0,
                bitmap=BitBuffer(window_list[last_window]))
        if enable_statsct:
            Statsct.set_msg_type("SCHC_ACK_KO")
        dprint(
            "----------------------- SCHC ACK KO SEND  -----------------------"
        )
        dprint("ACK failure sent:", schc_ack.__dict__)
        return schc_ack
Пример #2
0
    def resend_ack(self, schc_frag):
        dprint("resend ack method")
        dprint(schc_frag.__dict__)
        if self.mic_received is not None:
            schc_packet, mic_calced = self.get_mic_from_tiles_received()
            dprint("schc_frag.mic: {}, mic_calced: {}".format(
                self.mic_received, mic_calced))
            if self.mic_received == mic_calced:
                self.state = "DONE"
        if self.state == "DONE":
            # ACK message
            schc_ack = frag_msg.frag_receiver_tx_all1_ack(schc_frag.rule,
                                                          schc_frag.dtag,
                                                          schc_frag.win,
                                                          cbit=1)
            dprint("ACK success sent:", schc_ack.__dict__)
            if enable_statsct:
                Statsct.set_msg_type("SCHC_ACK_OK")
            dprint(
                "----------------------- SCHC ACK OK SEND  -----------------------"
            )
        else:
            if self.all1_received:
                dprint("all-1 received, building ACK")
                dprint('send ack before done {},{},{}'.format(
                    self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                    frag_msg.get_fcn_all_1(self.rule)))
                schc_ack = self.create_ack_schc_ko(schc_frag)
            else:
                #special case when the ALL-1 message is lost: 2 cases:
                #1) the all-1 carries a tile (bit in bitmap)
                #2) the all-1 only carries the MIC (no bit in bitmap)
                if self.fragment_received is False:
                    dprint("no fragments received yet, abort")
                    self.send_receiver_abort()

                    return
                dprint("all-1 not received, building ACK")
                dprint('send ack before done {},{},{}'.format(
                    self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                    frag_msg.get_fcn_all_1(self.rule)))
                for tile in self.tile_list:
                    dprint("w-num: {} t-num: {} nb_tiles:{}".format(
                        tile['w-num'], tile['t-num'], tile['nb_tiles']))
                    dprint("raw_tiles:{}".format(tile['raw_tiles']))

                bit_list = find_missing_tiles_no_all_1(
                    self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                    frag_msg.get_fcn_all_1(self.rule))
                dprint('send ack before done {}'.format(bit_list))
                assert bit_list is not None
                if len(bit_list) == 0:
                    bit_list = find_missing_tiles_no_all_1(
                        self.tile_list,
                        self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                        frag_msg.get_fcn_all_1(self.rule))
                for bl_index in range(len(bit_list)):
                    dprint("missing wn={} bitmap={}".format(
                        bit_list[bl_index][0], bit_list[bl_index][1]))
                    # XXX compress bitmap if needed.
                    # ACK failure message
                    schc_ack = frag_msg.frag_receiver_tx_all1_ack(
                        schc_frag.rule,
                        schc_frag.dtag,
                        win=bit_list[bl_index][0],
                        cbit=0,
                        bitmap=bit_list[bl_index][1])
                    if enable_statsct:
                        Statsct.set_msg_type("SCHC_ACK_KO")
                    dprint(
                        "----------------------- SCHC ACK KO SEND  -----------------------"
                    )

                    dprint("ACK failure sent:", schc_ack.__dict__)
        """
        Changement à corriger
        args = (schc_ack.packet.get_content(), self.context["devL2Addr"])
        """
        args = (schc_ack.packet.get_content(), "*")
        self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet,
                                          args)
Пример #3
0
    def receive_frag(self, bbuf, dtag):
        self._last_receive_info = []
        dprint('state: {}, recieved fragment -> {}, rule-> {}'.format(
            self.state, bbuf, self.rule))

        schc_frag = frag_msg.frag_receiver_rx(self.rule, bbuf)
        dprint("receiver frag received:", schc_frag.__dict__)
        # XXX how to authenticate the message from the peer. without
        # authentication, any nodes can cancel the invactive timer.
        self.cancel_inactive_timer()
        if self.state == "ABORT":
            self._last_receive_info = [("state-abort", )]
            self.send_receiver_abort()
            return
        #
        # input("")
        if schc_frag.abort == True:
            dprint(
                "----------------------- Sender-Abort ---------------------------"
            )
            # Statsct.set_msg_type("SCHC_SENDER_ABORT")
            # XXX needs to release all resources.
            self._last_receive_info = [("abort", )]
            return

        if schc_frag.ack_request == True:
            dprint("Received ACK-REQ")
            self._last_receive_info = [("ack-req", )]
            # if self.state != "DONE":
            #     #this can happen when the ALL-1 is not received, so the state is
            #     #not done and the sender is requesting an ACK.
            #     # sending Receiver abort.
            #     schc_frag = frag_msg.frag_receiver_tx_abort(self.rule, self.dtag)
            #     args = (schc_frag.packet.get_content(), self.context["devL2Addr"])
            #     dprint("Sent Receiver-Abort.", schc_frag.__dict__)
            #     dprint("----------------------- SCHC RECEIVER ABORT SEND  -----------------------")

            #     if enable_statsct:
            #         Statsct.set_msg_type("SCHC_RECEIVER_ABORT")
            #         #Statsct.set_header_size(frag_msg.get_sender_header_size(self.rule))
            #     self.protocol.scheduler.add_event(0,
            #                                 self.protocol.layer2.send_packet, args)
            #     # XXX needs to release all resources.
            #     return
            dprint("XXX need sending ACK back.")
            self.state = 'ACK_REQ'
            # input('')
            self.resend_ack(schc_frag)
            return

        info = self._last_receive_info
        self.fragment_received = True
        # append the payload to the tile list.
        # padding truncation is done later. see below.
        # nb_tiles = schc_frag.payload.count_added_bits()//self.rule["tileSize"]
        tile_size = self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_TILE]
        nb_tiles, last_tile_size = (schc_frag.payload.count_added_bits() //
                                    tile_size,
                                    schc_frag.payload.count_added_bits() %
                                    tile_size)
        info.append(("tile-info", {
            "nb-tiles": nb_tiles,
            "tile-size": tile_size,
            "last-tile-size": last_tile_size
        }))
        dprint("---------nb_tiles: ", nb_tiles, " -----last_tile_size ",
               last_tile_size)
        tiles = [
            schc_frag.payload.get_bits_as_buffer(tile_size)
            for _ in range(nb_tiles)
        ]
        dprint("---------tiles: ", tiles)

        # Note that nb_tiles is the number of tiles which is exact number of the
        # size of the tile.  the tile of which the size is less than the size
        # is not included.
        # The tile that is less than a tile size must be included, so a 1 can be added
        # in the bitmap when there is a tile in the all-1 message

        win = schc_frag.win
        fcn = schc_frag.fcn
        for tile_in_tiles in tiles:
            idx = tiles.index(tile_in_tiles)
            if tile_in_tiles.count_added_bits() % tile_size != 0:
                # tile found that is smaller than a normal tile
                dprint("tile found that is smaller than a normal tile")
                # nb_tiles = 1
            # tile should only be append if it is not in the list
            tile_in_list = False
            for tile in self.tile_list:
                if tile["w-num"] == win:
                    if tile["t-num"] == fcn - idx:
                        dprint("tile is already in tile list")
                        tile_in_list = True
                        info.append(("known-tile", idx, tile))
            if not tile_in_list:
                new_tile = ({
                    "w-num": win,
                    "t-num": fcn - idx,
                    "nb_tiles": 1,
                    "raw_tiles": tile_in_tiles
                })
                self.tile_list.append(new_tile)
                info.append(("new-tile", idx, new_tile))
                self.tile_list = sort_tile_list(
                    self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN])
            if (fcn - idx) == 0:
                win += 1
                fcn = self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN] << 1
                tiles = tiles[(idx + 1):]

        # !IMPORTANT: it's neccesary to change this condition for one more exact which consider the last tile size cases
        if last_tile_size > 8:
            last_tile = schc_frag.payload.get_bits_as_buffer(last_tile_size)
            dprint('---------tile:', last_tile)
            info.append(("last-tile", last_tile))
            tile_in_list = False
            for tile in self.tile_list:
                if tile["w-num"] == win:
                    if tile["t-num"] == 7:  # XXX: why 7?
                        dprint("tile is already in tile list")
                        tile_in_list = True
                        info.append(("known-last-tile", None, tile))
            if not tile_in_list:
                new_tile = ({
                    "w-num": win,
                    "t-num": 7,
                    "nb_tiles": 1,
                    "raw_tiles": last_tile
                })
                self.tile_list.append(new_tile)
                info.append(("new-last-tile", None, tile))
                self.tile_list = sort_tile_list(
                    self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN])

        # if schc_frag.payload.count_added_bits()%self.rule["tileSize"] != 0:
        #     #tile found that is smaller than a normal tile
        #     dprint("tile found that is smaller than a normal tile")
        #     #nb_tiles = 1
        # #tile should only be append if it is not in the list
        # tile_in_list = False
        # for tile in self.tile_list:
        #     if tile["w-num"] == schc_frag.win:
        #         if tile["t-num"] == schc_frag.fcn:
        #             dprint("tile is already in tile list")
        #             tile_in_list = True
        # if not tile_in_list:
        #     self.tile_list.append({
        #             "w-num": schc_frag.win,
        #             "t-num": schc_frag.fcn,
        #             "nb_tiles": nb_tiles,
        #             "raw_tiles":schc_frag.payload})
        #     self.tile_list = sort_tile_list(self.tile_list, self.rule["FCNSize"])
        for tile in self.tile_list:
            dprint("w-num: {} t-num: {} nb_tiles:{}".format(
                tile['w-num'], tile['t-num'], tile['nb_tiles']))
        dprint("")
        # dprint("raw_tiles:{}".format(tile['raw_tiles']))
        # self.tile_list = sort_tile_list(self.tile_list, self.rule["WSize"])

        # self.tile_list.append({
        #         "w-num": schc_frag.win,
        #         "t-num": schc_frag.fcn,
        #         "nb_tiles": nb_tiles,
        #         "raw_tiles":schc_frag.payload})
        # self.tile_list = sort_tile_list(self.tile_list, self.rule["FCNSize"])
        # self.tile_list = sort_tile_list(self.tile_list, self.rule["WSize"])

        should_send_ack = False
        if self.mic_received is not None:
            schc_packet, mic_calced = self.get_mic_from_tiles_received()
            if self.mic_received == mic_calced:
                info.append("mic-ok")
                self.finish(schc_packet, schc_frag)
                return
            else:
                # XXX waiting for the fragments requested by ACK.
                # during MAX_ACK_REQUESTS
                info.append("mic-not-ok")
                dprint("waiting for more fragments.")
                # XXX: do that only when necessary (one per window):
                #should_send_ack = True
        elif schc_frag.fcn == frag_msg.get_fcn_all_1(self.rule):
            # XXX: what if you receive two MICs?
            dprint(
                "----------------------- ALL1 received -----------------------"
            )
            self.all1_received = True
            Statsct.set_msg_type("SCHC_ALL_1")
            self.mic_received = schc_frag.mic
            schc_packet, mic_calced = self.get_mic_from_tiles_received()
            dprint("schc_frag.mic: {}, mic_calced: {}".format(
                schc_frag.mic, mic_calced))
            info.append(("mic-received", schc_frag.mic, mic_calced))
            if schc_frag.mic == mic_calced:
                dprint("SUCCESS: MIC matched. packet {} == result {}".format(
                    schc_frag.mic, mic_calced))
                info.append("mic-ok")
                self.mic_missmatched = False
                self.finish(schc_packet, schc_frag)
                return
            else:
                self.mic_missmatched = True
                self.state = 'ERROR_MIC'
                info.append("mic-not-ok")  # XXX: how do you leave ERROR state?
                dprint("----------------------- ERROR -----------------------")
                dprint("ERROR: MIC mismatched. packet {} != result {}".format(
                    schc_frag.mic, mic_calced))
                should_send_ack = True

        if should_send_ack:
            bit_list = find_missing_tiles(
                self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                frag_msg.get_fcn_all_1(self.rule))

            assert bit_list is not None
            schc_ack = self.create_ack_schc_ko(schc_frag)
            info.append(("mic-ack", bit_list))
            """
                Changement à corriger
                args = (schc_ack.packet.get_content(), self.context["devL2Addr"])
                """
            args = (schc_ack.packet.get_content(), '*')
            self.protocol.scheduler.add_event(0,
                                              self.protocol.layer2.send_packet,
                                              args)
            # XXX need to keep the ack message for the ack request.

        # XXX: maybe set timer in all cases (there are 'return's above)
        # set inactive timer.
        self.event_id_inactive_timer = self.protocol.scheduler.add_event(
            self.inactive_timer, self.event_inactive, tuple())
        dprint("---", schc_frag.fcn)
Пример #4
0
    def receive_frag(self, bbuf, dtag):
        dprint('state: {}, received fragment -> {}, rule-> {}'.format(
            self.state, bbuf, self.rule))

        schc_frag = frag_msg.frag_receiver_rx(self.rule, bbuf)
        dprint("receiver frag received:", schc_frag.__dict__)

        if schc_frag.rule[T_FRAG][T_FRAG_PROF][T_FRAG_DTAG] == 0:
            w_dtag = '-'
        else:
            w_dtag = schc_frag.dtag

        if schc_frag.rule[T_FRAG][T_FRAG_PROF][T_FRAG_W] == 0:
            w_w = '-'
        else:
            w_w = schc_frag.win

        all1 = 2**self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN] - 1
        if schc_frag.fcn == all1:
            w_fcn = "All-1"
        elif schc_frag.fcn == 0:
            w_fcn = "All-0"
        else:
            w_fcn = schc_frag.fcn

        dtrace("\t\t\tr:{}/{} (noA) DTAG={} W={} FCN={}".format(
            schc_frag.rule[T_RULEID], schc_frag.rule[T_RULEIDLENGTH], w_dtag,
            w_w, w_fcn))

        # XXX how to authenticate the message from the peer. without
        # authentication, any nodes can cancel the invactive timer.
        self.cancel_inactive_timer()
        dprint(schc_frag.__class__.__name__)
        #
        if schc_frag.abort == True:
            dprint(
                "----------------------- Sender-Abort ---------------------------"
            )
            # XXX needs to release all resources.
            return
        self.tile_list.append(schc_frag.payload)
        #
        if schc_frag.fcn == frag_msg.get_fcn_all_1(self.rule):
            dprint(
                "----------------------- Final Reassembly -----------------------"
            )
            dprint("ALL1 received")
            # MIC calculation
            dprint("tile_list")
            for _ in self.tile_list:
                dprint(_)
            schc_packet = BitBuffer()
            for i in self.tile_list:
                schc_packet += i
            dtrace(binascii.hexlify(schc_packet.get_content()))

            mic_calced = self.get_mic(schc_packet.get_content())
            if schc_frag.mic != mic_calced:
                dtrace("ERROR: MIC mismatched. packet {} != result {}".format(
                    schc_frag.mic, mic_calced))
                self.state = 'ERROR_MIC_NO_ACK'
                return
            else:
                dtrace("SUCCESS: MIC matched. packet {} == result {}".format(
                    schc_frag.mic, mic_calced))
            # decompression
            # dprint("----------------------- Decompression -----------------------")
            if not self.protocol.config.get("debug-fragment"):
                # XXX
                # XXX in hack105, we have separate databases for C/D and F/R.
                # XXX need to merge them into one.  Then, here searching database will
                # XXX be moved into somewhere.
                # XXX
                #rule = self.protocol.rule_manager.FindRuleFromSCHCpacket(schc=schc_packet)
                #dprint("debug: no-ack FindRuleFromSCHCpacket", rule)
                self.protocol.process_decompress(schc_packet,
                                                 self.sender_L2addr, "UP")
            self.state = 'DONE_NO_ACK'
            #dprint(self.state)
            return
        # set inactive timer.
        self.event_id_inactive_timer = self.protocol.scheduler.add_event(
            self.inactive_timer, self.event_inactive, tuple())
        dprint("---", schc_frag.fcn)
Пример #5
0
    def send_frag(self):
        if self.state == self.ACK_SUCCESS:
            dprint(
                "-----------------------------------------------------------------------"
            )
            return
        dprint(
            "----------------------- Preparing to send a message -----------------------"
        )
        scheduler = self.protocol.system.get_scheduler()
        dprint("{} send_frag!!!!!!!!!!!!!!!!!".format(
            scheduler.get_clock()))  # utime.time()
        dprint("all1_send-> {}, resend -> {}, state -> {}".format(
            self.all1_send, self.resend, self.state))
        dprint("all tiles unsend -> {}".format(self.all_tiles.get_all_tiles()))
        for tile in self.all_tiles.get_all_tiles():
            dprint("w: {}, t: {}, sent: {}".format(tile['w-num'],
                                                   tile['t-num'],
                                                   tile['sent']))
        dprint("")
        # if self.state == self.ACK_FAILURE and self.num_of_windows != 1 and self.number_of_ack_waits <= self.num_of_windows:
        #     #waiting for the acks of the others windows
        #     self.number_of_ack_waits += 1 #wait depends on the number of windows
        #     #set ack_time_out_timer
        #     dprint("waiting for more acks: {}".format(self.number_of_ack_waits))
        #     return

        # get contiguous tiles as many as possible fit in MTU.
        mtu_size = self.protocol.layer2.get_mtu_size()
        window_tiles, nb_remaining_tiles, remaining_size = self.all_tiles.get_tiles(
            mtu_size)
        dprint(
            "----window tiles to send: {}, nb_remaining_tiles: {}, remaining_size: {}"
            .format(window_tiles, nb_remaining_tiles, remaining_size))

        if window_tiles is None and self.resend:
            dprint("no more tiles to resend")
            # how to identify that all tiles are resend and that the ack timeout should be set
            # to wait for tha last ok. It should be set after retransmission of the last fragment
            if self.state == self.ACK_FAILURE and self.event_id_ack_wait_timer is None:
                win = self.last_window_tiles[0][
                    "w-num"] if self.last_window_tiles[0][
                        "w-num"] is not None else 0
                if self.last_window_tiles[0]["t-num"] == 0:
                    win += 1
                schc_frag = frag_msg.frag_sender_tx(self.rule,
                                                    dtag=self.dtag,
                                                    win=win,
                                                    fcn=frag_msg.get_fcn_all_1(
                                                        self.rule),
                                                    mic=self.mic_sent)
                # set ack waiting timer
                args = (
                    schc_frag,
                    win,
                )
                self.event_id_ack_wait_timer = self.protocol.scheduler.add_event(
                    self.ack_wait_timer, self.ack_timeout, args)
                dprint("*******event id {}".format(
                    self.event_id_ack_wait_timer))
                # if self.all1_send and self.state == self.ACK_FAILURE:
            #     #case when with the bitmap is not possible to identify the missing tile,
            #     #resend ALL-1 messages
            #     # send a SCHC fragment
            #     args = (self.schc_all_1.packet.get_content(), self._session_id[0],
            #     self.event_sent_frag)
            #     dprint("frag sent:", self.schc_all_1.__dict__)
            #     if enable_statsct:
            #         Statsct.set_msg_type("SCHC_ALL_1")
            #         Statsct.set_header_size(frag_msg.get_sender_header_size(self.rule) +
            #             frag_msg.get_mic_size(self.rule))
            #     self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet,
            #                                     args)
            #     dprint("Sending all-1 beacuse there is an ACK FaILURE but cannot find the missing tiles")
            #     input("")

            # win = self.last_window_tiles[0]["w-num"] if self.last_window_tiles[0]["w-num"] is not None else 0
            # if self.last_window_tiles[0]["t-num"] == 0:
            #     win += 1
            # schc_frag = frag_msg.frag_sender_tx(
            #         self.rule, dtag=self.dtag, win=win,
            #         fcn=frag_msg.get_fcn_all_1(self.rule),
            #         mic=self.mic_sent)
            # set ack waiting timer
            # args = (schc_frag, win,)
            # self.event_id_ack_wait_timer = self.protocol.scheduler.add_event(
            #        self.ack_wait_timer, self.ack_timeout, args)
            # dprint("*******event id {}".format(self.event_id_ack_wait_timer))

        # if window_tiles is not None and not self.all1_send and not self.resend:

        if window_tiles is not None:

            dprint("window_tiles is not None -> {}, resend -> {}".format(
                self.all1_send, self.resend))
            dprint("")
            # even when mic is sent, it comes here in the retransmission.
            if self.all1_send and self.state != self.ACK_FAILURE:
                # when there is a retransmission, the all-1 is send again and is send before
                # the ACK-OK is received. One option is not set the timer after the last
                # retransmission, but i don´t know how to idenfy that all missing fragments
                # have been send. Also the ALL-1 is not retransmisted (dont know if this should
                # be like this. For example, if the ALL-1s is lost, the receiver timer expires
                # and a receiver abort is send. If it arrives, there is not need to retransmit
                # the message after the retransmission of the missing fragments)
                # FIX, all-1 is resend
                dprint('All-1 ones already send')
                # cancel timer when there is success
                # if self.event_id_ack_wait_timer and self.state == self.ACK_SUCCESS:
                #     self.cancel_ack_wait_timer()
                # else:
                #    dprint("how to add a timer without sending a message")
                # fcn = frag_msg.get_fcn_all_1(self.rule)
                # args = (schc_frag, window_tiles[0]["w-num"],)
                # elf.event_id_ack_wait_timer = self.protocol.scheduler.add_event(
                # self.ack_wait_timer, self.ack_timeout, args)
                # fcn = frag_msg.get_fcn_all_1(self.rule)
                return
            elif (nb_remaining_tiles == 0 and len(window_tiles) == 1
                  and remaining_size >= frag_msg.get_mic_size(self.rule)):
                dprint("MESSSAGE TYPE ----> ALL-1 prepared")

                # make the All-1 frag with this tile.
                # the All-1 fragment can carry only one tile of which the size
                # is less than L2 word size.
                fcn = frag_msg.get_fcn_all_1(self.rule)
                last_frag_base_size = (
                    frag_msg.get_sender_header_size(self.rule) +
                    frag_msg.get_mic_size(self.rule) +
                    TileList.get_tile_size(window_tiles))
                # check if mic exists, no need no created again
                if self.mic_sent is None:
                    mic = self.get_mic(self.mic_base, last_frag_base_size)
                    # store the mic in order to know all-1 has been sent.
                    self.mic_sent = mic
                else:
                    mic = self.mic_sent
                dprint("mic_sent -> {}".format(self.mic_sent))
                if enable_statsct:
                    Statsct.set_msg_type("SCHC_ALL_1")
                    Statsct.set_header_size(
                        frag_msg.get_sender_header_size(self.rule) +
                        frag_msg.get_mic_size(self.rule))
                self.all1_send = True
                self.state = self.SEND_ALL_1
            else:
                dprint("MESSSAGE TYPE ----> regular SCHC frag")
                dprint("")
                # regular fragment.
                fcn = window_tiles[0]["t-num"]
                mic = None

                if enable_statsct:
                    Statsct.set_msg_type("SCHC_FRAG")
                    Statsct.set_header_size(
                        frag_msg.get_sender_header_size(self.rule))

            schc_frag = frag_msg.frag_sender_tx(
                self.rule,
                dtag=self.dtag,
                win=window_tiles[0]["w-num"],
                fcn=fcn,
                mic=mic,
                payload=TileList.concat(window_tiles))

            if mic is not None:
                dprint("mic is not None")
                # set ack waiting timer
                if enable_statsct:
                    Statsct.set_msg_type("SCHC_FRAG")
                    Statsct.set_header_size(
                        frag_msg.get_sender_header_size(self.rule))
                args = (
                    schc_frag,
                    window_tiles[0]["w-num"],
                )
                dprint("all ones")
                self.schc_all_1 = schc_frag
                self.event_id_ack_wait_timer = self.protocol.scheduler.add_event(
                    self.ack_wait_timer, self.ack_timeout, args)
                dprint("*******event id {}".format(
                    self.event_id_ack_wait_timer))
            # save the last window tiles.
            self.last_window_tiles = window_tiles
            dprint("self.last_window_tiles -> {}".format(
                self.last_window_tiles))
        elif self.mic_sent is not None or self.all1_send:
            dprint("self.mic_sent is not None state -> {}".format(self.state))
            # it looks that all fragments have been sent.
            dprint(
                "----------------------- all tiles have been sent -----------------------",
                window_tiles, nb_remaining_tiles, remaining_size)
            schc_frag = None
            self.all1_send = True
            if self.event_id_ack_wait_timer and self.state == self.ACK_SUCCESS:
                self.cancel_ack_wait_timer()
            return
        else:
            dprint("only mic all tiles send")
            # Here, only MIC will be sent since all tiles has been sent.
            assert self.last_window_tiles is not None
            # As the MTU would be changed anytime AND the size of the
            # significant padding bits would be changed, therefore the MIC
            # calculation may be needed again.
            # XXX maybe it's better to check whether the size of MTU is change
            # or not when the previous MIC was calculated..
            last_frag_base_size = (
                frag_msg.get_sender_header_size(self.rule) +
                TileList.get_tile_size(self.last_window_tiles))
            self.mic_sent = self.get_mic(self.mic_base, last_frag_base_size)
            # check the win number.
            # XXX if the last tile number is zero, here window number has to be
            # incremented.
            win = self.last_window_tiles[0]["w-num"]
            if self.last_window_tiles[0]["t-num"] == 0:
                win += 1
            schc_frag = frag_msg.frag_sender_tx(self.rule,
                                                dtag=self.dtag,
                                                win=win,
                                                fcn=frag_msg.get_fcn_all_1(
                                                    self.rule),
                                                mic=self.mic_sent)
            # set ack waiting timer
            args = (
                schc_frag,
                win,
            )
            if enable_statsct:
                Statsct.set_msg_type("SCHC_ALL_1")
                Statsct.set_header_size(
                    frag_msg.get_sender_header_size(self.rule) +
                    frag_msg.get_mic_size(self.rule))
            self.schc_all_1 = schc_frag
            self.state = self.SEND_ALL_1
            self.event_id_ack_wait_timer = self.protocol.scheduler.add_event(
                self.ack_wait_timer, self.ack_timeout, args)
            dprint("*******event id {}".format(self.event_id_ack_wait_timer))

        # send a SCHC fragment
        args = (schc_frag.packet.get_content(), self._session_id[0],
                self.event_sent_frag)
        dprint("frag sent:", schc_frag.__dict__)
        self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet,
                                          args)
Пример #6
0
    def send_frag(self):
        # XXX
        # because No-ACK mode supports variable MTU,
        # sender can't know the fact that it can't send all fragments
        # before it reachs to send the last fragment (All-1).
        #
        #     The All-1 fragment MUST be formed like below.
        #
        #     | header | MIC |    last tile     |
        #                    |<- L2 word size ->|
        #                                       |<- L2 Word
        #
        #     if the size of header+MIC+tile doesn't fit the L2 Word,
        #
        #     | header | MIC |     last tile    |    padding    |
        #                    |<- L2 word size ->|<- less than ->|
        #                                         L2 word size
        #                                                       |<- L2 Word
        payload_size = (self.protocol.layer2.get_mtu_size() -
                        frag_msg.get_sender_header_size(self.rule))
        remaining_data_size = self.packet_bbuf.count_remaining_bits()
        if remaining_data_size >= payload_size:
            dprint(
                "----------------------- Fragmentation process -----------------------"
            )
            # put remaining_size of bits of packet into the tile.
            tile = self.packet_bbuf.get_bits_as_buffer(payload_size)
            transmit_callback = self.event_sent_frag
            fcn = 0
            self.mic_sent = None

            if enable_statsct:
                Statsct.set_msg_type("SCHC_FRAG")
                Statsct.set_header_size(
                    frag_msg.get_sender_header_size(self.rule))
        elif remaining_data_size < payload_size:
            dprint(
                "----------------------- Fragmentation process -----------------------"
            )
            if remaining_data_size <= (payload_size -
                                       frag_msg.get_mic_size(self.rule)):
                tile = None
                if remaining_data_size > 0:
                    tile = self.packet_bbuf.get_bits_as_buffer()
                # make All-1 frag.
                assert self.mic_sent is None
                last_frag_base_size = 0
                if tile is not None:
                    last_frag_base_size += (
                        frag_msg.get_sender_header_size(self.rule) +
                        frag_msg.get_mic_size(self.rule) + remaining_data_size)
                self.mic_sent = self.get_mic(self.mic_base,
                                             last_frag_base_size)
                # callback doesn't need in No-ACK mode.
                transmit_callback = None
                fcn = frag_msg.get_fcn_all_1(self.rule)
                if enable_statsct:
                    Statsct.set_msg_type("SCHC_ALL_1 ")
                    Statsct.set_header_size(
                        frag_msg.get_sender_header_size(self.rule) +
                        frag_msg.get_mic_size(self.rule))
            else:
                # put the size of the complements of the header to L2 Word.
                tile_size = (remaining_data_size -
                             (frag_msg.get_sender_header_size(self.rule) +
                              remaining_data_size) % self.l2word)
                tile = self.packet_bbuf.get_bits_as_buffer(tile_size)
                transmit_callback = self.event_sent_frag
                fcn = 0
                self.mic_sent = None
                if enable_statsct:
                    Statsct.set_msg_type("SCHC_FRAG")
                    Statsct.set_header_size(
                        frag_msg.get_sender_header_size(self.rule))
        schc_frag = frag_msg.frag_sender_tx(self.rule,
                                            dtag=self.dtag,
                                            win=None,
                                            fcn=fcn,
                                            mic=self.mic_sent,
                                            payload=tile)

        # send a SCHC fragment
        args = (schc_frag.packet.get_content(), self._session_id[0],
                transmit_callback)
        dprint("frag sent:", schc_frag.__dict__)
        if self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_DTAG] == 0:
            w_dtag = '-'
        else:
            w_dtag = schc_frag.dtag

        if self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_W] == 0:
            w_w = '-'
        else:
            w_w = schc_frag.win

        all1 = 2**self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN] - 1
        if schc_frag.fcn == all1:
            w_fcn = "All-1"
        elif schc_frag.fcn == 0:
            w_fcn = "All-0"
        else:
            w_fcn = schc_frag.fcn

        dtrace("r:{}/{} (noA) DTAG={} W={} FCN={}".format(
            self.rule[T_RULEID], self.rule[T_RULEIDLENGTH], w_dtag, w_w,
            w_fcn))
        dtrace("|----{:3}------------->".format(len(
            schc_frag.packet._content)))

        self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet,
                                          args)