예제 #1
0
 def run(self):
     print "MojoCommunication listening..."
     while True:
         try:
             conn, addr = self.ss.accept()
             """
             print >>sys.stderr, "IP Address: ", addr
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             print >>sys.stderr, "hekhek"
             """
             buffer = StringIO()
             sizedata = readn(conn,4,buffer)
             size = toint(sizedata)
             msg = readn(conn,size,buffer)
             #print >>sys.stderr, "[MJ-Notif] Received Message: ", msg
             
             self.callback(addr, msg)
             conn.close()
             
         except:
             print "Unexpected error:", sys.exc_info()[0]
             print_exc()
예제 #2
0
    def run(self):
        print "MojoCommunication listening..."
        while True:
            try:
                conn, addr = self.ss.accept()
                """
                print >>sys.stderr, "IP Address: ", addr
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                print >>sys.stderr, "hekhek"
                """
                buffer = StringIO()
                sizedata = readn(conn, 4, buffer)
                size = toint(sizedata)
                msg = readn(conn, size, buffer)
                #print >>sys.stderr, "[MJ-Notif] Received Message: ", msg

                self.callback(addr, msg)
                conn.close()

            except:
                print "Unexpected error:", sys.exc_info()[0]
                print_exc()
    def check_hashpiece(self, resp):
        """ Merkle BEP style """
        print >> sys.stderr, "test: good_request: check_hashpiece"
        self.assert_(resp[0] == EXTEND)
        self.assert_(resp[1] == HASHPIECE)
        index = toint(resp[2:2 + 4])
        begin = toint(resp[6:6 + 4])
        ohlen = toint(resp[10:10 + 4])
        print >> sys.stderr, "test: good_request: check_hashpiece", index, begin, ohlen
        bohlist = resp[14:14 + ohlen]
        hisohlist = bdecode(bohlist)
        hischunk = resp[14 + ohlen:]

        if begin == 0:
            self.assert_(type(hisohlist) == ListType)
            for oh in hisohlist:
                self.assert_(type(oh) == ListType)
                self.assert_(len(oh) == 2)
                self.assert_(type(oh[0]) == IntType)
                self.assert_(type(oh[1]) == StringType)

            hisohlist.sort()
            print >> sys.stderr, "test: good_request: check_hashpiece", ` hisohlist `
            myohlist = self.tree.get_hashes_for_piece(index)
            myohlist.sort()

            self.assert_(len(hisohlist) == len(myohlist))
            for i in range(0, len(hisohlist)):
                hisoh = hisohlist[i]
                myoh = myohlist[i]
                self.assert_(hisoh == myoh)
        else:
            self.assert_(len(hisohlist) == 0)

        mylength = self.get_chunk_length(index, begin)
        mychunk = self.read_chunk(index, begin, mylength)

        self.assert_(hischunk == mychunk)

        return index == self.numpieces - 1 and mylength != 2**14
예제 #4
0
    def check_hashpiece(self,resp):
        """ Merkle BEP style """
        print >>sys.stderr,"test: good_request: check_hashpiece"
        self.assert_(resp[0] == EXTEND)
        self.assert_(resp[1] == HASHPIECE)
        index = toint(resp[2:2+4])
        begin = toint(resp[6:6+4])
        ohlen = toint(resp[10:10+4])
        print >>sys.stderr,"test: good_request: check_hashpiece",index,begin,ohlen
        bohlist = resp[14:14+ohlen]
        hisohlist = bdecode(bohlist)
        hischunk = resp[14+ohlen:]
        
        if begin == 0:
            self.assert_(type(hisohlist) == ListType)
            for oh in hisohlist:
                self.assert_(type(oh) == ListType)
                self.assert_(len(oh) == 2)
                self.assert_(type(oh[0]) == IntType)
                self.assert_(type(oh[1]) == StringType)
            
            hisohlist.sort()
            print >>sys.stderr,"test: good_request: check_hashpiece",`hisohlist`
            myohlist = self.tree.get_hashes_for_piece(index)
            myohlist.sort()
            
            self.assert_(len(hisohlist) == len(myohlist))
            for i in range(0,len(hisohlist)):
                hisoh = hisohlist[i]
                myoh = myohlist[i]
                self.assert_(hisoh == myoh)
        else:
            self.assert_(len(hisohlist) == 0)
        
        mylength = self.get_chunk_length(index,begin)
        mychunk = self.read_chunk(index,begin,mylength)
        
        self.assert_(hischunk == mychunk)

        return index == self.numpieces-1 and mylength != 2 ** 14
예제 #5
0
    def run(self):
        while True:
            try:
                conn, addr = self.ss.accept()
                buffer = StringIO()
                sizedata = readn(conn, 4, buffer)
                size = toint(sizedata)
                msg = readn(conn, size, buffer)

                if msg.startswith('START '):
                    url = msg[len('START '):]
                    self.callback('START', url)
                conn.close()

            except:
                print_exc()
