def test_paging_names(self): seed = "abcdefgh" amount = 10 names = helpers.create_paging_names(seed, amount=amount) self.assertEqual(len(names), amount) for i in range(amount-2): self.assertNotEqual(names[i], names[i+1]) self.assertNotEqual(md5(names[i]).digest(), names[i+1])
def handle_call(self, callee, blob): ''' Send paging for this call, no dummy ''' try: (location, seed, time) = self.factory.database[callee] except KeyError: print "Location for %s not found -> Broadcast" % callee for location in constants.locations: self.add_call_to_location(location, callee, blob) else: # create paging names if not already done if not callee in self.factory.paging_names: self.factory.paging_names[callee] = create_paging_names(seed) paging_name = self.factory.paging_names[callee].pop(0) # refill paging_names if necesarry if len(self.factory.paging_names[callee]) == 0: self.factory.paging_names[callee] = create_paging_names(seed, paging_name) self.add_call_to_location(location, paging_name, blob)
def handle_paging(self, payload): """ Handle paging packet """ # find amount of paging signals amount = len(payload) / 2 pseudos = payload[:amount] call = False used_name = None # check for pseudonym and paging names if self.user.pseudonym in pseudos: print "Found my pseudonym!" index = pseudos.index(self.user.pseudonym) # L did broadcast, send location update used_name = self.user.pseudonym elif self.user.paging_names is not None: for name in self.user.paging_names: if name in pseudos: print "Found a paging name: %s" % name index = pseudos.index(name) used_name = name break if used_name is None: # packet not for me return elif used_name == self.user.pseudonym: # was broadcast -> full location update self.user.update_providers() else: # update paging names self.user.paging_names = create_paging_names(self.user.seed, used_name, amount=100) else: # packet not for me return # try to decrypt the paging packet try: cid, caller = decrypt(payload[amount + index], self.user.secret) except ValueError: print "Was no well-formed ciphertext" else: # check if cid is msg if cid.startswith("M:"): # suboptimal, will eat calls msg = get_msg(cid) print "MSG: %s from %s" % (msg, caller) # check if we have seen this packet before elif cid not in self.used_cids: # really incoming call self.used_cids.append(cid) # Ask if user wants to take call self.user.io.transport.write("Do you want to take the call of %s? (y/n)\n" % caller) self.user.incoming_call = (cid, caller)
def update_providers(self, location="", retry=False): """ Update location of user and tell providers """ if retry: sleep(2) if location != "": self.location = location self.io.sendLine("Your location: %s" % self.location) # create new pseudonym for location self.pseudonym = pick_random() self.seed = pick_random() self.paging_names = create_paging_names(self.seed, amount=100) d = ClientCreator(reactor, UserClientProtocol, self).connectTCP(constants.N.external_ip, constants.N.port) d.addCallback(self._update_location) if retry: d.addErrback(self._error, "network operator") else: d.addErrback(self.retry)