upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/btfrv.h
blob: c776201b44d76cff964ebbbca73f45b6829ba46b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) 2026 Bitflash developers
// Distributed under the MIT/X11 software license.
//
// Bitflash rendezvous transport -- the "meeting node" that lets two nodes behind
// CGNAT connect without a public IP or port forwarding. Both sides connect OUT
// to the relay (which HAS a public IP, e.g. the VPS); the relay pairs them by
// `.btf` pubkey and forwards raw bytes between them. The endpoints then run the
// echan end-to-end channel on top, so the relay sees only ciphertext and cannot
// MITM (the client boxes to the service's STATIC key from its self-certified
// descriptor -- a substituting relay just breaks the channel, never reads it).
//
// This is the server that runs on the VPS (headless, Layer 6 ports it to Linux)
// plus the client/service helpers the desktop node uses.

#ifndef BITFLASH_BTFRV_H
#define BITFLASH_BTFRV_H

#include <stdint.h>

namespace btf
{

// A connected, paired rendezvous socket (opaque). Read/write raw bytes; the
// caller layers echan on top. Close with RvClose.
typedef intptr_t RvSocket;
static const RvSocket RV_INVALID = (RvSocket)-1;

// Initialize sockets (Winsock on Windows). Idempotent.
bool RvInit();

// ---- relay side (runs on the VPS) ----
// Run the rendezvous relay forever on `port`, forwarding between paired peers.
// Blocks; run it in a thread. Returns false if it can't bind/listen.
bool RvRelayRun(unsigned short port);

// Stop a running relay (signals RvRelayRun to return).
void RvRelayStop();

// ---- service side (the node being reached by its .btf address) ----
// Register at the relay under `my_pubkey` (32 bytes) and block until a client is
// paired to us. Returns the paired socket, or RV_INVALID on error.
RvSocket RvServiceRegister(const char* relay_host, unsigned short port,
                           const unsigned char my_pubkey[32]);

// ---- client side (the node dialing a .btf address) ----
// Connect through the relay to the service registered under `target_pubkey`.
// Returns the paired socket, or RV_INVALID on error.
RvSocket RvClientConnect(const char* relay_host, unsigned short port,
                         const unsigned char target_pubkey[32]);

// Blocking read/write of exactly n bytes over a rendezvous socket. Return false
// on error/EOF.
bool RvReadN(RvSocket s, void* buf, int n);
bool RvWriteN(RvSocket s, const void* buf, int n);
void RvClose(RvSocket s);

} // namespace btf

#endif