예제 #6
0
 def run(self):
     while True:
         try:
             conn, addr = self.ss.accept()
             buffer = StringIO()
             sizedata = readn(conn,4,buffer)
             size = toint(sizedata)
             msg = readn(conn,size,buffer)
             
             if msg.startswith('START '):
                 url = msg[len('START '):]
                 self.callback('START',url)
             conn.close()
             
         except:
             print_exc()
예제 #7
0
    def got_download_piece(self, permid, message, selversion):
        """ Handle the DOWNLOAD_PIECE message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """
        try:
            infohash = message[1:21]
            piece = toint(message[21:25])
        except:
            print >>sys.stderr, "proxy: got_download_piece: bad data in DOWNLOAD_PIECE"
            return False

        network_got_download_piece_lambda = lambda: self.network_got_download_piece(permid, selversion, infohash, piece)
        self.session.lm.rawserver.add_task(network_got_download_piece_lambda, 0)

        return True
예제 #8
0
    def got_piece_data(self, permid, message, selversion):
        """ Handle the PIECE_DATA message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """

        if DEBUG:
            print >> sys.stderr, "doe: got_piece_data"

        try:
            infohash = message[1:21]
            piece_number = toint(message[21:25])
            piece_data = message[25:]
        except Exception, e:
            print >> sys.stderr, "doe: got_piece_data: warning - bad data in PIECE_DATA"
            print_exc()
            return False
    def got_piece_data(self, permid, message, selversion):
        """ Handle the PIECE_DATA message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """

        if DEBUG:
            print >> sys.stderr, "doe: got_piece_data"

        try:
            infohash = message[1:21]
            piece_number = toint(message[21:25])
            piece_data = message[25:]
        except Exception, e:
            print >> sys.stderr, "doe: got_piece_data: warning - bad data in PIECE_DATA"
            print_exc()
            return False
예제 #10
0
    def got_cancel_uploading_piece(self, permid, message, selversion):
        """ Handle the CANCEL_UPLOADING_PIECE message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """
        try:
            infohash = message[1:21]
            piece = toint(message[21:25])
        except:
            print >> sys.stderr, "proxy: got_cancel_uploading_piece: bad data in CANCEL_UPLOADING_PIECE"
            return False

        network_got_cancel_uploading_piece_lambda = lambda: self.network_got_cancel_uploading_piece(
            permid, selversion, infohash, piece)
        self.session.lm.rawserver.add_task(
            network_got_cancel_uploading_piece_lambda, 0)

        return True
예제 #11
0
    def got_upload_piece(self, permid, message, selversion):
        """ Handle the UPLOAD_PIECE message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """
        try:
            infohash = message[1:21]
            piece_number = toint(message[21:25])
            piece_data = message[25:]
        except:
            print >> sys.stderr, "proxy: got_upload_piece: bad data in UPLOAD_PIECE"
            return False
        # TODO: rename all piece in piece_number
        network_got_upload_piece_lambda = lambda: self.network_got_upload_piece(
            permid, selversion, infohash, piece_number, piece_data)
        self.session.lm.rawserver.add_task(network_got_upload_piece_lambda, 0)

        return True
예제 #12
0
    def got_upload_piece(self, permid, message, selversion):
        """ Handle the UPLOAD_PIECE message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """
        try:
            infohash = message[1:21]
            piece_number = toint(message[21:25])
            piece_data = message[25:]
        except:
            print >>sys.stderr, "proxy: got_upload_piece: bad data in UPLOAD_PIECE"
            return False
        # TODO: rename all piece in piece_number
        network_got_upload_piece_lambda = lambda: self.network_got_upload_piece(
            permid, selversion, infohash, piece_number, piece_data
        )
        self.session.lm.rawserver.add_task(network_got_upload_piece_lambda, 0)

        return True
    def got_dropped_piece(self, permid, message, selversion):
        """ Handle the DROPPED_PIECE message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """

        if DEBUG:
            print >> sys.stderr, "doe: got_dropped_piece"

        try:
            infohash = message[1:21]
            piece = toint(message[21:25])
        except:
            print >> sys.stderr, "doe: got_dropped_piece: warning - bad data in DROPPED_PIECE"
            return False

        # Add a task to find the appropriate Doe object method 
        network_got_dropped_piece_lambda = lambda:self.network_got_dropped_piece(permid, infohash, piece, selversion)
        self.launchmany.rawserver.add_task(network_got_dropped_piece_lambda, 0)

        return True
예제 #14
0
    def got_dropped_piece(self, permid, message, selversion):
        """ Handle the DROPPED_PIECE message.
        
        @param permid: The permid of the peer who sent the message
        @param message: The message received
        @param selversion: selected Overlay protocol version
        """

        if DEBUG:
            print >> sys.stderr, "doe: got_dropped_piece"

        try:
            infohash = message[1:21]
            piece = toint(message[21:25])
        except:
            print >> sys.stderr, "doe: got_dropped_piece: warning - bad data in DROPPED_PIECE"
            return False

        # Add a task to find the appropriate Doe object method
        network_got_dropped_piece_lambda = lambda: self.network_got_dropped_piece(
            permid, infohash, piece, selversion)
        self.launchmany.rawserver.add_task(network_got_dropped_piece_lambda, 0)

        return True
