Ejemplo n.º 1
0
    def process_message(self, message, sender):
        if sender in self.bad_peers:
            return -1

        if self.is_current_message_from_splitter() or self.check_message(message, sender):
            if self.is_control_message(message) and message == 'B':
                return self.handle_bad_peers_request()
            else:
                return Peer_DBS.process_message(self, message, sender)
        else:
            self.process_bad_message(message, sender)
            return -1
Ejemplo n.º 2
0
    def process_message(self, message, sender):
        if sender in self.bad_peers:
            return -1

        if self.is_current_message_from_splitter() or self.check_message(message, sender):
            if self.is_control_message(message) and message == 'B':
                return self.handle_bad_peers_request()
            else:
                return Peer_DBS.process_message(self, message, sender)
        else:
            self.process_bad_message(message, sender)
            return -1
Ejemplo n.º 3
0
    def process_message(self, message, sender):
        # {{{ Handle NTS messages; pass other messages to base class

        if sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH + struct.calcsize("4sHHH"):
            # say [hello to (X)] received from splitter
            peer_id = message[:Common.PEER_ID_LENGTH].decode()
            IP_addr, source_port_to_splitter, port_diff, peer_number = \
                struct.unpack("4sHHH", message[Common.PEER_ID_LENGTH:])
            IP_addr = socket.inet_ntoa(IP_addr)
            source_port_to_splitter = socket.ntohs(source_port_to_splitter)
            port_diff = socket.ntohs(port_diff)
            peer_number = socket.ntohs(peer_number)

            peer = (IP_addr, source_port_to_splitter)  # Endpoint to splitter
            _p_("Received [send hello to %s %s]" % (peer_id, peer))
            _p_("port_diff = %s" % port_diff)
            _p_("peer_number = %s" % peer_number)
            # Here the port prediction happens:
            additional_ports = \
                self.get_probable_source_ports(source_port_to_splitter,
                                               port_diff, peer_number)
            self.say_hello(peer, additional_ports)
            # Directly start packet sending
            self.hello_messages_event.set()
        elif sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH + struct.calcsize("4sHHHH"):
            # say [hello to (X)] received from splitter
            peer_id = message[:Common.PEER_ID_LENGTH].decode()
            IP_addr, source_port_to_splitter, port_diff, peer_number, \
                extra_splitter_port = struct.unpack( \
                "4sHHHH", message[Common.PEER_ID_LENGTH:]) # Ojo, !H ????
            IP_addr = socket.inet_ntoa(IP_addr)
            source_port_to_splitter = socket.ntohs(source_port_to_splitter)
            port_diff = socket.ntohs(port_diff)
            peer_number = socket.ntohs(peer_number)
            extra_splitter_port = socket.ntohs(extra_splitter_port)

            peer = (IP_addr, source_port_to_splitter)  # Endpoint to splitter
            _p_("Received [send hello to %s %s]" % (peer_id, peer))
            # Here the port prediction happens:
            additional_ports = \
                self.get_probable_source_ports(source_port_to_splitter,
                                               port_diff, peer_number)
            self.say_hello(peer, additional_ports)
            # Send to extra splitter port to determine currently allocated
            # source port
            self.say_hello((self.splitter[0], extra_splitter_port))
            # Directly start packet sending
            self.hello_messages_event.set()
        elif message == self.peer_id.encode() or (sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH + struct.calcsize("H")) or \
        (sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH+1 + struct.calcsize("H")) or \
        len(message) == Common.PEER_ID_LENGTH+1: # All sent message sizes
            # Acknowledge received; stop sending the message
            with self.hello_messages_lock:
                for hello_data in self.hello_messages:
                    if message == hello_data[0] \
                    and sender[0] == hello_data[1][0] \
                    and sender[1] in self.hello_messages_ports[hello_data]:
                        _p_("Received acknowledge from %s" % (sender, ))
                        self.hello_messages.remove(hello_data)
                        del self.hello_messages_times[hello_data]
                        del self.hello_messages_ports[hello_data]
                        # No chunk number, as no chunk was received
                        return -1
            _print_(Common.NTS_COLOR +
                    "NTS: Received acknowledge from unknown host %s" %
                    (sender, ) + Color.none)
        elif len(message) == Common.PEER_ID_LENGTH:
            peer_id = message.decode()
            _p_("Received [hello (ID %s)] from %s" % (message, sender))
            # Send acknowledge
            self.team_socket.sendto(message, sender)

            if sender not in self.peer_list:
                _p_("Appending peer %s %s to list" % (peer_id, sender))
                self.peer_list.append(sender)
                self.debt[sender] = 0
                # Send source port information to splitter
                message += struct.pack("H", socket.htons(sender[1]))
                message_data = (message, self.splitter)
                self.send_message(message_data)

                if peer_id in self.initial_peer_list:
                    self.initial_peer_list.remove(peer_id)
        elif message == b'H':
            _p_("Received [DBS hello] from %s" % str(sender))
            # Ignore hello messages that are sent by Peer_DBS instances in
            # receive_the_list_of_peers() before a Peer_NTS instance is created
            pass
        elif sender != self.splitter and sender not in self.peer_list:
            _p_("Ignoring message of length %d from unknown %s" \
                  % (len(message), sender))
        elif len(self.initial_peer_list) == 0:
            # Start receiving chunks when fully incorporated
            return Peer_DBS.process_message(self, message, sender)

        # No chunk number, as no chunk was received
        return -1
