Пример #1
0
def update_contacts():
    lg.out(10, 'http_node.update_contacts ')
    global _Contacts
    _Contacts.clear()

    def update_contact(x, idurl):
        add_contact(idurl)

    def failed(x, idurl):
        lg.out(10, 'http_node.update_contacts.failed   NETERROR ' + idurl)

    contacts_list = contacts.getContactIDs()
    contacts_list.append(settings.CentralID())
    contacts_list.append(settings.MoneyServerID())
    contacts_list.append(settings.MarketServerID())
    for idurl in contacts_list:
        lg.out(10, 'http_node.update_contacts want ' + idurl)
        if idurl == misc.getLocalID():
            continue
        ident = contacts.getContact(idurl)
        if ident is None:
            d = identitycache.immediatelyCaching(idurl)
            d.addCallback(update_contact, idurl)
            d.addErrback(failed, idurl)
            continue

        update_contact('', idurl)
Пример #2
0
 def doRestartUDPSession(self, arg):
     idurl = arg
     ident = contacts.getContact(idurl)
     if ident is None:
         return
     address = ident.getProtoContact('udp')
     if address is None:
         return
     try:
         ip, port = address[6:].split(':')
         address = (ip, int(port))
     except:
         dhnio.DprintException()
         return
     address_local = self.remapAddress(address)
     for sess in transport_udp_session.sessions():
         if  sess.remote_idurl is not None and sess.remote_idurl == idurl and \
             ( sess.remote_address[0] == address[0] or sess.remote_address[0] == address_local[0] ) and \
             ( sess.remote_address[1] != address[1] or sess.remote_address[1] != address_local[1] ):
             self.addRedirection(sess.remote_address, address_local)
             sess.automat('shutdown')
             dhnio.Dprint(8, 'transport_udp.doRestartUDPSession "shutdown" to %s with [%s]' % (sess.name, nameurl.GetName(idurl)))
     def start_session(address_local, idurl):
         sess = transport_udp_session.open_session(address_local)
         dhnio.Dprint(8, 'transport_udp.doRestartUDPSession.start_session send "init" to %s with [%s]' % (sess.name, nameurl.GetName(idurl)))
         sess.automat('init', idurl)
     reactor.callLater(1, start_session, address_local, idurl)
Пример #3
0
def update_contacts():
    dhnio.Dprint(10, 'transport_http.update_contacts ')
    global _Contacts
    _Contacts.clear()

    def update_contact(x, idurl):
        add_contact(idurl)

    def failed(x, idurl):
        dhnio.Dprint(10, 'transport_http.update_contacts.failed   NETERROR ' + idurl)

    contacts_list = contacts.getContactIDs()
    contacts_list.append(settings.CentralID())
    contacts_list.append(settings.MoneyServerID())
    contacts_list.append(settings.MarketServerID())
    for idurl in contacts_list:
        dhnio.Dprint(10, 'transport_http.update_contacts want ' + idurl)
        if idurl == misc.getLocalID():
            continue
        ident = contacts.getContact(idurl)
        if ident is None:
            d = identitycache.immediatelyCaching(idurl)
            d.addCallback(update_contact, idurl)
            d.addErrback(failed, idurl)
            continue

        update_contact('', idurl)
Пример #4
0
 def doRemoveOldUDPSession(self, arg):
     for idurl in arg[0]:
         if idurl == misc.getLocalID():
             continue
         if idurl not in arg[1]:
             ident = contacts.getContact(idurl)
             if ident is None:
                 continue
             address = ident.getProtoContact('udp')
             if address is None:
                 continue
             try:
                 ip, port = address[6:].split(':')
                 address = (ip, int(port))
             except:
                 dhnio.DprintException()
                 continue
             if transport_udp_session.is_session_opened(address):
                 dhnio.Dprint(8, 'transport_udp.doRemoveOldUDPSession shutdown %s with [%s]' % (str(address), nameurl.GetName(idurl)))
                 reactor.callLater(0, transport_udp_session.A, address, 'shutdown')
             address_local = self.remapAddress(address)
             if address_local != address:
                 if transport_udp_session.is_session_opened(address_local):
                     dhnio.Dprint(8, 'transport_udp.doRemoveOldUDPSession shutdown %s with local peer [%s]' % (str(address_local), nameurl.GetName(idurl)))
                     self.eraseRedirection(address_local)
                     reactor.callLater(0, transport_udp_session.A, address_local, 'shutdown')
             self.eraseRedirection(address)
             self.eraseRedirection(address_local)
