Skip to the content.

Client SDK Overview

For the full API reference with code examples, see the SDK Guide.

What the SDK Does

LemonadeNexusSDK handles everything a client application needs to join and participate in the mesh VPN:

The mesh dataplane is in-process userspace BoringTun plus a smoltcp netstack: no kernel module, no TUN device, no elevated privileges, on every platform.

The tunnel MTU follows the path. When the dataplane comes up, and again after the process resumes from a suspension, the SDK sends echo requests to the session server’s tunnel address, one per MTU candidate (1420, 1340, 1280 and 1200 bytes), and makes the largest candidate whose request comes back the netstack MTU; when none comes back within 2.5 s it uses 1280. Below the full size each request is 26 bytes longer than its candidate, the relay packet header, so the chosen size also fits the path to a relayed peer. The netstack advertises that MTU as its TCP segment size, so the server sizes its segments to the same path. A tunnel behind a corporate or carrier gateway that drops full-size packets therefore still carries every TLS handshake. ln_mesh_status reports the value as tunnel_mtu (0 while the mesh is down).

Language Bindings

Language Interface Header
C++ lnsdk::LemonadeNexusClient class LemonadeNexusSDK/LemonadeNexusClient.hpp
C ln_* FFI functions LemonadeNexusSDK/lemonade_nexus.h
Dart/Flutter packages/lemonade_nexus_sdk (FFI wrapper, NexusWorker, DNS discovery; ships the iOS/Android library) Consumes the C ABI

The shared library exports only the ln_* C API (hidden visibility); the C++ class and the Dart wrapper both sit on top of it.

Logging

The SDK logs through one process-wide logger that writes to stdout. The level is info, or debug when LN_DEBUG is set in the environment at the first ln_create / ln_create_tls. A host whose stdout is lost (a Windows service, a mobile app) routes the lines to its own logger instead:

static void on_log(int level, const char* logger, const char* message, void* user) {
    /* level: 0 trace, 1 debug, 2 info, 3 warn, 4 error, 5 critical.
       logger is "lemonade-nexus-sdk"; message has no timestamp or level
       prefix. Both strings are valid only during this call. */
}

ln_set_log_callback(on_log, NULL);   /* before the first ln_create */
ln_set_log_callback(NULL, NULL);     /* back to stdout */

The callback runs on SDK threads while the SDK holds internal locks: it must return quickly, must not throw, and must not call any ln_* function. Make the call before the first ln_create, or while no client exists; the logger swap is not synchronized against a log line in progress on another thread. The level in force stays as it is. The setting is process-wide, not per client.

TLS Trust

The SDK verifies every server with OpenSSL’s default trust: the system certificate paths on Linux, the keychain on macOS. A host without default trust gives the SDK a PEM bundle of root certificates instead:

ln_set_ca_bundle_path("/path/to/cacert.pem");   /* before the first ln_create_tls */
ln_set_ca_bundle_path("");                       /* back to the default trust */

The setting is process-wide and applies to clients opened after the call; the file is read when a client connects, so it must stay in place. iOS and Android need it: neither ships an OpenSSL certificate directory, and an Android app process runs with AT_SECURE set, so OpenSSL ignores SSL_CERT_FILE there as well. The Flutter plugin writes Mozilla’s root certificates to the app’s cache directory and passes that path.

Tier 1 Servers

Only Tier 1 servers hold the permission tree. A Tier 2 server refuses every tree route, so the SDK never sends one there. Pin the network’s Genesis public key (genesis_pubkey in the server config) before the first tree call:

A client that has no stored key can discover it on first connect:

Discovery runs only when no key is pinned. It asks each candidate server for GET /api/onboard/info over verified HTTPS: the hosts the caller passes (for example, servers found through DNS discovery), the configured servers, and the servers they list in /api/servers. When those are fewer than 3 distinct hosts (one seed, or a seed whose /api/servers did not answer), it also asks the servers the region’s SEIP records name (<region>.seip.<domain>; the region comes from set_discovery_target / ln_set_discovery_target, ServerConfig::discovery_domain and discovery_region, or a known server’s SEIP FQDN). Those servers are candidates only. It asks at most 16 distinct hosts. Each server reports its configured genesis_pubkey and the network_id that derives from it. The SDK pins the key only when all of these are true:

  1. Every server that reports a key reports the same key, network id, and DNS base domain.
  2. The network id derives from the key under the compiled ruleset versions.
  3. At least 3 distinct agreeing hosts are inside that DNS domain.

A disagreement, a malformed key, a network id that does not derive from the key, or no DNS domain fails closed with LN_ERR_REJECTED. Fewer than 3 agreeing hosts fails closed with LN_ERR_CONNECT; the error names each host asked and what it answered, for example found 1; asked 3 host(s): sn01.example:9100 agrees; sn02.example:9100 unreachable (SSL server verification failed); sn03.example:9100 HTTP 429 (the other outcomes are no claim and outside domain <domain>). A network with fewer than 3 reachable servers needs an explicit set_genesis_key. On success the key is pinned as with set_genesis_key, and the call returns {"genesis_pubkey","network_id","dns_base_domain","hosts"}. Store the key and pin it with set_genesis_key on later runs.