Ejemplo n.º 4
0
    def process_message(self, message, sender):
        # {{{ Handle NTS messages; pass other messages to base class

        if sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH + struct.calcsize("4sHHH"):
            # say [hello to (X)] received from splitter
            peer_id = message[:Common.PEER_ID_LENGTH].decode()
            IP_addr, source_port_to_splitter, port_diff, peer_number = \
                struct.unpack("4sHHH", message[Common.PEER_ID_LENGTH:])
            IP_addr = socket.inet_ntoa(IP_addr)
            source_port_to_splitter = socket.ntohs(source_port_to_splitter)
            port_diff = socket.ntohs(port_diff)
            peer_number = socket.ntohs(peer_number)

            peer = (IP_addr, source_port_to_splitter) # Endpoint to splitter
            _p_("Received [send hello to %s %s]" % (peer_id, peer))
            _p_("port_diff = %s" % port_diff)
            _p_("peer_number = %s" % peer_number)
            # Here the port prediction happens:
            additional_ports = \
                self.get_probable_source_ports(source_port_to_splitter,
                                               port_diff, peer_number)
            self.say_hello(peer, additional_ports)
            # Directly start packet sending
            self.hello_messages_event.set()
        elif sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH + struct.calcsize("4sHHHH"):
            # say [hello to (X)] received from splitter
            peer_id = message[:Common.PEER_ID_LENGTH].decode()
            IP_addr, source_port_to_splitter, port_diff, peer_number, \
                extra_splitter_port = struct.unpack( \
                "4sHHHH", message[Common.PEER_ID_LENGTH:]) # Ojo, !H ????
            IP_addr = socket.inet_ntoa(IP_addr)
            source_port_to_splitter = socket.ntohs(source_port_to_splitter)
            port_diff = socket.ntohs(port_diff)
            peer_number = socket.ntohs(peer_number)
            extra_splitter_port = socket.ntohs(extra_splitter_port)

            peer = (IP_addr, source_port_to_splitter) # Endpoint to splitter
            _p_("Received [send hello to %s %s]" % (peer_id, peer))
            # Here the port prediction happens:
            additional_ports = \
                self.get_probable_source_ports(source_port_to_splitter,
                                               port_diff, peer_number)
            self.say_hello(peer, additional_ports)
            # Send to extra splitter port to determine currently allocated
            # source port
            self.say_hello((self.splitter[0], extra_splitter_port))
            # Directly start packet sending
            self.hello_messages_event.set()
        elif message == self.peer_id.encode() or (sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH + struct.calcsize("H")) or \
        (sender == self.splitter and \
        len(message) == Common.PEER_ID_LENGTH+1 + struct.calcsize("H")) or \
        len(message) == Common.PEER_ID_LENGTH+1: # All sent message sizes
            # Acknowledge received; stop sending the message
            with self.hello_messages_lock:
                for hello_data in self.hello_messages:
                    if message == hello_data[0] \
                    and sender[0] == hello_data[1][0] \
                    and sender[1] in self.hello_messages_ports[hello_data]:
                        _p_("Received acknowledge from %s" % (sender,))
                        self.hello_messages.remove(hello_data)
                        del self.hello_messages_times[hello_data]
                        del self.hello_messages_ports[hello_data]
                        # No chunk number, as no chunk was received
                        return -1
            _print_(Common.NTS_COLOR + "NTS: Received acknowledge from unknown host %s" % (sender,) + Color.none)
        elif len(message) == Common.PEER_ID_LENGTH:
            peer_id = message.decode()
            _p_("Received [hello (ID %s)] from %s" % (message, sender))
            # Send acknowledge
            self.team_socket.sendto(message, sender)

            if sender not in self.peer_list:
                _p_("Appending peer %s %s to list" % (peer_id, sender))
                self.peer_list.append(sender)
                self.debt[sender] = 0
                # Send source port information to splitter
                message += struct.pack("H", socket.htons(sender[1]))
                message_data = (message, self.splitter)
                self.send_message(message_data)

                if peer_id in self.initial_peer_list:
                    self.initial_peer_list.remove(peer_id)
        elif message == b'H':
            _p_("Received [DBS hello] from %s" % str(sender))
            # Ignore hello messages that are sent by Peer_DBS instances in
            # receive_the_list_of_peers() before a Peer_NTS instance is created
            pass
        elif sender != self.splitter and sender not in self.peer_list:
            _p_("Ignoring message of length %d from unknown %s" \
                  % (len(message), sender))
        elif len(self.initial_peer_list) == 0:
            # Start receiving chunks when fully incorporated
            return Peer_DBS.process_message(self, message, sender)

        # No chunk number, as no chunk was received
        return -1