Base Module

This module contains common functions and classes used throughout the rest of the library

CP2P_PROTOCOL_MAJOR_VERSION

This macro defines the major version number. A change here indicates a major change or release, and may be breaking. In a scheme x.y.z, it would be x

CP2P_PROTOCOL_MINOR_VERSION

This macro defines the minor version number. It refers specifically to minor protocol revisions, and all changes here are API compatible (after 1.0), but not compatbile with other nodes. In a scheme x.y.z, it would be y

CP2P_NODE_VERSION

This macro defines the patch version number. It refers specifically to node policies, and all changes here are backwards compatible. In a scheme x.y.z, it would be z

CP2P_VERSION

This macro is a string literal. It combines all the above macros into a single string. It will generate whatever a string literal would normally be interpreted as in that context.

CP2P_DEBUG_FLAG

This macro indicates whether cp2p should generate debug prints. If you define this as anything it will print

static const unsigned char *flags::reserved_cstr

This binary data string contains every reserved flag.

Note

This will be refactored later to an array of unsigned char * s, but for know just know that all flags are one char long.

static const size_t flags::reserved_len

The length of the above string

static const unsigned char *flags::implemented_compressions_cstr

This binary data string contains the flag of every implemented compression methods.

Note

This will be refactored later to an array of unsigned char * s, but for know just know that all flags are one char long.

static const size_t flags::compression_len

The length of the above string

static const unsigned char flags::other_flags

These are the flags currently reserved. They are guarunteed to be the same names and values as the flags within py2p.base.flags.

Note

This will be refactored later to an array of unsigned char * s, but for know just know that all flags are one char long.

static std::string get_user_salt()

This generates a uuid4 for use in this library

static const std::string user_salt

A generated uuid4 for use in this library

unsigned long long unpack_value(std::string str)

Unpacks a big-endian binary value into an unsigned long long

Parameters:str – The value you’d like to unpack
Returns:The value this string contained

Warning

Integer overflow will not be accounted for

std::string pack_value(size_t len, unsigned long long i)

Packs an unsigned long long into a big-endian binary string of length len

Parameters:
  • len – The length of the string you’d like to produce
  • i – The value you’d like to pack
Returns:

A std::string packed with the equivalent big-endian data

Warning

Integer overflow will not be accounted for

std::string sanitize_string(std::string str, bool sizeless)

This function takes in a string and removes metadata that the pathfinding_message deserializer can’t handle

Parameters:
  • str – The string you would like to sanitize
  • sizeless – A bool indicating if this string has a size header attached
Returns:

A std::string which has the safe version of str

std::string decompress_string(std::string str, std::vector<std::string> compressions)

This function is currently an identity function which returns str. In the future this function will decompress strings for the pathfinding_message parser to deal with.

Parameters:
  • str – The string you would like to decompress
  • compressions – A std::vector<std::string> which contains the list of possible compression methods
Returns:

A std::string which has the decompressed version of str

std::vector<std::string> process_string(std::string str)

This deserializes a pathfinding_message string into a std::vector<std::string> of packets

Parameters:str – The :cpp:class`std::string` you would like to parse
Returns:A std::vector<std::string> which contains the packets serialized in this string
class protocol

This class is used as a subnet object. Its role is to reject undesired connections. If you connect to someone who has a different protocol object than you, this descrepency is detected, and you are silently disconnected.

protocol::protocol(std::string, std::string encryption)
Parameters:
  • subnet – The subnet you’d like to use
  • encryption – The encryption method you’d like to use
protocol::~protocol()

An empty deconstructor

std::string protocol::id()
Returns:A std::string which contains the base_58 encoded, SHA256 based ID of this protocol object
std::string protocol::subnet
std::string protocol::encryption
class pathfinding_message

This is the message serialization/deserialization class.

pathfinding_message::pathfinding_message(std::string msg_type, std::string sender, std::vector<std::string> payload)
pathfinding_message::pathfinding_message(std::string msg_type, std::string sender, std::vector<std::string> payload, std::vector<std::string> compressions)
Parameters:
  • msg_type – This is the main flag checked by nodes, used for routing information
  • sender – The ID of the person sending the message
  • payload – A std::vector<std::string> of “packets” that you want your peers to receive
  • compression – A std::vector<std::string> of compression methods that the receiver supports
static pathfinding_message *pathfinding_message::feed_string(std::string msg)
static pathfinding_message *pathfinding_message::feed_string(std::string msg, bool sizeless)
static pathfinding_message *pathfinding_message::feed_string(std::string msg, std::vector<std::string> compressions)
static pathfinding_message *pathfinding_message::feed_string(std::string msg, bool sizeless, std::vector<std::string> compressions)
Parameters:
  • msg – A std::string which contains the serialized message
  • sizeless – A bool which indicates if the message has a size header attached (default: it does)
  • compressions – A std::vector<std::string> which contains the possible compression methods this message may be using
Returns:

A pointer to the deserialized message

pathfinding_message::~pathfinding_message()

An empty deconstructor

std::string pathfinding_message::msg_type
std::string pathfinding_message::sender
unsigned long pathfinding_message::timestamp
std::vector<std::string> pathfinding_message::payload
std::vector<std::string> pathfinding_message::compression
bool pathfinding_message::compression_fail
std::string pathfinding_message::compression_used()
Returns:The compression method this message was sent under
std::string pathfinding_message::time_58()
Returns:pathfinding_message::timestamp encoded in base_58
std::string pathfinding_message::id()
Returns:A SHA384 hash of this message encoded in base_58
std::vector<std::string> pathfinding_message::packets()

A copy of pathfinding_message::payload with some additional metadata appended to the front. Specifically:

  1. pathfinding_message::msg_type
  2. pathfinding_message::sender
  3. pathfinding_message::id()
  4. pathfinding_message::time_58()
  5. pathfinding_message::payload from here on out
Returns:A std::vector<std::string> in the above format
std::string pathfinding_message::base_string()
Returns:the serialized message, excepting the four byte size header at the beginning
std::string pathfinding_message::str()
Returns:the serialized message, including the four byte size header at the beginning
unsigned long long pathfinding_message::length()
Returns:the length of the serialized message, excepting the four byte size header at the beginning
std::string pathfinding_message::header()
Returns:the four byte size header at the beginning of the serialized message