Discovery never replaces a pinned key, even one pinned while it runs. A key change is always an explicit set_genesis_key call.

Trust limit: discovery is trust on first use. The key is not signed by anything the client already trusts. Trust rests on verified-HTTPS ownership of the network’s DNS domain and on agreement of several servers. An attacker who can obtain certificates for at least 3 names in that domain, or who controls every server the client reaches, can supply a false key on first connect. After the key is pinned, the Tier 1 verification below does not depend on DNS or on the servers’ honesty.

The SDK then:

  1. Walks the authority chain from the key (GET /api/tier1/authority on any server) and verifies the Genesis certificate and every finalized handoff with its commit proof. It asks for pages of 32 handoffs (max_links=32); a server that predates that parameter sends 4. When a server answers 429, the SDK waits a second and asks for the same page again, at most 30 times per server, before it treats the server as unreachable.
  2. Asks each candidate server (the configured servers, and the servers they list in /api/servers that are members) for an endpoint proof over a fresh nonce (GET /api/tier1/proof). It keeps a server only when a member of the verified epoch signed a proof for that exact host and port, in that epoch.
  3. Sends /api/auth*, /api/join, and the other tree routes only to those servers. It starts with the server that answered last and fails over to the next one when a server does not answer or answers 421. A session is bound to the server that issued it; private-API calls go only to that server, and only while it is still a verified Tier 1 server. When that server leaves the membership (it announces the next epoch in its heartbeat answers before the handoff and answers 421 after it), the SDK verifies the membership again and moves the session to another verified server with the device identity; the announcement itself chooses nothing (see SDK Guide — Session moves).

It verifies again every five minutes and after a server refuses a tree route.

A refresh starts from the server pool: the configured seeds, the servers /api/servers listed on first connect, and, after a join, the session server first. The join keeps the other known servers behind the session server, so an outage of the session server leaves the client servers to ask. When no known server answers at all, the SDK resolves the region’s SEIP records (<region>.seip.<domain>, the same records the first DNS discovery reads; see DNS Discovery) and verifies the servers they name with the same chain walk and endpoint proofs before it uses one. The region comes from ServerConfig::discovery_domain and discovery_region (set_discovery_target / ln_set_discovery_target sets them on a created client), or from the session server’s SEIP FQDN <id>.<region>.seip.<domain> when they are not set; without either, no fallback runs and a warning log line says so. The DNS step runs at most once a minute, so a long outage costs one lookup a minute rather than one per tree call; it supplies candidates only, and it never touches the pinned Genesis key. One info log line names what the lookup found. A refresh that still fails names every server it asked (tried: for the chain walk, probed: for the endpoint proofs) and what each one answered: unreachable with the transport’s reason (for example unreachable (SSL server verification failed)), or HTTP <status>. refresh_tier1() / ln_tier1_refresh() verifies now, and tier1_membership() / ln_tier1_membership() returns the last result. tier1_walk_progress() / ln_tier1_progress() reports the chain walk that runs now (active, epoch, target, pages_done, pages_total) without waiting for it, so an application can show progress while a join or sign-in waits. The Flutter plugin exposes it as tier1Progress.

Without a pinned key, a chain that does not verify, a stale epoch, or no reachable verified server, the SDK sends no tree request: the call fails with http_status 0 (LN_ERR_CONNECT) and an error that says why. Store the returned epoch and chain and pass both to set_epoch_floor / ln_set_epoch_floor on the next start: the SDK then refuses an older membership of that chain. A client with no stored epoch can be held at an old epoch only if every server it reaches withholds the newer handoffs and every member it reaches signs the older epoch. Catching up has no limit: a client that was away for any number of epochs pages the chain forward.

chain names the chain instance (the Epoch 1 anchor). Genesis signs one Epoch 1; a network founded again after losing its data is another chain under the same key. When the servers serve another chain, the refresh fails with http_status 409 and refounded: true in ln_tier1_refresh and ln_tier1_membership; the SDK neither applies the stored floor to that chain nor adopts it. The application tells the user to leave the network and join it again, which clears the stored floor with the account.

Direct Paths (Hole Punching)

When mesh networking is on (enable_mesh / ln_mesh_enable), the SDK tries a direct path to every peer before it uses a relay:

ln_mesh_status and ln_mesh_peers report a direct peer’s address in endpoint.

Suspension and resume

The SDK detects a suspension by itself from the wall-clock gap above, but only when its loop thread next runs, and a request already waiting on a connection the OS closed meanwhile holds the private transport until its read timeout (15 seconds, twice). A host that knows when it came back does better by saying so:

