Ejemplo n.º 1
0
 def process_data(self,data):
     if protocol.compare_command(data,"getaddr"): #get known peers
         pass
     elif protocol.compare_command(data,"addr"):#in reponse to getaddr
         self._process_addr(data)
     elif protocol.compare_command(data,"version"):
         pass
     elif protocol.compare_command(data,"verack"):
         pass
     elif protocol.compare_command(data,"inv"): #advertise knowledge of tx or block
         self._process_inv(data)
     elif protocol.compare_command(data,"getblocks"):#request an inv packet for blocks
         pass
     elif protocol.compare_command(data,"getheaders"):#request headers
         pass
     elif protocol.compare_command(data,"headers"):#return header in reponse to getheader
         pass
     elif protocol.compare_command(data,"getdata"):#get data from peer after broadcasting tx via inv
         self._process_get_data(data)  
     elif protocol.compare_command(data,"notfound"):#not found is sent after getdata recieved
         pass
     elif protocol.compare_command(data,"block"):#describe a block in reponse to getdata
         pass
     elif protocol.compare_command(data,"tx"):#describe a transaction in repones to getdata
         pass
     elif protocol.compare_command(data,"pong"):#response to ping
         pass
     elif protocol.compare_command(data,"ping"):#query if tcp ip is alive
         pass
     else:
         print("unhandled command recieved:",protocol.get_command_msgheader(data))
Ejemplo n.º 2
0
    def get_packet(self):

        if( len(self.recv_buffer) >= MSGHEADER_SIZE):
            data_length=protocol.get_length_msgheader(self.recv_buffer)
            self.expected_msg_size=data_length+MSGHEADER_SIZE
            #if valid command is not contained, packet will be thrown out 
            if protocol.is_valid_command(self.recv_buffer)==False:
                print('Invalid command:',protocol.get_command_msgheader(self.recv_buffer))
                self.total_junk_bytes_received+=len(self.recv_buffer)
                self.expected_msg_size=0
                self.recv_buffer=''
                return '' 
        try:
            self.recv_buffer+=self.my_socket.recv(TCP_RECV_PACKET_SIZE)
        except IOError as e:
            print("I/O error({0}): {1}".format(e.errno, e.strerror))
            return ''  

        #if entire message is assembled exactly, return it
        if(len(self.recv_buffer) >= self.expected_msg_size and self.expected_msg_size !=0):
            self.total_valid_bytes_received+=self.expected_msg_size
            out=self.recv_buffer[0:self.expected_msg_size]
            self.recv_buffer=self.recv_buffer[self.expected_msg_size:]
            self.expected_msg_size=0            
            return out 
            
        #otherwise output is not ready, return empty string
        else:
            return '' 
Ejemplo n.º 3
0
    def get_packet(self):

        if( len(self.recv_buffer) >= protocol.MSGHEADER_SIZE):
            data_length=protocol.get_length_msgheader(self.recv_buffer)
            self.expected_msg_size=data_length+protocol.MSGHEADER_SIZE
            #if valid command is not contained, packet will be thrown out 
            if protocol.is_valid_command(self.recv_buffer,self.crypto)==False:
                self.total_junk_bytes_received+=len(self.recv_buffer)
                self.expected_msg_size=0
                cmd = protocol.get_command_msgheader(self.recv_buffer)
                self.recv_buffer=''
                logging.error('Invalid command found in buffer: {}'.format(cmd))
                return '' 
        try:
            self.recv_buffer+=self.my_socket.recv(TCP_RECV_PACKET_SIZE)
        except IOError as e:
            logging.warn("Get packet from {0} I/O error({1}): {2}".format(self.address,e.errno, e.strerror))
            return ''  

        #if entire message is assembled exactly, return it
        if(len(self.recv_buffer) >= self.expected_msg_size and self.expected_msg_size !=0):
            self.total_valid_bytes_received+=self.expected_msg_size
            out=self.recv_buffer[0:self.expected_msg_size]
            self.recv_buffer=self.recv_buffer[self.expected_msg_size:]
            self.expected_msg_size=0            
            return out 
            
        #otherwise output is not ready, return empty string
        else:
            return '' 
Ejemplo n.º 4
0
 def process_data(self,data):
     if protocol.compare_command(data,"getaddr"): #get known peers
         pass
     elif protocol.compare_command(data,"addr"):#in reponse to getaddr
         self._process_addr(data)
     elif protocol.compare_command(data,"version"):
         pass
     elif protocol.compare_command(data,"verack"):
         pass
     elif protocol.compare_command(data,"inv"): #advertise knowledge of tx or block
         self._process_inv(data)
     elif protocol.compare_command(data,"getblocks"):#request an inv packet for blocks
         pass
     elif protocol.compare_command(data,"getheaders"):#request headers
         pass
     elif protocol.compare_command(data,"headers"):#return header in reponse to getheader
         pass
     elif protocol.compare_command(data,"getdata"):#get data from peer after broadcasting tx via inv
         self._process_get_data(data)  
     elif protocol.compare_command(data,"notfound"):#not found is sent after getdata recieved
         pass
     elif protocol.compare_command(data,"block"):#describe a block in reponse to getdata
         pass
     elif protocol.compare_command(data,"tx"):#describe a transaction in repones to getdata
         pass
     elif protocol.compare_command(data,"pong"):#response to ping
         pass
     elif protocol.compare_command(data,"ping"):#query if tcp ip is alive
         pass
     elif protocol.compare_command(data,"reject"): 
         self._process_reject(data)
     elif (self.crypto in ['dashpay','dashpay_testnet'] and 
           protocol.compare_command(data,'dseep')):
         pass
     else:
         logging.warn("unhandled command recieved:{}".format(
                                 protocol.get_command_msgheader(data)))