@@ -24,19 +24,19 @@ static const char* initWsaData(WSADATA* wsaData)
2424 }
2525
2626 //Dummy socket needed for WSAIoctl
27- SOCKET sockfd = socket (AF_INET , SOCK_STREAM , 0 );
27+ SOCKET sockfd = WSASocketW (AF_INET , SOCK_STREAM , 0 , NULL , 0 , 0 );
2828 if (sockfd == INVALID_SOCKET ) {
29- FF_DEBUG ("socket (AF_INET, SOCK_STREAM) failed" );
29+ FF_DEBUG ("WSASocketW (AF_INET, SOCK_STREAM) failed" );
3030 WSACleanup ();
31- return "socket (AF_INET, SOCK_STREAM) failed" ;
31+ return "WSASocketW (AF_INET, SOCK_STREAM) failed" ;
3232 }
3333
3434 DWORD dwBytes ;
3535 GUID guid = WSAID_CONNECTEX ;
3636 if (WSAIoctl (sockfd , SIO_GET_EXTENSION_FUNCTION_POINTER ,
37- & guid , sizeof (guid ),
38- & ConnectEx , sizeof (ConnectEx ),
39- & dwBytes , NULL , NULL ) != 0 ) {
37+ & guid , sizeof (guid ),
38+ & ConnectEx , sizeof (ConnectEx ),
39+ & dwBytes , NULL , NULL ) != 0 ) {
4040 FF_DEBUG ("WSAIoctl(sockfd, SIO_GET_EXTENSION_FUNCTION_POINTER) failed" );
4141 closesocket (sockfd );
4242 WSACleanup ();
@@ -92,26 +92,33 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
9292 return "initWsaData() failed before" ;
9393 }
9494
95- struct addrinfo * addr ;
96- struct addrinfo hints = {
95+ ADDRINFOW * addr ;
96+ ADDRINFOW hints = {
97+ .ai_flags = AI_NUMERICSERV ,
9798 .ai_family = state -> ipv6 ? AF_INET6 : AF_INET ,
9899 .ai_socktype = SOCK_STREAM ,
99- .ai_flags = AI_NUMERICSERV ,
100100 };
101101
102+ wchar_t hostW [256 ];
103+ if (!NT_SUCCESS (RtlUTF8ToUnicodeN (hostW , (ULONG ) sizeof (hostW ), NULL , host , (ULONG ) strlen (host ) + 1 )))
104+ {
105+ FF_DEBUG ("Failed to convert host to wide string: %s" , host );
106+ return "Failed to convert host to wide string" ;
107+ }
108+
102109 FF_DEBUG ("Resolving address: %s (%s)" , host , state -> ipv6 ? "IPv6" : "IPv4" );
103- if (getaddrinfo ( host , "80" , & hints , & addr ) != 0 )
110+ if (GetAddrInfoW ( hostW , L "80" , & hints , & addr ) != 0 )
104111 {
105- FF_DEBUG ("getaddrinfo () failed" );
106- return "getaddrinfo () failed" ;
112+ FF_DEBUG ("GetAddrInfoW () failed" );
113+ return "GetAddrInfoW () failed" ;
107114 }
108115
109- state -> sockfd = socket (addr -> ai_family , addr -> ai_socktype , addr -> ai_protocol );
116+ state -> sockfd = WSASocketW (addr -> ai_family , addr -> ai_socktype , addr -> ai_protocol , NULL , 0 , 0 );
110117 if (state -> sockfd == INVALID_SOCKET )
111118 {
112- FF_DEBUG ("socket () failed" );
113- freeaddrinfo (addr );
114- return "socket () failed" ;
119+ FF_DEBUG ("WSASocketW () failed" );
120+ FreeAddrInfoW (addr );
121+ return "WSASocketW () failed" ;
115122 }
116123
117124 DWORD flag = 1 ;
@@ -143,7 +150,7 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
143150 {
144151 FF_DEBUG ("bind() failed: %s" , ffDebugWin32Error ((DWORD ) WSAGetLastError ()));
145152 closesocket (state -> sockfd );
146- freeaddrinfo (addr );
153+ FreeAddrInfoW (addr );
147154 state -> sockfd = INVALID_SOCKET ;
148155 return "bind() failed" ;
149156 }
@@ -156,7 +163,7 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
156163 if (state -> overlapped .hEvent == WSA_INVALID_EVENT ) {
157164 FF_DEBUG ("WSACreateEvent() failed" );
158165 closesocket (state -> sockfd );
159- freeaddrinfo (addr );
166+ FreeAddrInfoW (addr );
160167 state -> sockfd = INVALID_SOCKET ;
161168 return "WSACreateEvent() failed" ;
162169 }
@@ -200,7 +207,7 @@ const char* ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* ho
200207 BOOL result = ConnectEx (state -> sockfd , addr -> ai_addr , (int )addr -> ai_addrlen ,
201208 state -> command .chars , state -> command .length , & sent , & state -> overlapped );
202209
203- freeaddrinfo (addr );
210+ FreeAddrInfoW (addr );
204211 addr = NULL ;
205212
206213 if (!result )
@@ -302,10 +309,14 @@ const char* ffNetworkingRecvHttpResponse(FFNetworkingState* state, FFstrbuf* buf
302309 FF_DEBUG ("Data reception loop #%d, current buffer size: %u, available space: %u" ,
303310 ++ recvCount , buffer -> length , ffStrbufGetFree (buffer ));
304311
305- ssize_t received = recv (state -> sockfd , buffer -> chars + buffer -> length , (int )ffStrbufGetFree (buffer ), 0 );
312+ DWORD received = 0 , recvFlags = 0 ;
313+ int recvResult = WSARecv (state -> sockfd , & (WSABUF ) {
314+ .buf = buffer -> chars + buffer -> length ,
315+ .len = (ULONG ) ffStrbufGetFree (buffer ),
316+ }, 1 , & received , & recvFlags , NULL , NULL );
306317
307- if (received < = 0 ) {
308- if (received == 0 ) {
318+ if (recvResult == SOCKET_ERROR || received = = 0 ) {
319+ if (recvResult == 0 && received == 0 ) {
309320 FF_DEBUG ("Connection closed (received=0)" );
310321 } else {
311322 FF_DEBUG ("Reception failed: %s" , ffDebugWin32Error ((DWORD ) WSAGetLastError ()));
@@ -316,7 +327,7 @@ const char* ffNetworkingRecvHttpResponse(FFNetworkingState* state, FFstrbuf* buf
316327 buffer -> length += (uint32_t ) received ;
317328 buffer -> chars [buffer -> length ] = '\0' ;
318329
319- FF_DEBUG ("Successfully received %zd bytes of data, total: %u bytes" , received , buffer -> length );
330+ FF_DEBUG ("Successfully received %u bytes of data, total: %u bytes" , ( unsigned ) received , buffer -> length );
320331
321332 // Check if HTTP header end marker is found
322333 if (headerEnd == 0 ) {
0 commit comments