Example #1
0
 def encode(self):
     size = len(self.data) + 4
     # encode the size into two bytes
     b_size = short_to_bytes(size)
     b_code = short_to_bytes(self.code)
     # add all the bytes up to construct the message
     b_msg = b_size + b_code + self.data
     return b_msg
Example #2
0
    def encode(self):
        """
        Encodes this message into a byte array

        :return: a byte array with the encoded header and payload
        """
        size = len(self.data) + 4
        # encode the size into two bytes
        b_size = short_to_bytes(size)
        # encode the message code into two bytes
        b_code = short_to_bytes(self.code)
        # add all the bytes up to construct the message
        b_msg = b_size + b_code + self.data
        return b_msg
Example #3
0
def send_msg(sock, code, msg):
    """
    Method by which a Message is encoded and sent

    :param sock: the socket to send the message on
    :param code: the code of the message
    :param msg: the message to encode
    """
    # the size of the message is size of the code to send + 4 bytes
    size = len(msg) + 4
    # encode the size into two bytes
    b_size = short_to_bytes(size)
    # encode the message code into two bytes
    b_code = short_to_bytes(code)
    # add all the bytes up to construct the message
    b_msg = b_size + b_code + msg
    # send everything
    logging.info('Send message: %d | %d | %s' % (size, code, msg))
    sock.send(b_msg)
Example #4
0
def pack_gossip_notification(msg_id, data_type, msg_data):
    """
    Method by which a message of type 'GOSSIP NOTIFICATION' is packed/encoded

    :param msg_id: the id of the message
    :param data_type: the data type of the message
    :param msg_data: the data to pack
    :return: dict, code and data
    """
    b_type = short_to_bytes(data_type)
    b_msg_id = short_to_bytes(msg_id)

    data = b''
    for i in list(msg_data):
        data += bytes([int(i)])
    return {
        'code': MESSAGE_CODE_NOTIFICATION,
        'data': b_msg_id + b_type + data
    }
def test_short_to_bytes():
    short_first = 65535
    short_second = 0
    short_third = 3999
    short_fourth = 255
    sf_bytes = byte_formatting.short_to_bytes(short_first)
    assert sf_bytes[0] == 0xff
    assert sf_bytes[1] == 0xff

    ss_bytes = byte_formatting.short_to_bytes(short_second)
    assert ss_bytes[0] == 0x00
    assert ss_bytes[1] == 0x00

    st_bytes = byte_formatting.short_to_bytes(short_third)
    assert st_bytes[0] == 0x0F
    assert st_bytes[1] == 0x9F

    sfo_bytes = byte_formatting.short_to_bytes(short_fourth)
    assert sfo_bytes[0] == 0x00
    assert sfo_bytes[1] == 0xff
Example #6
0
def pack_gossip_peer_init(identifier):
    """
    Method by which a message of type MESSAGE_CODE_PEER_INIT is packed/encoded

    :return: dict, code and data
    """
    address, port = identifier.split(':')
    ipv4_part_1, ipv4_part_2, ipv4_part_3, ipv4_part_4 = address.split('.')
    b_address = bytes([int(ipv4_part_1)]) + bytes([int(ipv4_part_2)]) + bytes(
        [int(ipv4_part_3)]) + bytes([int(ipv4_part_4)])
    b_port = short_to_bytes(port)

    return {'code': MESSAGE_CODE_PEER_INIT, 'data': b_address + b_port}
Example #7
0
def pack_gossip_notify(data_type):
    """
    Method by which a message of type 'GOSSIP NOTIFY' is packed/encoded

    :param data_type: the data type of the message
    :return: dict, code and data
    """
    # reserved byte
    b_reserved = bytes([0])
    b_type = short_to_bytes(data_type)
    return {
        'code': MESSAGE_CODE_NOTIFY,
        'data': b_reserved + b_reserved + b_type
    }
