upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/btfrv.cpp
blob: d4f079fe6848fa4fb89fbe029791493ac979c8ba (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright (c) 2026 Bitflash developers
// Distributed under the MIT/X11 software license.
//
// Bitflash rendezvous transport. See btfrv.h.
// Windows/Winsock implementation (Layer 6 will #ifdef this for Linux/BSD sockets).

#include "btfrv.h"
#ifdef BITFLASH_NODE_BUILD
#include "proxy.h"
#endif

// Portable sockets: Winsock on Windows, BSD sockets on Linux/POSIX (for the
// headless relay `bitflashd` that runs on the VPS).
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#define CLOSESOCK BtfCloseSocket
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <csignal>
typedef int SOCKET;
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define CLOSESOCK BtfCloseSocket
#ifndef SD_BOTH
#define SD_BOTH SHUT_RDWR
#endif
#endif

#include "sockcount.h"

#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <thread>
#include <mutex>
#include <atomic>

namespace btf
{

static std::atomic<bool> g_stop(false);
static SOCKET            g_listen = INVALID_SOCKET;

// Services that have registered and are waiting for a client, keyed by pubkey.
static std::map<std::string, SOCKET> g_waiting;
static std::mutex                    g_mtx;

bool RvInit()
{
    static bool done = false;
    if (done) return true;
#ifdef _WIN32
    WSADATA wsa;
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return false;
#else
    signal(SIGPIPE, SIG_IGN); // don't die when writing to a closed peer
#endif
    done = true;
    return true;
}

// ---- low-level helpers ----

static bool ReadN(SOCKET s, void* buf, int n)
{
    char* p = (char*)buf;
    int off = 0;
    while (off < n)
    {
        int r = recv(s, p + off, n - off, 0);
        if (r <= 0) return false;
        off += r;
    }
    return true;
}

static bool WriteN(SOCKET s, const void* buf, int n)
{
    const char* p = (const char*)buf;
    int off = 0;
    while (off < n)
    {
        int r = send(s, p + off, n - off, 0);
        if (r <= 0) return false;
        off += r;
    }
    return true;
}

static SOCKET ConnectTo(const char* host, unsigned short port)
{
#ifdef BITFLASH_NODE_BUILD
    SOCKET s = BtfConnectSocket(host, port, SOCK_RV_DIAL, 10);
    if (s == INVALID_SOCKET)
        return INVALID_SOCKET;
#else
    struct addrinfo hints, *res = NULL;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    char portstr[16]; sprintf(portstr, "%u", (unsigned)port);
    if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET;
    SOCKET s = BtfSocketTag(socket(res->ai_family, res->ai_socktype, res->ai_protocol), SOCK_RV_DIAL);
    if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; }

    // Apply a short timeout only for the connect() and initial handshake so an
    // unresponsive relay doesn't block the thread. Once the tunnel is live the
    // timeout is cleared -- a forwarding pipe must block indefinitely on recv().
#ifdef _WIN32
    DWORD tvShort = 10000;
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvShort, sizeof(tvShort));
    setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tvShort, sizeof(tvShort));
#else
    struct timeval tvShort; tvShort.tv_sec = 10; tvShort.tv_usec = 0;
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tvShort, sizeof(tvShort));
    setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tvShort, sizeof(tvShort));
#endif

    if (connect(s, res->ai_addr, (int)res->ai_addrlen) != 0)
    {
        CLOSESOCK(s); freeaddrinfo(res); return INVALID_SOCKET;
    }
    freeaddrinfo(res);
#endif

    // Clear the timeout -- the socket is now a live tunnel and must not
    // time out when idle. recv() will block until data arrives or peer closes.
#ifdef _WIN32
    DWORD tvOff = 0;
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvOff, sizeof(tvOff));
    setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tvOff, sizeof(tvOff));
#else
    struct timeval tvOff; tvOff.tv_sec = 0; tvOff.tv_usec = 0;
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tvOff, sizeof(tvOff));
    setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tvOff, sizeof(tvOff));