예제 #15
0
 def read_len(self, s):
     l = toint(s)
     if l > self.handler.get_max_len():
         return None
     return l, self.read_message
예제 #16
0
                connection.close()
                return
        elif t == G2G_PIECE_XFER:
            # EXTEND_MSG_G2G_V1 only, V2 is proper EXTEND msg
            if len(message) <= 12:
                if DEBUG:
                    print >> sys.stderr, "Close on bad G2G_PIECE_XFER: msg len"
                connection.close()
                return
            if not c.use_g2g:
                if DEBUG:
                    print >> sys.stderr, "Close on receiving G2G_PIECE_XFER over non-g2g connection"
                connection.close()
                return

            index = toint(message[1:5])
            begin = toint(message[5:9])
            length = toint(message[9:13])
            c.got_g2g_piece_xfer_v1(index, begin, length)
        else:
            connection.close()

        et = time.time()
        diff = et - st
        if diff > 0.1:
            print >> sys.stderr, "connecter: $$$$$$$$$$$$", getMessageName(
                t), "took", diff

    def got_extend_message(self, connection, c, message, ut_pex_enabled):
        # connection: Encrypter.Connection; c: Connecter.Connection
        if DEBUG:
 def check_request(self, data, pieces):
     piece = toint(data[0:4])
     self.assert_(piece in pieces)
예제 #18
0
 def got_message(self, connection, message):
     # connection: Encrypter.Connection; c: Connecter.Connection
     c = self.connections[connection]    
     t = message[0]
     # EXTEND handshake will be sent just after BT handshake, 
     # before BITFIELD even
     
     st = time.time()
     
     if DEBUG_NORMAL_MSGS:
         print >>sys.stderr,"connecter: Got",getMessageName(t),connection.get_ip()
     
     if t == EXTEND:
         self.got_extend_message(connection,c,message,self.ut_pex_enabled)
         return
     if t == BITFIELD and c.got_anything:
         if DEBUG:
             print >>sys.stderr,"Close on BITFIELD"
         connection.close()
         return
     c.got_anything = True
     if (t in [CHOKE, UNCHOKE, INTERESTED, NOT_INTERESTED] and 
             len(message) != 1):
         if DEBUG:
             print >>sys.stderr,"Close on bad (UN)CHOKE/(NOT_)INTERESTED",t
         connection.close()
         return
     if t == CHOKE:
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got CHOKE from",connection.get_ip()
         c.download.got_choke()
     elif t == UNCHOKE:
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got UNCHOKE from",connection.get_ip()
         c.download.got_unchoke()
     elif t == INTERESTED:
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got INTERESTED from",connection.get_ip()
         if c.upload is not None:
             c.upload.got_interested()
     elif t == NOT_INTERESTED:
         c.upload.got_not_interested()
     elif t == HAVE:
         if len(message) != 5:
             if DEBUG:
                 print >>sys.stderr,"Close on bad HAVE: msg len"
             connection.close()
             return
         i = toint(message[1:])
         if i >= self.numpieces:
             if DEBUG:
                 print >>sys.stderr,"Close on bad HAVE: index out of range"
             connection.close()
             return
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got HAVE(",i,") from",connection.get_ip()
         c.download.got_have(i)
     elif t == BITFIELD:
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got BITFIELD from",connection.get_ip()
         try:
             b = Bitfield(self.numpieces, message[1:])
         except ValueError:
             if DEBUG:
                 print >>sys.stderr,"Close on bad BITFIELD"
             connection.close()
             return
         if c.download is not None:
             c.download.got_have_bitfield(b)
     elif t == REQUEST:
         #print >>sys.stderr, "MOJO message: PoPOPOPO...Poker Face"
         #print >>sys.stderr,"connecter: Got REQUEST(",toint(message[1:5]),") from",connection.get_ip()
         if len(message) != 13:
             if DEBUG:
                 print >>sys.stderr,"Close on bad REQUEST: msg len"
             connection.close()
             return
         i = toint(message[1:5])
         if i >= self.numpieces:
             if DEBUG:
                 print >>sys.stderr,"Close on bad REQUEST: index out of range"
             connection.close()
             return
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got REQUEST(",i,") from",connection.get_ip()
         c.got_request(i, toint(message[5:9]), toint(message[9:]))
     elif t == CANCEL:
         if len(message) != 13:
             if DEBUG:
                 print >>sys.stderr,"Close on bad CANCEL: msg len"
             connection.close()
             return
         i = toint(message[1:5])
         if i >= self.numpieces:
             if DEBUG:
                 print >>sys.stderr,"Close on bad CANCEL: index out of range"
             connection.close()
             return
         c.upload.got_cancel(i, toint(message[5:9]), 
             toint(message[9:]))
     elif t == PIECE:
         x.update("RCVCOUNT", float(x.data["RCVCOUNT"][0]) + 1)
         if len(message) <= 9:
             if DEBUG:
                 print >>sys.stderr,"Close on bad PIECE: msg len"
             connection.close()
             return
         i = toint(message[1:5])
         if i >= self.numpieces:
             if DEBUG:
                 print >>sys.stderr,"Close on bad PIECE: msg len"
             connection.close()
             return
         if DEBUG_NORMAL_MSGS:
             print >>sys.stderr,"connecter: Got PIECE(",i,") from",connection.get_ip()
             
         try:
             if c.download.got_piece(i, toint(message[5:9]), [], message[9:]):
                 self.got_piece(i)
         except Exception,e:
             if DEBUG:
                 print >>sys.stderr,"Close on bad PIECE: exception",str(e)
                 traceback.print_exc()
             connection.close()
             return
 def check_request(self, data):
     index = toint(data[1:5])
     begin = toint(data[5:9])
     length = toint(data[9:])
     return (index, begin, length)