Example #8
0
def pack_gossip_peer_request(address_port):
    """
    Method by which a message of type MESSAGE_CODE_PEER_REQUEST is packed/encoded

    :return: dict, code and data
    """
    address, port = address_port.split(':')
    ipv4_part_1, ipv4_part_2, ipv4_part_3, ipv4_part_4 = address.split('.')
    b_address = bytes([int(ipv4_part_1)]) + bytes([int(ipv4_part_2)]) + bytes(
        [int(ipv4_part_3)]) + bytes([int(ipv4_part_4)])
    b_port = short_to_bytes(port)
    b_reserved = b'\x00'
    return {
        'code': MESSAGE_CODE_PEER_REQUEST,
        'data': b_address + b_port + b_reserved + b_reserved
    }
Example #9
0
def pack_gossip_peer_response(local_connections):
    """
    Method by which a message of type MESSAGE_CODE_PEER_RESPONSE is packed/encoded

    :param local_connections: list with all local connections
    :return: dict with format {'code': <message_code>, 'data': <data>}
    """
    b_connections = b''
    for key in local_connections:
        address, port = key.split(':')
        ipv4_part_1, ipv4_part_2, ipv4_part_3, ipv4_part_4 = address.split('.')
        b_address = bytes([int(ipv4_part_1)]) + bytes([
            int(ipv4_part_2)
        ]) + bytes([int(ipv4_part_3)]) + bytes([int(ipv4_part_4)])
        b_port = short_to_bytes(port)
        b_connections += b_address + b_port

    return {'code': MESSAGE_CODE_PEER_RESPONSE, 'data': b_connections}
Example #10
0
def pack_gossip_validation(msg_id, valid_bit):
    """
    Method by which a message of type 'GOSSIP VALIDATION' is packed/encoded

    :param msg_id: the id of the message
    :param valid_bit: set iff valid, unset else
        value may be 0 or 1
    :return: dict, code and data
    """
    b_msg_id = short_to_bytes(msg_id)
    if valid_bit == 0:
        b_valid_bit = b'\x00'
    elif valid_bit == 1:
        b_valid_bit = b'\x01'
    else:
        raise ValueError('Valid bit may only be 1 or 0')
    return {
        'code': MESSAGE_CODE_VALIDATION,
        'data': b_msg_id + bytes([0]) + b_valid_bit
    }
Example #11
0
def pack_gossip_peer_update(identifier, ttl, update_type):
    """
    Method by which a message of type MESSAGE_CODE_PEER_UPDATE is packed/encoded

    :return: dict, code and data
    """
    address, port = identifier.split(':')
    ipv4_part_1, ipv4_part_2, ipv4_part_3, ipv4_part_4 = address.split('.')
    b_address = bytes([int(ipv4_part_1)]) + bytes([int(ipv4_part_2)]) + bytes(
        [int(ipv4_part_3)]) + bytes([int(ipv4_part_4)])
    b_port = short_to_bytes(port)
    b_ttl = bytes([(int(ttl) & 0xff)])
    if update_type == PEER_UPDATE_TYPE_PEER_LOST:
        b_update = b'\x00'
    elif update_type == PEER_UPDATE_TYPE_PEER_FOUND:
        b_update = b'\x01'
    else:
        raise ValueError('update type may only be 0 or 1')
    return {
        'code': MESSAGE_CODE_PEER_UPDATE,
        'data': b_address + b_port + b_ttl + b_update
    }
Example #12
0
def pack_gossip_announce(ttl, data_type, msg_data):
    """
    Method by which a message of type 'GOSSIP ANNOUNCE' is packed/encoded

    :param ttl: the time to live
    :param data_type: the data type of the message
    :param msg_data: the data to pack
    :return: dict, code and data
    """
    if ttl >= 0xff:
        raise ValueError('TTL may not be larger than 1 byte')
    # encode the ttl
    b_ttl = bytes([ttl])
    # reserved byte
    b_reserved = bytes([0])
    b_type = short_to_bytes(data_type)
    data = b''
    for i in list(msg_data):
        data += bytes([int(i)])
    return {
        'code': MESSAGE_CODE_ANNOUNCE,
        'data': b_ttl + b_reserved + b_type + data
    }