Beispiel #1
0
    def receive_frag(self, bbuf, dtag):
        print('state: {}, recieved fragment -> {}, rule-> {}'.format(
            self.state, bbuf, self.rule))

        schc_frag = schcmsg.frag_receiver_rx(self.rule, bbuf)
        print("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()
        print(schc_frag)
        #
        if schc_frag.abort == True:
            print(
                "----------------------- Sender-Abort ---------------------------"
            )
            # XXX needs to release all resources.
            return
        self.tile_list.append(schc_frag.payload)
        #
        if schc_frag.fcn == schcmsg.get_fcn_all_1(self.rule):
            print(
                "----------------------- Final Reassembly -----------------------"
            )
            print("ALL1 received")
            # MIC calculation
            print("tile_list")
            for _ in self.tile_list:
                print(_)
            schc_packet = BitBuffer()
            for i in self.tile_list:
                schc_packet += i
            mic_calced = self.get_mic(schc_packet.get_content())
            if schc_frag.mic != mic_calced:
                print("ERROR: MIC mismatched. packet {} != result {}".format(
                    schc_frag.mic, mic_calced))
                self.state = 'ERROR_MIC_NO_ACK'
                return
            else:
                print("SUCCESS: MIC matched. packet {} == result {}".format(
                    schc_frag.mic, mic_calced))
            # decompression
            # print("----------------------- 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)
                #print("debug: no-ack FindRuleFromSCHCpacket", rule)
                self.protocol.process_decompress(schc_packet,
                                                 self.sender_L2addr, "UP")
            self.state = 'DONE_NO_ACK'
            #print(self.state)
            return
        # set inactive timer.
        self.event_id_inactive_timer = self.protocol.scheduler.add_event(
            self.inactive_timer, self.event_inactive, tuple())
        print("---", schc_frag.fcn)
Beispiel #2
0
 def receive_frag(self, bbuf, dtag):
     # XXX context should be passed from the lower layer.
     # XXX and pass the context to the parser.
     schc_frag = schcmsg.frag_receiver_rx(self.rule, bbuf)
     print("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 schc_frag.abort == True:
         print("Received Sender-Abort.")
         # XXX needs to release all resources.
         return
     self.tile_list.append(schc_frag.payload)
     #
     if schc_frag.fcn == schcmsg.get_fcn_all_1(self.rule):
         print("ALL1 received")
         # MIC calculation
         print("tile_list")
         for _ in self.tile_list:
             print(_)
         schc_packet = BitBuffer()
         for i in self.tile_list:
             schc_packet += i
         
         #schc_packet = bytearray(range(1, 100+1))
         mic_calced = self.get_mic(schc_packet.get_content())
         #mic_calced = self.get_mic(schc_packet)
         '''print()
         print("SCHC PACKET ", schc_packet)
         print()
         print("MIC calculado: ", mic_calced.hex())'''
         print()
         print("*****MIC recibido ",schc_frag.mic)
         print("*****MIC calculado ",mic_calced)
         if schc_frag.mic != mic_calced:
             print("ERROR: MIC mismatched. packet {} != result {}".format(
                     schc_frag.mic.hex(), mic_calced.hex()))
             return None
         # decompression
         self.protocol.process_decompress(self.context, self.sender_L2addr,
                                          schc_packet)
         
         return schc_packet
     # set inactive timer.
     self.event_id_inactive_timer = self.protocol.scheduler.add_event(
             self.inactive_timer, self.event_inactive, tuple())
     print("---", schc_frag.fcn)
     return None
Beispiel #3
0
 def receive_frag(self, bbuf, dtag):
     schc_frag = schcmsg.frag_receiver_rx(self.rule, bbuf)
     print("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 schc_frag.abort == True:
         print("Received Sender-Abort.")
         # XXX needs to release all resources.
         return
     if self.state == "DONE":
         print("XXX need sending ACK back.")
         return
     # 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"]
     # 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.
     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"])
     if self.mic_received is not None:
         schc_packet, mic_calced = self.get_mic_from_tiles_received()
         if self.mic_received == mic_calced:
             self.finish(schc_packet, schc_frag)
             return
         else:
             # XXX waiting for the fragments requested by ACK.
             # during MAX_ACK_REQUESTS
             print("waiting for more fragments.")
     elif schc_frag.fcn == schcmsg.get_fcn_all_1(self.rule):
         print("ALL1 received")
         self.mic_received = schc_frag.mic
         schc_packet, mic_calced = self.get_mic_from_tiles_received()
         if schc_frag.mic == mic_calced:
             self.finish(schc_packet, schc_frag)
             return
         else:
             print("ERROR: MIC mismatched. packet {} != result {}".format(
                 schc_frag.mic, mic_calced))
             bit_list = find_missing_tiles(self.tile_list,
                                           self.rule["FCNSize"],
                                           schcmsg.get_fcn_all_1(self.rule))
             assert bit_list is not None
             for bl_index in range(len(bit_list)):
                 print("missing wn={} bitmap={}".format(
                     bit_list[bl_index][0], bit_list[bl_index][1]))
                 # XXX compress bitmap if needed.
                 # ACK failure message
                 schc_ack = schcmsg.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])
                 print("ACK failure sent:", schc_ack.__dict__)
                 args = (schc_ack.packet.get_content(),
                         self.context["devL2Addr"])
                 self.protocol.scheduler.add_event(
                     0, self.protocol.layer2.send_packet, args)
                 # XXX need to keep the ack message for the ack request.
     # set inactive timer.
     self.event_id_inactive_timer = self.protocol.scheduler.add_event(
         self.inactive_timer, self.event_inactive, tuple())
     print("---", schc_frag.fcn)
    """ ReassemblerAckOnError class
    
    Todo : Redaction
    
    """
    # In ACK-on-Error, a fragment contains tiles belonging to different window.
    # A type of data structure holding tiles in each window is not suitable.  
    # So, here just appends a fragment into the tile_list like No-ACK.

    def receive_frag(self, bbuf, dtag):

<<<<<<< Updated upstream:src/schcrecv.py
        print('state: {}, recieved fragment -> {}, rule-> {}'.format(self.state,
                                                                     bbuf, self.rule))

        schc_frag = schcmsg.frag_receiver_rx(self.rule, bbuf)
        print("receiver frag received:", schc_frag.__dict__)
=======
        dprint('state: {}, received fragment -> {}, rule-> {}'.format(self.state,
                                                                     bbuf, self.rule))
        input("")
        schc_frag = frag_msg.frag_receiver_rx(self.rule, bbuf)
        dprint("receiver frag received:", schc_frag.__dict__)
>>>>>>> Stashed changes:src/frag_recv.py
        # 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.send_receiver_abort()
            return
        #
Beispiel #5
0
    def receive_frag(self, bbuf, dtag):
        
        print('state: {}, recieved fragment -> {}, rule-> {}'.format(self.state,
                                bbuf, self.rule))
        
        schc_frag = schcmsg.frag_receiver_rx(self.rule, bbuf)
        print("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.send_receiver_abort()
            return
        #
        #input("")
        if schc_frag.abort == True:
            print("Received Sender-Abort.")
            #Statsct.set_msg_type("SCHC_SENDER_ABORT")
            # XXX needs to release all resources.
            return
          
        if schc_frag.ack_request == True:
            print("Received 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 = schcmsg.frag_receiver_tx_abort(self.rule, self.dtag)
            #     args = (schc_frag.packet.get_content(), self.context["devL2Addr"])
            #     print("Sent Receiver-Abort.", schc_frag.__dict__)
            #     print("----------------------- SCHC RECEIVER ABORT SEND  -----------------------")

            #     if enable_statsct:
            #         Statsct.set_msg_type("SCHC_RECEIVER_ABORT")
            #         #Statsct.set_header_size(schcmsg.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
            print("XXX need sending ACK back.")
            #input('')
            self.resend_ack(schc_frag)
            return
        
        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"]
        # 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
        if schc_frag.payload.count_added_bits()%self.rule["tileSize"] != 0:
            #tile found that is smaller than a normal tile
            print("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:
                    print("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:
            print("w-num: {} t-num: {} nb_tiles:{}".format(
                    tile['w-num'],tile['t-num'],tile['nb_tiles']))
            #print("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"])

        if self.mic_received is not None:
            schc_packet, mic_calced = self.get_mic_from_tiles_received()
            if self.mic_received == mic_calced:
                self.finish(schc_packet, schc_frag)
                return
            else:
                # XXX waiting for the fragments requested by ACK.
                # during MAX_ACK_REQUESTS
                print("waiting for more fragments.")
        elif schc_frag.fcn == schcmsg.get_fcn_all_1(self.rule):
            print("----------------------- 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()
            print("schc_frag.mic: {}, mic_calced: {}".format(schc_frag.mic,mic_calced))
            if schc_frag.mic == mic_calced:
                self.mic_missmatched = False
                self.finish(schc_packet, schc_frag)
                return
            else:
                self.mic_missmatched = True
                print("----------------------- ERROR -----------------------")
                print("ERROR: MIC mismatched. packet {} != result {}".format(
                        schc_frag.mic, mic_calced))
                bit_list = find_missing_tiles_mic_ko_yes_all_1(self.tile_list,
                                              self.rule["FCNSize"],
                                              schcmsg.get_fcn_all_1(self.rule))
                
                assert bit_list is not None
                if len(bit_list) == 0:
                    #When the find_missing_tiles functions returns an empty array
                    #but we know something is missing because the MIC calculation is wrong
                    #this can happen when the first fragments are lost for example
                    print("bit list empty but the mic missmatched")
                    #if tiles are missing, then the packet is larger, should send a bitmap
                    #that considers the max_fcn and the tiles received
                    bit_list = find_missing_tiles_mic_ko_yes_all_1(self.tile_list,
                                                self.rule["FCNSize"],
                                                schcmsg.get_fcn_all_1(self.rule))
                    #print("new bit list, should it work???")
                    #input("")
                for bl_index in range(len(bit_list)):
                    print("missing wn={} bitmap={}".format(bit_list[bl_index][0],
                                                           bit_list[bl_index][1]))
                    # XXX compress bitmap if needed.
                    # ACK failure message
                    schc_ack = schcmsg.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")
                    print("----------------------- SCHC ACK KO SEND  -----------------------")

                    print("ACK failure sent:", schc_ack.__dict__)
                    args = (schc_ack.packet.get_content(),
                            self.context["devL2Addr"])
                    self.protocol.scheduler.add_event(
                            0, self.protocol.layer2.send_packet, args)
                    # XXX need to keep the ack message for the ack request.
                    break
        # set inactive timer.
        self.event_id_inactive_timer = self.protocol.scheduler.add_event(
                self.inactive_timer, self.event_inactive, tuple())
        print("---", schc_frag.fcn)
Beispiel #6
0
    def receive_frag(self, bbuf, dtag):

        print('state: {}, recieved fragment -> {}, rule-> {}'.format(
            self.state, bbuf, self.rule))

        schc_frag = schcmsg.frag_receiver_rx(self.rule, bbuf)
        print("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.send_receiver_abort()
            return
        #
        # input("")
        if schc_frag.abort == True:
            print(
                "----------------------- Sender-Abort ---------------------------"
            )
            # Statsct.set_msg_type("SCHC_SENDER_ABORT")
            # XXX needs to release all resources.
            return

        if schc_frag.ack_request == True:
            print("Received 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 = schcmsg.frag_receiver_tx_abort(self.rule, self.dtag)
            #     args = (schc_frag.packet.get_content(), self.context["devL2Addr"])
            #     print("Sent Receiver-Abort.", schc_frag.__dict__)
            #     print("----------------------- SCHC RECEIVER ABORT SEND  -----------------------")

            #     if enable_statsct:
            #         Statsct.set_msg_type("SCHC_RECEIVER_ABORT")
            #         #Statsct.set_header_size(schcmsg.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
            print("XXX need sending ACK back.")
            self.state = 'ACK_REQ'
            # input('')
            self.resend_ack(schc_frag)
            return

        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)
        print("---------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)
        ]
        print("---------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
                print("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:
                        print("tile is already in tile list")
                        tile_in_list = True
            if not tile_in_list:
                self.tile_list.append({
                    "w-num": win,
                    "t-num": fcn - idx,
                    "nb_tiles": 1,
                    "raw_tiles": tile_in_tiles
                })
                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)
            print('---------tile:', last_tile)
            tile_in_list = False
            for tile in self.tile_list:
                if tile["w-num"] == win:
                    if tile["t-num"] == 7:
                        print("tile is already in tile list")
                        tile_in_list = True
            if not tile_in_list:
                self.tile_list.append({
                    "w-num": win,
                    "t-num": 7,
                    "nb_tiles": 1,
                    "raw_tiles": last_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
        #     print("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:
        #             print("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:
            print("w-num: {} t-num: {} nb_tiles:{}".format(
                tile['w-num'], tile['t-num'], tile['nb_tiles']))
        print("")
        # print("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"])

        if self.mic_received is not None:
            schc_packet, mic_calced = self.get_mic_from_tiles_received()
            if self.mic_received == mic_calced:
                self.finish(schc_packet, schc_frag)
                return
            else:
                # XXX waiting for the fragments requested by ACK.
                # during MAX_ACK_REQUESTS
                print("waiting for more fragments.")
        elif schc_frag.fcn == schcmsg.get_fcn_all_1(self.rule):
            print(
                "----------------------- 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()
            print("schc_frag.mic: {}, mic_calced: {}".format(
                schc_frag.mic, mic_calced))
            if schc_frag.mic == mic_calced:
                print("SUCCESS: MIC matched. packet {} == result {}".format(
                    schc_frag.mic, mic_calced))
                self.mic_missmatched = False
                self.finish(schc_packet, schc_frag)
                return
            else:
                self.mic_missmatched = True
                self.state = 'ERROR_MIC'
                print("----------------------- ERROR -----------------------")
                print("ERROR: MIC mismatched. packet {} != result {}".format(
                    schc_frag.mic, mic_calced))
                bit_list = find_missing_tiles(
                    self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN],
                    schcmsg.get_fcn_all_1(self.rule))

                assert bit_list is not None

                schc_ack = self.create_ack_schc_ko(schc_frag)
                """
                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.
        # set inactive timer.
        self.event_id_inactive_timer = self.protocol.scheduler.add_event(
            self.inactive_timer, self.event_inactive, tuple())
        print("---", schc_frag.fcn)