def _send_remote_peer_request(self, infohash, callback): #make sure we have a circuit to send it out on: if self.circ and self.circ.is_done(): self.circ = None if not self.circ: self.circ = self.app.find_or_build_best_circuit(force=True, protocol="DHT") if self.circ == None: log_msg("Could not build circuit for DHT remote peer request", 0, "dht") return #generate the message: (version, infohash, peerList) msg = "" #header: msg += Basic.write_byte(Node.VERSION) #infohash: msg += infohash #peers: for host, port in self.knownNodes: #is this an IP address? if isIPAddress(host): msg += Basic.write_byte(0) msg += struct.pack("!4sH", socket.inet_aton(host), port) #otherwise, it's a URL that has to be resolved remotely else: msg += Basic.write_byte(1) msg += Basic.write_lenstr(host) msg += Basic.write_short(port) self.circ.send_dht_request(msg, self.make_callback_wrapper(callback))
def start_bank_process(self, readTokens, writeTokens, paymentId): """Create the bank message, and send it to the payment proxy for relaying to the bank. @param readTokens: how many read tokens to pay the merchant for @type readTokens: int @param writeTokens: how many write tokens to pay the merchant for @type writeTokens: int @param paymentId: the id for this payment, for tracking when it is completed @type paymentId: int """ #generate the response message: msg = Basic.write_byte(PAR_VERSION) msg += Basic.write_int(readTokens) msg += Basic.write_int(writeTokens) msg += Basic.write_long(paymentId) #figure out how many payments must be made: totalTokens = readTokens + writeTokens numPayments = totalTokens / Globals.CELLS_PER_PAYMENT msg += Basic.write_byte(numPayments) bankMsg = Basic.write_byte(numPayments) for i in range(0, numPayments): #get a token to use for this payment: requestId, token = self.paymentTokens.popitem() #send it to the bank for signing coin = self.parClient.bank.get_acoins(1) if not coin: paymentDeferred = self.paymentDeferreds[paymentId] del self.paymentDeferreds[paymentId] paymentDeferred.errback(InsufficientACoins("No ACoins left.")) return coin = coin[0] self.parClient.circ.app.coinsSpent += 1 # log_msg("Srsly, wtf is going on? %s" % (coin.interval), 4) bankMsg += coin.write_binary() + token msg += Basic.write_byte(COIN_TYPES['A']) + Basic.write_long(requestId) key = EncryptedDatagram.ClientSymKey(self.parClient.bank.PUBLIC_KEY) bankMsg = Basic.write_byte(1) + key.encrypt(Basic.write_byte(3) + bankMsg) msg = Basic.write_byte(PAR_VERSION) + Basic.write_byte(self.hop-1) + Basic.write_lenstr(bankMsg) + Basic.write_lenstr(msg) self.parClient.send_direct_tor_message(msg, "bank_relay", True, self.paymentProxyHop)