Пример #5
0
 def SignatureChecksOut(self):
     """
     This check correctness of signature, uses `lib.dhncrypto.Verify`.
     To verify we need 3 things:
         - the dhnpacket `Creator` identity ( it keeps the public key ),
         - hash of that dhnpacket - just call `GenerateHash()` to make it,
         - the signature itself.
     """
     ConIdentity = contacts.getContact(self.CreatorID)
     if ConIdentity is None:
         dhnio.Dprint(1, "dhnpacket.SignatureChecksOut ERROR could not get Identity for " + self.CreatorID + " so returning False")
         return False
     Result = dhncrypto.Verify(ConIdentity, self.GenerateHash(), self.Signature)
     return Result
Пример #6
0
def add_contact(idurl):
    global _Contacts
    global _PingDelayDict
    global _CurrentDelay
    dhnio.Dprint(14, 'transport_http.add_contact want to add %s' % idurl)
    ident = contacts.getContact(idurl)
    if ident is None:
        dhnio.Dprint(6, 'transport_http.add_contact WARNING %s not in contacts' % idurl)
        return
    http_contact = ident.getProtoContact('http')
    if http_contact is None:
        dhnio.Dprint(12, 'transport_http.add_contact %s have no http contact. skip.' % idurl)
        return
    proto, host, port, filename = nameurl.UrlParse(http_contact)
    _Contacts[idurl] = (host, port)
    _PingDelayDict[idurl] = _CurrentDelay
    dhnio.Dprint(10, 'transport_http.add_contact %s on %s:%s' % (idurl, host, port))
Пример #7
0
def add_contact(idurl):
    global _Contacts
    global _PingDelayDict
    global _CurrentDelay
    lg.out(14, 'http_node.add_contact want to add %s' % idurl)
    ident = contacts.getContact(idurl)
    if ident is None:
        lg.out(6, 'http_node.add_contact WARNING %s not in contacts' % idurl)
        return
    http_contact = ident.getProtoContact('http')
    if http_contact is None:
        lg.out(12, 'http_node.add_contact %s have no http contact. skip.' % idurl)
        return
    proto, host, port, filename = nameurl.UrlParse(http_contact)
    _Contacts[idurl] = (host, port)
    _PingDelayDict[idurl] = _CurrentDelay
    lg.out(10, 'http_node.add_contact %s on %s:%s' % (idurl, host, port))
Пример #8
0
 def doCreateNewUDPSession(self, arg):
     for idurl in arg[1]:
         if idurl == misc.getLocalID():
             continue
         if idurl not in arg[0]:
             ident = contacts.getContact(idurl)
             if ident is None:
                 continue
             address = ident.getProtoContact('udp')
             if address is None:
                 continue
             try:
                 proto, ip, port, filename = nameurl.UrlParse(address)
                 address = (ip, int(port))
             except:
                 dhnio.DprintException()
                 continue
             address = self.remapAddress(address)
             sess = transport_udp_session.open_session(address)
             dhnio.Dprint(8, 'transport_udp.doCreateNewUDPSession %s with [%s]' % (sess.name, nameurl.GetName(idurl)))
             sess.automat('init', idurl)
Пример #9
0
 def doStartAllUDPSessions(self, arg):
     all = contacts.getContactsAndCorrespondents()
     if not self.debug:
         all.append(settings.CentralID())
     for idurl in all:
         ident = contacts.getContact(idurl)
         if ident is None:
             continue
         address = ident.getProtoContact('udp')
         if address is None:
             continue
         try:
             proto, ip, port, filename = nameurl.UrlParse(address)
             address = (ip, int(port))
         except:
             dhnio.DprintException()
             continue
         address = self.remapAddress(address)
         sess = transport_udp_session.open_session(address)
         dhnio.Dprint(8, 'transport_udp.doStartAllUDPSessions init %s with [%s]' % (sess.name, nameurl.GetName(idurl)))
         sess.automat('init', idurl)
