Skip to content

File syn_wg.h

FileList > net > 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.

enum SYN_WgState {
    SYN_WG_DISCONNECTED,
    SYN_WG_HANDSHAKE_INIT,
    SYN_WG_ESTABLISHED
};


Public Functions Documentation

function syn_wg_disconnect

Disconnect the WireGuard tunnel and close socket.

void syn_wg_disconnect (
    SYN_WG * wg
) 

Parameters:

  • wg Client context.

function syn_wg_get_stats

Retrieve WireGuard tunnel metrics and statistics.

SYN_Status syn_wg_get_stats (
    const SYN_WG * wg,
    SYN_WgStats * stats
) 

Parameters:

  • wg Client 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:

  • wg Client context.
  • config Peer configuration (copied).
  • sntp SNTP time source (must be initialized, may not be synced yet).
  • rx_buf Receive buffer (at least SYN_WG_MTU + SYN_WG_TRANSPORT_OVERHEAD).
  • rx_buf_size Receive buffer capacity.
  • tx_buf Transmit buffer (same sizing).
  • tx_buf_size Transmit buffer capacity.

function syn_wg_send

Send an IP packet through the WireGuard tunnel.

SYN_Status syn_wg_send (
    SYN_WG * wg,
    const uint8_t * ip_packet,
    size_t len
) 

Encrypts the packet with the current session key and sends it as a WireGuard transport message.

Parameters:

  • wg Client context.
  • ip_packet Raw IP packet to send.
  • len Packet 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.

SYN_PT_Status syn_wg_task (
    SYN_PT * pt,
    SYN_Task * task
) 

Handles: NTP sync wait, handshake, incoming packet processing, keepalive, and rekeying. Pass the SYN_WG context via task->user_data.

Parameters:

  • pt Protothread.
  • task Task descriptor.

Returns:

PT status.


Public Static Functions Documentation

function syn_wg_is_established

Check if the tunnel is established and ready for data.

static inline bool syn_wg_is_established (
    const SYN_WG * wg
) 

Parameters:

  • wg Client context.

Returns:

true if the session is active.


Macro Definition Documentation

define SYN_WG_INITIATION_SIZE

#define SYN_WG_INITIATION_SIZE `148`

Message sizes. Size of initiation message


define SYN_WG_KEEPALIVE_TIMEOUT

#define SYN_WG_KEEPALIVE_TIMEOUT `10`

Send keepalive if no outbound (s)


#define SYN_WG_MSG_COOKIE `3`

Cookie reply message


define SYN_WG_MSG_INITIATION

#define SYN_WG_MSG_INITIATION `1`

WireGuard message types. Handshake initiation message


define SYN_WG_MSG_RESPONSE

#define SYN_WG_MSG_RESPONSE `2`

Handshake response message


define SYN_WG_MSG_TRANSPORT

#define SYN_WG_MSG_TRANSPORT `4`

Encrypted transport message


define SYN_WG_MTU

#define SYN_WG_MTU `1420`

Default tunnel MTU (inner IP packet before encryption).


define SYN_WG_REJECT_AFTER_TIME

#define SYN_WG_REJECT_AFTER_TIME `180`

Drop session after this many seconds


define SYN_WG_REKEY_AFTER_TIME

#define SYN_WG_REKEY_AFTER_TIME `120`

WireGuard protocol timers (seconds). Initiate rekey after this many seconds


define SYN_WG_REKEY_TIMEOUT

#define SYN_WG_REKEY_TIMEOUT `5`

Retry handshake if no response (s)


define SYN_WG_RESPONSE_SIZE

#define SYN_WG_RESPONSE_SIZE `92`

Size of response message


define SYN_WG_TRANSPORT_OVERHEAD

#define SYN_WG_TRANSPORT_OVERHEAD `32`

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