UDP-WG Implementation
Loading...
Searching...
No Matches
udp::packet Class Reference

A UDP Packet. More...

#include <udp.h>

Public Member Functions

 packet (const connection &dst, const std::string &data)
 Create a packet from a string.
 
 packet (const std::string &in)
 Construct a packet from a string buffer.
 
template<typename T >
 packet (const connection &dst, const T &data, const size_t &size)
 Construct a packet from any variable data, and its size.
 
std::string str () const
 Print the packet.
 
template<typename T >
const T cast () const
 Cast the content of the packet as a type.
 
std::string data () const
 Return the data.
 
std::string buffer () const
 Create a buffer of the packet that can be sent across the network.
 
connection destination () const
 Get the address and port of the destination.
 
connection source () const
 Get the address and port of the source.
 

Static Public Member Functions

static packet empty (const connection &dst)
 An empty packet.
 

Friends

void network::thread (port_t, wireguard::config wg)
 

Detailed Description

A UDP Packet.

Remarks
The entire UDP Packet contains 2 parts: a Header, and the Data. Looking at the structure, you may be slightly confused by this "pseudo-header". TCP/UDP operates at the Transport Layer, which deals with Ports; the Network Layer deals with IP. UDP/TCP should not need to deal with addresses, yet this pseudo-header contains them. Why is this? The creator of UDP, David Reed, explains why the pseudo-header exists: https://www.postel.org/pipermail/end2end-interest/2005-February/004616.html and it boils down to historical reasons. TCP used to be one big protocol that encompassed both IP and TCP. When TCP was split, we still wanted to include address within the checksum, and rather than include that information both within an IP header, and a TCP/UDP header, we have this pseudo-header that is "shared" by both layers. Interestingly, Reed further explains that the original intention was to have everything but the pseudo-header encrypted, and by encrypting the checksum that included this address information, man-in-the-middle attacks would be thwarted. This plan was blocked by both the NSA, and by "the terrorists who invented NAT". The pseudo-header isn't typically sent across the wire. The Reference states that: "[it's] conceptually prefixed to the UDP header", and Wikipedia stresses that: " "[it's] not the real IPv4 header used to send an IP packet, it is used only for the checksum calculation." (https://en.wikipedia.org/wiki/User_Datagram_Protocol#IPv4_pseudo_header). Since our implementation only deals with UDP, rather than implementing the IP protocol as well, we send the Pseudo-Header across the wire for destination resolving.

Constructor & Destructor Documentation

◆ packet() [1/3]

udp::packet::packet ( const connection & dst,
const std::string & data )
inline

Create a packet from a string.

Parameters
dstThe connection receiving the packet.
dataThe data string to send.
Warning
This function will only take UINT16_MAX bytes from the data string.

◆ packet() [2/3]

udp::packet::packet ( const std::string & in)
inline

Construct a packet from a string buffer.

Parameters
Thebuffer, should be the value from udp::packet.buffer().

◆ packet() [3/3]

template<typename T >
udp::packet::packet ( const connection & dst,
const T & data,
const size_t & size )
inline

Construct a packet from any variable data, and its size.

Parameters
dstThe destination.
dataThe data to store.
sizeThe size of that data.

Member Function Documentation

◆ buffer()

std::string udp::packet::buffer ( ) const
inline

Create a buffer of the packet that can be sent across the network.

Returns
The buffer.

◆ cast()

template<typename T >
const T udp::packet::cast ( ) const
inline

Cast the content of the packet as a type.

Template Parameters
TThe type to cast
Returns
The casted value.

◆ data()

std::string udp::packet::data ( ) const
inline

Return the data.

Returns
The data.

◆ destination()

connection udp::packet::destination ( ) const
inline

Get the address and port of the destination.

Returns
The connection.

◆ empty()

static packet udp::packet::empty ( const connection & dst)
inlinestatic

An empty packet.

Parameters
dstThe destination.
Returns
The empty packet.
Remarks
The Main Thread signals to the Network Thread that a destination should be closed by sending an empty packet. This packet doesn't actually get sent across the wire, just enqueued into the out thread. This is used when a WireGuard client stops the connection, and wants to close the FD associated with it. That way, the server can close the listening socket.

◆ source()

connection udp::packet::source ( ) const
inline

Get the address and port of the source.

Returns
The connection.

◆ str()

std::string udp::packet::str ( ) const
inline

Print the packet.

Returns
The string.
Remarks
Turn on –packet-print to see the packets like this.

The documentation for this class was generated from the following file: