c-libp2p/include/libp2p/record/message.h

78 lines
2.4 KiB
C
Raw Permalink Normal View History

#pragma once
#include <stdint.h>
#include "libp2p/record/record.h"
/**
2017-02-20 23:53:20 +00:00
* protobuf stuff for Message
* 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
};
2017-08-30 16:09:28 +00:00
struct KademliaMessage {
2017-02-17 04:13:16 +00:00
enum MessageType message_type; // protobuf field 1 (a varint)
char* key; // protobuf field 2
size_t key_size;
struct Libp2pRecord* record; // protobuf field 3
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 04:13:16 +00:00
/**
* create a new Libp2pMessage struct
* @returns a new Libp2pMessage with default settings
*/
2017-08-30 16:09:28 +00:00
struct KademliaMessage* libp2p_message_new();
/**
* Deallocate memory from a Message struct
* @param in the struct
*/
2017-08-30 16:09:28 +00:00
void libp2p_message_free(struct KademliaMessage* 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-08-30 16:09:28 +00:00
size_t libp2p_message_protobuf_encode_size(const struct KademliaMessage* in);
/**
* 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-08-30 16:09:28 +00:00
int libp2p_message_protobuf_encode(const struct KademliaMessage* in, unsigned char* buffer, size_t max_buffer_size, size_t* bytes_written);
2017-04-13 14:30:28 +00:00
/**
* 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)
*/
2017-08-30 16:09:28 +00:00
int libp2p_message_protobuf_allocate_and_encode(const struct KademliaMessage* in, unsigned char **buffer, size_t* buffer_size);
/**
* 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)
*/
2017-08-30 16:09:28 +00:00
int libp2p_message_protobuf_decode(unsigned char* buffer, size_t buffer_size, struct KademliaMessage** out);