#endif

    return s;
}

// ---- relay side ----

// Pump bytes from `from` to `to`. Owns closing `from`; shuts down `to` on exit
// so the peer-direction thread unblocks and closes its own socket exactly once.
static void Forward(SOCKET from, SOCKET to)
{
    char buf[32 * 1024];
    for (;;)
    {
        int r = recv(from, buf, sizeof(buf), 0);
        if (r <= 0) break;
        if (!WriteN(to, buf, r)) break;
    }
    shutdown(to, SD_BOTH);
    CLOSESOCK(from);
}

static void HandleIncoming(SOCKET s)
{
    unsigned char hdr[33];
    if (!ReadN(s, hdr, 33)) { CLOSESOCK(s); return; }
    char role = (char)hdr[0];
    std::string pubkey((char*)hdr + 1, 32);

    if (role == 'S')
    {
        // Register and wait; a future client will pair us.
        std::lock_guard<std::mutex> lk(g_mtx);
        // If a stale service for this key exists, drop it.
        std::map<std::string, SOCKET>::iterator it = g_waiting.find(pubkey);
        if (it != g_waiting.end()) CLOSESOCK(it->second);
        g_waiting[pubkey] = s;
        // leave the socket open in the map; do not close here.
    }
    else if (role == 'C')
    {
        SOCKET svc = INVALID_SOCKET;
        {
            std::lock_guard<std::mutex> lk(g_mtx);
            std::map<std::string, SOCKET>::iterator it = g_waiting.find(pubkey);
            if (it != g_waiting.end()) { svc = it->second; g_waiting.erase(it); }
        }
        if (svc == INVALID_SOCKET)
        {
            unsigned char no = 0x00;
            WriteN(s, &no, 1);
            CLOSESOCK(s);
            return;
        }
        // Pair: tell both sides, then forward raw bytes bidirectionally.
        unsigned char ok = 0x01;
        bool a = WriteN(svc, &ok, 1);
        bool b = WriteN(s, &ok, 1);
        if (!a || !b) { CLOSESOCK(svc); CLOSESOCK(s); return; }
        std::thread(Forward, s, svc).detach();
        std::thread(Forward, svc, s).detach();
    }
    else
    {
        CLOSESOCK(s);
    }
}

bool RvRelayRun(unsigned short port)
{
    if (!RvInit()) return false;
    g_stop = false;

    g_listen = BtfSocketTag(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), SOCK_RV_LISTEN);
    if (g_listen == INVALID_SOCKET) return false;
    int yes = 1;
    setsockopt(g_listen, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(yes));

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);
    if (bind(g_listen, (struct sockaddr*)&addr, sizeof(addr)) != 0) { CLOSESOCK(g_listen); return false; }
    if (listen(g_listen, 16) != 0) { CLOSESOCK(g_listen); return false; }

    while (!g_stop)
    {
        struct sockaddr_in cli;
        socklen_t len = sizeof(cli);
        SOCKET s = BtfSocketTag(accept(g_listen, (struct sockaddr*)&cli, &len), SOCK_RV_ACCEPT);
        if (s == INVALID_SOCKET)
        {
            if (g_stop) break;
            continue;
        }
        std::thread(HandleIncoming, s).detach();
    }
    CLOSESOCK(g_listen);
    g_listen = INVALID_SOCKET;
    return true;
}

void RvRelayStop()
{
    g_stop = true;
    if (g_listen != INVALID_SOCKET)
        CLOSESOCK(g_listen); // unblock accept()
}

// ---- service / client helpers ----

