Esempio n. 1
0
    def listen(self):
        self.log.info("Listening at: %s:%s", self.hostname, self.port)

        if network_util.is_loopback_addr(self.hostname):
            # we are in local test mode so bind that socket on the
            # specified IP
            self.log.info("PeerListener.socket.bind('%s') LOOPBACK", self.hostname)
            self._prepare_datagram_socket()
        elif '[' in self.hostname:
            self.log.info("PeerListener.socket.bind('tcp://[*]:%s') IPV6", self.port)
            self.socket.ipv6 = True
            self._prepare_datagram_socket(socket.AF_INET6)
        else:
            self.log.info("PeerListener.socket.bind('tcp://*:%s') IPV4", self.port)
            # Temporary while I fix things
            self.hostname = '0.0.0.0'
            self._prepare_datagram_socket()

        self.is_listening = True

        def start_listening():
            while self.is_listening:

                try:
                    data, addr = self.socket.recvfrom(PEERLISTENER_RECV_FROM_BUFFER_SIZE)
                    self.log.debug('Got data from %s:%d: %s', addr[0], addr[1], data[:50])
                    count_incoming_packet(data)

                    if data[:MSG_PING_ID_SIZE] == MSG_PING_ID:
                        self.socket.sendto('pong', (addr[0], addr[1]))
                        count_outgoing_packet('pong')
                    elif data[:MSG_PONG_ID_SIZE] == MSG_PONG_ID:
                        self.event_emitter.emit('on_pong_message', (data, addr))

                    elif data[:MSG_SEND_RELAY_PING_ID_SIZE] == MSG_SEND_RELAY_PING_ID:
                        self.event_emitter.emit('on_send_relay_ping', (data, addr))

                    elif data[:MSG_RELAY_PING_ID_SIZE] == MSG_RELAY_PING_ID:
                        data = data.split(' ')
                        sender = self.guid
                        recipient = data[1]
                        self.socket.sendto('send_relay_pong %s %s' % (sender, recipient), (addr[0], addr[1]))
                        count_outgoing_packet('send_relay_pong %s %s' % (sender, recipient))

                    elif data[:MSG_SEND_RELAY_PONG_ID_SIZE] == MSG_SEND_RELAY_PONG_ID:
                        self.event_emitter.emit('on_send_relay_pong', (data, addr))

                    elif data[:MSG_HEARTBEAT_ID_SIZE] == MSG_HEARTBEAT_ID:
                        self.log.debug('We just received a heartbeat.')

                    elif data[:MSG_RELAYTO_ID_SIZE] == MSG_RELAYTO_ID:
                        self.log.debug('Relay To Packet')
                        self.event_emitter.emit('on_relayto', data)

                    elif data[:MSG_RELAY_ID_SIZE] == MSG_RELAY_ID:
                        self.log.debug('Relay Packet')
                        self.event_emitter.emit('on_message', (data, addr))

                    else:
                        self.event_emitter.emit('on_message', (data, addr))

                except socket.timeout as exc:
                    err = exc.args[0]

                    if err == 'timed out':
                        time.sleep(0.5)
                        continue
                    else:
                        sys.exit(1)
                except socket.error:
                    # No data. This is normal.
                    pass
                    # except AttributeError as err:
                    # print 'Packet was jacked up: %s', err

        Thread(target=start_listening).start()
Esempio n. 2
0
    def test_is_loopback_addr(self):
        self.assertTrue(network_util.is_loopback_addr("127.0.0.1"))
        self.assertTrue(network_util.is_loopback_addr("localhost"))

        self.assertFalse(network_util.is_loopback_addr("10.0.0.1"))
        self.assertFalse(network_util.is_loopback_addr("192.168.0.1"))
    def test_is_loopback_addr(self):
        self.assertTrue(network_util.is_loopback_addr("127.0.0.1"))
        self.assertTrue(network_util.is_loopback_addr("localhost"))

        self.assertFalse(network_util.is_loopback_addr("10.0.0.1"))
        self.assertFalse(network_util.is_loopback_addr("192.168.0.1"))
Esempio n. 4
0
    def listen(self):
        self.log.info("Listening at: %s:%s", self.hostname, self.port)

        if network_util.is_loopback_addr(self.hostname):
            # we are in local test mode so bind that socket on the
            # specified IP
            self.log.info("PeerListener.socket.bind('%s') LOOPBACK",
                          self.hostname)
            self._prepare_datagram_socket()
        elif '[' in self.hostname:
            self.log.info("PeerListener.socket.bind('tcp://[*]:%s') IPV6",
                          self.port)
            self.socket.ipv6 = True
            self._prepare_datagram_socket(socket.AF_INET6)
        else:
            self.log.info("PeerListener.socket.bind('tcp://*:%s') IPV4",
                          self.port)
            # Temporary while I fix things
            self.hostname = '0.0.0.0'
            self._prepare_datagram_socket()

        self.is_listening = True

        def start_listening():
            while self.is_listening:

                try:
                    data, addr = self.socket.recvfrom(2048)
                    self.log.debug('Got data from %s:%d: %s', addr[0], addr[1],
                                   data[:50])
                    count_incoming_packet(data)

                    if data[:4] == 'ping':
                        self.socket.sendto('pong', (addr[0], addr[1]))
                        count_outgoing_packet('pong')
                    elif data[:4] == 'pong':
                        self.ee.emit('on_pong_message', (data, addr))

                    elif data[:15] == 'send_relay_ping':
                        self.ee.emit('on_send_relay_ping', (data, addr))

                    elif data[:10] == 'relay_ping':
                        data = data.split(' ')
                        sender = self.guid
                        recipient = data[1]
                        self.socket.sendto(
                            'send_relay_pong %s %s' % (sender, recipient),
                            (addr[0], addr[1]))
                        count_outgoing_packet('send_relay_pong %s %s' %
                                              (sender, recipient))

                    elif data[:15] == 'send_relay_pong':
                        self.ee.emit('on_send_relay_pong', (data, addr))

                    elif data[:9] == 'heartbeat':
                        self.log.debug('We just received a heartbeat.')

                    elif data[:7] == 'relayto':
                        self.log.debug('Relay To Packet')
                        self.ee.emit('on_relayto', data)

                    elif data[:6] == 'relay ':
                        self.log.debug('Relay Packet')
                        self.ee.emit('on_message', (data, addr))

                    else:
                        self.ee.emit('on_message', (data, addr))

                except socket.timeout as e:
                    err = e.args[0]

                    if err == 'timed out':
                        time.sleep(0.5)
                        continue
                    else:
                        sys.exit(1)
                except socket.error:
                    # No data. This is normal.
                    pass
                    # except AttributeError as err:
                    # print 'Packet was jacked up: %s', err

        Thread(target=start_listening).start()