예제 #20
0
 def read_len(self, s):
     l = toint(s)
     if l > self.Encoder.max_len:
         return None
     return l, self.read_message
예제 #21
0
                connection.close()
                return
        elif t == G2G_PIECE_XFER: 
            # EXTEND_MSG_G2G_V1 only, V2 is proper EXTEND msg 
            if len(message) <= 12:
                if DEBUG:
                    print >>sys.stderr,"Close on bad G2G_PIECE_XFER: msg len"
                connection.close()
                return
            if not c.use_g2g:
                if DEBUG:
                    print >>sys.stderr,"Close on receiving G2G_PIECE_XFER over non-g2g connection"
                connection.close()
                return

            index = toint(message[1:5])
            begin = toint(message[5:9])
            length = toint(message[9:13])
            c.got_g2g_piece_xfer_v1(index,begin,length)
        else:
            connection.close()

        et = time.time()
        diff = et - st
        if diff > 0.1:
            print >>sys.stderr,"connecter: $$$$$$$$$$$$",getMessageName(t),"took",diff


    def got_extend_message(self,connection,c,message,ut_pex_enabled):
        # connection: Encrypter.Connection; c: Connecter.Connection
        if DEBUG:
 def check_request(self, data):
     piece = toint(data[0:4])
     self.assert_(piece == 1)
예제 #23
0
 def check_request(self, data):
     piece = toint(data[0:4])
     self.assert_(piece == 1)