Пример #10
0
 def recreate(idurl, count):
     if count > 5:
         return
     ident = contacts.getContact(idurl)
     if ident is None:
         return
     address = ident.getProtoContact('udp')
     if address is None:
         return
     try:
         ip, port = address[6:].split(':')
         address = (ip, int(port))
     except:
         dhnio.DprintException()
         return
     if transport_udp_session.is_session_opened(address):
         reactor.callLater(1, recreate, idurl, count+1)
         dhnio.Dprint(8, 'transport_udp.doRecreateUDPSession.recreate wait 1 second to finish old session with %s' % str(address))
         return
     address_local = self.remapAddress(address)
     sess = transport_udp_session.open_session(address_local)
     dhnio.Dprint(8, 'transport_udp.doRecreateUDPSession init %s with [%s]' % (sess.name, nameurl.GetName(idurl)))
     sess.automat('init', idurl)
Пример #11
0
 def isIPPortChanged(self, arg):
     idurl = arg
     ident = contacts.getContact(idurl)
     if ident is None:
         return False
     address = ident.getProtoContact('udp')
     if address is None:
         return False
     try:
         ip, port = address[6:].split(':')
         address = (ip, int(port))
     except:
         dhnio.DprintException()
         return False
     address_local = self.remapAddress(address)
     found = False
     for sess in transport_udp_session.sessions():
         if  sess.remote_idurl is not None and \
             sess.remote_idurl == idurl and \
             ( sess.remote_address[0] == address[0] or sess.remote_address[0] == address_local[0] ) and \
             ( sess.remote_address[1] != address[1] and sess.remote_address[1] != address_local[1] ):
             found = True
             dhnio.Dprint(6, 'transport_udp.isIPPortChanged found related session %s with [%s]' % (sess.name, sess.remote_name))
     return found        