RvSocket RvServiceRegister(const char* relay_host, unsigned short port,
                           const unsigned char my_pubkey[32])
{
    if (!RvInit()) return RV_INVALID;
    SOCKET s = ConnectTo(relay_host, port);
    if (s == INVALID_SOCKET) return RV_INVALID;
    unsigned char hdr[33];
    hdr[0] = (unsigned char)'S';
    memcpy(hdr + 1, my_pubkey, 32);
    if (!WriteN(s, hdr, 33)) { CLOSESOCK(s); return RV_INVALID; }
    // Return here: registered, not yet paired. The relay lists the service as
    // soon as it reads this header. The 0x01 that used to be consumed below
    // only arrives when somebody dials us, so blocking on it made "registered"
    // and "already in use" indistinguishable to the caller -- and left the node
    // unable to advertise a meeting node until after it had been reached there.
    return (RvSocket)s;
}

bool RvServiceWaitPaired(RvSocket s, int nTimeoutSecs)
{
    // A registration socket is idle on purpose -- we announce ourselves and
    // wait, possibly for a long time, for someone to dial. ConnectTo() clears
    // the socket timeout for exactly that reason, and with no timeout at all
    // this recv() has no way to end when the path dies without either side
    // saying so: a NAT drops the idle mapping, a relay restarts, a route
    // changes. No FIN arrives, nothing is readable, and the call never
    // returns. The thread stays alive and asleep, so ThreadBtfAccept never
    // reaches the `continue` that would register again, and the node goes deaf
    // while every outward sign says it is healthy.
    //
    // Seen in production on the bootstrap seed: 27 minutes without a block,
    // eight behind the network, relay reachable throughout, fixed instantly by
    // a restart.
    //
    // TCP keepalive is the obvious answer and it does not work here -- I
    // measured it. With probes every 5s and a 2-probe limit, a socket whose
    // replies were being dropped was still ESTAB after four minutes, the probe
    // counter stuck at zero. So the timeout is ours to enforce, at the one
    // place that knows what waiting means.
    //
    // Expiring is not a failure. It costs one reconnect per interval and
    // returns the loop to a known state.
    if (nTimeoutSecs > 0)
    {
#ifdef _WIN32
        DWORD tv = (DWORD)nTimeoutSecs * 1000;
        setsockopt((SOCKET)s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv));
#else
        struct timeval tv; tv.tv_sec = nTimeoutSecs; tv.tv_usec = 0;
        setsockopt((SOCKET)s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
#endif
    }

    unsigned char paired = 0;
    bool fOk = ReadN((SOCKET)s, &paired, 1) && paired == 0x01;

    // Hand back a socket that blocks again: from here it is a data tunnel, and
    // a tunnel legitimately sits quiet between messages.
    if (fOk && nTimeoutSecs > 0)
    {
#ifdef _WIN32
        DWORD tvOff = 0;
        setsockopt((SOCKET)s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvOff, sizeof(tvOff));
#else
        struct timeval tvOff; tvOff.tv_sec = 0; tvOff.tv_usec = 0;
        setsockopt((SOCKET)s, SOL_SOCKET, SO_RCVTIMEO, &tvOff, sizeof(tvOff));
#endif
    }
    return fOk;
}

RvSocket RvClientConnect(const char* relay_host, unsigned short port,
                         const unsigned char target_pubkey[32])
{
    if (!RvInit()) return RV_INVALID;
    SOCKET s = ConnectTo(relay_host, port);
    if (s == INVALID_SOCKET) return RV_INVALID;
    unsigned char hdr[33];
    hdr[0] = (unsigned char)'C';
    memcpy(hdr + 1, target_pubkey, 32);
    if (!WriteN(s, hdr, 33)) { CLOSESOCK(s); return RV_INVALID; }
    unsigned char paired = 0;
    if (!ReadN(s, &paired, 1) || paired != 0x01) { CLOSESOCK(s); return RV_INVALID; }
    return (RvSocket)s;
}

bool RvReadN(RvSocket s, void* buf, int n)  { return ReadN((SOCKET)s, buf, n); }
bool RvWriteN(RvSocket s, const void* buf, int n) { return WriteN((SOCKET)s, buf, n); }
void RvClose(RvSocket s) { if (s != RV_INVALID) CLOSESOCK((SOCKET)s); }

} // namespace btf