Internal Message Header

This header contains the C functions needed for using the message format in the p2p.today project.

It automatically includes base.h and BaseConverter.h

Using this requires a compiled copy of the sha2 hashes, provided in c_src/sha/sha2.c

typedef struct InternalMessageStruct
unsigned char msg_type

The type of message this is. These are described in protocol/flags.

char *sender

The message sender’s ID

size_t sender_len
unsigned long long timestamp

The time at which this message was sent, in UTC seconds since 1/1/1970.

Note

Members after these are not garunteed to be present. A function to ensure their existence will be provided as noted. Otherwise check if their length term is 0 to determine existence.

char **compression

An array of possible compression algorigthms

This can be initialized with setInternalMessageCompressions()

size_t *compression_lens

The length of each compression string, in the same order

size_t num_compression

The number of compression methods

char *id

The checksum/ID of this message

To ensure this value is set, call ensureInternalMessageID()

size_t id_len
char *str

The serialized version of this message

size_t str_len
static InternalMessageStruct *startInternalMessage(const size_t num_packets, const char *type, size_t type_len, const char *sender, const size_t sender_len, const unsigned long long timestamp)

Constructs an InternalMessageStruct. This copies all given data into a struct, then returns this struct’s pointer.

:param num_packets The number of items you will pack (must be exact) :param type: The item to place in InternalMessageStruct.msg_type :param sender: The item to place in InternalMessageStruct.sender :param sender_len: The length of the above :param sender_is_unicode: If true, pack sender as a string, not a buffer :param timestamp: If non-zero, pack timestamp as this value

Returns:A pointer to the resulting InternalMessageStruct

Warning

You must use destroyInternalMessage() on the resulting object, or you will develop a memory leak

static void destroyInternalMessage(InternalMessageStruct *des)

free() an InteralMessageStruct and its members

Parameters:
  • des – A pointer to the InternalMessageStruct you wish to destroy
static void setInternalMessageCompressions(InternalMessageStruct *des, char **compression, size_t *compression_lens, size_t num_compressions)

Sets the compression methods for a particular InternalMessageStruct. These methods are formatted as an array of strings, an array of lengths, and a number of methods. The data is copied, so you inputs can be local variables.

Parameters:
  • des – A pointer to the relevant InternalMessageStruct
  • compression – An array of compression methods
  • compression_lens – An array of lengths for each compression method
  • num_compressions – The number of compression methods
static void ensureInternalMessageID(InternalMessageStruct *des)

Ensures that the InternalMessageStruct has an ID calculated and assigned

Parameters:
  • des – A pointer to the relevant InternalMessageStruct
static void ensureInternalMessageStr(InternalMessageStruct *des)

Ensures that the InternalMessageStruct has a serialized string calculated and assigned

Parameters:
  • des – A pointer to the relevant InternalMessageStruct
static InternalMessageStruct *deserializeInternalMessage(const char *serialized, size_t len, int sizeless)

Deserializes an uncompressed InternalMessageStruct. The sizeless parameter indicates whether the network size header is still present on the given string.

Parameters:
  • serialized – The serialized message
  • len – The length of the serialized message
  • sizeless – A boolean which indicates whether the network size header is still present on the given string
  • errored – A pointer to a boolean. If this is set with a non-zero value, it indicates that the checksum test failed
Returns:

An equivalent InternalMessageStruct, or NULL if there was an error

static InternalMessageStruct *deserializeCompressedInternalMessage(const char *serialized, size_t len, int sizeless, char **compression, size_t *compression_lens, size_t num_compressions)

Deserializes a compressed InternalMessageStruct. The sizeless parameter indicates whether the network size header is still present on the given string.

Parameters:
Returns:

An equivalent InternalMessageStruct, or NULL if there was an error