upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/btfrv.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/btfrv.cpp')
-rw-r--r--src/btfrv.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/btfrv.cpp b/src/btfrv.cpp
index ffda08c..812d077 100644
--- a/src/btfrv.cpp
+++ b/src/btfrv.cpp
@@ -98,11 +98,38 @@ static SOCKET ConnectTo(const char* host, unsigned short port)
98 if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET; 98 if (getaddrinfo(host, portstr, &hints, &res) != 0 || !res) return INVALID_SOCKET;
99 SOCKET s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 99 SOCKET s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
100 if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; } 100 if (s == INVALID_SOCKET) { freeaddrinfo(res); return INVALID_SOCKET; }
101
102 // Apply a short timeout only for the connect() and initial handshake so an
103 // unresponsive relay doesn't block the thread. Once the tunnel is live the
104 // timeout is cleared — a forwarding pipe must block indefinitely on recv().
105#ifdef _WIN32
106 DWORD tvShort = 10000;
107 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvShort, sizeof(tvShort));
108 setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tvShort, sizeof(tvShort));
109#else
110 struct timeval tvShort; tvShort.tv_sec = 10; tvShort.tv_usec = 0;
111 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tvShort, sizeof(tvShort));
112 setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tvShort, sizeof(tvShort));
113#endif
114
101 if (connect(s, res->ai_addr, (int)res->ai_addrlen) != 0) 115 if (connect(s, res->ai_addr, (int)res->ai_addrlen) != 0)
102 { 116 {
103 CLOSESOCK(s); freeaddrinfo(res); return INVALID_SOCKET; 117 CLOSESOCK(s); freeaddrinfo(res); return INVALID_SOCKET;
104 } 118 }
105 freeaddrinfo(res); 119 freeaddrinfo(res);
120
121 // Clear the timeout — the socket is now a live tunnel and must not
122 // time out when idle. recv() will block until data arrives or peer closes.
123#ifdef _WIN32
124 DWORD tvOff = 0;
125 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tvOff, sizeof(tvOff));
126 setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tvOff, sizeof(tvOff));
127#else
128 struct timeval tvOff; tvOff.tv_sec = 0; tvOff.tv_usec = 0;
129 setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tvOff, sizeof(tvOff));
130 setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tvOff, sizeof(tvOff));
131#endif
132
106 return s; 133 return s;
107} 134}
108 135