Example #1
0
def receive_msg(sock):
    """
    Method by which a byte message is decoded

    :param sock: tcp socket to read from
    :return: a dict of the decoded values
    """
    msg_hdr = sock.recv(4)
    if len(msg_hdr) is 0:
        raise GossipClientDisconnectedException('Client disconnected')
    elif len(msg_hdr) < 4:
        raise GossipMessageException('Invalid header (< 4)')
    # the first and the second byte encode the message length
    size_fst, size_snd = struct.unpack('{}B'.format(2), msg_hdr[:2])
    size = bytes_to_short(size_fst, size_snd)
    if size < 4:
        raise GossipMessageException('Invalid size (< 4)')
    # third and forth byte encode the message code
    msg_code_fst, msg_code_snd = struct.unpack('{}B'.format(2), msg_hdr[2:4])
    code = bytes_to_short(msg_code_fst, msg_code_snd)
    if not MESSAGE_CODE_GOSSIP_MIN <= code < MESSAGE_CODE_GOSSIP_MAX:
        raise GossipMessageException('Invalid message code')
    data = sock.recv(size - 4)
    logging.info('Received message: %d | %d | %s' % (size, code, data))
    msg = {'size': size, 'code': code, 'message': data}
    return msg
def test_bytes_to_short():
    short_first = byte_formatting.bytes_to_short(0xff, 0xff)
    short_second = byte_formatting.bytes_to_short(0x00, 0x00)
    short_third = byte_formatting.bytes_to_short(0x0f, 0x9f)
    short_fourth = byte_formatting.bytes_to_short(0x00, 0xff)
    assert short_first == 65535
    assert short_second == 0
    assert short_third == 3999
    assert short_fourth == 255
Example #3
0
    def __init__(self, data):
        """
        C'Tor

        :param bytes data: the data from this message
        """
        super().__init__(MESSAGE_CODE_NOTIFICATION, data)
        msg_id_fst, msg_id_snd = struct.unpack('{}B'.format(2), data[:2])
        self.msg_id = bytes_to_short(msg_id_fst, msg_id_snd)
        data_type_fst, data_type_snd = struct.unpack('{}B'.format(2),
                                                     data[2:4])
        self.data_type = bytes_to_short(data_type_fst, data_type_snd)
        self.msg = struct.unpack('{}B'.format(len(data) - 4),
                                 data[4:len(data)])
Example #4
0
    def __init__(self, data):
        """
        C'Tor

        :param bytes data: the data of the message
        """
        super().__init__(MESSAGE_CODE_NOTIFY, data)
        data_type_fst, data_type_snd = struct.unpack('{}B'.format(2),
                                                     data[2:4])
        self.data_type = bytes_to_short(data_type_fst, data_type_snd)
Example #5
0
    def __init__(self, data):
        """
        C'Tor

        :param bytes data: the data of the message
        """
        super().__init__(MESSAGE_CODE_ANNOUNCE, data)
        self.ttl = struct.unpack('{}B'.format(1), data[:1])[0]
        data_type_fst, data_type_snd = struct.unpack('{}B'.format(2),
                                                     data[2:4])
        self.data_type = bytes_to_short(data_type_fst, data_type_snd)
        self.msg = struct.unpack('{}B'.format(len(data) - 4),
                                 data[4:len(data)])
Example #6
0
    def __init__(self, data):
        """
        C'Tor

        :param data: the data from this message
        """
        super().__init__(MESSAGE_CODE_VALIDATION, data)
        msg_id_fst, msg_id_snd = struct.unpack('{}B'.format(2), data[:2])
        self.msg_id = bytes_to_short(msg_id_fst, msg_id_snd)
        _, valid = struct.unpack('{}B'.format(2), data[2:4])
        if valid == 0:
            self.valid = False
        else:
            self.valid = True
Example #7
0
    def __init__(self, data):
        """
        C'Tor

        :param data: the data from this message
        """
        super().__init__(MESSAGE_CODE_PEER_INIT, data)
        self.data = data
        ipv4_part_1 = int(self.data[0])
        ipv4_part_2 = int(self.data[1])
        ipv4_part_3 = int(self.data[2])
        ipv4_part_4 = int(self.data[3])
        port = bytes_to_short(self.data[4], self.data[5])
        self.address = '%s.%s.%s.%s:%s' % (ipv4_part_1, ipv4_part_2,
                                           ipv4_part_3, ipv4_part_4, port)
Example #8
0
    def __init__(self, data):
        """
        C'Tor

        :param data: the data from this message
        """
        super().__init__(MESSAGE_CODE_PEER_RESPONSE, data)
        self.data = data
        self.connections = []
        if len(self.data) > 0:
            data = struct.unpack('{}B'.format(len(self.data)), self.data)
            for i in range(0, len(self.data), 6):
                ipv4_part_1 = int(data[i])
                ipv4_part_2 = int(data[i + 1])
                ipv4_part_3 = int(data[i + 2])
                ipv4_part_4 = int(data[i + 3])
                port = bytes_to_short(self.data[i + 4], self.data[i + 5])
                address = '%s.%s.%s.%s:%s' % (ipv4_part_1, ipv4_part_2,
                                              ipv4_part_3, ipv4_part_4, port)
                self.connections.append(address)