55 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66 */
77
8+ #ifdef _WIN32
9+ #define _WINSOCK_DEPRECATED_NO_WARNINGS
10+ #include <winsock2.h>
11+ typedef int socklen_t ;
12+ typedef SSIZE_T ssize_t ;
13+ #else
814#include <arpa/inet.h>
9- #include <stdio.h>
10- #include <stdlib.h>
11- #include <string.h>
1215#include <sys/prctl.h>
1316#include <sys/socket.h>
1417#include <unistd.h>
18+ #endif
19+
20+ #include <stdio.h>
21+ #include <stdlib.h>
22+ #include <string.h>
1523
1624#include "ipc_common.h"
1725
@@ -53,25 +61,40 @@ Generally communication between the producer and the consumer looks like:
5361*/
5462
5563int consumer_connect (int port ) {
64+
65+ #ifdef _WIN32
66+ WSADATA wsaData ;
67+ SOCKET producer_socket , consumer_socket ;
68+ #else
69+ int producer_socket = -1 ;
70+ int consumer_socket = -1 ;
71+ #endif
72+
5673 struct sockaddr_in consumer_addr ;
5774 struct sockaddr_in producer_addr ;
5875 int producer_addr_len ;
59- int producer_socket = -1 ;
60- int consumer_socket = -1 ;
6176 int ret = -1 ;
6277
78+ #ifdef _WIN32
79+ // initialize Winsock
80+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
81+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
82+ return -1 ;
83+ }
84+ #endif
85+
6386 // create a socket
6487 consumer_socket = socket (AF_INET , SOCK_STREAM , 0 );
6588 if (consumer_socket < 0 ) {
6689 fprintf (stderr , "[consumer] ERROR: creating socket failed\n" );
67- return -1 ;
90+ goto err_WSA_cleanup ;
6891 }
6992
7093 fprintf (stderr , "[consumer] Socket created\n" );
7194
7295 // set the IP address and the port
7396 consumer_addr .sin_family = AF_INET ;
74- consumer_addr .sin_port = htons (port );
97+ consumer_addr .sin_port = htons (( uint16_t ) port );
7598 consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
7699
77100 // bind to the IP address and the port
@@ -101,12 +124,22 @@ int consumer_connect(int port) {
101124 }
102125
103126 fprintf (stderr , "[consumer] Producer connected at IP %s and port %i\n" ,
104- inet_ntoa (producer_addr .sin_addr ), ntohs (producer_addr .sin_port ));
127+ inet_ntoa (producer_addr .sin_addr ),
128+ (int )ntohs (producer_addr .sin_port ));
105129
106- ret = producer_socket ; // success
130+ ret = ( int ) producer_socket ; // success
107131
108132err_close_consumer_socket :
133+ #ifdef _WIN32
134+ closesocket (consumer_socket );
135+ #else
109136 close (consumer_socket );
137+ #endif
138+
139+ err_WSA_cleanup :
140+ #ifdef _WIN32
141+ WSACleanup ();
142+ #endif
110143
111144 return ret ;
112145}
@@ -117,7 +150,13 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
117150 void * provider_params , memcopy_callback_t memcopy_callback ,
118151 void * memcopy_ctx ) {
119152 char consumer_message [MSG_SIZE ];
153+
154+ #ifdef _WIN32
155+ SOCKET producer_socket ;
156+ #else
120157 int producer_socket = -1 ;
158+ #endif
159+
121160 int ret = -1 ;
122161 umf_memory_provider_handle_t provider = NULL ;
123162 umf_result_t umf_result = UMF_RESULT_ERROR_UNKNOWN ;
@@ -171,8 +210,8 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
171210 IPC_handle_size );
172211
173212 // send confirmation to the producer (IPC handle size)
174- recv_len =
175- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
213+ recv_len = send ( producer_socket , ( const char * ) & IPC_handle_size ,
214+ sizeof (IPC_handle_size ), 0 );
176215 if (recv_len < 0 ) {
177216 fprintf (stderr , "[consumer] ERROR: sending confirmation failed\n" );
178217 goto err_free_recv_buffer ;
@@ -214,8 +253,8 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
214253 strcpy (consumer_message , "SKIP" );
215254
216255 // send the SKIP response to the producer
217- send (producer_socket , consumer_message , strlen ( consumer_message ) + 1 ,
218- 0 );
256+ send (producer_socket , consumer_message ,
257+ ( int ) strlen ( consumer_message ) + 1 , 0 );
219258
220259 goto err_free_recv_buffer ;
221260 }
@@ -249,8 +288,8 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
249288 strcpy (consumer_message , CONSUMER_MSG );
250289
251290 // send response to the producer
252- if (send (producer_socket , consumer_message , strlen ( consumer_message ) + 1 ,
253- 0 ) < 0 ) {
291+ if (send (producer_socket , consumer_message ,
292+ ( int ) strlen ( consumer_message ) + 1 , 0 ) < 0 ) {
254293 fprintf (stderr , "[consumer] ERROR: send() failed\n" );
255294 goto err_closeIPCHandle ;
256295 }
@@ -273,7 +312,11 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
273312 free (recv_buffer );
274313
275314err_close_producer_socket :
315+ #ifdef _WIN32
316+ closesocket (producer_socket );
317+ #else
276318 close (producer_socket );
319+ #endif
277320
278321err_umfMemoryPoolDestroy :
279322 umfPoolDestroy (pool );
@@ -295,20 +338,34 @@ int run_consumer(int port, const umf_memory_pool_ops_t *pool_ops,
295338
296339int producer_connect (int port ) {
297340 struct sockaddr_in consumer_addr ;
341+
342+ #ifdef _WIN32
343+ WSADATA wsaData ;
344+ SOCKET producer_socket ;
345+ #else
298346 int producer_socket = -1 ;
347+ #endif
348+
349+ #ifdef _WIN32
350+ // initialize Winsock
351+ if (WSAStartup (MAKEWORD (2 , 2 ), & wsaData ) != 0 ) {
352+ fprintf (stderr , "Failed. Error Code : %d" , WSAGetLastError ());
353+ return -1 ;
354+ }
355+ #endif
299356
300357 // create a producer socket
301358 producer_socket = socket (AF_INET , SOCK_STREAM , 0 );
302359 if (producer_socket < 0 ) {
303360 fprintf (stderr , "[producer] ERROR: Unable to create socket\n" );
304- return -1 ;
361+ goto err_WSA_cleanup ;
305362 }
306363
307364 fprintf (stderr , "[producer] Socket created\n" );
308365
309366 // set IP address and port the same as for the consumer
310367 consumer_addr .sin_family = AF_INET ;
311- consumer_addr .sin_port = htons (port );
368+ consumer_addr .sin_port = htons (( uint16_t ) port );
312369 consumer_addr .sin_addr .s_addr = inet_addr (INET_ADDR );
313370
314371 // send connection request to the consumer
@@ -321,10 +378,19 @@ int producer_connect(int port) {
321378
322379 fprintf (stderr , "[producer] Connected to the consumer\n" );
323380
324- return producer_socket ; // success
381+ return ( int ) producer_socket ; // success
325382
326383err_close_producer_socket_connect :
384+ #ifdef _WIN32
385+ closesocket (producer_socket );
386+ #else
327387 close (producer_socket );
388+ #endif
389+
390+ err_WSA_cleanup :
391+ #ifdef _WIN32
392+ WSACleanup ();
393+ #endif
328394
329395 return -1 ;
330396}
@@ -340,18 +406,20 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
340406 int producer_socket = -1 ;
341407 char consumer_message [MSG_SIZE ];
342408
409+ #if !defined(_WIN32 )
343410 ret = prctl (PR_SET_PTRACER , getppid ());
344411 if (ret == -1 ) {
345412 perror ("PR_SET_PTRACER may be not supported. prctl() call failed" );
346413 goto err_end ;
347414 }
415+ #endif
348416
349417 // create OS memory provider
350418 umf_result =
351419 umfMemoryProviderCreate (provider_ops , provider_params , & provider );
352420 if (umf_result != UMF_RESULT_SUCCESS ) {
353421 fprintf (stderr , "[producer] ERROR: creating memory provider failed\n" );
354- return -1 ;
422+ goto err_end ;
355423 }
356424
357425 umf_memory_pool_handle_t pool ;
@@ -421,8 +489,8 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
421489 }
422490
423491 // send the IPC_handle_size to the consumer
424- ssize_t len =
425- send ( producer_socket , & IPC_handle_size , sizeof (IPC_handle_size ), 0 );
492+ ssize_t len = send ( producer_socket , ( const char * ) & IPC_handle_size ,
493+ sizeof (IPC_handle_size ), 0 );
426494 if (len < 0 ) {
427495 fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
428496 goto err_close_producer_socket ;
@@ -459,7 +527,8 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
459527 }
460528
461529 // send the IPC_handle of IPC_handle_size to the consumer
462- if (send (producer_socket , IPC_handle , IPC_handle_size , 0 ) < 0 ) {
530+ if (send (producer_socket , (const char * )IPC_handle , (int )IPC_handle_size ,
531+ 0 ) < 0 ) {
463532 fprintf (stderr , "[producer] ERROR: unable to send the message\n" );
464533 goto err_close_producer_socket ;
465534 }
@@ -512,7 +581,11 @@ int run_producer(int port, const umf_memory_pool_ops_t *pool_ops,
512581 }
513582
514583err_close_producer_socket :
584+ #ifdef _WIN32
585+ closesocket (producer_socket );
586+ #else
515587 close (producer_socket );
588+ #endif
516589
517590err_PutIPCHandle :
518591 umf_result = umfPutIPCHandle (IPC_handle );
0 commit comments