Пример #12
0
 def doReceiveData(self, arg):
     global _ReceiveControlFunc
     try:
         datagram, addr = arg
     except:
         return
     io = cStringIO.StringIO(datagram)
     code = io.read(2)
     cmd = CODES.get(code, None)
     if cmd is None:
         # dhnio.Dprint(2, 'transport_udp_session.doReceiveData WARNING incorrect data from %s: [%s]' % (self.remote_address, code))
         return
     if _ReceiveControlFunc:
         seconds_pause = _ReceiveControlFunc(len(datagram))
         if seconds_pause > 0:
             # transport_udp.A().client.transport.pauseProducing()
             parent().client.client.transport.pauseProducing()
             # self.client.transport.pauseProducing()
             # reactor.callLater(seconds_pause, transport_udp.A().client.transport.resumeProducing)
             reactor.callLater(seconds_pause, parent().client.transport.resumeProducing)
     # dhnio.Dprint(8, '                                UDP:[%s] from %s' % (cmd, self.remote_address))
     self.last_alive_packet_time = time.time()
     #--- DATA
     if cmd == 'DATA':
         try:
             file_id = struct.unpack('i', io.read(4))[0]
             block_id = struct.unpack('i', io.read(4))[0]
             num_blocks = struct.unpack('i', io.read(4))[0]
             data_size = struct.unpack('i', io.read(4))[0]
         except:
             dhnio.DprintException()
             return
         if not self.incommingFiles.has_key(file_id):
             if self.receivedFiles.has_key(file_id):
                 if time.time() - self.receivedFiles[file_id] > 10:
                     # we receive the file, but 10 seconds since that we still receive the acks
                     # definitely something wrong - drop session 
                     self.automat('shutdown')
                     return 
                 self.sendDatagram(COMMANDS['REPORT'] + struct.pack('i', file_id) + struct.pack('i', block_id))
                 return
             if len(self.incommingFiles) >= 2 * self.maxOutgoingFiles:
                 # too many incoming files - drop session
                 self.automat('shutdown') 
                 return
             self.incommingFiles[file_id] = InboxFile(self, file_id, num_blocks)
         inp_data = io.read()
         if len(inp_data) != data_size:
             dhnio.Dprint(2, 'transport_udp_session.doReceiveData WARNING incorrect datagram received from %s, not full data' % str(self.remote_address))
             return
         self.incommingFiles[file_id].input_block(block_id, inp_data) 
         self.sendDatagram(COMMANDS['REPORT'] + struct.pack('i', file_id) + struct.pack('i', block_id))
         # dhnio.Dprint(12, 'transport_udp_session.doReceiveData [%d] (%d/%d) from %s, state=%s' % (file_id, block_id, num_blocks, self.remote_address, self.state))
         if len(self.incommingFiles[file_id].blocks) == num_blocks:
             # dhnio.Dprint(12, 'transport_udp_session.doReceiveData new file [%d] (%d/%d) from %s' % (file_id, block_id, num_blocks, self.remote_address))
             self.incommingFiles[file_id].build()
             file_received(self.incommingFiles[file_id].filename, 'finished', 'udp', self.remote_address)
             self.incommingFiles[file_id].close()   
             del self.incommingFiles[file_id]   
             self.receivedFiles[file_id] = time.time()
             self.eraseOldFileIDs()
     #--- REPORT
     elif cmd == 'REPORT':
         try:
             file_id = struct.unpack('i', io.read(4))[0]
         except:
             dhnio.DprintException()
             return
         if not self.outgoingFiles.has_key(file_id):
             # if not self.receivedFiles.has_key(file_id):
             #     dhnio.Dprint(8, 'transport_udp_session.doReceiveData WARNING unknown file_id in REPORT packet received from %s: [%d]' % (self.remote_address, file_id))
             return
         try:
             block_id = struct.unpack('i', io.read(4))[0]
         except:
             dhnio.DprintException()
             return
         self.outgoingFiles[file_id].report_block(block_id)
         self.sliding_window.pop((file_id, block_id), None)
         self.sliding_window_size += 1
         if self.sliding_window_size > MAX_WINDOW_SIZE:
             self.sliding_window_size = MAX_WINDOW_SIZE
         if len(self.outgoingFiles[file_id].blocks) == 0:
             self.reportSentDone(file_id)
             self.closeOutboxFile(file_id)
         # dhnio.Dprint(8, 'transport_udp_session.doReceiveData REPORT on [%d,%d] received, blocks=%d, window=%d' % (file_id, block_id, len(self.outgoingFiles[file_id]), self.sliding_window_size))
     #--- GREETING
     elif cmd == 'GREETING':
         greeting_idurl = io.read()
         dhnio.Dprint(12, 'transport_udp_session.doReceiveData GREETING "%s" from %s, remote_idurl=%s, remote_address=%s, state=%s' % (
             greeting_idurl[:120], str(addr), self.remote_idurl, self.remote_address, self.state))
         if greeting_idurl:
             if nameurl.UrlMake(parts=nameurl.UrlParse(greeting_idurl)) != greeting_idurl:
                 dhnio.Dprint(2, 'transport_udp_session.doReceiveData WARNING incorrect idurl=%s in GREETING packet from %s' % (greeting_idurl, self.name))
                 self.sendDatagram(COMMANDS['PING'])
             else:
                 if self.remote_idurl is None:
                     self.remote_idurl = greeting_idurl
                     self.remote_name = nameurl.GetName(self.remote_idurl)
                     dhnio.Dprint(6, 'transport_udp_session.doReceiveData got idurl=%s in GREETING packet from %s' % (greeting_idurl, self.name))
                     parent().automat('recognize-remote-id', (self.index, self.remote_idurl))
                 else:
                     if self.remote_idurl != greeting_idurl:
                         dhnio.Dprint(2, 'transport_udp_session.doReceiveData WARNING wrong idurl=%s in GREETING packet from %s' % (greeting_idurl, self.name))
                 ident = contacts.getContact(greeting_idurl)
                 if ident:
                     udpip = ident.getIP('udp')
                     if udpip:
                         if udpip != addr[0]:
                             parent().automat('detect-remote-ip', (self.index, self.remote_idurl))
                 self.sendDatagram(COMMANDS['ALIVE'])
         else:
             dhnio.Dprint(2, 'transport_udp_session.doReceiveData WARNING did not found idurl in GREETING packet from %s, send PING again' % self.name)
             self.sendDatagram(COMMANDS['PING'])
     #--- PING
     elif cmd == 'PING':
         self.sendDatagram(COMMANDS['GREETING']+misc.getLocalID())
     #--- ALIVE            
     elif cmd == 'ALIVE':
         pass