UDP-WG Implementation
Loading...
Searching...
No Matches
shared Namespace Reference

The shared namespace. More...

Classes

union  connection
 
struct  sys_packet
 A system packet. More...
 

Typedefs

using port_t = uint16_t
 
using address_t = uint32_t
 
using fd_t = int32_t
 
using con_t = uint64_t
 
using tag = uint8_t
 
typedef struct shared::sys_packet sys_packet
 A system packet.
 

Enumerations

enum  status { DEAD , INIT , READY , TERMINATE }
 Status used to communicate between Main and Network Threads.
 
enum  message_type {
  STANDARD , INFO , WARN , ERROR ,
  SUCCESS
}
 

Functions

std::string connection_string (const connection &c)
 Get a string representation of the connection.
 
crypto::string Timestamp ()
 Get the current TAI64N timestamp.
 
bool TimestampGreater (const crypto::string &a, const crypto::string &b)
 Compares two timestamps.
 
size_t TimestampDiff (const crypto::string &a, const crypto::string &b=Timestamp())
 Return the time between each timestamp in seconds.
 
uint64_t TimeSeconds ()
 Get the current UNIX timestamp.
 
std::mt19937 rng (dev())
 
void sleep (const size_t &milliseconds=100)
 Sleep.
 
void output (const std::string &message, const std::string &thread="SYS", const message_type &t=STANDARD)
 Output to STDOUT, thread safe.
 
void clear ()
 Clear the screen.
 
template<typename T = bool>
input (const std::string &title, const T &error_ret=T())
 std::cin can be a little difficult to use, particularly handling bad input. This sanitized it.
 
void prompt (const std::string &message)
 Prompt the user and wait until they have confirmed it.
 
void pick_mode ()
 
void pick_title ()
 
std::string print_title (const uint64_t &cycle)
 

Variables

tag NONE = 0x0
 
tag WG_HANDSHAKE = 0x1
 
tag SYS = 0x2
 
tag WG_COOKIE = 0x3
 
tag WG_TRANSPORT = 0x4
 
connection self = {.pair {.a = inet_addr("127.0.0.1"), .p = 5000}}
 
std::atomic< statusstat = DEAD
 
std::atomic< bool > verbose = false
 
std::atomic< bool > log = false
 
std::string logfile = "main.log"
 
std::random_device dev
 
std::mutex io_lock
 
constexpr char END [] = "\e[0m"
 
constexpr char RED [] = "\e[31m"
 
constexpr char YELLOW [] = "\e[1;33m"
 
constexpr char GREEN [] = "\e[32m"
 
constexpr char BLUE [] = "\e[34m"
 
constexpr char VIOLET [] = "\e[35m"
 
std::vector< std::vector< std::string > > titles
 
size_t title = 0
 
size_t mode = 0
 

Detailed Description

The shared namespace.

Parameters
Thisnamespace contains a smattering of objects and functions that are used by the other files.

Typedef Documentation

◆ sys_packet

typedef struct shared::sys_packet shared::sys_packet

A system packet.

Remarks
Originally, I had planned on making a separate program that would generate WireGuard configurations to files, which would then be read by the client/server prior to connecting. However, I decided against it to not make the connection process cumbersome; instead, when a client wants to establish a wireguard connection, they'll send one of these across, which just contains their public key and source (The only parts of knowledge needed to facilitate a handshake). Does this theoretically present a security vulnerability? I don't think so. The local public key is truncated and displayed on the main page, and both the server and client need to manually confirm that the public key they received is the one they expected. Without the corresponding private key, it's impossible for an attacker to try and sit in the middle.

Function Documentation

◆ clear()

void shared::clear ( )
inline

Clear the screen.

Remarks
This works on UNIX/Windows. It's a shell escape sequence.

◆ connection_string()

std::string shared::connection_string ( const connection & c)

Get a string representation of the connection.

Returns
the string.

◆ input()

template<typename T = bool>
T shared::input ( const std::string & title,
const T & error_ret = T() )
inline

std::cin can be a little difficult to use, particularly handling bad input. This sanitized it.

Template Parameters
TThe type of input to be returned.
Parameters
titleA title to be drawn for the input
error_retIf something causes an error, what we should return to let the caller know.
Returns
The user input, or the error return.
Remarks
This function does not re-prompt upon errors; it is the responsibility of the caller to check for the error return and act accordingly.
Warning
This function is blocking.

◆ output()

void shared::output ( const std::string & message,
const std::string & thread = "SYS",
const message_type & t = STANDARD )

Output to STDOUT, thread safe.

Parameters
messageThe message to print.
threadFor non-standard output types, the thread where the message is being sent from.
tThe type of message. Standard gets output without formatting. Others print the thread and color code the output.

◆ prompt()

void shared::prompt ( const std::string & message)
inline

Prompt the user and wait until they have confirmed it.

Parameters
messageThe message to display.
Warning
This function is blocking.

◆ sleep()

void shared::sleep ( const size_t & milliseconds = 100)

Sleep.

Parameters
millisecondsThe amount of milliseconds to sleep for.
Remarks
This program doesn't use semaphores or any similar construct to wake a thread up Instead, we just loop and sleep. It's less efficient, but it's simpler.

◆ TimeSeconds()

uint64_t shared::TimeSeconds ( )

Get the current UNIX timestamp.

Returns
The seconds since January 1st, 1970.

◆ Timestamp()

crypto::string shared::Timestamp ( )

Get the current TAI64N timestamp.

Returns
The formatted timestamp.
Remarks
As per the WireGuard Reference (5.4): "[is] 12 bytes of output, the first 8 bytes being a big-endian integer of the number of seconds since 1970 TAI and the last 4 bytes being a big-endian integer of the number of nanoseconds from the beginning of that second.
Getting the needed information is rather trivial, although we do need to do some bitshifting to get the little-endian values into big-endian format into the string.

◆ TimestampDiff()

size_t shared::TimestampDiff ( const crypto::string & a,
const crypto::string & b = Timestamp() )

Return the time between each timestamp in seconds.

Parameters
aThe first timestamp.
bThe second timestamp, defaulting to the current time.
Returns
The time between the two in seconds.
Remarks
This function returns the absolute difference.

◆ TimestampGreater()

bool shared::TimestampGreater ( const crypto::string & a,
const crypto::string & b )

Compares two timestamps.

Parameters
aThe first
bThe second
Returns
If the timepoint at a is greater or equal to b.