def inbox(newpacket, proto='', host=''): """ """ if newpacket.Command == commands.Identity(): # contact sending us current identity we might not have # so we handle it before check that packet is valid # because we might not have his identity on hands and so can not verify the packet # so we check that his Identity is valid and save it into cache # than we check the packet to be valid too. Identity(newpacket) return True # check that signed by a contact of ours if not newpacket.Valid(): dhnio.Dprint(1, 'p2p_service.inbox ERROR new packet is not Valid') return False if newpacket.CreatorID != misc.getLocalID() and newpacket.RemoteID != misc.getLocalID(): dhnio.Dprint(1, "p2p_service.inbox ERROR packet is NOT for us") dhnio.Dprint(1, "p2p_service.inbox getLocalID=" + misc.getLocalID() ) dhnio.Dprint(1, "p2p_service.inbox CreatorID=" + newpacket.CreatorID ) dhnio.Dprint(1, "p2p_service.inbox RemoteID=" + newpacket.RemoteID ) dhnio.Dprint(1, "p2p_service.inbox PacketID=" + newpacket.PacketID ) return False handled = inboxPacket(newpacket, proto, host) if handled: dhnio.Dprint(12, "p2p_service.inbox [%s] from %s|%s (%s://%s) handled" % ( newpacket.Command, nameurl.GetName(newpacket.CreatorID), nameurl.GetName(newpacket.OwnerID), proto, host)) return handled
def __init__(self, eccMap, backupID, blockNum, supplierSet, remoteData, remoteParity, localData, localParity, creatorId = None, ownerId = None): self.eccMap = eccMap self.backupID = backupID self.blockNum = blockNum self.supplierSet = supplierSet self.supplierCount = len(self.supplierSet.suppliers) self.remoteData = remoteData self.remoteParity = remoteParity self.localData = localData self.localParity = localParity self.creatorId = creatorId self.ownerId = ownerId # at some point we may be dealing with when we're scrubbers if self.creatorId == None: self.creatorId = misc.getLocalID() if self.ownerId == None: self.ownerId = misc.getLocalID() # this files we want to rebuild # need to identify which files to work on self.missingData = [0] * self.supplierCount self.missingParity = [0] * self.supplierCount # array to remember requested files self.reconstructedData = [0] * self.supplierCount self.reconstructedParity = [0] * self.supplierCount
def cmd_key(opts, args, overDict): if len(args) == 2: if args[1] == 'copy': import lib.dhncrypto as dhncrypto TextToSave = misc.getLocalID() + "\n" + dhncrypto.MyPrivateKey() misc.setClipboardText(TextToSave) print 'now you can "paste" with Ctr+V your private key where you want.' del TextToSave return 0 elif args[1] == 'print': import lib.dhncrypto as dhncrypto TextToSave = misc.getLocalID() + "\n" + dhncrypto.MyPrivateKey() print print TextToSave return 0 elif len(args) == 3: if args[1] == 'copy': filenameto = args[2] import lib.dhncrypto as dhncrypto TextToSave = misc.getLocalID() + "\n" + dhncrypto.MyPrivateKey() if not dhnio.AtomicWriteFile(filenameto, TextToSave): print 'error writing to', filenameto return 1 print 'your private key were copied to file %s' % filenameto del TextToSave return 0 return 2
def SendDeleteBackup(SupplierID, BackupID): dhnio.Dprint(6, "p2p_service.SendDeleteBackup SupplierID=%s BackupID=%s " % (SupplierID, BackupID)) MyID = misc.getLocalID() PacketID = BackupID RemoteID = SupplierID result = dhnpacket.dhnpacket(commands.DeleteBackup(), MyID, MyID, PacketID, "", RemoteID) transport_control.outboxAck(result)
def SendSingleCustomer(idurl, response_callback=None): dhnio.Dprint(6, "identitypropagate.SendSingleCustomer [%s]" % nameurl.GetName(idurl)) MyID = misc.getLocalID() packet = dhnpacket.dhnpacket(commands.Identity(), MyID, MyID, MyID, misc.getLocalIdentity().serialize(), idurl) transport_control.outboxNoAck(packet) if response_callback is not None: transport_control.RegisterInterest(response_callback, packet.RemoteID, packet.PacketID)
def SendDeleteListBackups(SupplierID, ListBackupIDs): dhnio.Dprint(6, "p2p_service.SendDeleteListBackups SupplierID=%s BackupIDs number: %d" % (SupplierID, len(ListBackupIDs))) MyID = misc.getLocalID() PacketID = packetid.UniqueID() RemoteID = SupplierID Payload = '\n'.join(ListBackupIDs) result = dhnpacket.dhnpacket(commands.DeleteBackup(), MyID, MyID, PacketID, Payload, RemoteID) transport_control.outboxAck(result)
def SendBid(maxamount, price, days, comment, btcaddress, transactionid, doAck=False): """ Send a "Transfer" packet to Market server to place a bid (to buy DHN credits). """ dhnio.Dprint(4, 'central_service.SendBid ' + str((maxamount, price, days))) data = '%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s' % (misc.getLocalID(), 'bid', misc.float2str(maxamount), misc.float2str(price), str(days), str(btcaddress), str(transactionid), str(comment).replace('\n', '')) # send bid to the Market server return send2market(commands.Transfer(), data, doAck)
def SendCancelOffer(offerID, doAck=False): """ Send a "Transfer" packet to the Market server to cancel given offer, Your DHN credits will return to you. """ dhnio.Dprint(4, 'central_service.SendCancelOffer ' + str(offerID)) data = '%s\n%s\n%s' % (misc.getLocalID(), 'canceloffer', offerID) # send 'cancel offer' to Market server return send2market(commands.Transfer(), data, doAck)
def SendCancelBid(bidID, doAck=False): """ Send a "Transfer" packet to Market server to cancel a bid, Your bitcoins should be returned to you. """ dhnio.Dprint(4, 'central_service.SendCancelBid ' + str(bidID)) data = '%s\n%s\n%s' % (misc.getLocalID(), 'cancelbid', bidID) # send 'cencel bid' to Market server return send2market(commands.Transfer(), data, doAck)
def RequestIdentity(request): dhnio.Dprint(6, "p2p_service.RequestIdentity starting") MyID = misc.getLocalID() RemoteID = request.OwnerID PacketID = request.PacketID identitystr = misc.getLocalIdentity().serialize() dhnio.Dprint(12, "p2p_service.RequestIdentity returning ") result = dhnpacket.dhnpacket(commands.Identity(), MyID, MyID, PacketID, identitystr, RemoteID) transport_control.outboxNoAck(result)
def SendIdentity(doAck=False): """ Register our identity with Central server. """ LocalIdentity = misc.getLocalIdentity() dhnio.Dprint(4, "central_service.SendIdentity") #' version=[%s] contacts=[%s]" % (str(LocalIdentity.version), str(LocalIdentity.contacts))) data = LocalIdentity.serialize() ret = send2central(commands.Identity(), data, doAck, misc.getLocalID()) return ret
def SendToID(idurl, AckHandler=None, Payload=None, NeedAck=False, wide=False): dhnio.Dprint(8, "identitypropagate.SendToID [%s] NeedAck=%s" % (nameurl.GetName(idurl), str(NeedAck))) thePayload = Payload if thePayload is None: thePayload = misc.getLocalIdentity().serialize() packet = dhnpacket.dhnpacket( commands.Identity(), misc.getLocalID(), #MyID, misc.getLocalID(), #MyID, misc.getLocalID(), #PacketID, thePayload, idurl) if AckHandler is not None: transport_control.RegisterInterest(AckHandler, packet.RemoteID, packet.PacketID) transport_control.outbox(packet, NeedAck, wide) if wide: # this is a ping packet - need to clear old info transport_control.ErasePeerProtosStates(idurl) transport_control.EraseMyProtosStates(idurl)
def OneFromList(filename): WholeFile=dhnio.ReadBinaryFile(filename) FileList=WholeFile.split("\n") num=len(FileList) dhnio.Dprint(7, "supplierpatrol.OneFromList number of items is " + str(num)) rnd=random.randint(0,num-1) item=FileList[rnd] command=commands.Data() OwnerID=misc.getLocalID() CreatorID=misc.getLocalID() PacketID=item Payload="" supnum=packetid.SupplierNumber(item) RemoteID=contacts.getSupplierID(supnum) request=dhnpacket.dhnpacket(command, OwnerID, CreatorID, PacketID, Payload, RemoteID) global DataResultsOutstanding DataResultsOutstanding.append(item) transport_control.RegisterInterest(DataResult, RemoteID, PacketID) transport_control.outboxAck(request)
def doScanAndQueue(self, arg): global _ShutdownFlag dhnio.Dprint(10, 'data_sender.doScanAndQueue') log = open(os.path.join(settings.LogsDir(), 'data_sender.log'), 'w') log.write('doScanAndQueue %s\n' % time.asctime()) if _ShutdownFlag: log.write('doScanAndQueue _ShutdownFlag is True\n') self.automat('scan-done') log.flush() log.close() return if '' not in contacts.getSupplierIDs(): for backupID in misc.sorted_backup_ids(backup_matrix.local_files().keys(), True): packetsBySupplier = backup_matrix.ScanBlocksToSend(backupID) log.write('%s\n' % packetsBySupplier) for supplierNum in packetsBySupplier.keys(): supplier_idurl = contacts.getSupplierID(supplierNum) if not supplier_idurl: dhnio.Dprint(2, 'data_sender.doScanAndQueue WARNING ?supplierNum? %s for %s' % (supplierNum, backupID)) continue for packetID in packetsBySupplier[supplierNum]: backupID_, blockNum, supplierNum_, dataORparity = packetid.BidBnSnDp(packetID) if backupID_ != backupID: dhnio.Dprint(2, 'data_sender.doScanAndQueue WARNING ?backupID? %s for %s' % (packetID, backupID)) continue if supplierNum_ != supplierNum: dhnio.Dprint(2, 'data_sender.doScanAndQueue WARNING ?supplierNum? %s for %s' % (packetID, backupID)) continue if io_throttle.HasPacketInSendQueue(supplier_idurl, packetID): log.write('%s in the send queue to %s\n' % (packetID, supplier_idurl)) continue if not io_throttle.OkToSend(supplier_idurl): log.write('ok to send %s ? - NO!\n' % supplier_idurl) continue tranByiID = transport_control.transfers_by_idurl(supplier_idurl) if len(tranByiID) > 3: log.write('transfers by %s: %d\n' % (supplier_idurl, len(tranByiID))) continue filename = os.path.join(settings.getLocalBackupsDir(), packetID) if not os.path.isfile(filename): log.write('%s is not file\n' % filename) continue io_throttle.QueueSendFile( filename, packetID, supplier_idurl, misc.getLocalID(), self._packetAcked, self._packetFailed) log.write('io_throttle.QueueSendFile %s\n' % packetID) # dhnio.Dprint(6, ' %s for %s' % (packetID, backupID)) self.automat('scan-done') log.flush() log.close()
def UpdateListFiles(): if (not os.path.exists(settings.FileListDir())): os.mkdir(settings.FileListDir()) for supnum in range(0, contacts.numSuppliers()): filename= os.path.join(settings.FileListDir(), str(supnum)) dhnio.Dprint(7, "supplierpatrol.UpdateListFiles looking at = " + filename) if (not os.path.exists(filename) or (fileAgeInSeconds(filename) > 3600*24)): dhnio.Dprint(7, "supplierpatrol.UpdateListFiles found one to update " + filename) command=commands.ListFiles() OwnerID=misc.getLocalID() CreatorID=misc.getLocalID() PacketID="ListFiles" + str(supnum) Payload="" RemoteID= contacts.getSupplierID(supnum) request=dhnpacket.dhnpacket(command, OwnerID, CreatorID, PacketID, Payload, RemoteID) transport_control.RegisterInterest(ListResult, RemoteID, PacketID) transport_control.outboxAck(request) global NumRequestsOutstanding NumRequestsOutstanding += 1 dhnio.Dprint(7, "supplierpatrol.UpdateListFiles sent request - now outstanding=" + str(NumRequestsOutstanding))
def SendToIDs(idlist, AckHandler=None, wide=False): dhnio.Dprint(8, "identitypropagate.SendToIDs to %d users" % len(idlist)) MyID = misc.getLocalID() PacketID = MyID LocalIdentity = misc.getLocalIdentity() Payload = LocalIdentity.serialize() Hash = dhncrypto.Hash(Payload) alreadysent = set() for contact in idlist: if not contact: continue if contact in alreadysent: # just want to send once even if both customer and supplier continue found_previous_packets = 0 for transfer_id in transport_control.transfers_by_idurl(contact): ti = transport_control.transfer_by_id(transfer_id) if ti and ti.description.count('Identity'): found_previous_packets += 1 break if found_previous_packets >= 3: dhnio.Dprint(8, ' skip sending to %s' % contact) continue packet = dhnpacket.dhnpacket( commands.Identity(), misc.getLocalID(), #MyID, misc.getLocalID(), #MyID, misc.getLocalID(), #PacketID, Payload, contact) dhnio.Dprint(8, " sending [Identity] to %s" % nameurl.GetName(contact)) if AckHandler is not None: transport_control.RegisterInterest(AckHandler, packet.RemoteID, packet.PacketID) transport_control.outboxNoAck(packet, wide) if wide: # this is a ping packet - need to clear old info transport_control.ErasePeerProtosStates(contact) transport_control.EraseMyProtosStates(contact) alreadysent.add(contact) del alreadysent
def Valid(self): """ Validate signature to verify the `dhnblock`. """ if not self.Ready(): dhnio.Dprint(4, "dhnblock.Valid WARNING block is not ready yet " + str(self)) return False hash = self.GenerateHash() ConIdentity = contacts.getContact(misc.getLocalID()) if ConIdentity is None: dhnio.Dprint(2, "dhnblock.Valid WARNING could not get Identity so returning False") return False result = dhncrypto.Verify(ConIdentity, hash, self.Signature) # At block level only work on own stuff return result
def _doBlock(): dt = time.time() src = self.currentBlockData.getvalue() block = dhnblock.dhnblock( misc.getLocalID(), self.backupID, self.blockNumber, dhncrypto.NewSessionKey(), dhncrypto.SessionKeyType(), self.stateEOF, src,) del src dhnio.Dprint(12, 'backup.doBlock blockNumber=%d size=%d atEOF=%s dt=%s' % (self.blockNumber, self.currentBlockSize, self.stateEOF, str(time.time()-dt))) return block
def RequestListFiles(supplierNumORidurl): if isinstance(supplierNumORidurl, str): RemoteID = supplierNumORidurl else: RemoteID = contacts.getSupplierID(supplierNumORidurl) if not RemoteID: dhnio.Dprint(4, "p2p_service.RequestListFiles WARNING RemoteID is empty supplierNumORidurl=%s" % str(supplierNumORidurl)) return dhnio.Dprint(8, "p2p_service.RequestListFiles [%s]" % nameurl.GetName(RemoteID)) MyID = misc.getLocalID() PacketID = packetid.UniqueID() Payload = settings.ListFilesFormat() result = dhnpacket.dhnpacket(commands.ListFiles(), MyID, MyID, PacketID, Payload, RemoteID) transport_control.outboxNoAck(result) return PacketID
def send2market(command, data, doAck=False, PacketID=None): """ Same - send a packet to Market server. """ MyID = misc.getLocalID() RemoteID = settings.MarketServerID() if PacketID is None: PacketID = packetid.UniqueID() packet = dhnpacket.dhnpacket( command, MyID, MyID, PacketID, data, RemoteID,) transport_control.outbox(packet, doAck) return PacketID
def ListFiles(request): MyID = misc.getLocalID() RemoteID = request.OwnerID PacketID = request.PacketID Payload = request.Payload dhnio.Dprint(8, "p2p_service.ListFiles from [%s], format is %s" % (nameurl.GetName(request.OwnerID), Payload)) custdir = settings.getCustomersFilesDir() ownerdir = os.path.join(custdir, nameurl.UrlFilename(request.OwnerID)) if not os.path.isdir(ownerdir): dhnio.Dprint(8, "p2p_service.ListFiles did not find customer dir " + ownerdir) src = PackListFiles('', Payload) result = dhnpacket.dhnpacket(commands.Files(), MyID, MyID, PacketID, src, RemoteID) transport_control.outboxNoAck(result) return plaintext = TreeSummary(ownerdir) src = PackListFiles(plaintext, Payload) outpacket = dhnpacket.dhnpacket(commands.Files(), MyID, MyID, PacketID, src, RemoteID) transport_control.outboxNoAck(outpacket)
def doSuppliersRequestDBInfo(self, arg): # dhnio.Dprint(4, 'backup_db_keeper.doSuppliersRequestDBInfo') # packetID_ = settings.BackupInfoFileName() # packetID = settings.BackupInfoEncryptedFileName() packetID = settings.BackupIndexFileName() for supplierId in contacts.getSupplierIDs(): if supplierId: transport_control.RemoveInterest(supplierId, packetID) self.requestedSuppliers.clear() Payload = '' localID = misc.getLocalID() for supplierId in contacts.getSupplierIDs(): if not supplierId: continue newpacket = dhnpacket.dhnpacket(commands.Retrieve(), localID, localID, packetID, Payload, supplierId) transport_control.outboxAck(newpacket) transport_control.RegisterInterest(self.SupplierResponse, supplierId, packetID) self.requestedSuppliers.add(supplierId)
def SendMessage(RemoteID, messagebody, PacketID=""): dhnio.Dprint(6, "message.SendMessage to: " + str(RemoteID)) # TODO ERROR HERE (return Defer) if not identitycache.scheduleForCaching(RemoteID): dhnio.Dprint(1, "message.SendMessage ERROR. Can't find identity: " + str(RemoteID)) return RemoteIdentity = identitycache.FromCache(RemoteID) if RemoteIdentity == "": dhnio.Dprint(1, "message.SendMessage ERROR. Can't retreive identity: " + str(RemoteID)) return Amessage = MessageClass(RemoteIdentity, messagebody) MyID = misc.getLocalID() if PacketID == "": PacketID = packetid.UniqueID() Payload = misc.ObjectToString(Amessage) dhnio.Dprint(6, "message.SendMessage about to send to " + RemoteID) result = dhnpacket.dhnpacket(commands.Message(), MyID, MyID, PacketID, Payload, RemoteID) transport_control.outboxAck(result)
def send2central(command, data, doAck=False, PacketID=None): """ A method to prepare a packet for Central server and send it to the transport gate. """ MyID = misc.getLocalID() RemoteID = settings.CentralID() if PacketID is None: PacketID = packetid.UniqueID() packet = dhnpacket.dhnpacket( command, MyID, MyID, PacketID, data, RemoteID,) transport_control.outbox(packet, doAck) #del packet return PacketID
def doSuppliersSendDBInfo(self, arg): # dhnio.Dprint(4, 'backup_db_keeper.doSuppliersSendDBInfo') # packetID = settings.BackupInfoEncryptedFileName() packetID = settings.BackupIndexFileName() for supplierId in contacts.getSupplierIDs(): if supplierId: transport_control.RemoveInterest(supplierId, packetID) self.sentSuppliers.clear() # src = dhnio.ReadBinaryFile(settings.BackupInfoFileFullPath()) src = dhnio.ReadBinaryFile(settings.BackupIndexFilePath()) localID = misc.getLocalID() block = dhnblock.dhnblock(localID, packetID, 0, dhncrypto.NewSessionKey(), dhncrypto.SessionKeyType(), True, src) Payload = block.Serialize() for supplierId in contacts.getSupplierIDs(): if not supplierId: continue if not contact_status.isOnline(supplierId): continue newpacket = dhnpacket.dhnpacket(commands.Data(), localID, localID, packetID, Payload, supplierId) transport_control.outboxAck(newpacket) transport_control.RegisterInterest(self.SupplierAcked, supplierId, packetID) self.sentSuppliers.add(supplierId)
def Data(request): # 1. this is our Data! if request.OwnerID == misc.getLocalID(): if request.PacketID in [ settings.BackupIndexFileName(), ]: backup_control.IncomingSupplierBackupIndex(request) # elif request.PacketID in [ settings.BackupInfoFileName(), settings.BackupInfoFileNameOld(), settings.BackupInfoEncryptedFileName(), ]: # return return # 2. this Data is not belong to us if not contacts.IsCustomer(request.OwnerID): # SECURITY # may be we did not get the ListCustomers packet from the Central yet? dhnio.Dprint(6, "p2p_service.Data WARNING %s not a customer, packetID=%s" % (request.OwnerID, request.PacketID)) central_service.SendRequestCustomers() return filename = makeFilename(request.OwnerID, request.PacketID) if filename == "": transport_control.SendFail(request, 'empty filename, you are not a customer maybe?') dhnio.Dprint(6,"p2p_service.Data WARNING got empty filename, bad customer or wrong packetID? ") return dirname = os.path.dirname(filename) if not os.path.exists(dirname): try: dhnio._dirs_make(dirname) except: dhnio.Dprint(2, "p2p_service.Data ERROR can not create sub dir " + dirname) transport_control.SendFail(request, 'write error') return data = request.Serialize() if not dhnio.WriteFile(filename, data): dhnio.Dprint(2, "p2p_service.Data ERROR can not write to " + str(filename)) transport_control.SendFail(request, 'write error') return transport_control.SendAck(request, str(len(request.Payload))) reactor.callLater(3, local_tester.TestSpaceTime) del data dhnio.Dprint(8, "p2p_service.Data saved from [%s/%s], packetID is %s" % ( nameurl.GetName(request.OwnerID), nameurl.GetName(request.CreatorID), request.PacketID,))
def __init__(self, BackupID, OutputFile): # OutputFileName self.CreatorID = misc.getLocalID() self.BackupID = BackupID self.PathID, self.Version = packetid.SplitBackupID(self.BackupID) self.File = OutputFile # is current active block - so when add 1 we get to first, which is 0 self.BlockNumber = -1 self.OnHandData = [] self.OnHandParity = [] self.AbortState = False self.Done = False self.EccMap = eccmap.Current() self.LastAction = time.time() self.InboxPacketsQueue = [] self.InboxQueueWorker = None self.InboxQueueDelay = 1 # For anyone who wants to know when we finish self.MyDeferred = Deferred() self.packetInCallback = None self.blockRestoredCallback = None automat.Automat.__init__(self, 'restore', 'AT_STARTUP', 4) self.automat('init') events.info('restore', '%s start restoring' % self.BackupID)
def __init__(self): self.creatorID = misc.getLocalID() self.supplierQueues = {} # self.dprintAdjust = 0 self.paintFunc = None
def MakeMessage(to, subj, body, dt=datetime.datetime.now().strftime("%Y/%m/%d %I:%M:%S %p")): dhnio.Dprint(6, "message.MakeMessage to " + to) msg = (misc.getLocalID(), to, subj, dt, body) return "\n".join(msg)
def Correspondent(request): dhnio.Dprint(6, "p2p_service.Correspondent") MyID = misc.getLocalID() RemoteID = request.OwnerID PacketID = request.PacketID Msg = misc.decode64(request.Payload)