예제 #24
0
class Connecter:
    # 2fastbt_
    def __init__(self,
                 make_upload,
                 downloader,
                 choker,
                 numpieces,
                 piece_size,
                 totalup,
                 config,
                 ratelimiter,
                 merkle_torrent,
                 sched=None,
                 coordinator=None,
                 helper=None,
                 mylistenport=None,
                 use_g2g=False,
                 infohash=None,
                 tracker=None):
        self.downloader = downloader
        self.make_upload = make_upload
        self.choker = choker
        self.numpieces = numpieces
        self.piece_size = piece_size
        self.config = config
        self.ratelimiter = ratelimiter
        self.rate_capped = False
        self.sched = sched
        self.totalup = totalup
        self.rate_capped = False
        self.connections = {}
        self.external_connection_made = 0
        self.merkle_torrent = merkle_torrent
        self.use_g2g = use_g2g
        # 2fastbt_
        self.coordinator = coordinator
        self.helper = helper
        self.round = 0
        self.mylistenport = mylistenport
        self.infohash = infohash
        self.tracker = tracker
        try:
            (scheme, netloc, path, pars, query,
             fragment) = urlparse.urlparse(self.tracker)
            host = netloc.split(':')[0]
            self.tracker_ip = socket.getaddrinfo(host, None)[0][4][0]
        except:
            print_exc()
            self.tracker_ip = None

        print >> sys.stderr, "Connecter: live: source/tracker is", self.tracker_ip

        self.overlay_enabled = 0
        if self.config['overlay']:
            self.overlay_enabled = True

        if DEBUG:
            if self.overlay_enabled:
                print >> sys.stderr, "connecter: Enabling overlay"
            else:
                print >> sys.stderr, "connecter: Disabling overlay"

        if DEBUG:
            if self.overlay_enabled:
                print >> sys.stderr, "connecter: Enabling overlay"
            else:
                print >> sys.stderr, "connecter: Disabling overlay"

        self.ut_pex_enabled = 0
        if 'ut_pex_max_addrs_from_peer' in self.config:
            self.ut_pex_max_addrs_from_peer = self.config[
                'ut_pex_max_addrs_from_peer']
            self.ut_pex_enabled = self.ut_pex_max_addrs_from_peer > 0
        self.ut_pex_previous_conns = [
        ]  # last value of 'added' field for all peers

        if DEBUG_UT_PEX:
            if self.ut_pex_enabled:
                print >> sys.stderr, "connecter: Enabling uTorrent PEX", self.ut_pex_max_addrs_from_peer
            else:
                print >> sys.stderr, "connecter: Disabling uTorrent PEX"

        # The set of messages we support. Note that the msg ID is an int not a byte in
        # this dict.
        self.EXTEND_HANDSHAKE_M_DICT = {}

        if self.overlay_enabled:
            # Say in the EXTEND handshake we support the overlay-swarm ext.
            d = {EXTEND_MSG_OVERLAYSWARM: ord(CHALLENGE)}
            self.EXTEND_HANDSHAKE_M_DICT.update(d)
        if self.ut_pex_enabled:
            # Say in the EXTEND handshake we support uTorrent's peer exchange ext.
            d = {EXTEND_MSG_UTORRENT_PEX: ord(EXTEND_MSG_UTORRENT_PEX_ID)}
            self.EXTEND_HANDSHAKE_M_DICT.update(d)
            self.sched(self.ut_pex_callback, 6)
        if self.use_g2g:
            # Say in the EXTEND handshake we want to do G2G.
            d = {EXTEND_MSG_G2G_V2: ord(G2G_PIECE_XFER)}
            self.EXTEND_HANDSHAKE_M_DICT.update(d)
            self.sched(self.g2g_callback, G2G_CALLBACK_INTERVAL)

        # LIVEHACK
        livekey = EXTEND_MSG_LIVE_PREFIX + str(CURRENT_LIVE_VERSION)
        d = {livekey: ord(LIVE_FAKE_MESSAGE_ID)}
        self.EXTEND_HANDSHAKE_M_DICT.update(d)

        print >> sys.stderr, "Connecter: EXTEND: my dict", self.EXTEND_HANDSHAKE_M_DICT

        # BarterCast
        if config['overlay']:
            self.overlay_bridge = OverlayThreadingBridge.getInstance()
        else:
            self.overlay_bridge = None

    def how_many_connections(self):
        return len(self.connections)

    def connection_made(self, connection):
        c = Connection(connection, self)

        #MojoCommunicationClient(MJ_LISTENPORT,'[download-tstream] testing...',c.get_ip())
        #print_stack()
        print >> sys.stderr, "Connecter: live: setting", connection.get_ip(
        ), c.get_ip()
        """
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        print >>sys.stderr,"MOJO"
        """
        self.connections[connection] = c

        if connection.supports_extend_messages():
            """
            print >>sys.stderr,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            print >>sys.stderr,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            print >>sys.stderr,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            print >>sys.stderr,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            print >>sys.stderr,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
            """
            #print_stack()
            # The peer either supports our overlay-swarm extension or
            # the utorrent extended protocol.

            [client, version] = decodePeerID(connection.id)

            if DEBUG:
                print >> sys.stderr, "connecter: Peer is client", client, "version", version, c.get_ip(
                )

            if self.overlay_enabled and client == TRIBLER_PEERID_LETTER and version <= '3.5.0' and connection.locally_initiated:
                # Old Tribler, establish overlay connection
                if DEBUG:
                    print >> sys.stderr, "connecter: Peer is previous Tribler version, attempt overlay connection"
                c.connect_overlay()
            elif self.ut_pex_enabled:
                # EXTEND handshake must be sent just after BT handshake,
                # before BITFIELD even
                c.send_extend_handshake()

        #TODO: overlay swarm also needs upload and download to control transferring rate
        c.upload = self.make_upload(c, self.ratelimiter, self.totalup)
        c.download = self.downloader.make_download(c)
        c.downloader = self.downloader
        self.choker.connection_made(c)
        return c

    def connection_lost(self, connection):
        c = self.connections[connection]

        ######################################
        # BarterCast
        if self.overlay_bridge is not None:
            ip = c.get_ip(False)
            port = c.get_port(False)
            down_kb = int(c.total_downloaded / 1024)
            up_kb = int(c.total_uploaded / 1024)

            if DEBUG:
                print >> sys.stderr, "BARTERCAST: attempting database update, adding olthread"

            olthread_bartercast_conn_lost_lambda = lambda: olthread_bartercast_conn_lost(
                ip, port, down_kb, up_kb)
            self.overlay_bridge.add_task(olthread_bartercast_conn_lost_lambda,
                                         0)
        else:
            if DEBUG:
                print >> sys.stderr, "BARTERCAST: no overlay bridge found"

        #########################

        print >> sys.stderr, "Connecter: live: del1: deleting", connection.get_ip(
        ), c.get_ip()
        #print_stack()
        if c.get_ip() == self.tracker_ip:
            print >> sys.stderr, "connecter: connection_lost: live: WAAH2 closing SOURCE"
            #print_stack()

        del self.connections[connection]
        if c.download:
            c.download.disconnected()
        self.choker.connection_lost(c)

    def connection_flushed(self, connection):
        conn = self.connections[connection]
        if conn.next_upload is None and (conn.partial_message is not None
                                         or conn.upload.buffer):
            self.ratelimiter.queue(conn)

    # MOJO WAS HERE
    def get_packet_loss(self):
        notRcv = x.data["REQCOUNT"][0] - x.data["RCVCOUNT"][0]
        if (x.data["REQCOUNT"][0] > 0):
            x.update("PCKTLOSS", notRcv / x.data["REQCOUNT"][0])

        return x.data["PCKTLOSS"][0]

    def get_num_msgs(self):
        return x.data["COUNTMSG"][0]

    def kick_peers(self, highpeers, lowpeers):
        """
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        print >>sys.stderr, "KICK PEERS"
        """
        peerlist = highpeers + lowpeers
        print >> sys.stderr, "[Helpedpeerlist]:\t", peerlist
        global helping
        global helpedhighpeers
        global helpedlowpeers
        helping = True
        helpedhighpeers = highpeers
        helpedlowpeers = lowpeers

        for co in self.connections.values():
            print >> sys.stderr, "[COIP]:\t", co.get_ip()
            if (co.get_ip() not in peerlist):
                co.downloader.try_kick(co.download)

    # add isServer indicator, peers to send
    def got_piece(self, i):
        global helping
        global helpedhighpeers
        maxBroadcast = (float(len(self.connections.values())) / 2) + .5
        maxBroadcast = round(maxBroadcast)
        counter = 0
        for co in self.connections.values():
            #print >>sys.stderr, "[KOKO]\t%s" % (co.get_ip())
            if helping and co.get_ip() in helpedhighpeers:
                continue
            co.send_have(i)
            counter = counter + 1
            #if counter == maxBroadcast:
            #    break

    def set_flag(self, ipAddr):
        x.update(ipAddr, 1)

    def get_flag(self, ipAddr):
        return x.is_existing(ipAddr)

    def our_extend_msg_id_to_name(self, ext_id):
        """ find the name for the given message id (byte) """
        for key, val in self.EXTEND_HANDSHAKE_M_DICT.iteritems():
            if val == ord(ext_id):
                return key
        return None

    def get_ut_pex_conns(self):
        conns = []
        for conn in self.connections.values():
            if conn.get_extend_listenport() is not None:
                conns.append(conn)
        return conns

    def get_ut_pex_previous_conns(self):
        return self.ut_pex_previous_conns

    def set_ut_pex_previous_conns(self, conns):
        self.ut_pex_previous_conns = conns

    def ut_pex_callback(self):
        """ Periocially send info about the peers you know to the other peers """
        if DEBUG_UT_PEX:
            print >> sys.stderr, "connecter: Periodic ut_pex update"

        currconns = self.get_ut_pex_conns()
        (addedconns, droppedconns) = ut_pex_get_conns_diff(
            currconns, self.get_ut_pex_previous_conns())
        self.set_ut_pex_previous_conns(currconns)
        if DEBUG_UT_PEX:
            for conn in addedconns:
                print >> sys.stderr, "connecter: ut_pex: Added", conn.get_ip(
                ), conn.get_extend_listenport()
            for conn in droppedconns:
                print >> sys.stderr, "connecter: ut_pex: Dropped", conn.get_ip(
                ), conn.get_extend_listenport()

        for c in currconns:
            if c.supports_extend_msg(EXTEND_MSG_UTORRENT_PEX):
                try:
                    if DEBUG_UT_PEX:
                        print >> sys.stderr, "connecter: ut_pex: Creating msg for", c.get_ip(
                        ), c.get_extend_listenport()
                    if c.first_ut_pex():
                        aconns = currconns
                        dconns = []
                    else:
                        aconns = addedconns
                        dconns = droppedconns
                    payload = create_ut_pex(aconns, dconns, c)
                    c.send_extend_ut_pex(payload)
                except:
                    traceback.print_exc()
        self.sched(self.ut_pex_callback, 60)

    def g2g_callback(self):
        try:
            self.sched(self.g2g_callback, G2G_CALLBACK_INTERVAL)
            for c in self.connections.itervalues():
                if not c.use_g2g:
                    continue

                c.dequeue_g2g_piece_xfer()
        except:
            print_exc()

    def got_message(self, connection, message):
        # connection: Encrypter.Connection; c: Connecter.Connection
        c = self.connections[connection]
        t = message[0]
        # EXTEND handshake will be sent just after BT handshake,
        # before BITFIELD even

        st = time.time()

        if DEBUG_NORMAL_MSGS:
            print >> sys.stderr, "connecter: Got", getMessageName(
                t), connection.get_ip()

        if t == EXTEND:
            self.got_extend_message(connection, c, message,
                                    self.ut_pex_enabled)
            return
        if t == BITFIELD and c.got_anything:
            if DEBUG:
                print >> sys.stderr, "Close on BITFIELD"
            connection.close()
            return
        c.got_anything = True
        if (t in [CHOKE, UNCHOKE, INTERESTED, NOT_INTERESTED]
                and len(message) != 1):
            if DEBUG:
                print >> sys.stderr, "Close on bad (UN)CHOKE/(NOT_)INTERESTED", t
            connection.close()
            return
        if t == CHOKE:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got CHOKE from", connection.get_ip(
                )
            c.download.got_choke()
        elif t == UNCHOKE:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got UNCHOKE from", connection.get_ip(
                )
            c.download.got_unchoke()
        elif t == INTERESTED:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got INTERESTED from", connection.get_ip(
                )
            if c.upload is not None:
                c.upload.got_interested()
        elif t == NOT_INTERESTED:
            c.upload.got_not_interested()
        elif t == HAVE:
            if len(message) != 5:
                if DEBUG:
                    print >> sys.stderr, "Close on bad HAVE: msg len"
                connection.close()
                return
            i = toint(message[1:])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad HAVE: index out of range"
                connection.close()
                return
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got HAVE(", i, ") from", connection.get_ip(
                )
            c.download.got_have(i)
        elif t == BITFIELD:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got BITFIELD from", connection.get_ip(
                )
            try:
                b = Bitfield(self.numpieces, message[1:])
            except ValueError:
                if DEBUG:
                    print >> sys.stderr, "Close on bad BITFIELD"
                connection.close()
                return
            if c.download is not None:
                c.download.got_have_bitfield(b)
        elif t == REQUEST:
            #print >>sys.stderr, "MOJO message: PoPOPOPO...Poker Face"
            #print >>sys.stderr,"connecter: Got REQUEST(",toint(message[1:5]),") from",connection.get_ip()
            if len(message) != 13:
                if DEBUG:
                    print >> sys.stderr, "Close on bad REQUEST: msg len"
                connection.close()
                return
            i = toint(message[1:5])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad REQUEST: index out of range"
                connection.close()
                return
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got REQUEST(", i, ") from", connection.get_ip(
                )
            c.got_request(i, toint(message[5:9]), toint(message[9:]))
        elif t == CANCEL:
            if len(message) != 13:
                if DEBUG:
                    print >> sys.stderr, "Close on bad CANCEL: msg len"
                connection.close()
                return
            i = toint(message[1:5])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad CANCEL: index out of range"
                connection.close()
                return
            c.upload.got_cancel(i, toint(message[5:9]), toint(message[9:]))
        elif t == PIECE:
            x.update("RCVCOUNT", float(x.data["RCVCOUNT"][0]) + 1)
            if len(message) <= 9:
                if DEBUG:
                    print >> sys.stderr, "Close on bad PIECE: msg len"
                connection.close()
                return
            i = toint(message[1:5])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad PIECE: msg len"
                connection.close()
                return
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got PIECE(", i, ") from", connection.get_ip(
                )

            try:
                if c.download.got_piece(i, toint(message[5:9]), [],
                                        message[9:]):
                    self.got_piece(i)
            except Exception, e:
                if DEBUG:
                    print >> sys.stderr, "Close on bad PIECE: exception", str(
                        e)
                    traceback.print_exc()
                connection.close()
                return

        elif t == HASHPIECE:
            # Merkle: Handle pieces with hashes
            try:
                if len(message) <= 13:
                    if DEBUG:
                        print >> sys.stderr, "Close on bad HASHPIECE: msg len"
                    connection.close()
                    return
                i = toint(message[1:5])
                if i >= self.numpieces:
                    if DEBUG:
                        print >> sys.stderr, "Close on bad HASHPIECE: index out of range"
                    connection.close()
                    return
                begin = toint(message[5:9])
                len_hashlist = toint(message[9:13])
                bhashlist = message[13:13 + len_hashlist]
                hashlist = bdecode(bhashlist)
                if not isinstance(hashlist, list):
                    raise AssertionError, "hashlist not list"
                for oh in hashlist:
                    if not isinstance(oh,list) or \
                    not (len(oh) == 2) or \
                    not isinstance(oh[0],int) or \
                    not isinstance(oh[1],str) or \
                    not ((len(oh[1])==20)):                         \
                                                raise AssertionError, "hashlist entry invalid"
                piece = message[13 + len_hashlist:]

                if DEBUG_NORMAL_MSGS:
                    print >> sys.stderr, "connecter: Got HASHPIECE", i, begin

                if c.download.got_piece(i, begin, hashlist, piece):
                    self.got_piece(i)
            except Exception, e:
                if DEBUG:
                    print >> sys.stderr, "Close on bad HASHPIECE: exception", str(
                        e)
                    traceback.print_exc()
                connection.close()
                return
