File syn_wg.h¶
Go to the source code of this file
WireGuard VPN client — Noise IK, pure C99, cooperative. More...
#include "../common/syn_defs.h"#include "../port/syn_port_socket.h"#include "../pt/syn_pt.h"#include "../sched/syn_task.h"#include "syn_sntp.h"#include <stdbool.h>
Classes¶
| Type | Name |
|---|---|
| struct | SYN_WG WireGuard client context — caller-owned. |
| struct | SYN_WgConfig WireGuard peer configuration — set once at init. |
| struct | SYN_WgSession Active session derived from a completed handshake. |
| struct | SYN_WgStats WireGuard tunnel statistics structure. |
Public Types¶
| Type | Name |
|---|---|
| enum | SYN_WgState WireGuard client connection state. |
Public Functions¶
| Type | Name |
|---|---|
| void | syn_wg_disconnect (SYN_WG * wg) Disconnect the WireGuard tunnel and close socket. |
| SYN_Status | syn_wg_get_stats (const SYN_WG * wg, SYN_WgStats * stats) Retrieve WireGuard tunnel metrics and statistics. |
| void | syn_wg_init (SYN_WG * wg, const SYN_WgConfig * config, SYN_SNTP * sntp, uint8_t * rx_buf, size_t rx_buf_size, uint8_t * tx_buf, size_t tx_buf_size) Initialize the WireGuard client. |
| SYN_Status | syn_wg_send (SYN_WG * wg, const uint8_t * ip_packet, size_t len) Send an IP packet through the WireGuard tunnel. |
| SYN_PT_Status | syn_wg_task (SYN_PT * pt, SYN_Task * task) Cooperative protothread task — drives the WireGuard client. |
Public Static Functions¶
| Type | Name |
|---|---|
| bool | syn_wg_is_established (const SYN_WG * wg) Check if the tunnel is established and ready for data. |
Macros¶
| Type | Name |
|---|---|
| define | SYN_WG_INITIATION_SIZE 148 |
| define | SYN_WG_KEEPALIVE_TIMEOUT 10 |
| define | SYN_WG_MSG_COOKIE 3 |
| define | SYN_WG_MSG_INITIATION 1 |
| define | SYN_WG_MSG_RESPONSE 2 |
| define | SYN_WG_MSG_TRANSPORT 4 |
| define | SYN_WG_MTU 1420 |
| define | SYN_WG_REJECT_AFTER_TIME 180 |
| define | SYN_WG_REKEY_AFTER_TIME 120 |
| define | SYN_WG_REKEY_TIMEOUT 5 |
| define | SYN_WG_RESPONSE_SIZE 92 |
| define | SYN_WG_TRANSPORT_OVERHEAD 32 |
Detailed Description¶
Implements a WireGuard client as a cooperative protothread task. Handles the Noise_IKpsk2 handshake, ChaCha20-Poly1305 transport encryption, keepalive timers, and rekeying.
The client connects to a single WireGuard peer (server) over UDP. Decrypted IP packets are delivered to user code via a callback. User code sends packets into the tunnel via syn_wg_send().
No TUN device, no host routing — all traffic is handled in userspace. The host OS only sees a single UDP socket.
Requires: BLAKE2S, CHACHA20POLY1305, X25519, SNTP.
** **
static SYN_WG wg;
static uint8_t rx[1500], tx[1500];
SYN_WgConfig cfg = {
.endpoint = { .ip = {1,2,3,4}, .port = 51820 },
.keepalive_interval_s = 25,
};
memcpy(cfg.private_key, my_key, 32);
memcpy(cfg.peer_public_key, server_key, 32);
syn_wg_init(&wg, &cfg, &sntp, rx, sizeof(rx), tx, sizeof(tx));
wg.on_recv = my_handler;
syn_task_create(&tasks[1], "wg", syn_wg_task, 1, &wg);
Public Types Documentation¶
enum SYN_WgState¶
WireGuard client connection state.
Public Functions Documentation¶
function syn_wg_disconnect¶
Disconnect the WireGuard tunnel and close socket.
Parameters:
wgClient context.
function syn_wg_get_stats¶
Retrieve WireGuard tunnel metrics and statistics.
Parameters:
wgClient context.stats[out] Output statistics structure.
Returns:
SYN_OK on success.
function syn_wg_init¶
Initialize the WireGuard client.
void syn_wg_init (
SYN_WG * wg,
const SYN_WgConfig * config,
SYN_SNTP * sntp,
uint8_t * rx_buf,
size_t rx_buf_size,
uint8_t * tx_buf,
size_t tx_buf_size
)
Derives the public key from the private key and prepares state. Does NOT open a socket or start a handshake — that happens when the protothread task runs.
Parameters:
wgClient context.configPeer configuration (copied).sntpSNTP time source (must be initialized, may not be synced yet).rx_bufReceive buffer (at least SYN_WG_MTU + SYN_WG_TRANSPORT_OVERHEAD).rx_buf_sizeReceive buffer capacity.tx_bufTransmit buffer (same sizing).tx_buf_sizeTransmit buffer capacity.
function syn_wg_send¶
Send an IP packet through the WireGuard tunnel.
Encrypts the packet with the current session key and sends it as a WireGuard transport message.
Parameters:
wgClient context.ip_packetRaw IP packet to send.lenPacket length.
Returns:
SYN_OK on success, SYN_ERROR if no session or send failed.
function syn_wg_task¶
Cooperative protothread task — drives the WireGuard client.
Handles: NTP sync wait, handshake, incoming packet processing, keepalive, and rekeying. Pass the SYN_WG context via task->user_data.
Parameters:
ptProtothread.taskTask descriptor.
Returns:
PT status.
Public Static Functions Documentation¶
function syn_wg_is_established¶
Check if the tunnel is established and ready for data.
Parameters:
wgClient context.
Returns:
true if the session is active.
Macro Definition Documentation¶
define SYN_WG_INITIATION_SIZE¶
Message sizes. Size of initiation message
define SYN_WG_KEEPALIVE_TIMEOUT¶
Send keepalive if no outbound (s)
define SYN_WG_MSG_COOKIE¶
Cookie reply message
define SYN_WG_MSG_INITIATION¶
WireGuard message types. Handshake initiation message
define SYN_WG_MSG_RESPONSE¶
Handshake response message
define SYN_WG_MSG_TRANSPORT¶
Encrypted transport message
define SYN_WG_MTU¶
Default tunnel MTU (inner IP packet before encryption).
define SYN_WG_REJECT_AFTER_TIME¶
Drop session after this many seconds
define SYN_WG_REKEY_AFTER_TIME¶
WireGuard protocol timers (seconds). Initiate rekey after this many seconds
define SYN_WG_REKEY_TIMEOUT¶
Retry handshake if no response (s)
define SYN_WG_RESPONSE_SIZE¶
Size of response message
define SYN_WG_TRANSPORT_OVERHEAD¶
WireGuard transport overhead: type(4) + receiver(4) + counter(8) + tag(16) = 32
The documentation for this class was generated from the following file src/syntropic/net/syn_wg.h