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.

typedef struct InternalMessageStruct
char *msg_type

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

size_t msg_type_len
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.

char **payload

An array of “payload” packets in this message. In other words, every item which isn’t metadata.

size_t *payload_lens

The length of each payload item, in the same order

size_t num_payload

The number of payload packets

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 *constructInternalMessage(const char *type, size_t type_len, const char *sender, size_t sender_len, char **payload, size_t *payload_lens, size_t num_payload)

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

Parameters:
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