class AP(): """I'm a router!!!""" def __init__(self, contract_address): # Global vars # UDP Socket self.emulation_time = 200 self.sock_udp = socket(AF_INET, SOCK_DGRAM) self.sock_udp.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) self.contract = App() self.contract_address = contract_address # node PIT data structure self.this_PIT = dict() # General PIT self.PIT = dict() try: self.contract.initWeb3() self.contract.initContract(self.contract_address) self.contract.initContract(self.contract_address) except Exception as err: print(err) exit(-1) def onInterest(self): # Consumer side sock_udp = socket(AF_INET, SOCK_DGRAM) sock_udp.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) # Listen for requests from anywhere sock_udp.bind(("", 2222)) while 1: data, addr = sock_udp.recvfrom(1024) ndn_packet = json.loads(data.decode('utf-8')) pkt_type = ndn_packet['type'] # Data Infos provider = ndn_packet['name'].split('/')[1] name = ndn_packet['name'].split('/')[2] # Here starts the attack <---- if pkt_type == "VER": # TODO: Check if this packet was already verified if self.PIT.get(name): # 4s default NDN interestLifetime # Link provider ID with an wallet account prefix = self.contract.getAnAccount(int(provider)) content_name = "/{0}/{1}".format(prefix, name) try: provider = self.contract.getAnAccount( int(ndn_packet['provider'])) # print("[DEBUG-AP] AP verifying Content %s for provider %s" % (name,provider)) status = self.contract._verifyContent( content_name, provider) # print("[DEBUG-AP] Status ", status) message = { 'hop_count': 15, 'type': 'VER', 'name': ndn_packet['name'], 'status': status } message2json = str(json.dumps(message)).encode('ascii') self.sock_udp.sendto( message2json, ("10.0.0.255", 2222)) # Using broadcast address of eth0 except Exception as err: # print ("Error: {0} => {1}".format(err,ndn_packet)) pass elif pkt_type == "REG": # TODO: Finish this implementation provider = self.contract.getAnAccount(int(provider)) content_name = "/{0}/{1}".format(provider, name) if not self.PIT.get(name): # print("[DEBUG-AP] AP registering content %s" % (content_name)) self.contract._registerContent(content_name, provider) self.writeRegisteredContents(ndn_packet['name']) self.PIT[name] = 4 # TODO: Finish this implementation # elif data_2_json['type'] == "AUT": # self.contract._registerProvider(name,provider) def writeRegisteredContents(self, content_name): content_list = open("/tmp/content_list.txt", "a+") content_list.write(content_name + "\n") content_list.close() def StopEmulation(self): time.sleep(self.emulation_time + 2) output_file = open("/tmp/emulation_output.txt", "a+") output_file.write(self.contract.getStatistics()) output_file.close() os.system("killall python3.7")
class Producer(): """Producer class""" def __init__(self, contract_address, emulation_time, my_id): # Global vars self.contract = App() self.content_file_write = open("/tmp/contents.txt", "a+") self.num_provided_contents = 20 # maximum number of provided contents self.emulation_time = float(emulation_time) # this node index for self.my_id = int(my_id) self.this_account = None self.contract_address = contract_address # UDP Socket self.sock_udp = socket(AF_INET, SOCK_DGRAM) self.sock_udp.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) # Data packet structure self.Data = dict() # node PIT data structure self.this_PIT = dict() # General PIT self.PIT = dict() try: self.contract.initWeb3() self.this_account = self.contract.getAnAccount(self.my_id) # self.this_account = self.contract.getAnAccount() self.contract.initContract(self.contract_address) except Exception as err: print(err) exit(-1) def register_contents(self): print("[DEBUG] Registering contents") for i in range(self.num_provided_contents): # Random content's name rand = str(random.randint(0, 1000)).encode('ascii') content = hashlib.sha256(rand).hexdigest() # Content with producer ID (e.g., /1/foobar) name = "/{0}/{1}".format(str(self.this_account), str(content)) # Write contetns names in an file for consumer usage self.content_file_write.write(name + "\n") # register my content on Blockchain status = self.contract._registerContent(name, self.this_account) if not status: # Try to register again i -= 1 self.content_file_write.close() def onInterest(self): # Consumer side sock_udp = socket(AF_INET, SOCK_DGRAM) sock_udp.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) # Listen for requests from anywhere sock_udp.bind(("", 2222)) print("[DEBUG] Server is on") while 1: data, addr = sock_udp.recvfrom(1024) data_2_json = json.loads(data.decode('utf-8')) # Data Infos producer = str(data_2_json['name']).split('/')[1] name = data_2_json['name'] if str(producer) == str( self.this_account) and data_2_json['type'] == "interest": # print("[DEBUG] Producer {0} has received an interest for {1}".format(producer,name) ) # Now return a data packet data_pkt = { 'type': 'data', 'name': name, 'account': str(self.this_account), 'payload': [hex(x) for x in range(40)] } message = str(json.dumps(data_pkt)).encode('ascii') self.sock_udp.sendto(message, ('<broadcast>', 2222)) elif self.PIT.get(name): # return data to origin self.sock_udp.sendto(data, ('<broadcast>', 2222)) del (self.PIT[name]) elif data_2_json['type'] == "interest": # Decrement hop count hop_count = data_2_json['hop_count'] if hop_count >= 1: data_2_json['hop_count'] = data_2_json['hop_count'] - 1 # Forward interest packet message = str(json.dumps(data_2_json)).encode('ascii') # Send self.sock_udp.sendto(message, ('<broadcast>', 2222)) # Add on PIT self.PIT[name] = True def StopEmulation(self): time.sleep(self.emulation_time + 2) output_file = open("/tmp/emulation_output", "a+") output_file.write(self.contract.getStatistics()) output_file.close() self.contract.getStatistics() os.system("killall python3.7")