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)
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)
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
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)