Skip to content

Commit 8851821

Browse files
committed
Networking: don't enable TFO if server doesn't support it
1 parent 3489cc8 commit 8851821

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

src/common/networking/networking.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef struct FFNetworkingState {
2626
uint32_t timeout;
2727
bool ipv6;
2828
bool compression; // if true, HTTP content compression will be enabled if supported
29+
bool tfo; // if true, TCP Fast Open will be attempted first, and fallback to traditional connection if it fails
2930
} FFNetworkingState;
3031

3132
const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* host, const char* path, const char* headers);

src/common/networking/networking_linux.c

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
// Try to use TCP Fast Open to send data
2020
static const char* tryTcpFastOpen(FFNetworkingState* state)
2121
{
22-
#if !defined(TCP_FASTOPEN) || (defined(__linux__) && !defined(MSG_FASTOPEN))
22+
if (!state->tfo)
23+
{
24+
#ifndef __APPLE__
25+
FF_DEBUG("TCP Fast Open disabled, skipping");
26+
return "TCP Fast Open disabled";
27+
#else
28+
FF_DEBUG("TCP Fast Open disabled, using connectx() to send data");
29+
#endif
30+
}
31+
32+
#if (!defined(__APPLE__) && !defined(TCP_FASTOPEN)) || (defined(__linux__) && !defined(MSG_FASTOPEN))
2333
FF_DEBUG("TCP Fast Open not supported on this system");
2434
FF_UNUSED(state);
2535
return "TCP Fast Open not supported";
@@ -81,7 +91,7 @@ static const char* tryTcpFastOpen(FFNetworkingState* state)
8191
.sae_dstaddr = state->addr->ai_addr,
8292
.sae_dstaddrlen = state->addr->ai_addrlen,
8393
},
84-
SAE_ASSOCID_ANY, CONNECT_DATA_IDEMPOTENT,
94+
SAE_ASSOCID_ANY, state->tfo ? CONNECT_DATA_IDEMPOTENT : 0,
8595
&(struct iovec) {
8696
.iov_base = state->command.chars,
8797
.iov_len = state->command.length,
@@ -91,31 +101,40 @@ static const char* tryTcpFastOpen(FFNetworkingState* state)
91101
return "fcntl(F_SETFL) failed";
92102
}
93103
#endif
94-
if (sent >= 0 || (errno == EAGAIN || errno == EWOULDBLOCK))
104+
if (sent > 0 || (errno == EAGAIN || errno == EWOULDBLOCK
105+
#ifdef __APPLE__
106+
// On macOS EINPROGRESS means the connection cannot be completed immediately
107+
// On Linux, it means the TFO cookie is not available locally
108+
|| errno == EINPROGRESS
109+
#endif
110+
))
95111
{
96-
FF_DEBUG("TCP Fast Open %s (sent=%zd, errno=%d: %s)", errno == 0 ? "succeeded" : "was in progress",
97-
sent, errno, strerror(errno));
112+
FF_DEBUG(
113+
#ifdef __APPLE__
114+
"connectx()"
115+
#else
116+
"sendto()"
117+
#endif
118+
" %s (sent=%zd, errno=%d: %s)", errno == 0 ? "succeeded" : "was in progress",
119+
sent, errno, strerror(errno));
98120
freeaddrinfo(state->addr);
99121
state->addr = NULL;
100122
ffStrbufDestroy(&state->command);
101123
return NULL;
102124
}
103125

104-
if (errno == EINPROGRESS)
105-
{
106-
FF_DEBUG("TCP Fast Open cookie is not available locally");
107-
return "sendto() reports EINPROGRESS";
108-
}
109-
else
110-
{
111-
// Fast Open failed
112-
FF_DEBUG("TCP Fast Open failed: %s (errno=%d)", strerror(errno), errno);
126+
FF_DEBUG(
113127
#ifdef __APPLE__
114-
return "connectx() failed";
128+
"connectx()"
115129
#else
116-
return "sendto() failed";
130+
"sendto()"
117131
#endif
118-
}
132+
" failed: %s (errno=%d)", strerror(errno), errno);
133+
#ifdef __APPLE__
134+
return "connectx() failed";
135+
#else
136+
return "sendto() failed";
137+
#endif
119138
#endif
120139
}
121140

@@ -316,10 +335,10 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
316335

317336
const char* tfoResult = tryTcpFastOpen(state);
318337
if (tfoResult == NULL) {
319-
FF_DEBUG("TCP Fast Open succeeded or in progress");
338+
FF_DEBUG("TryTcpFastOpen succeeded or in progress");
320339
return NULL;
321340
}
322-
FF_DEBUG("TCP Fast Open unavailable or failed: %s, trying traditional connection", tfoResult);
341+
FF_DEBUG("TryTcpFastOpen failed: %s, trying traditional connection", tfoResult);
323342

324343
#ifdef FF_HAVE_THREADS
325344
if (instance.config.general.multithreading)

src/common/networking/networking_windows.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,19 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
166166
ffStrbufAppendS(&command, "\r\n");
167167

168168
#ifdef TCP_FASTOPEN
169-
// Set TCP Fast Open
170-
flag = 1;
171-
if (setsockopt(state->sockfd, IPPROTO_TCP, TCP_FASTOPEN, (char*)&flag, sizeof(flag)) != 0) {
172-
FF_DEBUG("Failed to set TCP_FASTOPEN option: %s", ffDebugWin32Error((DWORD) WSAGetLastError()));
173-
} else {
174-
FF_DEBUG("Successfully set TCP_FASTOPEN option");
169+
if (state->tfo)
170+
{
171+
// Set TCP Fast Open
172+
flag = 1;
173+
if (setsockopt(state->sockfd, IPPROTO_TCP, TCP_FASTOPEN, (char*)&flag, sizeof(flag)) != 0) {
174+
FF_DEBUG("Failed to set TCP_FASTOPEN option: %s", ffDebugWin32Error((DWORD) WSAGetLastError()));
175+
} else {
176+
FF_DEBUG("Successfully set TCP_FASTOPEN option");
177+
}
178+
}
179+
else
180+
{
181+
FF_DEBUG("TCP Fast Open disabled");
175182
}
176183
#endif
177184

@@ -181,6 +188,7 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
181188
command.chars, command.length, &sent, &state->overlapped);
182189

183190
freeaddrinfo(addr);
191+
addr = NULL;
184192

185193
if(!result)
186194
{

src/detection/publicip/publicip.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void ffPreparePublicIp(FFPublicIpOptions* options)
2121
if (options->url.length == 0)
2222
{
2323
state->compression = true;
24+
state->tfo = true;
2425
*status = ffNetworkingSendHttpRequest(state, options->ipv6 ? "v6.ipinfo.io" : "ipinfo.io", "/json", NULL);
2526
}
2627
else

0 commit comments

Comments
 (0)