예제 #25
0
 def check_request(self,data,pieces):
     piece = toint(data[0:4])
     self.assert_(piece in pieces)
예제 #26
0
    def got_message(self, connection, message):
        # connection: Encrypter.Connection; c: Connecter.Connection
        c = self.connections[connection]
        t = message[0]
        # EXTEND handshake will be sent just after BT handshake,
        # before BITFIELD even

        st = time.time()

        if DEBUG_NORMAL_MSGS:
            print >> sys.stderr, "connecter: Got", getMessageName(
                t), connection.get_ip()

        if t == EXTEND:
            self.got_extend_message(connection, c, message,
                                    self.ut_pex_enabled)
            return
        if t == BITFIELD and c.got_anything:
            if DEBUG:
                print >> sys.stderr, "Close on BITFIELD"
            connection.close()
            return
        c.got_anything = True
        if (t in [CHOKE, UNCHOKE, INTERESTED, NOT_INTERESTED]
                and len(message) != 1):
            if DEBUG:
                print >> sys.stderr, "Close on bad (UN)CHOKE/(NOT_)INTERESTED", t
            connection.close()
            return
        if t == CHOKE:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got CHOKE from", connection.get_ip(
                )
            c.download.got_choke()
        elif t == UNCHOKE:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got UNCHOKE from", connection.get_ip(
                )
            c.download.got_unchoke()
        elif t == INTERESTED:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got INTERESTED from", connection.get_ip(
                )
            if c.upload is not None:
                c.upload.got_interested()
        elif t == NOT_INTERESTED:
            c.upload.got_not_interested()
        elif t == HAVE:
            if len(message) != 5:
                if DEBUG:
                    print >> sys.stderr, "Close on bad HAVE: msg len"
                connection.close()
                return
            i = toint(message[1:])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad HAVE: index out of range"
                connection.close()
                return
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got HAVE(", i, ") from", connection.get_ip(
                )
            c.download.got_have(i)
        elif t == BITFIELD:
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got BITFIELD from", connection.get_ip(
                )
            try:
                b = Bitfield(self.numpieces, message[1:])
            except ValueError:
                if DEBUG:
                    print >> sys.stderr, "Close on bad BITFIELD"
                connection.close()
                return
            if c.download is not None:
                c.download.got_have_bitfield(b)
        elif t == REQUEST:
            #print >>sys.stderr, "MOJO message: PoPOPOPO...Poker Face"
            #print >>sys.stderr,"connecter: Got REQUEST(",toint(message[1:5]),") from",connection.get_ip()
            if len(message) != 13:
                if DEBUG:
                    print >> sys.stderr, "Close on bad REQUEST: msg len"
                connection.close()
                return
            i = toint(message[1:5])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad REQUEST: index out of range"
                connection.close()
                return
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got REQUEST(", i, ") from", connection.get_ip(
                )
            c.got_request(i, toint(message[5:9]), toint(message[9:]))
        elif t == CANCEL:
            if len(message) != 13:
                if DEBUG:
                    print >> sys.stderr, "Close on bad CANCEL: msg len"
                connection.close()
                return
            i = toint(message[1:5])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad CANCEL: index out of range"
                connection.close()
                return
            c.upload.got_cancel(i, toint(message[5:9]), toint(message[9:]))
        elif t == PIECE:
            x.update("RCVCOUNT", float(x.data["RCVCOUNT"][0]) + 1)
            if len(message) <= 9:
                if DEBUG:
                    print >> sys.stderr, "Close on bad PIECE: msg len"
                connection.close()
                return
            i = toint(message[1:5])
            if i >= self.numpieces:
                if DEBUG:
                    print >> sys.stderr, "Close on bad PIECE: msg len"
                connection.close()
                return
            if DEBUG_NORMAL_MSGS:
                print >> sys.stderr, "connecter: Got PIECE(", i, ") from", connection.get_ip(
                )

            try:
                if c.download.got_piece(i, toint(message[5:9]), [],
                                        message[9:]):
                    self.got_piece(i)
            except Exception, e:
                if DEBUG:
                    print >> sys.stderr, "Close on bad PIECE: exception", str(
                        e)
                    traceback.print_exc()
                connection.close()
                return
예제 #27
0
 def read_len(self, s):
     l = toint(s)
     # if l > self.Encoder.max_len:
     #     return None
     # if DEBUG: print >> sys.stderr, "waiting for", l, "bytes"
     return l, self.read_message
예제 #28
0
 def read_len(self, s):
     l = toint(s)
     if l > self.Encoder.max_len:
         return None
     return l, self.read_message
예제 #29
0
 def read_len(self, s):
     l = toint(s)
     # if l > self.Encoder.max_len:
     #     return None
     # if DEBUG: print >> sys.stderr, "waiting for", l, "bytes"
     return l, self.read_message
예제 #30
0
 def check_request(self,data):
     index = toint(data[1:5])
     begin = toint(data[5:9])
     length = toint(data[9:])
     return (index,begin,length)