1919// Try to use TCP Fast Open to send data 
2020static  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 )
0 commit comments