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], schcmsg.get_fcn_all_1(self.rule)) 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'])) print('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 print("missing wn={} bitmap={}".format(bit_list[0][0], bit_list[0][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[0][0], cbit=0, bitmap=bit_list[0][1]) else: window_list = make_bit_list_mic_ko( self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN], schcmsg.get_fcn_all_1(self.rule)) last_window = max(window_list.keys()) # No tiles are detected missing, send ACK for last window print("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 = schcmsg.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") print( "----------------------- SCHC ACK KO SEND -----------------------" ) print("ACK failure sent:", schc_ack.__dict__) return schc_ack
def set_packet(self, packet_bbuf): super().set_packet(packet_bbuf) self.all_tiles = TileList(self.rule, packet_bbuf) #print('all tils : ') #print(self.all_tiles.get_all_tiles()) # XXX # check whether the size of the last tile is less than L2 word # AND the tile number is zero # because draft-17 doesn't specify how to handle it. #print('set_packet RULE: ',self.rule) profile = self.rule["profile"] L2WordSize = profile["L2WordSize"] print('L2WordSize: ', L2WordSize) a = self.all_tiles.get_all_tiles() #print('all tiles: ', a) if (a[-1]["t-num"] == 0 and a[-1]["tile"].count_added_bits() < L2WordSize): raise ValueError( "The size of the last tile with the tile number 0 must be equal to or greater than L2 word size." ) # make the bitmap frag = self.rule["fragmentation"] Mode = frag["FRModeProfile"] FCNSize = Mode["FCNSize"] self.bit_list = make_bit_list(self.all_tiles.get_all_tiles(), FCNSize, schcmsg.get_fcn_all_1(self.rule))
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)
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
def set_packet(self, packet_bbuf): super().set_packet(packet_bbuf) self.all_tiles = TileList(self.rule, packet_bbuf) # XXX # check whether the size of the last tile is less than L2 word # AND the tile number is zero # because draft-17 doesn't specify how to handle it. a = self.all_tiles.get_all_tiles() if (a[-1]["t-num"] == 0 and a[-1]["tile"].count_added_bits() < self.rule["L2WordSize"]): raise ValueError( "The size of the last tile with the tile number 0 must be equal to or greater than L2 word size." ) # make the bitmap self.bit_list = make_bit_list(self.all_tiles.get_all_tiles(), self.rule["FCNSize"], schcmsg.get_fcn_all_1(self.rule)) print("bit_list:", self.bit_list)
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 send_frag(self): print("{} send_frag!!!!!!!!!!!!!!!!!".format(utime.time())) print("all1_send-> {}, resend -> {}, state -> {}".format( self.all1_send, self.resend, self.state)) print("all tiles unsend -> {}".format(self.all_tiles)) for tile in self.all_tiles.get_all_tiles(): print("w: {}, t: {}, sent: {}".format(tile['w-num'], tile['t-num'], tile['sent'])) if self.state == self.ACK_SUCCESS: return # 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 # print("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) print("window tiles: {}, nb_remaining_tiles: {}, remaining_size: {}". format(window_tiles, nb_remaining_tiles, remaining_size)) if window_tiles is None and self.resend: print("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 = schcmsg.frag_sender_tx(self.rule, dtag=self.dtag, win=win, fcn=schcmsg.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) print("*******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.context["devL2Addr"], # self.event_sent_frag) # print("frag sent:", self.schc_all_1.__dict__) # if enable_statsct: # Statsct.set_msg_type("SCHC_ALL_1") # Statsct.set_header_size(schcmsg.get_sender_header_size(self.rule) + # schcmsg.get_mic_size(self.rule)) # self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet, # args) # print("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 = schcmsg.frag_sender_tx( # self.rule, dtag=self.dtag, win=win, # fcn=schcmsg.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) #print("*******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: print("window_tiles is not None -> {}, resend -> {}".format( self.all1_send, self.resend)) # 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 print('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: # print("how to add a timer without sending a message") # fcn = schcmsg.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 = schcmsg.get_fcn_all_1(self.rule) return elif (nb_remaining_tiles == 0 and len(window_tiles) == 1 and remaining_size >= schcmsg.get_mic_size(self.rule)): print("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 = schcmsg.get_fcn_all_1(self.rule) last_frag_base_size = ( schcmsg.get_sender_header_size(self.rule) + schcmsg.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 print("mic_sent -> {}".format(self.mic_sent)) if enable_statsct: Statsct.set_msg_type("SCHC_ALL_1") Statsct.set_header_size( schcmsg.get_sender_header_size(self.rule) + schcmsg.get_mic_size(self.rule)) self.all1_send = True self.state = self.SEND_ALL_1 else: print("regular SCHC frag") # regular fragment. fcn = window_tiles[0]["t-num"] mic = None if enable_statsct: Statsct.set_msg_type("SCHC_FRAG") Statsct.set_header_size( schcmsg.get_sender_header_size(self.rule)) schc_frag = schcmsg.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: print("mic is not None") # set ack waiting timer #if enable_statsct: # Statsct.set_msg_type("SCHC_FRAG") # Statsct.set_header_size(schcmsg.get_sender_header_size(self.rule)) args = ( schc_frag, window_tiles[0]["w-num"], ) print("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) print("*******event id {}".format( self.event_id_ack_wait_timer)) # save the last window tiles. self.last_window_tiles = window_tiles print("self.last_window_tiles -> {}".format( self.last_window_tiles)) elif self.mic_sent is not None or self.all1_send: print("self.mic_sent is not None state -> {}".format(self.state)) # it looks that all fragments have been sent. print( "----------------------- 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: print("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 = ( schcmsg.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 = schcmsg.frag_sender_tx(self.rule, dtag=self.dtag, win=win, fcn=schcmsg.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( schcmsg.get_sender_header_size(self.rule) + schcmsg.get_mic_size(self.rule)) 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) print("*******event id {}".format(self.event_id_ack_wait_timer)) # send a SCHC fragment args = (schc_frag.packet.get_content(), self.context["devL2Addr"], self.event_sent_frag) print("frag sent:", schc_frag.__dict__) self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet, args)
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() - schcmsg.get_sender_header_size(self.rule)) remaining_data_size = self.packet_bbuf.count_remaining_bits() if remaining_data_size >= payload_size: # 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( schcmsg.get_sender_header_size(self.rule)) elif remaining_data_size < payload_size: if remaining_data_size <= (payload_size - schcmsg.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 += ( schcmsg.get_sender_header_size(self.rule) + schcmsg.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 = schcmsg.get_fcn_all_1(self.rule) if enable_statsct: Statsct.set_msg_type("SCHC_ALL_1") Statsct.set_header_size( schcmsg.get_sender_header_size(self.rule) + schcmsg.get_mic_size(self.rule)) else: # put the size of the complements of the header to L2 Word. tile_size = (remaining_data_size - (schcmsg.get_sender_header_size(self.rule) + remaining_data_size) % self.rule["L2WordSize"]) 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( schcmsg.get_sender_header_size(self.rule)) schc_frag = schcmsg.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.context["devL2Addr"], transmit_callback) print("frag sent:", schc_frag.__dict__) self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet, args)
def send_frag(self): # 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) #print('**********window_tiles: ', window_tiles) #print('**********nb_remaining_tiles: ', nb_remaining_tiles) #print('**********remaining_size: ', remaining_size) if window_tiles is not None: # even when mic is sent, it comes here in the retransmission. if (nb_remaining_tiles == 0 and len(window_tiles) == 1 and remaining_size >= schcmsg.get_mic_size(self.rule)): #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 = schcmsg.get_fcn_all_1(self.rule) last_frag_base_size = ( schcmsg.get_sender_header_size(self.rule) + schcmsg.get_mic_size(self.rule) + TileList.get_tile_size(window_tiles)) 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: # regular fragment. fcn = window_tiles[0]["t-num"] mic = None schc_frag = schcmsg.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: # set ack waiting timer args = ( schc_frag, window_tiles[0]["w-num"], ) self.event_id_ack_wait_timer = self.protocol.scheduler.add_event( self.ack_wait_timer, self.ack_timeout, args) # save the last window tiles. self.last_window_tiles = window_tiles elif self.mic_sent is not None: # it looks that all fragments have been sent. print("xxx looks all tiles have been sent.", window_tiles, nb_remaining_tiles, remaining_size) schc_frag = None return else: # 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 = ( schcmsg.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 = schcmsg.frag_sender_tx(self.rule, dtag=self.dtag, win=win, fcn=schcmsg.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) # send a SCHC fragment args = (schc_frag.packet.get_content(), self.protocol.layer2.mac_id, None, self.event_sent_frag, True) print("frag sent:", schc_frag.__dict__) self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet, args)
def resend_ack(self, schc_frag): print("resend ack method") print(schc_frag.__dict__) if self.mic_received is not None: schc_packet, mic_calced = self.get_mic_from_tiles_received() print("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 = schcmsg.frag_receiver_tx_all1_ack( schc_frag.rule, schc_frag.dtag, schc_frag.win, cbit=1) print("ACK success sent:", schc_ack.__dict__) if enable_statsct: Statsct.set_msg_type("SCHC_ACK_OK") print("----------------------- SCHC ACK OK SEND -----------------------") else: if self.all1_received: print("all-1 received, building ACK") print('send ack before done {},{},{}'.format(self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN], schcmsg.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: print("no fragments received yet, abort") self.send_receiver_abort() return print("all-1 not received, building ACK") print('send ack before done {},{},{}'.format(self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN], schcmsg.get_fcn_all_1(self.rule))) 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'])) bit_list = find_missing_tiles_no_all_1(self.tile_list, self.rule[T_FRAG][T_FRAG_PROF][T_FRAG_FCN], schcmsg.get_fcn_all_1(self.rule)) print('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], schcmsg.get_fcn_all_1(self.rule)) 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__) """ 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)
# "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'
def resend_ack(self, schc_frag): print("resend ack method") print(schc_frag.__dict__) if self.mic_received is not None: schc_packet, mic_calced = self.get_mic_from_tiles_received() print("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 = schcmsg.frag_receiver_tx_all1_ack( schc_frag.rule, schc_frag.dtag, schc_frag.win, cbit=1) print("ACK success sent:", schc_ack.__dict__) if enable_statsct: Statsct.set_msg_type("SCHC_ACK_OK") print("----------------------- SCHC ACK OK SEND -----------------------") else: if self.all1_received: print("all-1 received, building ACK") print('send ack before done {},{},{}'.format(self.tile_list, self.rule["FCNSize"], schcmsg.get_fcn_all_1(self.rule))) bit_list = find_missing_tiles_mic_ko_yes_all_1(self.tile_list, self.rule["FCNSize"], schcmsg.get_fcn_all_1(self.rule)) 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'])) print('send ack before done {}'.format(bit_list)) 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 print("bit list empty") 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???") 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__) break 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: print("no fragments received yet, abort") self.send_receiver_abort() return print("all-1 not received, building ACK") print('send ack before done {},{},{}'.format(self.tile_list, self.rule["FCNSize"], schcmsg.get_fcn_all_1(self.rule))) 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'])) bit_list = find_missing_tiles_no_all_1(self.tile_list, self.rule["FCNSize"], schcmsg.get_fcn_all_1(self.rule)) print('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["FCNSize"], schcmsg.get_fcn_all_1(self.rule)) 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__) break args = (schc_ack.packet.get_content(), self.context["devL2Addr"]) self.protocol.scheduler.add_event(0, self.protocol.layer2.send_packet, args)
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)
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)