2017-02-17 01:59:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-22 15:55:39 +00:00
|
|
|
#include <stdint.h>
|
2017-02-17 05:30:41 +00:00
|
|
|
#include "libp2p/record/record.h"
|
|
|
|
|
2017-02-17 01:59:59 +00:00
|
|
|
/**
|
2017-02-20 23:53:20 +00:00
|
|
|
* protobuf stuff for Message
|
2017-02-17 01:59:59 +00:00
|
|
|
* This is used for the KAD / DHT stuff
|
|
|
|
*/
|
|
|
|
|
2017-02-17 04:13:16 +00:00
|
|
|
enum MessageType {
|
|
|
|
MESSAGE_TYPE_PUT_VALUE = 0,
|
|
|
|
MESSAGE_TYPE_GET_VALUE = 1,
|
|
|
|
MESSAGE_TYPE_ADD_PROVIDER = 2,
|
|
|
|
MESSAGE_TYPE_GET_PROVIDERS = 3,
|
|
|
|
MESSAGE_TYPE_FIND_NODE = 4,
|
|
|
|
MESSAGE_TYPE_PING = 5
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Libp2pMessage {
|
|
|
|
enum MessageType message_type; // protobuf field 1 (a varint)
|
|
|
|
char* key; // protobuf field 2
|
|
|
|
size_t key_size;
|
|
|
|
struct Libp2pRecord* record; // protobuf field 3
|
2017-04-17 19:03:27 +00:00
|
|
|
struct Libp2pLinkedList* closer_peer_head; // protobuf field 8 linked list of Libp2pPeers
|
|
|
|
struct Libp2pLinkedList* provider_peer_head; // protobuf field 9 linked list of Libp2pPeers
|
2017-02-17 04:13:16 +00:00
|
|
|
int32_t cluster_level_raw; // protobuf field 10
|
2017-02-17 01:59:59 +00:00
|
|
|
};
|
2017-02-17 04:13:16 +00:00
|
|
|
|
2017-02-17 05:30:41 +00:00
|
|
|
/**
|
|
|
|
* create a new Libp2pMessage struct
|
|
|
|
* @returns a new Libp2pMessage with default settings
|
|
|
|
*/
|
|
|
|
struct Libp2pMessage* libp2p_message_new();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deallocate memory from a Message struct
|
|
|
|
* @param in the struct
|
|
|
|
*/
|
|
|
|
void libp2p_message_free(struct Libp2pMessage* in);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* determine the size necessary for a message struct to be protobuf'd
|
|
|
|
* @param in the struct to be protobuf'd
|
|
|
|
* @returns the size required
|
|
|
|
*/
|
2017-04-13 14:30:28 +00:00
|
|
|
size_t libp2p_message_protobuf_encode_size(const struct Libp2pMessage* in);
|
2017-02-17 05:30:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode a Message into a protobuf
|
|
|
|
* @param in the message
|
|
|
|
* @param buffer the byte array that will hold the protobuf
|
|
|
|
* @param max_buffer_size the amount of memory previously reserved for buffer
|
|
|
|
* @param bytes_written will hold the number of bytes written to buffer
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
2017-04-13 14:30:28 +00:00
|
|
|
int libp2p_message_protobuf_encode(const struct Libp2pMessage* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Libp2pMessage into protobuf format,
|
|
|
|
* allocating memory as needed
|
|
|
|
* @param in the Libp2pMessage to convert
|
|
|
|
* @param buffer where to store the protobuf
|
|
|
|
* @param buffer_size the size written into buffer
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int libp2p_message_protobuf_allocate_and_encode(const struct Libp2pMessage* in, unsigned char **buffer, size_t* buffer_size);
|
2017-02-17 05:30:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* turn a protobuf back into a message
|
|
|
|
* @param buffer the protobuf
|
|
|
|
* @param buffer_size the length of the buffer
|
|
|
|
* @param out the message
|
|
|
|
* @returns true(1) on success, otherwise false(0)
|
|
|
|
*/
|
|
|
|
int libp2p_message_protobuf_decode(unsigned char* buffer, size_t buffer_size, struct Libp2pMessage** out);
|
2017-02-23 16:15:48 +00:00
|
|
|
|