LAN-Direct Paths

Two devices behind one router that does not hairpin UDP never meet at their public endpoints. They connect over the LAN instead:

The dataplane binds a UDP port the OS chooses at every start, so the NAT mapping comes only from STUN. A device behind a router mapping or a manual port forward sets listen_port in the mesh config (MeshConfig::listen_port, ln_mesh_enable_config key listen_port, default 0). The SDK then binds that port, exclusively, at every start and rebind: the join, enable_mesh (a port other than the one in use restarts the dataplane), and resume. A port that cannot be bound is an error, logged with the port and the OS error; the SDK never takes another port in its place, because the forward points at the configured one. mesh_status / ln_mesh_status report the port in use as dataplane_port (0 while the dataplane is down).

The SDK also asks the default gateway for a mapping of that port by itself, with PCP first and NAT-PMP when the router does not speak PCP, and renews the lease before it ends. While the lease is live, the mapped endpoint replaces the STUN reflexive endpoint in the heartbeat, so peers punch toward the router’s forward; when the lease ends, the reflexive endpoint returns. A changed endpoint goes out in a heartbeat at once. mesh_status / ln_mesh_status report the live mapping as mapped_endpoint ("a.b.c.d:port") and mapping_protocol ("pcp" or "natpmp"), both empty without one. A router that speaks neither protocol changes nothing: the heartbeat carries the reflexive endpoint as before.

Relay Fallback

The SDK moves a peer onto a relay when the dataplane cannot reach it directly:

A relay that refuses, does not answer, or gives no handshake is skipped for five minutes, and the peer is retried with backoff. Set prefer_direct to false in the mesh config to relay every peer.

With prefer_direct (the default), the SDK keeps probing a relayed peer’s listed endpoint and LAN endpoints: it finishes the first burst, then sends two probes at each pair slot (every 60 seconds). When the peer answers there with a valid handshake, the peer moves to the direct path and the SDK closes the relay session. This does not count as a relay failure. A relay request whose path is not in use yet closes the same way when the direct handshake arrives first. A relay that carries traffic stays open; it is abandoned only after 180 seconds plus the grace period without an authenticated packet.

ln_mesh_status and ln_mesh_peers report a relayed peer with an empty endpoint and its relay address (ip:port) in relay_endpoint.

A peer that publishes no direct endpoint (no STUN answer, or STUN turned off) always uses a relay.

Authentication Status

Method Status
Ed25519 challenge-response Primary. Requires an identity (set_identity / ln_set_identity). Auto-registers new pubkeys on first use.
Passkey / WebAuthn Backup. Challenge bound to the user; assertion verified server-side.
Password Deprecated. The server stub always fails; the SDK keeps the functions for compatibility.
Token link One-time invite codes for a group: a new device joins under the group; an existing account becomes a member of it (ln_invite_create, ln_invite_accept). See SDK Guide.

Account Deletion

delete_account() / ln_account_delete(client, &out_json) permanently deletes the account of the identity set with set_identity / ln_set_identity. It cannot be undone. The SDK gets a challenge from the session’s server over the private API and signs a deletion proof with the identity key, so a copied session token cannot delete an account. The server side is described in Security.

On status "deleted" the SDK stops the mesh, clears its session token and node id, and (C ABI) stops the account image service. The answer can be status "pending" with an op_id. The SDK then keeps its session, and the deletion takes effect when Tier 1 commits it.

A committed deletion revokes the identity key before its answer travels back, and the tunnel the answer takes can end with the account. When the request gets no answer, or no session is left to ask for the challenge, the SDK authenticates once more. A key the server refuses as revoked confirms the deletion: the answer is status "deleted" with confirmed: "revoked" and an empty account_id. A key the server still accepts leaves the failure as it is, so the caller can retry.

The SDK does not delete local files. After a deletion, the application deletes:

The Flutter client removes the account’s keyring entry (group key), the stored session, and the group loopback certificates, signs out, and then removes the revoked device identity from the per-user identity store and creates a new one (see the client README, “Device identity”).

Linking

Build the shared library first:

cmake --build build --target LemonadeNexusSDKShared --parallel

For a C/C++ consumer, link against the shared library (or the static LemonadeNexusSDK target when building inside this CMake project):

find_library(LEMONADE_NEXUS_SDK liblemonade_nexus_sdk REQUIRED)
target_link_libraries(myapp PRIVATE ${LEMONADE_NEXUS_SDK})

libsodium, BoringTun, and the netstack are embedded in the shared library. A Linux SDK-only build (-DLEMONADE_NEXUS_SDK_ONLY=ON, as the release and the Linux client build it) links the system OpenSSL 3 (libssl.so.3); a full Linux build with the default LEMONADE_NEXUS_STATIC_DEPS=ON links the pinned static OpenSSL. On macOS, Windows, iOS, and Android it embeds the pinned static OpenSSL.

Platform Notes