diff options
Diffstat (limited to 'src/btfrv.h')
| -rw-r--r-- | src/btfrv.h | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/btfrv.h b/src/btfrv.h new file mode 100644 index 0000000..9cd7476 --- /dev/null +++ b/src/btfrv.h | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | // Copyright (c) 2026 Bitflash developers | ||
| 2 | // Distributed under the MIT/X11 software license. | ||
| 3 | // | ||
| 4 | // Bitflash rendezvous transport -- the "meeting node" that lets two nodes behind | ||
| 5 | // CGNAT connect without a public IP or port forwarding. Both sides connect OUT | ||
| 6 | // to the relay (which HAS a public IP, e.g. the VPS); the relay pairs them by | ||
| 7 | // `.btf` pubkey and forwards raw bytes between them. The endpoints then run the | ||
| 8 | // echan end-to-end channel on top, so the relay sees only ciphertext and cannot | ||
| 9 | // MITM (the client boxes to the service's STATIC key from its self-certified | ||
| 10 | // descriptor -- a substituting relay just breaks the channel, never reads it). | ||
| 11 | // | ||
| 12 | // This is the server that runs on the VPS (headless, Layer 6 ports it to Linux) | ||
| 13 | // plus the client/service helpers the desktop node uses. | ||
| 14 | |||
| 15 | #ifndef BITFLASH_BTFRV_H | ||
| 16 | #define BITFLASH_BTFRV_H | ||
| 17 | |||
| 18 | #include <stdint.h> | ||
| 19 | |||
| 20 | namespace btf | ||
| 21 | { | ||
| 22 | |||
| 23 | // A connected, paired rendezvous socket (opaque). Read/write raw bytes; the | ||
| 24 | // caller layers echan on top. Close with RvClose. | ||
| 25 | typedef intptr_t RvSocket; | ||
| 26 | static const RvSocket RV_INVALID = (RvSocket)-1; | ||
| 27 | |||
| 28 | // Initialize sockets (Winsock on Windows). Idempotent. | ||
| 29 | bool RvInit(); | ||
| 30 | |||
| 31 | // ---- relay side (runs on the VPS) ---- | ||
| 32 | // Run the rendezvous relay forever on `port`, forwarding between paired peers. | ||
| 33 | // Blocks; run it in a thread. Returns false if it can't bind/listen. | ||
| 34 | bool RvRelayRun(unsigned short port); | ||
| 35 | |||
| 36 | // Stop a running relay (signals RvRelayRun to return). | ||
| 37 | void RvRelayStop(); | ||
| 38 | |||
| 39 | // ---- service side (the node being reached by its .btf address) ---- | ||
| 40 | // Register at the relay under `my_pubkey` (32 bytes). Returns as soon as the | ||
| 41 | // relay has us listed, WITHOUT waiting for anyone to dial -- the caller needs | ||
| 42 | // that moment to start advertising this relay as its meeting node. Waiting for | ||
| 43 | // the dial was folded into this call before, which deadlocked discovery: a node | ||
| 44 | // could not be advertised until it had been dialled, and could not be dialled | ||
| 45 | // until it was advertised. Returns RV_INVALID on error. | ||
| 46 | RvSocket RvServiceRegister(const char* relay_host, unsigned short port, | ||
| 47 | const unsigned char my_pubkey[32]); | ||
| 48 | |||
| 49 | // Block on a registered service socket until a client is paired to us. Returns | ||
| 50 | // false if the relay drops us or errors, in which case the caller should close | ||
| 51 | // the socket and register again. | ||
| 52 | // Blocks until a client is paired with us. Give it a timeout, in seconds, and | ||
| 53 | // it returns false once that passes with nobody arriving -- which the caller | ||
| 54 | // should treat as "register again", not as an error. Zero means wait forever, | ||
| 55 | // which is what this used to do unconditionally and must not be used for a | ||
| 56 | // registration socket: see the note on the implementation. | ||
| 57 | bool RvServiceWaitPaired(RvSocket s, int nTimeoutSecs = 0); | ||
| 58 | |||
| 59 | // ---- client side (the node dialing a .btf address) ---- | ||
| 60 | // Connect through the relay to the service registered under `target_pubkey`. | ||
| 61 | // Returns the paired socket, or RV_INVALID on error. | ||
| 62 | RvSocket RvClientConnect(const char* relay_host, unsigned short port, | ||
| 63 | const unsigned char target_pubkey[32]); | ||
| 64 | |||
| 65 | // Blocking read/write of exactly n bytes over a rendezvous socket. Return false | ||
| 66 | // on error/EOF. | ||
| 67 | bool RvReadN(RvSocket s, void* buf, int n); | ||
| 68 | bool RvWriteN(RvSocket s, const void* buf, int n); | ||
| 69 | void RvClose(RvSocket s); | ||
| 70 | |||
| 71 | } // namespace btf | ||
| 72 | |||
| 73 | #endif | ||